Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oauth2 keys setup in installer #1865

Merged
merged 4 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 159 additions & 10 deletions core/scripts/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,45 @@ class Backend extends Abstract {
})
}

/**
* Validate magento integration settings.
*
* @returns {Promise}
*/
validateM2Integration () {
return new Promise((resolve, reject) => {
const Magento2Client = require('magento2-rest-client').Magento2Client

Message.info(`Validating magento integration configuration...`)

let m2Url = urlParser(this.answers.m2_url).href
let apiUrl = urlParser(this.answers.m2_api_url).href

if (!m2Url.length) {
reject(new Error('Invalid magento url supplied.'))
}
if (!apiUrl.length) {
reject(new Error('Invalid magento rest api url supplied.'))
}

let options = {
'url': apiUrl,
'consumerKey': this.answers.m2_api_consumer_key,
'consumerSecret': this.answers.m2_api_consumer_secret,
'accessToken': this.answers.m2_api_access_token,
'accessTokenSecret': this.answers.m2_api_access_token_secret
}
let client = Magento2Client(options)

client.categories.list()
.then((categories) => {
resolve()
}).catch((e) => {
reject(new Error('Invalid magento integration settings. Original error: ' + e))
})
})
}

/**
* Creating backend config/local.json
*
Expand All @@ -224,6 +263,13 @@ class Backend extends Abstract {

config.imageable.whitelist.allowedHosts.push(host)

config.magento2.url = urlParser(this.answers.m2_url).href
config.magento2.api.url = urlParser(this.answers.m2_api_url).href
config.magento2.api.consumerKey = this.answers.m2_api_consumer_key
config.magento2.api.consumerSecret = this.answers.m2_api_consumer_secret
config.magento2.api.accessToken = this.answers.m2_api_access_token
config.magento2.api.accessTokenSecret = this.answers.m2_api_access_token_secret

jsonFile.writeFileSync(TARGET_BACKEND_CONFIG_FILE, config, {spaces: 2})
} catch (e) {
reject(new Error('Can\'t create backend config. Original error: ' + e))
Expand Down Expand Up @@ -267,6 +313,23 @@ class Backend extends Abstract {
})
}

/**
* Run 'yarn mage2vs import'
*
* @returns {Promise}
*/
importElasticSearch () {
return new Promise((resolve, reject) => {
Message.info('Importing data from magento into ElasticSearch...')

if (shell.exec(`yarn mage2vs import >> ${Abstract.infoLogStream} 2>&1`).code !== 0) {
reject(new Error('Can\'t import data into ElasticSearch.'))
}

resolve()
})
}

/**
* Cloning magento sample data
*
Expand Down Expand Up @@ -492,16 +555,26 @@ class Manager extends Abstract {
initBackend () {
if (this.answers.is_remote_backend === false) {
Abstract.wasLocalBackendInstalled = true

return this.backend.cloneRepository()
.then(this.backend.goToDirectory.bind(this.backend))
.then(this.backend.npmInstall.bind(this.backend))
.then(this.backend.createConfig.bind(this.backend))
.then(this.backend.dockerComposeUp.bind(this.backend))
.then(this.backend.restoreElasticSearch.bind(this.backend))
.then(this.backend.migrateElasticSearch.bind(this.backend))
.then(this.backend.cloneMagentoSampleData.bind(this.backend))
.then(this.backend.runDevEnvironment.bind(this.backend))
if (this.answers.m2_api_oauth2 === true) {
return this.backend.validateM2Integration()
.then(this.backend.cloneRepository.bind(this.backend))
.then(this.backend.goToDirectory.bind(this.backend))
.then(this.backend.npmInstall.bind(this.backend))
.then(this.backend.createConfig.bind(this.backend))
.then(this.backend.dockerComposeUp.bind(this.backend))
.then(this.backend.importElasticSearch.bind(this.backend))
.then(this.backend.runDevEnvironment.bind(this.backend))
} else {
return this.backend.cloneRepository()
.then(this.backend.goToDirectory.bind(this.backend))
.then(this.backend.npmInstall.bind(this.backend))
.then(this.backend.createConfig.bind(this.backend))
.then(this.backend.dockerComposeUp.bind(this.backend))
.then(this.backend.restoreElasticSearch.bind(this.backend))
.then(this.backend.migrateElasticSearch.bind(this.backend))
.then(this.backend.cloneMagentoSampleData.bind(this.backend))
.then(this.backend.runDevEnvironment.bind(this.backend))
}
} else {
return Promise.resolve()
}
Expand Down Expand Up @@ -655,6 +728,82 @@ let questions = [
// add extra slash as suffix if was not set
return url.slice(-1) === '/' ? url : `${url}/`
}
},
{
type: 'confirm',
name: 'm2_api_oauth2',
message: `Would You like to perform initial data import from Magento2 instance?`,
default: false,
when: function (answers) {
return answers.is_remote_backend === false
}
},
{
type: 'input',
name: 'm2_url',
message: 'Please provide your magento url',
default: 'http://magento2.demo-1.xyz.com',
when: function (answers) {
return answers.m2_api_oauth2 === true
}
},
{
type: 'input',
name: 'm2_api_url',
message: 'Please provide the url to your magento rest api',
default: 'http://demo-magento2.vuestorefront.io/rest',
when: function (answers) {
return answers.m2_api_oauth2 === true
},
filter: function (url) {
let prefix = 'http://'
let prefixSsl = 'https://'

url = url.trim()

// add http:// if no protocol set
if (url.substr(0, prefix.length) !== prefix && url.substr(0, prefixSsl.length) !== prefixSsl) {
url = prefix + url
}

return url
}
},
{
type: 'input',
name: 'm2_api_consumer_key',
message: 'Please provide your consumer key',
default: 'byv3730rhoulpopcq64don8ukb8lf2gq',
when: function (answers) {
return answers.m2_api_oauth2 === true
}
},
{
type: 'input',
name: 'm2_api_consumer_secret',
message: 'Please provide your consumer secret',
default: 'u9q4fcobv7vfx9td80oupa6uhexc27rb',
when: function (answers) {
return answers.m2_api_oauth2 === true
}
},
{
type: 'input',
name: 'm2_api_access_token',
message: 'Please provide your access token',
default: '040xx3qy7s0j28o3q0exrfop579cy20m',
when: function (answers) {
return answers.m2_api_oauth2 === true
}
},
{
type: 'input',
name: 'm2_api_access_token_secret',
message: 'Please provide your access token secret',
default: '7qunl3p505rubmr7u1ijt7odyialnih9',
when: function (answers) {
return answers.m2_api_oauth2 === true
}
}
]

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"vue-server-renderer": "^2.5.17",
"vuelidate": "^0.6.2",
"vuex": "^3.0.1",
"vuex-router-sync": "^5.0.0"
"vuex-router-sync": "^5.0.0",
"magento2-rest-client": "github:DivanteLtd/magento2-rest-client"
},
"devDependencies": {
"@types/object-hash": "^1.2.0",
Expand Down
Loading