forked from ryanmurakami/hbfl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
128 lines (111 loc) · 3.17 KB
/
deploy.js
File metadata and controls
128 lines (111 loc) · 3.17 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
const archiver = require('archiver')
const ProgressBar = require('progress')
const fs = require('fs')
const path = require('path')
const os = require('os')
const client = require('scp2')
const exec = require('ssh-exec')
const archivePath = path.join(__dirname, 'archive.zip')
const output = fs.createWriteStream(archivePath)
const archive = archiver('zip', {
zlib: { level: 9 }
})
let bar
let len
let prevProgress = 0
output.on('close', () => {
const ip = process.argv[2]
console.log('\nArchiving Complete')
upload(archivePath, ip)
.then(() => {
console.log('\nUploading Complete')
return unpack(ip)
})
.then(() => {
console.log('Deployment Complete')
})
.catch(err => console.error(err))
})
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.error('ENOENT', err)
} else {
throw err
}
})
archive.on('progress', (progress) => {
if (!len || len !== progress.entries.total) {
len = progress.entries.total
bar = new ProgressBar('Archiving [:bar] :percent :etas remaining', {
complete: '=',
incomplete: ' ',
width: 20,
total: len
})
} else {
bar.tick(progress.entries.processed - prevProgress)
prevProgress = progress.entries.processed
}
})
archive.on('error', (err) => {
throw err
})
archive.pipe(output)
archive.directory('app/', 'app')
archive.directory('handlers/', 'handlers')
archive.directory('lib/', 'lib')
archive.directory('plugins/', 'plugins')
archive.directory('public/', 'public')
archive.directory('routes/', 'routes')
archive.directory('util/', 'util')
archive.append(fs.createReadStream(path.join(__dirname, 'index.js')), { name: 'index.js' })
archive.append(fs.createReadStream(path.join(__dirname, 'package.json')), { name: 'package.json' })
archive.append(fs.createReadStream(path.join(__dirname, 'webpack.config.js')), { name: 'webpack.config.js' })
archive.finalize()
function upload (file, ip) {
let uploadBar
let uploadLen
let prevProgress = 0
return new Promise((resolve, reject) => {
const myClient = new client.Client()
myClient.on('transfer', (buffer, uploaded, total) => {
if (!uploadLen || uploaded === total) {
uploadLen = +total
uploadBar = new ProgressBar('Uploading [:bar] :percent :etas remaining', {
complete: '=',
incomplete: ' ',
width: 20,
total: uploadLen
})
} else {
uploadBar.tick(uploaded - prevProgress)
prevProgress = uploaded
}
})
client.scp(file, {
host: ip,
username: 'bitnami',
privateKey: require('fs').readFileSync(path.join(os.homedir(), '.ssh', 'hamster_key')),
path: '/home/bitnami/'
},
myClient,
(err) => {
if (err) reject(err)
else resolve('Successfully Uploaded')
})
})
}
function unpack (ip) {
return new Promise((resolve, reject) => {
const command = 'cd /home/bitnami && ' +
'unzip -o -q ./archive.zip -d hbfl && ' +
'cd hbfl && ' +
'npm install && ' +
'sudo npm start'
exec(command, {
user: 'bitnami',
host: ip,
key: require('fs').readFileSync(path.join(os.homedir(), '.ssh', 'hamster_key'))
}).pipe(process.stdout)
})
}