-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
139 lines (112 loc) · 3.46 KB
/
index.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
"use strict";
const fs = require('fs');
const path = require('path');
const fsp = require('fs-promise');
const exec = require('child_process').exec;
function AppNotFoundError(message) {
let error = new Error(message);
error.name = 'AppNotFoundError';
return error;
}
let gcloud = require('gcloud');
let storage = gcloud.storage();
/*
* Downloader class that downloads the latest version of the deployed
* app from GCloud Storage and unzips it.
*/
class GCloudStorageDownloader {
constructor(options) {
this.ui = options.ui;
this.configBucket = options.bucket;
this.configKey = options.key;
}
download() {
if (!this.configBucket || !this.configKey) {
this.ui.writeError('no gcloud Storage bucket or key; not downloading app');
return Promise.reject(new AppNotFoundError());
}
return this.fetchCurrentVersion()
.then(() => this.removeOldApp())
.then(() => this.downloadAppZip())
.then(() => this.unzipApp())
.then(() => this.installNPMDependencies())
.then(() => this.outputPath);
}
removeOldApp() {
this.ui.writeLine('removing ' + this.outputPath);
return fsp.remove(this.outputPath);
}
fetchCurrentVersion() {
let bucket = this.configBucket;
let key = this.configKey;
this.ui.writeLine('fetching current app version from ' + bucket + '/' + key);
let stream = this.readStream(bucket, key);
return this.streamToPromise(stream)
.then(data => {
let config = JSON.parse(data);
this.ui.writeLine('got config', config);
this.appBucket = config.bucket;
this.appKey = config.key;
this.zipPath = path.basename(config.key);
this.outputPath = outputPathFor(this.zipPath);
});
}
readStream(bucket, key) {
let file = storage.bucket(bucket).file(key);
return file.createReadStream();
}
streamToPromise(stream) {
return new Promise(function(resolve, reject){
let data = '';
stream.on('error', reject)
.on('data', chunk => data+=chunk )
.on('end', function() {
resolve(data);
});
});
}
downloadAppZip() {
return new Promise((res, rej) => {
let bucket = this.appBucket;
let key = this.appKey;
let zipPath = this.zipPath;
let file = fs.createWriteStream(zipPath);
let stream = this.readStream(bucket, key);
this.ui.writeLine("saving gcloud Storage object " + bucket + "/" + key + " to " + zipPath);
stream.pipe(file)
.on('close', res)
.on('error', rej);
});
}
unzipApp() {
let zipPath = this.zipPath;
return this.exec('unzip ' + zipPath)
.then(() => {
this.ui.writeLine("unzipped " + zipPath);
});
}
installNPMDependencies() {
return this.exec(`cd ${this.outputPath} && npm install`)
.then(() => this.ui.writeLine('installed npm dependencies'))
.catch(() => this.ui.writeError('unable to install npm dependencies'));
}
exec(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
this.ui.writeError(`error running command ${command}`);
this.ui.writeError(stderr);
reject(error);
} else {
resolve();
}
});
});
}
}
function outputPathFor(zipPath) {
let name = path.basename(zipPath, '.zip');
// Remove MD5 hash
return name.split('-').slice(0, -1).join('-');
}
module.exports = GCloudStorageDownloader;