@@ -7,6 +7,10 @@ declare var forge: any;
77
88const POLL_TIMEOUT : number = 5000 ; //milliseconds
99
10+ // To see available regions:
11+ // https://developers.digitalocean.com/documentation/v2/#list-all-regions
12+ const DEFAULT_REGION : string = "nyc3" ;
13+
1014const STATUS_CODES : { [ k : string ] : string ; } = {
1115 "START" : "Starting provisioner" ,
1216 "OAUTH_INIT" : "Initializing oauth flow" ,
@@ -45,22 +49,24 @@ class Provisioner {
4549 * One-click setup of a VM
4650 * See freedom-module.json for return and error types
4751 * @param {String } name of VM to create
52+ * @param {String } region to create VM in
4853 * @return {Promise.<Object> }
4954 */
50- public start = ( name : string ) : Promise < Object > => {
55+ public start = ( name : string , region : string = DEFAULT_REGION ) :
56+ Promise < Object > => {
5157 this . sendStatus_ ( "START" ) ;
5258 // Do oAuth
5359 return this . doOAuth_ ( ) . then ( ( oauthObj : any ) => {
5460 this . state_ . oauth = oauthObj ;
5561 return this . getSshKey_ ( name ) ;
56- // Get SSH keys
62+ // Get SSH keys
5763 } ) . then ( ( keys : KeyPair ) => {
5864 this . state_ . ssh = keys ;
59- return this . setupDigitalOcean_ ( name ) ;
60- // Setup Digital Ocean (SSH key + droplet)
65+ return this . setupDigitalOcean_ ( name , region ) ;
66+ // Setup Digital Ocean (SSH key + droplet)
6167 } ) . then ( ( ) => {
6268 return this . doRequest_ ( "GET" , "droplets/" + this . state_ . cloud . vm . id ) ;
63- // Get the droplet's configuration
69+ // Get the droplet's configuration
6470 } ) . then ( ( resp : any ) => {
6571 this . sendStatus_ ( "CLOUD_DONE_VM" ) ;
6672 this . state_ . cloud . vm = resp . droplet ;
@@ -149,11 +155,11 @@ class Provisioner {
149155 this . sendStatus_ ( "OAUTH_INIT" ) ;
150156 oauth . initiateOAuth ( REDIRECT_URIS ) . then ( ( obj : any ) => {
151157 var url = "https://cloud.digitalocean.com/v1/oauth/authorize?" +
152- "client_id=c16837b5448cd6cf2582d2c2f767cfb7d11844ec395a91b43f26ca72513416c8&" +
153- "response_type=token&" +
154- "redirect_uri=" + encodeURIComponent ( obj . redirect ) + "&" +
155- "state=" + encodeURIComponent ( obj . state ) + "&" +
156- "scope=read%20write" ;
158+ "client_id=c16837b5448cd6cf2582d2c2f767cfb7d11844ec395a91b43f26ca72513416c8&" +
159+ "response_type=token&" +
160+ "redirect_uri=" + encodeURIComponent ( obj . redirect ) + "&" +
161+ "state=" + encodeURIComponent ( obj . state ) + "&" +
162+ "scope=read%20write" ;
157163 return oauth . launchAuthFlow ( url , obj ) ;
158164 } ) . then ( ( responseUrl : string ) => {
159165 var query = responseUrl . substr ( responseUrl . indexOf ( '#' ) + 1 ) ,
@@ -194,7 +200,7 @@ class Provisioner {
194200 storage . get ( "DigitalOcean-" + name + "-PrivateKey" )
195201 ] ) . then ( ( val : string [ ] ) => {
196202 if ( val [ 0 ] === null ||
197- val [ 1 ] === null ) {
203+ val [ 1 ] === null ) {
198204 result = Provisioner . generateKeyPair_ ( ) ;
199205 storage . set ( "DigitalOcean-" + name + "-PublicKey" , result . public ) ;
200206 storage . set ( "DigitalOcean-" + name + "-PrivateKey" , result . private ) ;
@@ -274,9 +280,11 @@ class Provisioner {
274280 * This method will use this.waitDigitalOceanActions_() to wait until all actions complete
275281 * before resolving
276282 * @param {String } name of droplet
283+ * @param {String } region to create VM in
277284 * @return {Promise.<void> } resolves on success, rejects on failure
278285 */
279- private setupDigitalOcean_ = ( name : string ) : Promise < void > => {
286+ private setupDigitalOcean_ = ( name : string , region : string ) :
287+ Promise < void > => {
280288 return new Promise < void > ( ( F , R ) => {
281289 this . state_ . cloud = { } ;
282290 this . sendStatus_ ( "CLOUD_INIT_ADDKEY" ) ;
@@ -294,13 +302,13 @@ class Provisioner {
294302 name : name ,
295303 public_key : this . state_ . ssh . public
296304 } ) ) ;
297- // If missing, put SSH key into account
305+ // If missing, put SSH key into account
298306 } ) . then ( ( resp : any ) => {
299307 this . state_ . cloud . ssh = resp . ssh_key ;
300308 this . sendStatus_ ( "CLOUD_DONE_ADDKEY" ) ;
301309 this . sendStatus_ ( "CLOUD_INIT_VM" ) ;
302310 return this . doRequest_ ( "GET" , "droplets" ) ;
303- // Get list of droplets
311+ // Get list of droplets
304312 } ) . then ( ( resp : any ) => {
305313 for ( var i = 0 ; i < resp . droplets . length ; i ++ ) {
306314 if ( resp . droplets [ i ] . name === name ) {
@@ -312,12 +320,12 @@ class Provisioner {
312320 }
313321 return this . doRequest_ ( "POST" , "droplets" , JSON . stringify ( {
314322 name : name ,
315- region : "nyc3" ,
323+ region : region ,
316324 size : "512mb" ,
317325 image : "ubuntu-14-04-x64" ,
318326 ssh_keys : [ this . state_ . cloud . ssh . id ]
319327 } ) ) ;
320- // If missing, create the droplet
328+ // If missing, create the droplet
321329 } ) . then ( ( resp : any ) => {
322330 this . state_ . cloud . vm = resp . droplet ;
323331 if ( resp . droplet . status == "off" ) {
@@ -330,11 +338,11 @@ class Provisioner {
330338 } else {
331339 return Promise . resolve ( ) ;
332340 }
333- // If the machine exists, but powered off, turn it on
341+ // If the machine exists, but powered off, turn it on
334342 } ) . then ( ( resp : any ) => {
335343 this . sendStatus_ ( "CLOUD_WAITING_VM" ) ;
336344 this . waitDigitalOceanActions_ ( ) . then ( F , R ) ;
337- // Wait for all in-progress actions to complete
345+ // Wait for all in-progress actions to complete
338346 } ) . catch ( ( err : Error ) => {
339347 console . error ( "Error w/DigitalOcean: " + err ) ;
340348 this . sendStatus_ ( "CLOUD_FAILED" ) ;
0 commit comments