Skip to content

Commit bb3225c

Browse files
committed
Merge branch 'master' into bemasc-integration
2 parents 6a79be5 + 1871b19 commit bb3225c

File tree

15 files changed

+177
-108
lines changed

15 files changed

+177
-108
lines changed

Gruntfile.coffee

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ config =
6262
###
6363
# Samples.
6464
###
65-
libsForProvisionChromeApp:
65+
libsForDeployerChromeApp:
6666
Rule.copyLibs
6767
npmLibNames: ['freedom-for-chrome', 'forge-min']
68-
pathsFromDevBuild: ['loggingprovider', 'cloud/provision']
69-
pathsFromThirdPartyBuild: [
70-
]
71-
localDestPath: 'samples/provision-chromeapp/'
68+
pathsFromDevBuild: ['loggingprovider', 'cloud/deployer', 'cloud/digitalocean']
69+
localDestPath: 'samples/deployer-chromeapp/'
70+
libsForDeployerFirefoxApp:
71+
Rule.copyLibs
72+
npmLibNames: ['freedom-for-firefox', 'forge-min']
73+
pathsFromDevBuild: ['loggingprovider', 'cloud/deployer', 'cloud/digitalocean']
74+
localDestPath: 'samples/deployer-firefoxapp/data'
75+
7276
libsForZorkChromeApp:
7377
Rule.copyLibs
7478
npmLibNames: ['freedom-for-chrome']
@@ -295,6 +299,7 @@ config =
295299
ignore: ['ws', 'path']
296300
browserifyOptions: { standalone: 'browserified_exports' }
297301
})
302+
digitalOceanFreedomModule: Rule.browserify 'cloud/digitalocean/freedom-module'
298303
# Sample app freedom modules.
299304
copypasteChatFreedomModule: Rule.browserify 'copypaste-chat/freedom-module'
300305
copypasteSocksFreedomModule: Rule.browserify 'copypaste-socks/freedom-module'
@@ -312,12 +317,12 @@ config =
312317
'./src/cloud/social/alias/randombytes.js:randombytes'
313318
]
314319
})
320+
deployerFreedomModule: Rule.browserify 'cloud/deployer/freedom-module'
315321
simpleChatFreedomModule: Rule.browserify 'simple-chat/freedom-module'
316322
simpleSocksFreedomModule: Rule.browserify 'simple-socks/freedom-module'
317323
simpleTurnFreedomModule: Rule.browserify 'simple-turn/freedom-module'
318324
uprobeFreedomModule: Rule.browserify 'uprobe/freedom-module'
319325
zorkFreedomModule: Rule.browserify 'zork/freedom-module'
320-
provisionFreedomModule: Rule.browserify 'cloud/provision/freedom-module'
321326
# Sample app main environments (samples with UI).
322327
copypasteChatMain: Rule.browserify 'copypaste-chat/main.core-env'
323328
copypasteSocksMain: Rule.browserify 'copypaste-socks/main.core-env'
@@ -428,18 +433,19 @@ taskManager.add 'base', [
428433
'browserify:loggingProvider'
429434
'browserify:churnPipeFreedomModule'
430435
'browserify:cloudSocialProviderFreedomModule'
436+
'browserify:digitalOceanFreedomModule'
431437
]
432438

433439
taskManager.add 'samples', [
434440
'echoServer'
435441
'copypasteChat'
436442
'copypasteSocks'
443+
'deployer'
437444
'simpleChat'
438445
'simpleSocks'
439446
'simpleTurn'
440447
'uprobe'
441448
'zork'
442-
'provision'
443449
]
444450

445451
# Makes the distribution build.
@@ -479,6 +485,13 @@ taskManager.add 'copypasteSocks', [
479485
'copy:libsForCopyPasteSocksFirefoxApp'
480486
]
481487

488+
taskManager.add 'deployer', [
489+
'base'
490+
'browserify:deployerFreedomModule'
491+
'copy:libsForDeployerChromeApp'
492+
'copy:libsForDeployerFirefoxApp'
493+
]
494+
482495
taskManager.add 'simpleChat', [
483496
'base'
484497
'browserify:simpleChatFreedomModule'
@@ -517,12 +530,6 @@ taskManager.add 'zork', [
517530
'copy:libsForZorkNode'
518531
]
519532

520-
taskManager.add 'provision', [
521-
'base'
522-
'browserify:provisionFreedomModule'
523-
'copy:libsForProvisionChromeApp'
524-
]
525-
526533
specList = Rule.getTests('src', undefined, ['integration-tests'])
527534

528535
# Run unit tests
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "deployer",
3+
"description": "Deploy a uProxy cloud instance.",
4+
"app": {
5+
"script": [
6+
"freedom-module.static.js"
7+
]
8+
},
9+
"dependencies": {
10+
"loggingcontroller": {
11+
"url": "../../loggingprovider/freedom-module.json",
12+
"api": "loggingcontroller"
13+
},
14+
"logginglistener": {
15+
"url": "../../loggingprovider/freedom-module.json",
16+
"api": "logginglistener"
17+
},
18+
"digitalocean": {
19+
"url": "../digitalocean/freedom-module.json",
20+
"api": "digitalocean"
21+
}
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='../../../../third_party/typings/es6-promise/es6-promise.d.ts' />
2+
/// <reference path='../../../../third_party/typings/freedom/freedom-module-env.d.ts' />
3+
4+
import logging = require('../../logging/logging');
5+
import loggingTypes = require('../../loggingprovider/loggingprovider.types');
6+
7+
const loggingController = freedom['loggingcontroller']();
8+
loggingController.setDefaultFilter(loggingTypes.Destination.console,
9+
loggingTypes.Level.debug);
10+
11+
const log :logging.Log = new logging.Log('deployer');
12+
13+
const digitalOcean = freedom['digitalocean']();
14+
15+
digitalOcean.on('status', (msg:any) => {
16+
log.info('status: %1', msg.message);
17+
});
18+
19+
log.info('deploying...');
20+
21+
digitalOcean.start('test').then((ret: any) => {
22+
log.info('final result: %1', ret);
23+
}, (e:Error) => {
24+
log.error('failed to deploy: %1', e);
25+
});

src/cloud/provision/freedom-module.json renamed to src/cloud/digitalocean/freedom-module.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"name": "provision",
2+
"name": "digitalocean",
33
"description": "Provisions VMs on Digital Ocean",
44
"app": {
55
"script": [
66
"../../../forge-min/forge.min.js",
77
"freedom-module.static.js"
88
]
99
},
10-
"provides": [ "provision" ],
11-
"default": "provision",
10+
"provides": [ "digitalocean" ],
11+
"default": "digitalocean",
1212
"api": {
13-
"provision": {
13+
"digitalocean": {
1414
"constructor": { "value": [ ] },
1515
"start": {
1616
"type": "method",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path='../../../../third_party/typings/freedom/freedom-module-env.d.ts' />
2+
3+
import Provisioner = require('./provisioner');
4+
5+
freedom().providePromises(Provisioner);
6+
7+
export = Provisioner
Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
/// <reference path='../../../../third_party/typings/es6-promise/es6-promise.d.ts' />
33
/// <reference path='../../../../third_party/typings/freedom/freedom-module-env.d.ts' />
44

5-
const forge = require("forge-min");
5+
// TODO: https://github.com/uProxy/uproxy/issues/2051
6+
declare var forge: any;
67

78
const POLL_TIMEOUT: number = 5000; //milliseconds
89

@@ -38,7 +39,7 @@ interface KeyPair {
3839

3940
class Provisioner {
4041

41-
constructor(private dispatch_: Function, private state_: any) {}
42+
constructor(private dispatch_: Function, private state_: any = {}) {}
4243

4344
/**
4445
* One-click setup of a VM
@@ -57,14 +58,15 @@ class Provisioner {
5758
this.state_.ssh = keys;
5859
return this.setupDigitalOcean_(name);
5960
// Setup Digital Ocean (SSH key + droplet)
60-
}).then((actions: any) => {
61-
//console.log(actions);
61+
}).then(() => {
6262
return this.doRequest_("GET", "droplets/" + this.state_.cloud.vm.id);
6363
// Get the droplet's configuration
6464
}).then((resp: any) => {
6565
this.sendStatus_("CLOUD_DONE_VM");
6666
this.state_.cloud.vm = resp.droplet;
67-
this.state_.network.ssh_port = 22;
67+
this.state_.network = {
68+
"ssh_port": 22
69+
};
6870
// Retrieve public IPv4 address
6971
for (var i = 0; i < resp.droplet.networks.v4.length; i++) {
7072
if (resp.droplet.networks.v4[i].type === "public") {
@@ -156,7 +158,7 @@ class Provisioner {
156158
}).then((responseUrl: string) => {
157159
var query = responseUrl.substr(responseUrl.indexOf('#') + 1),
158160
param: string,
159-
params: { [k: string]: string },
161+
params: { [k: string]: string } = {},
160162
keys = query.split('&'),
161163
i = 0;
162164
for (i = 0; i < keys.length; i++) {
@@ -186,7 +188,7 @@ class Provisioner {
186188
private getSshKey_ = (name: string) : Promise<KeyPair> => {
187189
var storage = freedom["core.storage"]();
188190
return new Promise((F, R) => {
189-
var result : KeyPair;
191+
var result: KeyPair = <KeyPair>{};
190192
Promise.all([
191193
storage.get("DigitalOcean-" + name + "-PublicKey"),
192194
storage.get("DigitalOcean-" + name + "-PrivateKey")
@@ -222,12 +224,12 @@ class Provisioner {
222224
return new Promise((F, R) => {
223225
var url = 'https://api.digitalocean.com/v2/' + actionPath;
224226
var xhr = freedom["core.xhr"]();
225-
xhr.on("onload", (resolve: Function, reject: Function, xhr: any, e: Error) => {
226-
xhr.getResponseText().then((resolve: Function, reject: Function, resp: string) => {
227+
xhr.on("onload", (loadInfo: any) => {
228+
xhr.getResponseText().then((response: string) => {
227229
try {
228-
resolve(JSON.parse(resp));
230+
F(JSON.parse(response));
229231
} catch(e) {
230-
reject(e);
232+
R(e);
231233
}
232234
});
233235
});
@@ -247,22 +249,22 @@ class Provisioner {
247249
/**
248250
* Waits for all in-progress Digital Ocean actions to complete
249251
* e.g. after powering on a machine, or creating a VM
250-
* @param {Function} resolve - call when done
251-
* @param {Function} reject - call on failure
252252
*/
253-
private waitDigitalOceanActions_ = (resolve: Function, reject: Function) : void => {
253+
private waitDigitalOceanActions_ = () : Promise<void> => {
254254
console.log("Polling for Digital Ocean in-progress actions");
255-
this.doRequest_("GET", "droplets/" + this.state_.cloud.vm.id + "/actions").then((resp: any) => {
255+
return this.doRequest_("GET", "droplets/" + this.state_.cloud.vm.id + "/actions").then((resp: any) => {
256256
for (var i = 0; i < resp.actions.length; i++) {
257257
if (resp.actions[i].status === "in-progress") {
258-
setTimeout(this.waitDigitalOceanActions_, POLL_TIMEOUT);
259-
return;
258+
return new Promise<void>((F, R) => {
259+
setTimeout(() => {
260+
this.waitDigitalOceanActions_().then(F, R);
261+
}, POLL_TIMEOUT);
262+
});
260263
}
261264
}
262-
resolve(resp);
263265
}).catch((e: Error) => {
264266
console.error("Error waiting for digital ocean actions:" + JSON.stringify(e));
265-
reject(e);
267+
throw e;
266268
});
267269
}
268270

@@ -280,7 +282,6 @@ class Provisioner {
280282
this.sendStatus_("CLOUD_INIT_ADDKEY");
281283
// Get SSH keys in account
282284
this.doRequest_("GET", "account/keys").then((resp: any) => {
283-
//console.log(resp);
284285
for (var i = 0; i < resp.ssh_keys.length; i++) {
285286
if (resp.ssh_keys[i].public_key === this.state_.ssh.public) {
286287
return Promise.resolve({
@@ -295,14 +296,12 @@ class Provisioner {
295296
}));
296297
// If missing, put SSH key into account
297298
}).then((resp: any) => {
298-
//console.log(resp);
299299
this.state_.cloud.ssh = resp.ssh_key;
300300
this.sendStatus_("CLOUD_DONE_ADDKEY");
301301
this.sendStatus_("CLOUD_INIT_VM");
302302
return this.doRequest_("GET", "droplets");
303303
// Get list of droplets
304304
}).then((resp: any) => {
305-
//console.log(resp);
306305
for (var i = 0; i < resp.droplets.length; i++) {
307306
if (resp.droplets[i].name === name) {
308307
return Promise.resolve({
@@ -320,7 +319,6 @@ class Provisioner {
320319
}));
321320
// If missing, create the droplet
322321
}).then((resp: any) => {
323-
//console.log(resp);
324322
this.state_.cloud.vm = resp.droplet;
325323
if (resp.droplet.status == "off") {
326324
// Need to power on VM
@@ -334,9 +332,8 @@ class Provisioner {
334332
}
335333
// If the machine exists, but powered off, turn it on
336334
}).then((resp: any) => {
337-
//console.log(resp);
338335
this.sendStatus_("CLOUD_WAITING_VM");
339-
this.waitDigitalOceanActions_(F, R);
336+
this.waitDigitalOceanActions_().then(F, R);
340337
// Wait for all in-progress actions to complete
341338
}).catch((err: Error) => {
342339
console.error("Error w/DigitalOcean: " + err);
@@ -350,8 +347,4 @@ class Provisioner {
350347
}
351348
}
352349

353-
if (typeof freedom !== 'undefined') {
354-
freedom['provisioner']().providePromises(Provisioner);
355-
}
356-
357350
export = Provisioner;

src/cloud/social/freedom-module.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"inviteUser": {
6060
"type": "method",
6161
"value": ["string"],
62-
"ret": "Object"
62+
"ret": "object"
6363
},
6464

6565
"acceptUserInvitation": {

0 commit comments

Comments
 (0)