forked from jasonbio/space-personal
/
linodefunctions.py
191 lines (154 loc) · 4.71 KB
/
linodefunctions.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import data
import linode
import time
import log
def get_api():
config = data.get_config()
try:
api = linode.Api(config['linode_api_key'])
return api
except:
pass
def get_linode(linodeID):
api = get_api()
try:
linode = api.linode.list(LinodeID=linodeID)
return linode[0]
except:
pass
def get_linodes():
api = get_api()
try:
linodes = api.linode.list()
return linodes
except:
pass
def import_linodes():
linodes = get_linodes()
try:
for linode in linodes:
linode_id = linode['LINODEID']
server = data.get_server_provider_id(int(linode_id))
if server.count() == 0:
if linode['STATUS'] == 1:
state = 1
elif linode['STATUS'] == 2:
state = 0
else:
state = 2
linode_ip = get_linode_ip(int(linode['LINODEID']))
plan = data.get_linode_plan_id(linode['PLANID'])
server_id = data.make_server(linode['LABEL'], plan[0]['disk'], linode['DISTRIBUTIONVENDOR'], linode[
'TOTALRAM'], plan[0]['cores'], type="linode", id=int(linode['LINODEID']), ip=linode_ip['IPADDRESS'], state=state)
except:
pass
def get_linode_ip(linodeID):
api = get_api()
try:
linode_ip = api.linode.ip.list(LinodeID=linodeID)
return linode_ip[0]
except:
pass
def get_datacenters():
api = get_api()
try:
facilities = api.avail.datacenters()
for facility in facilities:
data.make_linode_facility(
facility['DATACENTERID'], facility['LOCATION'])
except:
pass
def get_plans():
api = get_api()
try:
plans = api.avail.linodeplans()
for plan in plans:
data.make_linode_plan(plan['PLANID'], plan['RAM'], plan['DISK'], plan[
'CORES'], plan['XFER'], plan['LABEL'], plan['PRICE'], plan['HOURLY'])
except:
pass
def get_kernels():
api = get_api()
try:
kernels = api.avail.kernels()
for kernel in kernels:
data.make_linode_kernel(kernel['KERNELID'], kernel['LABEL'])
except:
pass
def get_distributions():
api = get_api()
try:
distributions = api.avail.distributions()
for dist in distributions:
if dist['IS64BIT'] == 1:
data.make_linode_distribution(dist['DISTRIBUTIONID'], dist['LABEL'])
except:
pass
def make_linode(datacenterID, planID):
api = get_api()
try:
linode = api.linode.create(DatacenterID=datacenterID, PlanID=planID)
return linode['LinodeID']
except:
pass
def make_config(linodeID, kernelID, label, diskID):
api = get_api()
try:
config = api.linode.config.create(
LinodeID=linodeID, KernelID=kernelID, Label=label, DiskList="%s" % str(diskID))
except:
pass
def make_disk(linodeID, distributionID, label, size, rootPass):
api = get_api()
try:
disk = api.linode.disk.createfromdistribution(
LinodeID=linodeID, DistributionID=distributionID, Label=label, Size=size, rootPass=rootPass)
return disk['DiskID']
except:
pass
def boot_linode(LinodeID):
api = get_api()
try:
api.linode.boot(LinodeID=LinodeID)
except linode.api.LinodeException as e:
message = "Tried to boot Linode %s but failed, API returned: %s" % (str(LinodeID), str(e))
log.create_log(message, 3)
return 0
def shutdown_linode(linodeID):
api = get_api()
try:
api.linode.shutdown(LinodeID=linodeID)
except:
pass
def reboot_linode(linodeID):
api = get_api()
try:
api.linode.reboot(LinodeID=linodeID)
except:
pass
def delete_linode(LinodeID):
api = get_api()
try:
api.linode.delete(LinodeID=LinodeID, skipChecks="True")
except:
pass
def resize_linode(LinodeID, PlanID):
api = get_api()
try:
api.linode.resize(LinodeID=LinodeID, PlanID=PlanID)
except:
pass
def rename_linode(LinodeID, Label):
api = get_api()
try:
api.linode.update(LinodeID=LinodeID, Label=Label)
except:
pass
def set_linode_rdns(LinodeID, Hostname, IPAddressID=0):
api = get_api()
ip_obj = api.linode.ip.list(LinodeID=LinodeID)
try:
api.linode.ip.setrdns(IPAddressID=ip_obj[0]['IPADDRESSID'], Hostname=Hostname)
except:
message = "Attempted to set rDNS for Linode %s, couldn't do so because a forward lookup record doesn't exist for that hostname." % str(LinodeID)
log.create_log(message, 3)