/
nodeshift
executable file
路237 lines (218 loc) 路 7.91 KB
/
nodeshift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#! /usr/bin/env node
/*
*
* Copyright 2016-2017 Red Hat, Inc, and individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
'use strict';
// Providing a title to the process in `ps`
process.title = 'nodeshift'; // Thanks Ember-cli :)
const fs = require('fs');
const yargs = require('yargs');
const cli = require('./cli');
const log = require('../lib/common-log')();
const helpers = require('../lib/helpers');
/* eslint no-unused-expressions: "warn" */
yargs
.usage('[--options]')
.command(['deploy', '$0'], 'default command - deploy', { cmd: { default: 'deploy' } }, commandHandler)
.command('build', 'build command', { cmd: { default: 'build' } }, commandHandler)
.command('resource', 'resource command', { cmd: { default: 'resource' } }, commandHandler)
.command('apply-resource', 'apply resource command', { cmd: { default: 'apply-resource' } }, commandHandler)
.command('undeploy [removeAll]', 'undeploy resources', { cmd: { default: 'undeploy' } }, commandHandler)
.option('projectLocation', {
describe: 'change the default location of the project',
type: 'string'
})
.option('kube', {
describe: 'Flag to deploy an application to a vanilla kubernetes cluster. At the moment only Minikube is supported.',
type: 'boolean'
})
.option('configLocation', {
describe: 'change the default location of the config',
type: 'string'
})
.options('dockerImage', {
describe: 'the s2i image to use, defaults to registry.access.redhat.com/ubi8/nodejs-10',
type: 'string'
})
.options('imageTag', {
describe: 'The tag of the docker image to use for the deployed application.',
type: 'string',
default: 'latest'
})
.option('web-app', {
describe: 'flag to automatically set the appropriate docker image for web app deployment',
type: 'boolean',
default: false
})
.option('resourceProfile', {
describe: 'Define a subdirectory below .nodeshift/ that indicates where Openshift resources are stored',
type: 'string'
})
.options('outputImageStream', {
describe: 'The name of the ImageStream to output to. Defaults to project name from package.json',
type: 'string'
})
.options('outputImageStreamTag', {
describe: 'The tag of the ImageStream to output to.',
type: 'string',
default: 'latest'
})
.env('NODESHIFT')
.option('quiet', {
describe: 'suppress INFO and TRACE lines from output logs',
type: 'boolean'
})
.array('d')
.options('expose', {
describe: 'flag to create a default Route and expose the default service',
choices: [true, false],
type: 'boolean',
default: false
})
.options('namespace.displayName', {
describe: 'flag to specify the project namespace display name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files',
type: 'string'
})
.options('namespace.create', {
describe: 'flag to create the namespace if it does not exist. Only applicable for the build and deploy command. Must be used with namespace.name',
type: 'boolean'
})
.options('namespace.remove', {
describe: 'flag to remove the user created namespace. Only applicable for the undeploy command. Must be used with namespace.name',
type: 'boolean'
})
.options('namespace.name', {
describe: 'flag to specify the project namespace name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files',
type: 'string'
})
.options('deploy.port', {
describe: 'flag to update the default ports on the resource files. Defaults to 8080',
default: 8080
})
.array('deploy.env')
.option('build.recreate', {
describe: 'flag to recreate a buildConfig or Imagestream',
choices: ['buildConfig', 'imageStream', false, true],
default: false
})
.options('build.forcePull', {
describe: 'flag to make your BuildConfig always pull a new image from dockerhub or not',
choices: [true, false],
type: 'boolean',
default: false
})
.options('build.incremental', {
describe: 'flag to perform incremental builds, which means it reuses artifacts from previously-built images',
choices: [true, false],
type: 'boolean',
default: false
})
.options('build.strategy', {
describe: 'flag to change the build strategy. Defaults to Source',
choices: ['Source', 'Docker']
})
.array('build.env')
.options('metadata.out', {
describe: 'determines what should be done with the response metadata from OpenShift',
choices: ['stdout', 'ignore', '<filename>'],
type: 'string',
default: 'ignore'
})
.options('useDeployment', {
describe: 'flag to deploy the application using a Deployment instead of a DeploymentConfig',
choices: [true, false],
type: 'boolean',
default: false
})
.options('knative', {
describe: 'EXPERIMENTAL. flag to deploy an application as a Knative Serving Service',
choices: [true, false],
type: 'boolean',
default: false
})
.argv;
function commandHandler (argv) {
if (argv.quiet) {
log.info = function () { };
log.trace = function () { };
}
const options = createOptions(argv);
const metadataOut = (options.metadata.out === 'ignore') ? undefined : options.metadata.out;
cli(options).then((response) => {
if (metadataOut && (metadataOut.trim() !== '')) {
if (metadataOut === 'stdout') {
process.stdout.write(JSON.stringify(response, null, 2));
} else {
fs.writeFileSync(options.metadata.out, JSON.stringify(response, null, 2), 'utf8');
}
} else {
log.info('complete');
}
})
.catch((err) => {
log.error(err.message);
if (err && err.statusCode) {
log.error('Status code', err.statusCode);
}
require('util').debuglog('nodeshift.cli')(err.stack);
process.exit(1);
});
}
function createOptions (argv) {
const options = {};
options.useDeployment = argv.useDeployment;
options.knative = argv.knative === true || argv.knative === 'true';
options.kube = argv.kube === true || argv.kube === 'true';
options.projectLocation = argv.projectLocation;
options.dockerImage = argv.dockerImage;
options.imageTag = argv.imageTag;
options.webApp = argv.webApp;
options.resourceProfile = argv.resourceProfile;
options.outputImageStreamName = argv.outputImageStream;
options.outputImageStreamTag = argv.outputImageStreamTag;
process.env.NODESHIFT_QUIET = argv.quiet === true;
options.metadata = argv.metadata;
options.build = argv.build;
options.deploy = argv.deploy;
options.cmd = argv.cmd;
// undeploy might have a positional argument
options.removeAll = argv.removeAll;
options.namespace = argv.namespace;
options.expose = argv.expose;
options.configLocation = argv.configLocation;
// Check for the --build.env array
// If it is there, we need to parse it
// The values should be in the format KEY=value
if (argv.build.env) {
options.build.env = helpers.parseMultiOption(argv.build.env);
}
// Check for the --deploy.env array
// If it is there, we need to parse it
// The values should be in the format KEY=value
if (argv.deploy.env) {
options.deploy.env = helpers.parseMultiOption(argv.deploy.env);
}
// Check for the -d array
// If it is there, we need to parse it.
// The values should be in the format KEY=value
if (argv.d) {
options.definedProperties = helpers.parseMultiOption(argv.d);
} else {
options.definedProperties = [];
}
return options;
}