-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsl-deploy.js
executable file
·157 lines (137 loc) · 4.02 KB
/
sl-deploy.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
#!/usr/bin/env node
'use strict';
var Parser = require('posix-getopt').BasicParser;
var debug = require('debug')('strong-deploy');
var defaults = require('strong-url-defaults');
var defaultPackagePath = require('../lib/package').getPackagePath;
var deploy = require('../');
var fs = require('fs');
var getPackageInfo = require('../lib/package').getPackageInfo;
var g = require('strong-globalize');
var maybeTunnel = require('strong-tunnel');
var path = require('path');
function printHelp($0, prn) {
var USAGE = g.t('sl-deploy.txt')
.replace(/%MAIN%/g, $0).
trim();
prn(USAGE);
}
g.setRootDir(path.resolve(__dirname, '..'));
var argv = process.argv;
var $0 = process.env.CMD ? process.env.CMD : path.basename(argv[1]);
var parser = new Parser([
':v(version)',
'h(help)',
's:(service)',
'z:(size)',
'L(local)' // Undocumented for now, just for local testing
].join(''),
argv);
var option;
var branchOrPack;
var local;
var serviceName;
var clusterSize;
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'v':
console.log(require('../package.json').version);
process.exit(0);
break;
case 'h':
printHelp($0, console.log);
process.exit(0);
break;
case 's':
serviceName = option.optarg;
break;
case 'z':
clusterSize = option.optarg;
break;
case 'L':
local = true;
break;
default:
g.error('Invalid usage (near option \'%s\'), try {{`%s --help`}}.',
option.optopt,
$0);
process.exit(1);
}
}
var numArgs = argv.length - parser.optind();
if (numArgs > 2) {
g.error('Invalid usage, try {{`%s --help`}}.', $0);
process.exit(1);
}
var workingDir = process.cwd();
// XXX(sam) our CWD isn't necessarily the package we are deploying, we should
// get the package name from the git branch, or the tarball being deployed,
// or the path being deployed.
var packageInfo = getPackageInfo(workingDir);
serviceName = serviceName || (packageInfo ? packageInfo.name : null);
if (!serviceName) {
g.error(
'Unable to detect service name, {{package.json}} ' +
'has no "name" property.\n' +
'Please update your package.json or specify a service name.\n' +
'See {{`%s --help`}} for more details.', $0
);
process.exit(1);
}
var baseURL = argv[parser.optind()];
branchOrPack = argv[parser.optind() + 1];
baseURL = baseURL || 'http://';
branchOrPack = branchOrPack || defaultPackagePath(workingDir) || 'deploy';
// Truncate any paths from the baseURL, because the final path of the URL is
// dependent on the ID of the service being updated by this deployment.
// Older versions of deploy use to allow paths on the git push.
baseURL = defaults(baseURL, {host: '127.0.0.1', port: 8701}, {path: '/'});
debug('deploy %j to %j', branchOrPack, baseURL);
var sshOpts = {};
if (process.env.SSH_USER) {
sshOpts.username = process.env.SSH_USER;
}
if (process.env.SSH_KEY) {
sshOpts.privateKey = fs.readFileSync(process.env.SSH_KEY);
}
if (process.env.SSH_PORT) {
sshOpts.port = process.env.SSH_PORT;
}
exit.url = baseURL;
if (!local) {
maybeTunnel(baseURL, sshOpts, function(err, url) {
exit.url = url;
if (err) {
g.error('Error setting up tunnel: %s', err);
return exit(err);
}
debug('Connecting to %s via %s', baseURL, url);
var options = {
workingDir: workingDir,
baseURL: url,
serviceName: serviceName,
branchOrPack: branchOrPack,
clusterSize: clusterSize,
};
deploy(options, exit);
});
} else {
var options = {
baseURL: baseURL,
serviceName: serviceName,
branchOrPack: branchOrPack,
clusterSize: clusterSize,
};
deploy.local(options, exit);
}
function exit(err, service) {
var svc = service ? (service.name || service.id) : serviceName;
if (err) {
g.error('Failed to deploy `%s` as `%s` to `%s` via `%s`',
branchOrPack, svc, baseURL, exit.url);
process.exit(1);
} else {
g.log('Deployed `%s` as `%s` to `%s`', branchOrPack, svc, baseURL);
process.exit(0);
}
}