-
Notifications
You must be signed in to change notification settings - Fork 72
/
flightplan.js
105 lines (82 loc) · 3.51 KB
/
flightplan.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
// Flightplan - Deployment and Server Administration
//
// Acceptable arguments:
// --skip-webpack If I've recently bundled, I can skip the bundling.
// --fresh-dependencies Don't copy cached NPM module dependencies.
'use strict';
require('babel-core/register');
require('./server/initialize');
const plan = require('flightplan');
const _ = require('lodash');
const nconf = require('nconf');
const moment = require('moment');
const privateKey = process.env.HOME + "/.ssh/id_rsa";
const user = 'deploy';
const appName = 'panther';
const newDirectoryName = 'panther_' + moment().format('YYYY-MM-DD_hh[h]mm[m]ss[s]');
const tempDir = `/tmp/${newDirectoryName}`;
const projectDir = `/home/${user}/${appName}`;
const newDirectory = `${projectDir}/${newDirectoryName}`;
const linkedDirectory = `${projectDir}/current`;
const MAX_SAVED_DEPLOYS = 3;
const restartServer = (remote) => {
// Start/Restart the application
// First, figure out if the app is already running
let appDetails = remote.exec(`pm2 show ${appName}`, {failsafe: true});
let appNotRunning = !!appDetails.stderr;
if ( appNotRunning ) {
remote.log("App is not already running. Starting it fresh")
remote.exec(`pm2 start ${linkedDirectory}/server --name="${appName}"`)
} else {
remote.log("Restarting app")
remote.exec(`pm2 restart ${appName}`)
}
}
plan.target('production', {
host: nconf.get('SERVER_HOST'),
username: nconf.get('SERVER_USER'),
agent: process.env.SSH_AUTH_SOCK
});
plan.local( 'deploy', local => {
local.log(`Deployment started! Deploying to ${newDirectoryName}`);
if ( !plan.runtime.options['skip-webpack'] ) {
local.log('Webpacking everything up.');
local.exec('webpack -p --config webpack.prod.js');
} else {
local.log('Skipping webpack bundle.')
}
local.log('Copying files to remote');
// const filenames = ['index.html', 'dist', 'client', 'server', 'package.json'];
// const files = filenames.reduce( (memo, filename) => {
// let file = local.find(filename, { silent: true }).stdout.split('\n');
// memo.push(file);
// return memo;
// }, []);
const index = local.find('index.html', {silent: true}).stdout.split('\n');
const dist = local.find('dist', {silent: true}).stdout.split('\n');
const client = local.find('client', {silent: true}).stdout.split('\n');
const server = local.find('server', {silent: true}).stdout.split('\n');
const packjson = local.find('package.json', {silent: true}).stdout.split('\n');
const files = [].concat(index, dist, client, server, packjson);
local.transfer(files, `/tmp/${newDirectoryName}`);
});
plan.remote( 'deploy', remote => {
remote.log('Move folder to web root')
remote.sudo(`cp -R ${tempDir} ${newDirectory}`, { user });
remote.rm(`-rf ${tempDir}`); // clean up after ourselves
if ( !plan.runtime.options['fresh-dependencies'] ) {
remote.log('Copying dependencies from last deploy');
remote.exec(`cp -R ${linkedDirectory}/node_modules ${newDirectory}/node_modules`)
}
remote.log('Installing dependencies');
remote.sudo(
`npm --production --prefix ${newDirectory} install ${newDirectory}`,
{ user }
);
remote.log('Creating symlink');
remote.sudo(`ln -snf ${newDirectory} ${linkedDirectory}`, { user });
restartServer(remote);
remote.log('Removing oldest copies of deploy');
remote.exec(`cd ${projectDir} && rm -rf \`ls -td wws_* | awk 'NR>${MAX_SAVED_DEPLOYS}'\``);
});
plan.remote("restart", restartServer);