-
Notifications
You must be signed in to change notification settings - Fork 0
/
shipwright.js
executable file
·321 lines (257 loc) · 6.33 KB
/
shipwright.js
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env node
"use strict";
var async = require( 'async' );
var dashdash = require( 'dashdash' );
var fs = require( 'fs' );
var osenv = require( 'osenv' );
var path = require( 'path' );
var prettyjson = require( 'prettyjson' );
var req = require( 'request' ).defaults( {
baseUrl: 'https://api.digitalocean.com/v2/',
json: true
} );
var untildify = require( 'untildify' );
var val = require( './val' );
var scriptName = path.basename( __filename );
var parserOptions = require( './parseroptions' );
var parser = dashdash.createParser( {
options: parserOptions
} );
try {
var opts = parser.parse( process.argv );
}
catch ( exception ) {
console.error( exception.message );
process.exit( 1 );
}
var args = opts._args;
if ( opts.version ) {
var pkg = require( './package.json' );
console.log( scriptName + ' v' + pkg.version );
process.exit( 0 );
}
if ( opts.help || args.length < 2 ) {
var help = parser.help( {
includeEnv: true
} ).trimRight();
console.log( 'usage: node ' + scriptName + ' [OPTIONS] OPERATION RESOURCE [ARGS]\n' + 'options:\n' + help );
process.exit( 0 );
}
var operationAliases = {
'create': 'POST',
'destroy': 'DELETE'
};
var operation = args.shift();
operation = operationAliases[ operation.toLowerCase() ] || operation;
var resource = args.shift();
var requestData = null;
var responseData = null;
var finalQuery = null;
var output = null;
async.series( [
// check if we need to read token from disk
function( next ) {
if ( opts.token ) {
next();
return;
}
var tokenFile = path.join( osenv.home(), '.digitalocean.token' );
if ( !fs.existsSync( tokenFile ) ) {
next();
return;
}
var tokenFileContent = fs.readFileSync( tokenFile, 'utf8' );
if ( !tokenFileContent ) {
next();
return;
}
var lines = tokenFileContent.split( '\n' );
if ( !lines.length ) {
next();
return;
}
opts.token = lines[ 0 ].trim();
next();
},
// check if we have a token
function( next ) {
if ( !opts.token ) {
next( 'You must specify a DigitalOcean API token, either via the --token option, the DO_TOKEN environment variable or in ~/.digitalocean.token' );
return;
}
next();
},
// see if we need to read the request body from stdin
function( next ) {
if ( !( opts.stdin || !process.stdin.isTTY ) ) {
next();
return;
}
var input = '';
process.stdin.setEncoding( 'utf8' );
process.stdin.on( 'readable', function() {
var chunk = process.stdin.read();
if ( chunk !== null ) {
input += chunk;
}
} );
process.stdin.on( 'end', function() {
var error = null;
try {
requestData = JSON.parse( input );
} catch( exception ) {
error = exception.message;
}
next( error );
} );
},
// check for parameters to add to request data
function( next ) {
var nameValueRegex = new RegExp( /^(.*?)=(.*)$/ );
var arg = args.shift();
while( arg ) {
var match = nameValueRegex.exec( arg );
if ( !match || match.length !== 3 ) {
next( 'Unparsable parameter: ' + arg );
return;
}
var name = match[ 1 ];
var value = val( match[ 2 ] );
requestData = requestData || {};
requestData[ name ] = value;
arg = args.shift();
}
next();
},
// check if we need to read any parameters from files
function( next ) {
if ( !opts.fromfile ) {
next();
return;
}
var paramsFromFiles = opts.fromfile;
var nameValueRegex = new RegExp( /^(.*?),(.*)$/ );
var paramFromFile = paramsFromFiles.shift();
while( paramFromFile ) {
var match = nameValueRegex.exec( paramFromFile );
if ( !match || match.length !== 3 ) {
next( 'Unparsable fromfile parameter: ' + paramFromFile );
return;
}
var name = match[ 1 ];
var filename = path.resolve( untildify( match[ 2 ] ) );
if ( !fs.existsSync( filename ) ) {
next( 'File does not exist: ' + filename );
return;
}
var value = fs.readFileSync( filename, 'utf8' );
requestData = requestData || {};
requestData[ name ] = value ? value.trim() : null;
paramFromFile = paramsFromFiles.shift();
}
next();
},
// make the request to DO
function( next ) {
var request = {
method: operation.toUpperCase(),
uri: resource,
headers: {
'Authorization': 'Bearer ' + opts.token
}
};
if ( opts.verbose ) {
console.log( "Request data: " );
console.log( JSON.stringify( requestData, null, 4 ) );
}
if ( requestData ) {
if ( operation.toUpperCase() === 'GET' ) {
request.qs = requestData;
}
else {
request.body = requestData;
}
}
req( request, function( error, response, body ) {
if ( error ) {
next( error );
return;
}
if ( response.statusCode >= 400 && response.statusCode < 600 ) {
next( body );
return;
}
if ( !body ) {
next( 'no response body' );
return;
}
responseData = body;
output = opts.json ? JSON.stringify( responseData, null, opts.pretty ? 4 : 0 ) : prettyjson.render( responseData, {} );
next();
} );
},
function( next ) {
if ( !opts.wait ) {
next();
return;
}
var action = responseData.links.actions[ 0 ];
var request = {
method: 'GET',
uri: 'actions/' + action.id,
headers: {
'Authorization': 'Bearer ' + opts.token
}
};
(function checkAction() {
req( request, function( error, response, body ) {
if ( error ) {
next( error );
return;
}
if ( body.action.status.toLowerCase() === 'completed' ) {
finalQuery = body.action.resource_type + 's/' + body.action.resource_id; // note the pluralization before the slash
next();
return;
}
if ( body.action.status.toLowerCase() === 'errored' ) {
next( 'Action errored!' );
return;
}
setTimeout( checkAction, 1000 );
} );
})();
},
// check if we have a final query to put into the output
function( next ) {
if ( !finalQuery ) {
next();
return;
}
var request = {
method: 'GET',
uri: finalQuery,
headers: {
'Authorization': 'Bearer ' + opts.token
}
};
req( request, function( error, response, body ) {
if ( error ) {
next( error );
return;
}
responseData = body;
output = opts.json ? JSON.stringify( responseData, null, opts.pretty ? 4 : 0 ) : prettyjson.render( responseData, {} );
next();
} );
}
], function( error ) {
if ( error ) {
console.error( error );
process.exit( 1 );
}
if ( output ) {
console.log( output );
}
process.exit( 0 );
} );