Skip to content

Commit 7b3fd02

Browse files
committed
fix issue where start will never resolve if droplet has been deleted
1 parent daecb8d commit 7b3fd02

1 file changed

Lines changed: 21 additions & 28 deletions

File tree

src/cloud/digitalocean/provisioner.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class Provisioner {
7272
}).then((keys: KeyPair) => {
7373
// Get SSH keys
7474
this.state_.ssh = keys;
75-
return this.getDroplet_(name).then((unused :any) => {
75+
76+
return this.getDropletByName_(name).then((unused :any) => {
7677
// Droplet exists so raise error
7778
return Promise.reject({
7879
'errcode': 'VM_AE',
@@ -85,23 +86,23 @@ class Provisioner {
8586
});
8687
}).then(() => {
8788
// Get the droplet's configuration
88-
return this.doRequest_('GET', 'droplets/' + this.state_.cloud.vm.id);
89-
}).then((resp: any) => {
89+
return this.getDropletByName_(name);
90+
}).then((droplet:any) => {
9091
this.sendStatus_('CLOUD_DONE_VM');
91-
this.state_.cloud.vm = resp.droplet;
92+
this.state_.cloud.vm = droplet;
9293
this.state_.network = {
9394
'ssh_port': 22
9495
};
9596
// Retrieve public IPv4 address
96-
for (var i = 0; i < resp.droplet.networks.v4.length; i++) {
97-
if (resp.droplet.networks.v4[i].type === 'public') {
98-
this.state_.network['ipv4'] = resp.droplet.networks.v4[i].ip_address;
97+
for (var i = 0; i < droplet.networks.v4.length; i++) {
98+
if (droplet.networks.v4[i].type === 'public') {
99+
this.state_.network['ipv4'] = droplet.networks.v4[i].ip_address;
99100
}
100101
}
101102
// Retrieve public IPv6 address
102-
for (var i = 0; i < resp.droplet.networks.v6.length; i++) {
103-
if (resp.droplet.networks.v6[i].type === 'public') {
104-
this.state_.network['ipv6'] = resp.droplet.networks.v6[i].ip_address;
103+
for (var i = 0; i < droplet.networks.v6.length; i++) {
104+
if (droplet.networks.v6[i].type === 'public') {
105+
this.state_.network['ipv6'] = droplet.networks.v6[i].ip_address;
105106
}
106107
}
107108
console.log(this.state_);
@@ -132,10 +133,10 @@ class Provisioner {
132133
private destroyServer_ = (name: string): Promise<void> => {
133134
return this.doRequest_('GET', 'droplets').then((resp: any) => {
134135
// Find and delete the server with the same name
135-
return this.getDroplet_(name);
136-
}).then((resp: any) => {
136+
return this.getDropletByName_(name);
137+
}).then((droplet: any) => {
137138
this.state_.cloud = this.state_.cloud || {};
138-
this.state_.cloud.vm = this.state_.cloud.vm || resp.droplet;
139+
this.state_.cloud.vm = this.state_.cloud.vm || droplet;
139140
// Make sure there are no actions in progress before deleting
140141
this.sendStatus_('CLOUD_WAITING_VM');
141142
return this.waitDigitalOceanActions_();
@@ -169,10 +170,10 @@ class Provisioner {
169170
return this.doOAuth_().then((oauthObj: any) => {
170171
this.state_.oauth = oauthObj;
171172
}).then(() => {
172-
return this.getDroplet_(name);
173-
}).then((resp: any) => {
173+
return this.getDropletByName_(name);
174+
}).then((droplet: any) => {
174175
this.state_.cloud = this.state_.cloud || {};
175-
this.state_.cloud.vm = this.state_.cloud.vm || resp.droplet;
176+
this.state_.cloud.vm = this.state_.cloud.vm || droplet;
176177
// Make sure there are no actions in progress before rebooting
177178
this.sendStatus_('CLOUD_WAITING_VM');
178179
return this.waitDigitalOceanActions_();
@@ -188,20 +189,12 @@ class Provisioner {
188189
});
189190
}
190191

191-
/**
192-
* Finds a droplet with this name
193-
* @param {String} droplet name, as a string
194-
* @return {Promise.<Object>}, resolves with {droplet: droplet_with_name}
195-
* or rejects if droplet doesn't exist
196-
*/
197-
private getDroplet_ = (name: string) : Promise<Object> => {
192+
// Resolves with the (de-serialised) droplet, rejecting if not found.
193+
private getDropletByName_ = (name: string) : Promise<Object> => {
198194
return this.doRequest_('GET', 'droplets').then((resp: any) => {
199-
// Find and delete the server with the same name
200-
for (var i = 0; i < resp.droplets.length; i++) {
195+
for (let i = 0; i < resp.droplets.length; i++) {
201196
if (resp.droplets[i].name === name) {
202-
return Promise.resolve({
203-
droplet: resp.droplets[i]
204-
});
197+
return Promise.resolve(resp.droplets[i]);
205198
}
206199
}
207200
return Promise.reject({

0 commit comments

Comments
 (0)