forked from woz-u/hackathon-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
137 lines (126 loc) · 4.11 KB
/
Copy pathdeploy.js
File metadata and controls
137 lines (126 loc) · 4.11 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
//Deployment Script
var cmd = require('node-cmd'),
fs = require('fs'),
path = require('path'),
node_ssh = require('node-ssh'),
ssh = new node_ssh();
//Configuration
var repo = 'hackathon-starter';
var repoPath = 'https://github.com/Xuroth/hackathon-starter.git';
var remotePath = '/home/ubuntu';
var server = '18.218.105.29';
var username = 'ubuntu';
var pemFile = 'hs-key.pem';
var manualFiles = [
'.env.example'
];
function main() {
console.log('Deployment Started.');
cloneRepo();
}
function cloneRepo() {
console.log(`Cloning Repo "${repo}"`);
cmd.get(`rm -rf ${repo} && git clone ${repoPath}`, (err, data, stderr) => {
console.log(`cloneRepo Callback\n\t err: ${err}\n\t data: ${data}\n\t stderr: ${stderr}`);
if (err == null){
sshConnect();
}
});
}
function transferProjectToRemote(failed, successful) {
return ssh.putDirectory(__dirname + `/${repo}`, `${remotePath}/${repo}-temp`, {
recursive: true,
concurrency: 1,
validate: (itemPath) => {
const baseName = path.basename(itemPath);
console.log('Basename is '+baseName);
if (manualFiles.indexOf(baseName) > -1){
console.log(`Required file: "${baseName}" found!`);
// throw new Error(baseName);
return true;
} else if(baseName.substr(0,1) !== '.' && baseName !== 'node_modules') {
return true;
} else {
return false;
}
// return baseName in manualFiles || (baseName.substr(0,1) !== '.' && baseName !== 'node_modules')
},
tick: (localPath, remote, error) => {
if (error) {
failed.push(localPath);
console.log(`Failed to transfer "${localPath}"`);
} else {
successful.push(localPath);
console.log(`Successfully transferred "${localPath}" to "${remote}"`);
}
}
})
}
function createRemoteTempFolder() {
console.log('Creating temp directory on remote host.');
return ssh.execCommand(`rm -rf ${repo}-temp && mkdir ${repo}-temp`, {cwd: remotePath});
}
function stopRemoteServices() {
console.log(`Stopping remote services on host`);
return ssh.execCommand(`npm stop && sudo service mongod stop`, {cwd: remotePath});
}
function updateRemoteApp() {
console.log('Updating files on host');
return ssh.execCommand(`mkdir -p ${repo} && shopt -s dotglob && cp -rT ${repo}-temp/. ${repo}/ && rm -rf ${repo}-temp/*`, {cwd: remotePath});
}
function restartRemoteServices() {
console.log('Restarting remote services on host');
return ssh.execCommand('npm start && sudo service mongod start', {cwd: remotePath+'/'+repo});
}
function sshConnect() {
console.log(`Establishing connection to remote server: ${server}`);
ssh.connect({
host: server,
username: username,
privateKey: pemFile
})
.then( () => {
console.log(`Connection to remote server established.`);
return createRemoteTempFolder();
})
.then( (result) => {
const failed = [];
const successful = [];
if (result.stdout) { console.log(`STDOUT: ${result.stdout}`); }
if (result.stderr) {
console.log(`STDERR: ${result.stderr}`);
return Promise.reject(result.stderr);
}
return transferProjectToRemote(failed, successful);
})
.then( (status) => {
if (status) {
return stopRemoteServices();
} else {
return Promise.reject(failed.join(', '));
}
})
.then( (status) => {
if (status) {
return updateRemoteApp();
} else {
return Promise.reject(failed.join(', '));
}
})
.then( (status) => {
if (status) {
return restartRemoteServices();
} else {
return Promise.reject(failed.join(', '));
}
})
.then( () => {
console.log('Deployment complete.');
process.exit(0);
})
.catch( e=> {
console.log(e);
process.exit(1);
})
}
main();