Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue where start will never resolve if droplet has been deleted #407

Merged
merged 4 commits into from
May 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions src/cloud/digitalocean/provisioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class Provisioner {
}).then((keys: KeyPair) => {
// Get SSH keys
this.state_.ssh = keys;
return this.getDroplet_(name).then((unused :any) => {

return this.getDropletByName_(name).then((unused :any) => {
// Droplet exists so raise error
return Promise.reject({
'errcode': 'VM_AE',
Expand All @@ -85,23 +86,25 @@ class Provisioner {
});
}).then(() => {
// Get the droplet's configuration
return this.doRequest_('GET', 'droplets/' + this.state_.cloud.vm.id);
}).then((resp: any) => {
return this.getDropletByName_(name);
}).then((droplet:any) => {
this.sendStatus_('CLOUD_DONE_VM');
this.state_.cloud.vm = resp.droplet;
this.state_.cloud.vm = droplet;
this.state_.network = {
'ssh_port': 22
};
// Retrieve public IPv4 address
for (var i = 0; i < resp.droplet.networks.v4.length; i++) {
if (resp.droplet.networks.v4[i].type === 'public') {
this.state_.network['ipv4'] = resp.droplet.networks.v4[i].ip_address;
for (var i = 0; i < droplet.networks.v4.length; i++) {
if (droplet.networks.v4[i].type === 'public') {
this.state_.network['ipv4'] = droplet.networks.v4[i].ip_address;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we break out of this loop once we set this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Some functional programming approach would be even better...but can wait.

break;
}
}
// Retrieve public IPv6 address
for (var i = 0; i < resp.droplet.networks.v6.length; i++) {
if (resp.droplet.networks.v6[i].type === 'public') {
this.state_.network['ipv6'] = resp.droplet.networks.v6[i].ip_address;
for (var i = 0; i < droplet.networks.v6.length; i++) {
if (droplet.networks.v6[i].type === 'public') {
this.state_.network['ipv6'] = droplet.networks.v6[i].ip_address;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

break;
}
}
console.log(this.state_);
Expand Down Expand Up @@ -132,10 +135,10 @@ class Provisioner {
private destroyServer_ = (name: string): Promise<void> => {
return this.doRequest_('GET', 'droplets').then((resp: any) => {
// Find and delete the server with the same name
return this.getDroplet_(name);
}).then((resp: any) => {
return this.getDropletByName_(name);
}).then((droplet: any) => {
this.state_.cloud = this.state_.cloud || {};
this.state_.cloud.vm = this.state_.cloud.vm || resp.droplet;
this.state_.cloud.vm = this.state_.cloud.vm || droplet;
// Make sure there are no actions in progress before deleting
this.sendStatus_('CLOUD_WAITING_VM');
return this.waitDigitalOceanActions_();
Expand Down Expand Up @@ -169,10 +172,10 @@ class Provisioner {
return this.doOAuth_().then((oauthObj: any) => {
this.state_.oauth = oauthObj;
}).then(() => {
return this.getDroplet_(name);
}).then((resp: any) => {
return this.getDropletByName_(name);
}).then((droplet: any) => {
this.state_.cloud = this.state_.cloud || {};
this.state_.cloud.vm = this.state_.cloud.vm || resp.droplet;
this.state_.cloud.vm = this.state_.cloud.vm || droplet;
// Make sure there are no actions in progress before rebooting
this.sendStatus_('CLOUD_WAITING_VM');
return this.waitDigitalOceanActions_();
Expand All @@ -188,20 +191,12 @@ class Provisioner {
});
}

/**
* Finds a droplet with this name
* @param {String} droplet name, as a string
* @return {Promise.<Object>}, resolves with {droplet: droplet_with_name}
* or rejects if droplet doesn't exist
*/
private getDroplet_ = (name: string) : Promise<Object> => {
// Resolves with the (de-serialised) droplet, rejecting if not found.
private getDropletByName_ = (name: string) : Promise<Object> => {
return this.doRequest_('GET', 'droplets').then((resp: any) => {
// Find and delete the server with the same name
for (var i = 0; i < resp.droplets.length; i++) {
for (let i = 0; i < resp.droplets.length; i++) {
if (resp.droplets[i].name === name) {
return Promise.resolve({
droplet: resp.droplets[i]
});
return Promise.resolve(resp.droplets[i]);
}
}
return Promise.reject({
Expand Down