This repository was archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportAndDeploy.js
More file actions
executable file
·145 lines (130 loc) · 5.14 KB
/
importAndDeploy.js
File metadata and controls
executable file
·145 lines (130 loc) · 5.14 KB
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
#! /usr/local/bin/node
/*jslint node:true */
// importAndDeploy.js
// ------------------------------------------------------------------
// import and deploy an Apigee Edge proxy bundle or shared flow.
//
// Copyright 2017-2018 Google LLC.
//
// 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
//
// https://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.
//
// last saved: <2018-May-31 17:21:26>
const edgejs = require('apigee-edge-js'),
common = edgejs.utility,
apigeeEdge = edgejs.edge,
sprintf = require('sprintf-js').sprintf,
Getopt = require('node-getopt'),
version = '20180531-1645',
defaults = { basepath : '/' },
getopt = new Getopt(common.commonOptions.concat([
['d' , 'source=ARG', 'source directory for the proxy files. Should be parent of dir "apiproxy" or "sharedflowbundle"'],
['N' , 'name=ARG', 'override the name for the API proxy or shared flow. By default it\'s extracted from the XML file.'],
['e' , 'env=ARG', 'the Edge environment(s) to which to deploy the asset. Separate multiple environments with a comma.'],
['b' , 'basepath=ARG', 'basepath for deploying the API Proxy. Default: ' + defaults.basepath + ' Does not apply to sf.'],
['S' , 'sharedflow', 'import and deploy as a sharedflow. Default: import + deploy a proxy.'],
['T' , 'notoken', 'optional. do not try to get a authentication token.']
])).bindHelp();
// ========================================================
function promisifyDeployment(collection, options) {
return function deploy(env) {
return new Promise(function(resolve, reject) {
options.environment = env;
collection.deploy(options, function(e, result) {
if (e) {
common.logWrite(JSON.stringify(e, null, 2));
if (result) { common.logWrite(JSON.stringify(result, null, 2)); }
reject(e);
}
common.logWrite('deploy ok.');
resolve();
});
});
};
}
console.log(
'Apigee Edge Proxy/Sharedflow Import + Deploy tool, version: ' + version + '\n' +
'Node.js ' + process.version + '\n');
common.logWrite('start');
// process.argv array starts with 'node' and 'scriptname.js'
var opt = getopt.parse(process.argv.slice(2));
if ( !opt.options.source ) {
console.log('You must specify a source directory');
getopt.showHelp();
process.exit(1);
}
if (opt.options.basepath && opt.options.sharedflow) {
console.log('It does not make sense to use a basepath when deploying a sharedflow.');
getopt.showHelp();
process.exit(1);
}
common.verifyCommonRequiredParameters(opt.options, getopt);
var options = {
mgmtServer: opt.options.mgmtserver,
org : opt.options.org,
user: opt.options.username,
password: opt.options.password,
no_token: opt.options.notoken,
verbosity: opt.options.verbose || 0
};
apigeeEdge.connect(options, function(e, org){
if (e) {
common.logWrite(JSON.stringify(e, null, 2));
process.exit(1);
}
common.logWrite('connected');
var collection = (opt.options.sharedflow) ? org.sharedflows : org.proxies;
var term = (opt.options.sharedflow) ? 'sharedflow' : 'proxy';
common.logWrite('importing');
collection.import({name:opt.options.name, source:opt.options.source}, function(e, result) {
if (e) {
common.logWrite('error: ' + JSON.stringify(e, null, 2));
if (result) { common.logWrite(JSON.stringify(result, null, 2)); }
//console.log(e.stack);
process.exit(1);
}
common.logWrite(sprintf('import ok. %s name: %s r%d', term, result.name, result.revision));
var env = opt.options.env || process.env.ENV;
if (env) {
// env may be a comma-separated list
var options = {
name: result.name,
revision: result.revision,
};
if ( ! opt.options.sharedflow) {
options.basepath = opt.options.basepath || defaults.basepath;
}
// this magic deploys to each environment in series
var deployIt = promisifyDeployment(collection, options);
var reducer = function (promise, env) {
return promise.then(() => {
return deployIt(env).then(result => results.push(result));
})
.catch(console.error);
};
let results = [];
var p = opt.options.env.split(',')
.reduce(reducer, Promise.resolve())
.then(() => { common.logWrite('all done...'); })
.catch(console.error);
// Promise.all(opt.options.env.split(',').forEach(deployIt))
// .then(() => { common.logWrite('all don...'); })
// .catch((data) => {
// console.log(data);
// });
}
else {
common.logWrite('not deploying...');
common.logWrite('finish');
}
});
});