From cc0159080d38afc30897c79f76963df06c160f51 Mon Sep 17 00:00:00 2001 From: mkucharz Date: Mon, 22 Jan 2018 12:11:12 +0100 Subject: [PATCH 1/6] feat(): adding feature of creating instance during the deploy --- packages/cli/src/cli.js | 1 + packages/cli/src/commands/init.js | 26 +++------------------- packages/cli/src/commands/socket-deploy.js | 6 +++++ packages/cli/tests/e2e/deploy.test-e2e.js | 14 ++++++++++++ 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/packages/cli/src/cli.js b/packages/cli/src/cli.js index 8a60029a..7e93820d 100755 --- a/packages/cli/src/cli.js +++ b/packages/cli/src/cli.js @@ -98,6 +98,7 @@ const setup = async () => { .description('Synchronize your project to Syncano') .option('--hot', 'Enable Hot deploy') .option('-b, --bail', 'Bail after first deploy failure') + .option('-i, --create-instance ', 'Create instance if doesn\'t exist') .option('-t, --trace', 'Turn on showing traces') .action(async (...options) => { trackAndDebug(options) diff --git a/packages/cli/src/commands/init.js b/packages/cli/src/commands/init.js index aec9fb13..dd184e98 100644 --- a/packages/cli/src/commands/init.js +++ b/packages/cli/src/commands/init.js @@ -2,7 +2,8 @@ import format from 'chalk' import inquirer from 'inquirer' import logger from '../utils/debug' -import { p, echo, echon, error } from '../utils/print-tools' +import { createInstance } from './helpers/create-instance' +import { p, echo } from '../utils/print-tools' import Login from './login' const { debug } = logger('cmd-init') @@ -58,28 +59,7 @@ class InitCmd { } if (!project && !instance) { - let newInstance = null - try { - debug('Creating Instance') - echo() - echon(4)('Creating Syncano Instance... ') - newInstance = await this.session.createInstance() - } catch (err) { - echo() - echo() - if (err.message === 'No such API Key.') { - error(4)('It looks like your account key is invalid.') - echo(4)(`Try ${format.cyan('syncano-cli logout')} and ${format.cyan('syncano-cli login')} again.`) - } else { - error(4)(err.message || 'Error while creating instance. Try again!') - } - echo() - process.exit() - } finally { - echo(`${format.green('Done')}`) - echo(4)(`Syncano Instance ${format.cyan(newInstance.name)} has been created!`) - echo() - } + const newInstance = createInstance() this.init.addConfigFiles({ instance: newInstance.name }) this.init.createFilesAndFolders() diff --git a/packages/cli/src/commands/socket-deploy.js b/packages/cli/src/commands/socket-deploy.js index b516f3ca..c19051d9 100644 --- a/packages/cli/src/commands/socket-deploy.js +++ b/packages/cli/src/commands/socket-deploy.js @@ -4,6 +4,7 @@ import Promise from 'bluebird' import logger from '../utils/debug' import { SimpleSpinner } from './helpers/spinner' +import { createInstance } from './helpers/create-instance' import { askQuestions } from './helpers/socket' import { p, error, echo } from '../utils/print-tools' import { currentTime, Timer } from '../utils/date-utils' @@ -28,6 +29,11 @@ export default class SocketDeployCmd { // echo(2)(`♻️ ${format.grey(' Deploying...')}`); + // Create Instance if needed + if (cmd.instance) { + await createInstance(cmd.instance) + } + if (socketName) { debug(`Deploying Socket: ${socketName}`) const msg = p(2)(`${format.magenta('getting sockets:')} ${currentTime()}`) diff --git a/packages/cli/tests/e2e/deploy.test-e2e.js b/packages/cli/tests/e2e/deploy.test-e2e.js index 7ecacde5..dff42fdd 100644 --- a/packages/cli/tests/e2e/deploy.test-e2e.js +++ b/packages/cli/tests/e2e/deploy.test-e2e.js @@ -64,4 +64,18 @@ describe('[E2E] CLI Deploy', function () { .stdout(/Hello TEST CLI/) .end(done) }) + + it('can deploy with instance creation', function (done) { + let testInstance = uniqueInstance() + + const testNixt = () => nixt() + .env('SYNCANO_AUTH_KEY', process.env.E2E_CLI_ACCOUNT_KEY) + .cwd(path.join(testsLocation, testInstance)) + + testNixt() + .after(() => deleteInstance(testInstance)) + .run(`${cliLocation} deploy --create-instance ${testInstance}`) + .stdout(/project synced:/) + .end(done) + }) }) From ab62edb3334b44fedd5e94b5a5b8b8b324fecf17 Mon Sep 17 00:00:00 2001 From: mkucharz Date: Mon, 22 Jan 2018 13:10:02 +0100 Subject: [PATCH 2/6] feat(commands-helpers): helper for adding instance --- .../src/commands/helpers/create-instance.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/cli/src/commands/helpers/create-instance.js diff --git a/packages/cli/src/commands/helpers/create-instance.js b/packages/cli/src/commands/helpers/create-instance.js new file mode 100644 index 00000000..f17677cc --- /dev/null +++ b/packages/cli/src/commands/helpers/create-instance.js @@ -0,0 +1,32 @@ +import format from 'chalk' + +import logger from '../../utils/debug' +import { echo, echon, error } from '../../utils/print-tools' + +const { debug } = logger('cmd-helpers-socket') + +export const createInstance = async (instance) => { + let newInstance = null + try { + debug('Creating Instance') + echo() + echon(4)('Creating Syncano Instance... ') + newInstance = await this.session.createInstance() + } catch (err) { + echo() + echo() + if (err.message === 'No such API Key.') { + error(4)('It looks like your account key is invalid.') + echo(4)(`Try ${format.cyan('syncano-cli logout')} and ${format.cyan('syncano-cli login')} again.`) + } else { + error(4)(err.message || 'Error while creating instance. Try again!') + } + echo() + process.exit(1) + } finally { + echo(`${format.green('Done')}`) + echo(4)(`Syncano Instance ${format.cyan(newInstance.name)} has been created!`) + echo() + } + return newInstance +} From 5f5752ccebd910e418bc231cc08b2b8031af39c7 Mon Sep 17 00:00:00 2001 From: mkucharz Date: Mon, 22 Jan 2018 14:37:01 +0100 Subject: [PATCH 3/6] tests(): adding tests for creating instance during the deploy --- packages/cli/src/cli.js | 2 +- packages/cli/src/commands/helpers/create-instance.js | 4 ++-- packages/cli/src/commands/socket-deploy.js | 9 ++++++--- packages/cli/tests/e2e/deploy.test-e2e.js | 7 +++++++ packages/test-tools/src/index.js | 4 +++- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/cli.js b/packages/cli/src/cli.js index 7e93820d..a48c3a6d 100755 --- a/packages/cli/src/cli.js +++ b/packages/cli/src/cli.js @@ -103,7 +103,7 @@ const setup = async () => { .action(async (...options) => { trackAndDebug(options) session.isAuthenticated() - session.hasProject() + session.hasProjectPath() await session.checkConnection() echo() new commands.SocketDeploy(context).run(options) diff --git a/packages/cli/src/commands/helpers/create-instance.js b/packages/cli/src/commands/helpers/create-instance.js index f17677cc..69453643 100644 --- a/packages/cli/src/commands/helpers/create-instance.js +++ b/packages/cli/src/commands/helpers/create-instance.js @@ -5,13 +5,13 @@ import { echo, echon, error } from '../../utils/print-tools' const { debug } = logger('cmd-helpers-socket') -export const createInstance = async (instance) => { +export const createInstance = async (instanceName, session) => { let newInstance = null try { debug('Creating Instance') echo() echon(4)('Creating Syncano Instance... ') - newInstance = await this.session.createInstance() + newInstance = await session.createInstance(instanceName) } catch (err) { echo() echo() diff --git a/packages/cli/src/commands/socket-deploy.js b/packages/cli/src/commands/socket-deploy.js index c19051d9..4bd799e9 100644 --- a/packages/cli/src/commands/socket-deploy.js +++ b/packages/cli/src/commands/socket-deploy.js @@ -29,9 +29,12 @@ export default class SocketDeployCmd { // echo(2)(`♻️ ${format.grey(' Deploying...')}`); - // Create Instance if needed - if (cmd.instance) { - await createInstance(cmd.instance) + // Create Instance if --create-instance provided + if (cmd.createInstance) { + await createInstance(cmd.createInstance, this.session) + } else { + // If not, we have to check if we have a project attached to any instance + this.session.hasProject() } if (socketName) { diff --git a/packages/cli/tests/e2e/deploy.test-e2e.js b/packages/cli/tests/e2e/deploy.test-e2e.js index dff42fdd..6ca7efb8 100644 --- a/packages/cli/tests/e2e/deploy.test-e2e.js +++ b/packages/cli/tests/e2e/deploy.test-e2e.js @@ -1,4 +1,5 @@ /* global describe it before after */ +import fs from 'fs-extra' import path from 'path' import { nixt, @@ -68,11 +69,17 @@ describe('[E2E] CLI Deploy', function () { it('can deploy with instance creation', function (done) { let testInstance = uniqueInstance() + const projectTestTemplate = path.join(__dirname, './assets/project/empty/') + const moveTestProject = (template) => { + fs.copySync(template, path.join(testsLocation, testInstance)) + } + const testNixt = () => nixt() .env('SYNCANO_AUTH_KEY', process.env.E2E_CLI_ACCOUNT_KEY) .cwd(path.join(testsLocation, testInstance)) testNixt() + .before(() => moveTestProject(projectTestTemplate)) .after(() => deleteInstance(testInstance)) .run(`${cliLocation} deploy --create-instance ${testInstance}`) .stdout(/project synced:/) diff --git a/packages/test-tools/src/index.js b/packages/test-tools/src/index.js index 43c85ee1..f7d7907f 100644 --- a/packages/test-tools/src/index.js +++ b/packages/test-tools/src/index.js @@ -96,7 +96,9 @@ const createInstance = (instanceName) => connection.instance const deleteInstance = (instanceName) => connection.instance .delete(instanceName) - .catch((error) => process.stderr.write(JSON.stringify(error.message, null, ''))) + .catch((error) => process.stderr.write( + JSON.stringify(`deleteInstance: ${error.message}`, null, '') + )) const deleteEachInstance = (instances) => { const list = [] From c6703b384bb44b8a5003dc21d25b7825e5ed53b2 Mon Sep 17 00:00:00 2001 From: mkucharz Date: Mon, 22 Jan 2018 14:59:10 +0100 Subject: [PATCH 4/6] docs(cli:deploy): docs for --create-instance option --- docs/cheatsheet/web/index.html | 3 +++ docs/docs/cli-reference/commands.md | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/docs/cheatsheet/web/index.html b/docs/cheatsheet/web/index.html index 086c6c2c..d2634912 100644 --- a/docs/cheatsheet/web/index.html +++ b/docs/cheatsheet/web/index.html @@ -485,6 +485,9 @@

Deploy

Deploy whole project

s deploy
+

Deploy whole project (with instance creation)

+
s deploy --create-instance <new instance name>
+

Deploy whole project

s deploy <socket name>
diff --git a/docs/docs/cli-reference/commands.md b/docs/docs/cli-reference/commands.md index 494b1bfb..754f9d61 100644 --- a/docs/docs/cli-reference/commands.md +++ b/docs/docs/cli-reference/commands.md @@ -63,6 +63,11 @@ syncano-cli deploy ``` It compiles and deploys all global configuration and Syncano Sockets in your project. From now on, you can call every endpoint from every Socket in your project. Dependencies will be also deployed in that process. +I you don't have instance for that project yet, you can create it during `deploy` process: +```sh +syncano-cli deploy --create-instance +``` + #### Deploy single Socket To deploy single Socket provide socket name as an additional argument: From 98a4f681b1b6b06eb98c8fd9e3eff4e25e2171c9 Mon Sep 17 00:00:00 2001 From: mkucharz Date: Mon, 22 Jan 2018 15:15:01 +0100 Subject: [PATCH 5/6] tests(cli:deploy): additional test case for deploy with instance creation --- .../src/commands/helpers/create-instance.js | 3 +++ packages/cli/tests/e2e/deploy.test-e2e.js | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/cli/src/commands/helpers/create-instance.js b/packages/cli/src/commands/helpers/create-instance.js index 69453643..c1509056 100644 --- a/packages/cli/src/commands/helpers/create-instance.js +++ b/packages/cli/src/commands/helpers/create-instance.js @@ -18,6 +18,9 @@ export const createInstance = async (instanceName, session) => { if (err.message === 'No such API Key.') { error(4)('It looks like your account key is invalid.') echo(4)(`Try ${format.cyan('syncano-cli logout')} and ${format.cyan('syncano-cli login')} again.`) + } else if (err.message === 'name: This field must be unique.') { + error(4)('Instance already exist!') + echo(4)('Try another instace name.') } else { error(4)(err.message || 'Error while creating instance. Try again!') } diff --git a/packages/cli/tests/e2e/deploy.test-e2e.js b/packages/cli/tests/e2e/deploy.test-e2e.js index 6ca7efb8..1197ba01 100644 --- a/packages/cli/tests/e2e/deploy.test-e2e.js +++ b/packages/cli/tests/e2e/deploy.test-e2e.js @@ -6,6 +6,7 @@ import { testsLocation, deleteInstance, createProject, + createInstance, uniqueInstance, getRandomString } from '@syncano/test-tools' @@ -85,4 +86,27 @@ describe('[E2E] CLI Deploy', function () { .stdout(/project synced:/) .end(done) }) + + it.only('can\'t deploy with instance creation if instance already exist', function (done) { + let testInstance = uniqueInstance() + + const projectTestTemplate = path.join(__dirname, './assets/project/empty/') + const moveTestProject = (template) => { + fs.copySync(template, path.join(testsLocation, testInstance)) + } + + const testNixt = () => nixt() + .env('SYNCANO_AUTH_KEY', process.env.E2E_CLI_ACCOUNT_KEY) + .cwd(path.join(testsLocation, testInstance)) + + testNixt() + .before(async () => { + await moveTestProject(projectTestTemplate) + await createInstance(testInstance) + }) + .after(() => deleteInstance(testInstance)) + .run(`${cliLocation} deploy --create-instance ${testInstance}`) + .stdout(/Instance already exist!/) + .end(done) + }) }) From 5722edba57c877fd5a525186932fc36dea59d847 Mon Sep 17 00:00:00 2001 From: mkucharz Date: Mon, 22 Jan 2018 15:17:15 +0100 Subject: [PATCH 6/6] typo() --- packages/cli/src/cli.js | 2 +- packages/cli/tests/e2e/deploy.test-e2e.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/cli.js b/packages/cli/src/cli.js index a48c3a6d..ef6c8103 100755 --- a/packages/cli/src/cli.js +++ b/packages/cli/src/cli.js @@ -98,7 +98,7 @@ const setup = async () => { .description('Synchronize your project to Syncano') .option('--hot', 'Enable Hot deploy') .option('-b, --bail', 'Bail after first deploy failure') - .option('-i, --create-instance ', 'Create instance if doesn\'t exist') + .option('-i, --create-instance ', 'Create instance if it doesn\'t exist') .option('-t, --trace', 'Turn on showing traces') .action(async (...options) => { trackAndDebug(options) diff --git a/packages/cli/tests/e2e/deploy.test-e2e.js b/packages/cli/tests/e2e/deploy.test-e2e.js index 1197ba01..832c25db 100644 --- a/packages/cli/tests/e2e/deploy.test-e2e.js +++ b/packages/cli/tests/e2e/deploy.test-e2e.js @@ -87,7 +87,7 @@ describe('[E2E] CLI Deploy', function () { .end(done) }) - it.only('can\'t deploy with instance creation if instance already exist', function (done) { + it('can\'t deploy with instance creation if instance already exist', function (done) { let testInstance = uniqueInstance() const projectTestTemplate = path.join(__dirname, './assets/project/empty/')