-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
118 lines (98 loc) · 3.23 KB
/
main.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
var needle = require("needle");
var os = require("os");
var fs=require("fs");
var aw=require("./ec2_request.js")
var myobj={};
var config = {};
//config.token = "";
var headers = {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + config.token
};
var client = {
createDroplet: function(dropletName, region, imageName, onResponse) {
var data = {
"name": dropletName,
"region": region,
"size": "512mb",
"image": imageName,
// Id to ssh_key already associated with account.
//"ssh_keys": [],
//"ssh_keys":null,
"backups": false,
"ipv6": false,
"user_data": null,
"private_networking": null
};
console.log("Attempting to create: " + JSON.stringify(data));
needle.post("https://api.digitalocean.com/v2/droplets", data, {
headers: headers,
json: true
}, onResponse);
},
getDropletInfo: function(dropletID, onResponse) {
needle.get(`https://api.digitalocean.com/v2/droplets/${dropletID}`, {
headers: headers
}, onResponse)
},
deleteDroplet: function(dropletID, onResponse) {
needle.delete(`https://api.digitalocean.com/v2/droplets/${dropletID}`, null,{
headers: headers,
}, onResponse)
}
};
// to Create an droplet with the specified name, region, and image
// Comment out when completed. ONLY RUN ONCE!!!!!
// Write down/copy droplet id.
var name = "agopi" + os.hostname();
var region = "nyc3";
var image = "ubuntu-14-04-x64";
client.createDroplet(name, region, image, function(err, resp, body) {
if (resp.headers) {
console.log("\n\n\nCalls remaining", resp.headers["ratelimit-remaining"]);
}
console.log("Creating a droplet");
// StatusCode 202 - Means server accepted request.
if (!err && resp.statusCode == 202) {
console.log("Created successfully droplet id is",body.droplet.id);
myobj.dropletID=body.droplet.id;
getinfo();
}
else{
console.log("Digitalocean is not able to provision so switching to aws");
aw.runaws();
}
});
function getinfo(){
var dropletId=myobj.dropletID;
client.getDropletInfo(dropletId, function(err, resp, body) {
console.log("Getting IP address");
// console.log(body);
var ipadd = resp.body;
// StatusCode 202 - Means server accepted request.
if (!err) {
console.log("The IP address is:" + ipadd.droplet.networks.v4[0].ip_address);
fs.writeFile('output.txt',ipadd.droplet.networks.v4[0].ip_address,function(err){
if (err){
return console.error(err);
}
console.log("Ip address written successfully");
});
}
});
}
//end of create
//To delete the droplet please enter the dropletId in the below line and uncomment the creation part
/*
var dropletId=myobj.dropletID;
var dropletId="38168206";
client.deleteDroplet(dropletId, function(err, resp, body) {
console.log("Deleting the droplet ");
var droplet = resp;
// StatusCode 204 - Means server accepted request.
if (!err && resp.statusCode == 204) {
console.log("Deleted!");
}
});
*/
//end of delete