-
Notifications
You must be signed in to change notification settings - Fork 0
/
vultr.py
executable file
·115 lines (85 loc) · 2.8 KB
/
vultr.py
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
#!/usr/local/bin/python
import sys
import getopt
import urllib2
import json
from pprint import pprint
def main(argv):
grammar = "kant.xml"
try:
opts, args = getopt.getopt(argv, "hg:d", ["help", "grammar="])
if len(args) > 0:
command = args[0]
if command == "provision":
provision()
return
usage()
except getopt.GetoptError:
print "debug/3"
usage()
sys.exit(2)
def usage():
print "Usage: vultr COMMAND"
print ""
print " where COMMAND is one of:"
print " provision: provisions a server"
def provision():
print " ... provisioning servers"
plan = getPlan()
# region = getRegion()
#
# if region == None:
# print "Error provisioning servers: region not available"
# return None
#
# os = getOS('ubuntu', 'x64', 'Ubuntu 16.04 x64')
#
# if os == None:
# print "Error provisioning servers: Ubuntu 16.04 x64 not supported"
# return None
#
# print os
def getPlan():
response = urllib2.urlopen("https://api.vultr.com/v1/plans/list")
if response.getcode() != 200:
print "Error retrieving plans list: ", response.getcode()
return None
if response.info()['content-type'] != 'application/json':
print "Error retrieving plans list: invalid Content-Type"
return None
data = json.load(response)
for id in data:
plan = data[id]
print " > ",plan['VPSPLANID'],plan['vcpu_count'],plan['ram'],plan['price_per_month']
return None
def getRegion():
response = urllib2.urlopen("https://api.vultr.com/v1/regions/list")
if response.getcode() != 200:
print "Error retrieving region list: ", response.getcode()
return None
if response.info()['content-type'] != 'application/json':
print "Error retrieving region list: invalid Content-Type"
return None
data = json.load(response)
for id in data:
region = data[id]
print " > ",region['DCID'],region['name']
return None
def getOS(family, architecture, name):
response = urllib2.urlopen("https://api.vultr.com/v1/os/list")
if response.getcode() != 200:
print "Error retrieving operating system list: ", response.getcode()
return None
if response.info()['content-type'] != 'application/json':
print "Error retrieving operating system list: invalid Content-Type"
return None
data = json.load(response)
for id in data:
os = data[id]
if os['family'] == family:
if os['arch'] == architecture:
if os['name'] == name:
return os
return None
if __name__ == "__main__":
main(sys.argv[1:])