Skip to content

Commit

Permalink
Support starting workspaces from remote devfiles (#118)
Browse files Browse the repository at this point in the history
* Support starting workspaces from remote devfiles
  • Loading branch information
l0rd authored May 29, 2019
1 parent 254bc44 commit 1de1bd9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ OPTIONS
--all see all commands in CLI
```

_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.1.4/src/commands/help.ts)_
_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.1.6/src/commands/help.ts)_

## `chectl server:delete`

Expand Down Expand Up @@ -268,7 +268,7 @@ USAGE
$ chectl workspace:start
OPTIONS
-f, --devfile=devfile path to a valid devfile
-f, --devfile=devfile path or URL to a valid devfile
-h, --help show CLI help
-n, --chenamespace=chenamespace [default: che] kubernetes namespace where Che server is deployed
-w, --workspaceconfig=workspaceconfig path to a valid workspace configuration json file
Expand Down
10 changes: 9 additions & 1 deletion src/api/che.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class CheHelper {
let devfile
let response
try {
devfile = fs.readFileSync(devfilePath, 'utf8')
devfile = await this.parseDevfile(devfilePath)
response = await axios.post(endpoint, devfile, {headers: {'Content-Type': 'text/yaml'}})
} catch (error) {
if (!devfile) { throw new Error(`E_NOT_FOUND_DEVFILE - ${devfilePath} - ${error.message}`) }
Expand All @@ -237,7 +237,15 @@ export class CheHelper {
} else {
throw new Error('E_BAD_RESP_CHE_SERVER')
}
}

async parseDevfile(devfilePath = ''): Promise<string> {
if (devfilePath.startsWith('http')) {
const response = await axios.get(devfilePath)
return response.data
} else {
return fs.readFileSync(devfilePath, 'utf8')
}
}

async createWorkspaceFromWorkspaceConfig(namespace: string | undefined, workspaceConfigPath = ''): Promise<string> {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devfile/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default class Generate extends Command {
const component: DevfileComponent = {
type: TheEndpointName.Kubernetes,
alias: `${flags.selector}`,
referenceContent: `${yaml.safeDump(k8sList)}`
referenceContent: `${yaml.safeDump(k8sList, { skipInvalid: true })}`
}
if (devfile.components) {
devfile.components.push(component)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/workspace/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class Start extends Command {
}),
devfile: string({
char: 'f',
description: 'path to a valid devfile',
description: 'path or URL to a valid devfile',
env: 'DEVFILE_PATH',
required: false,
}),
Expand All @@ -49,7 +49,7 @@ export default class Start extends Command {
const Listr = require('listr')
const notifier = require('node-notifier')
const che = new CheHelper()
if (!flags.devfile || !flags.workspaceconfig) {
if (!flags.devfile && !flags.workspaceconfig) {
this.error('workspace:start command is expecting a devfile or workspace configuration parameter.')
}
const tasks = new Listr([
Expand Down
23 changes: 23 additions & 0 deletions test/api/che.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { CheHelper } from '../../src/api/che'
const namespace = 'che'
const workspace = 'workspace-0123'
const cheURL = 'https://che-che.192.168.64.34.nip.io'
const devfileServerURL = 'https://devfile-server'
let ch = new CheHelper()
let kc = ch.kc
let k8sApi = new Core_v1Api()
Expand Down Expand Up @@ -122,6 +123,28 @@ describe('Che helper', () => {
.do(() => ch.createWorkspaceFromDevfile(namespace, __dirname + '/requests/devfile.inexistent'))
.catch(/E_NOT_FOUND_DEVFILE/)
.it('fails creating a workspace from a non-existing devfile')
fancy
.stub(ch, 'cheNamespaceExist', () => true)
.stub(ch, 'cheURL', () => cheURL)
.nock(devfileServerURL, api => api
.get('/devfile.yaml')
.replyWithFile(200, __dirname + '/requests/devfile.valid', { 'Content-Type': 'text/plain; charset=utf-8' }))
.nock(cheURL, api => api
.post('/api/devfile')
.replyWithFile(201, __dirname + '/replies/create-workspace-from-valid-devfile.json', { 'Content-Type': 'application/json' }))
.it('succeeds creating a workspace from a remote devfile', async () => {
const res = await ch.createWorkspaceFromDevfile(namespace, devfileServerURL + '/devfile.yaml')
expect(res).to.equal('https://che-che.192.168.64.39.nip.io/dashboard/#/ide/che/chectl')
})
fancy
.stub(ch, 'cheNamespaceExist', () => true)
.stub(ch, 'cheURL', () => cheURL)
.nock(devfileServerURL, api => api
.get('/devfile.yaml')
.reply(404, '404 - Not Found'))
.do(() => ch.createWorkspaceFromDevfile(namespace, devfileServerURL + '/devfile.yaml'))
.catch(/E_NOT_FOUND_DEVFILE/)
.it('fails creating a workspace from a non-existing remote devfile')
})
describe('createWorkspaceFromWorkspaceConfig', () => {
fancy
Expand Down

0 comments on commit 1de1bd9

Please sign in to comment.