Skip to content

Commit

Permalink
feat(cli:deploy): deploy from CLI with instance creation (#87)
Browse files Browse the repository at this point in the history
Now it is possible to deploy project with creating during the process, e.g.:

```
s deploy --create-instance <my_new_instance>
s deploy --i <my_new_instance>
```
  • Loading branch information
mkucharz authored Jan 23, 2018
1 parent 4c80f04 commit 0ab3e67
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 25 deletions.
3 changes: 3 additions & 0 deletions docs/cheatsheet/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ <h3>Deploy</h3>
<h4>Deploy whole project</h4>
<pre><code class="lang-bash">s deploy</code></pre>

<h4>Deploy whole project (with instance creation)</h4>
<pre><code class="lang-bash">s deploy --create-instance &lt;new instance name&gt;</code></pre>

<h4>Deploy whole project</h4>
<pre><code class="lang-bash">s deploy &lt;socket name&gt;</code></pre>

Expand Down
5 changes: 5 additions & 0 deletions docs/docs/cli-reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <my new instance>
```

#### Deploy single Socket

To deploy single Socket provide socket name as an additional argument:
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ 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 <instance>', 'Create instance if it doesn\'t exist')
.option('-t, --trace', 'Turn on showing traces')
.action(async (...options) => {
trackAndDebug(options)
session.isAuthenticated()
session.hasProject()
session.hasProjectPath()
await session.checkConnection()
echo()
new commands.SocketDeploy(context).run(options)
Expand Down
35 changes: 35 additions & 0 deletions packages/cli/src/commands/helpers/create-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 (instanceName, session) => {
let newInstance = null
try {
debug('Creating Instance')
echo()
echon(4)('Creating Syncano Instance... ')
newInstance = await session.createInstance(instanceName)
} 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 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!')
}
echo()
process.exit(1)
} finally {
echo(`${format.green('Done')}`)
echo(4)(`Syncano Instance ${format.cyan(newInstance.name)} has been created!`)
echo()
}
return newInstance
}
26 changes: 3 additions & 23 deletions packages/cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/commands/socket-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -28,6 +29,14 @@ export default class SocketDeployCmd {

// echo(2)(`♻️ ${format.grey(' Deploying...')}`);

// 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) {
debug(`Deploying Socket: ${socketName}`)
const msg = p(2)(`${format.magenta('getting sockets:')} ${currentTime()}`)
Expand Down
45 changes: 45 additions & 0 deletions packages/cli/tests/e2e/deploy.test-e2e.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* global describe it before after */
import fs from 'fs-extra'
import path from 'path'
import {
nixt,
testsLocation,
deleteInstance,
createProject,
createInstance,
uniqueInstance,
getRandomString
} from '@syncano/test-tools'
Expand Down Expand Up @@ -64,4 +66,47 @@ describe('[E2E] CLI Deploy', function () {
.stdout(/Hello TEST CLI/)
.end(done)
})

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:/)
.end(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/')
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)
})
})
4 changes: 3 additions & 1 deletion packages/test-tools/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down

0 comments on commit 0ab3e67

Please sign in to comment.