-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstart.js
More file actions
187 lines (163 loc) · 5.22 KB
/
start.js
File metadata and controls
187 lines (163 loc) · 5.22 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
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
'use strict';
/**
* External dependencies
*/
const http = require('http');
const program = require('commander');
const fs = require('fs');
const semver = require('semver');
const Step = require('step');
const shelljs = require('shelljs/global');
const path = require('path');
/**
* Local modules
*/
const stop = require('./stop');
const mongod = require('../util/mongod');
const repl = require('../client/repl');
const packageInfo = require('../../package');
const latestversionFile = path.join(__dirname, '../../.latestversion');
const createServer = require('./createserver');
/**
* Start the server
*/
const start = function (file) {
let port = program.port;
const host = program.host || '127.0.0.1';
const dbname = program.dbname || '-deployd';
const mongoPort = program.mongoPort ? Number(program.mongoPort) : '27017';
const env = program.environment || process.env.DPD_ENV || 'development';
let retries = 0;
const credentials = {};
if (!port) {
port = 2403;
retries = env === 'development' && 5;
}
if (file) {
process.chdir(path.dirname(file));
}
if (test('-f', 'app.dpd')) {
console.log(`deployd CLI version ${packageInfo.version}`);
console.log('starting deployd');
if (fs.existsSync(latestversionFile)) {
const latest = fs.readFileSync(latestversionFile, 'utf-8');
if (latest && semver.gt(latest, packageInfo.version)) {
console.log(`deployd CLI v${latest} is available.`);
console.log();
}
}
checkForUpdates();
if (!test('-d', './.dpd')) mkdir('-p', './.dpd');
if (!test('-d', './.dpd/pids')) mkdir('-p', './.dpd/pids');
if (!test('-d', './data')) mkdir('-p', './data');
if (program.auth && program.host === undefined) {
console.error("Authentication requires the '-h' host flag... exiting.");
process.exit();
}
function setCredentials(username, password) { // eslint-disable-line no-inner-declarations
Step(function () {
const next = this;
if (username && username !== '') {
credentials.username = username;
next();
} else {
console.error('Username cannot be blank.');
process.exit();
}
},
function () {
const next = this;
if (password && password !== '') {
credentials.password = password;
next();
} else {
console.error('Password cannot be blank.');
process.exit();
}
},
startup
);
}
if (program.host) {
if (program.auth) {
const auth = program.auth.split(':');
const username = auth[0];
const password = auth[1];
setCredentials(username, password);
} else if (program.username || program.password) {
setCredentials(program.username, program.password);
} else {
startup();
}
} else {
mongod.restart(program.mongod || 'mongod', env, mongoPort, startup);
}
} else {
console.log('This directory does not contain a Deployd app!');
console.log('Use "dpd create <appname>" to create a new app');
console.log('or use "dpd path/to/app.dpd" to start an app in another directory');
stop(1);
}
function startup(err) {
if (err) {
console.log("Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)");
return stop(1);
}
const options = { port, env: 'development', db: { host, port: mongoPort, name: dbname } };
options.env = program.environment || process.env.DPD_ENV || options.env;
if (options.env !== 'development') console.log('starting in %s mode', options.env);
if (credentials !== undefined) options.db.credentials = credentials;
let dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
dpd.deploydPath = program.deploydPath;
function onListening() {
console.info('listening on port', options.port);
const commands = repl(dpd);
if (program.dashboard) {
commands.dashboard();
} else if (program.open) {
commands.open();
}
}
function onError(err2) {
if (err2.code === 'EADDRINUSE') {
console.error();
console.error(`ERROR: port ${options.port} is already in use`);
if (retries > 0) {
options.port += 1;
console.log(`Trying again on port ${options.port}...`);
console.log();
retries -= 1;
dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
} else {
process.exit();
}
} else {
console.error(err2);
process.exit();
}
}
}
};
function checkForUpdates() {
http.get('http://registry.npmjs.org/deployd-cli', (err, res, body) => {
if (!err) {
let json;
try {
json = JSON.parse(body);
} catch (ex) {
console.log('Could not parse body', body);
}
if (json && json['dist-tags'] && json['dist-tags'].latest) {
const latest = json['dist-tags'].latest;
fs.writeFile(latestversionFile, latest);
}
}
});
}
module.exports = start;