|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+import json
|
|
|
2
|
+import os
|
|
|
3
|
+import re
|
|
|
4
|
+from datetime import datetime
|
|
|
5
|
+from pytz import utc
|
|
|
6
|
+from linode.api import Api
|
|
|
7
|
+
|
|
|
8
|
+_datacenter_dict = None
|
|
|
9
|
+_pricing_plan_dict = None
|
|
|
10
|
+_distribution_dict = None
|
|
|
11
|
+_stackscript_dict = None
|
|
|
12
|
+_linode_dict = None
|
|
|
13
|
+_kernel_dict = None
|
|
|
14
|
+
|
|
|
15
|
+def get_api(apiKey):
|
|
|
16
|
+ return Api(key=apiKey)
|
|
|
17
|
+
|
|
|
18
|
+def get_datacenter_dict(api, reload=False):
|
|
|
19
|
+ global _datacenter_dict
|
|
|
20
|
+ if reload or (_datacenter_dict is None):
|
|
|
21
|
+ _datacenter_dict = {dc['ABBR']: dc for dc in api.avail_datacenters()}
|
|
|
22
|
+ return _datacenter_dict
|
|
|
23
|
+
|
|
|
24
|
+def get_pricing_plan_dict(api, reload=False):
|
|
|
25
|
+ global _pricing_plan_dict
|
|
|
26
|
+ if reload or (_pricing_plan_dict is None):
|
|
|
27
|
+ _pricing_plan_dict = {plan['LABEL']: plan for plan in api.avail_linodeplans()}
|
|
|
28
|
+ return _pricing_plan_dict
|
|
|
29
|
+
|
|
|
30
|
+def get_distribution_dict(api, reload=False):
|
|
|
31
|
+ global _distribution_dict
|
|
|
32
|
+ if reload or (_distribution_dict is None):
|
|
|
33
|
+ _distribution_dict = {dist['LABEL']: dist for dist in api.avail_distributions()}
|
|
|
34
|
+ return _distribution_dict
|
|
|
35
|
+
|
|
|
36
|
+def get_stackscript_dict(api, reload=False):
|
|
|
37
|
+ global _stackscript_dict
|
|
|
38
|
+ if reload or (_stackscript_dict is None):
|
|
|
39
|
+ _stackscript_dict = {script['LABEL']: script for script in api.stackscript_list()}
|
|
|
40
|
+ return _stackscript_dict
|
|
|
41
|
+
|
|
|
42
|
+def get_kernel_dict(api, reload=False):
|
|
|
43
|
+ global _kernel_dict
|
|
|
44
|
+ if reload or (_kernel_dict is None):
|
|
|
45
|
+ _kernel_dict = {kernel['LABEL']: kernel for kernel in api.avail_kernels()}
|
|
|
46
|
+ return _kernel_dict
|
|
|
47
|
+
|
|
|
48
|
+def get_latest_kernel(api, is_64_bit=True):
|
|
|
49
|
+ kernels = get_kernel_dict(api)
|
|
|
50
|
+ prefix = 'latest 64 bit' if is_64_bit else 'latest 32 bit'
|
|
|
51
|
+ for label, kernel in kernels.items():
|
|
|
52
|
+ if label.lower().startswith(prefix):
|
|
|
53
|
+ return kernel
|
|
|
54
|
+ raise RuntimeError('Latest %s bit kernel not found' % ('64' if is_64_bit else '32',))
|
|
|
55
|
+
|
|
|
56
|
+def get_linode_dict(api, reload=False):
|
|
|
57
|
+ global _linode_dict
|
|
|
58
|
+ if reload or (_linode_dict is None):
|
|
|
59
|
+ _linode_dict = {linode['LABEL']: linode for linode in api.linode_list()}
|
|
|
60
|
+ return _linode_dict
|
|
|
61
|
+
|
|
|
62
|
+def get_linode_by_label(api, linode_label, reload=False):
|
|
|
63
|
+ linodes = get_linode_dict(api, reload)
|
|
|
64
|
+ if linode_label in linodes:
|
|
|
65
|
+ return linodes[linode_label]
|
|
|
66
|
+ else:
|
|
|
67
|
+ raise ValueError('Invalid Linode label: ' + linode_label)
|
|
|
68
|
+
|
|
|
69
|
+def get_linode_by_id(api, linode_id, reload=False):
|
|
|
70
|
+ linodes = get_linode_dict(api, reload)
|
|
|
71
|
+ for linode in linodes.values():
|
|
|
72
|
+ if linode['LINODEID'] == linode_id:
|
|
|
73
|
+ return linode
|
|
|
74
|
+ raise ValueError('Invalid Linode ID: ' + linode_id)
|
|
|
75
|
+
|
|
|
76
|
+def get_swap_disk(api, linode_id):
|
|
|
77
|
+ for disk in api.linode_disk_list(LinodeID=linode_id):
|
|
|
78
|
+ if disk['TYPE'] == 'swap':
|
|
|
79
|
+ return disk
|
|
|
80
|
+ return None
|
|
|
81
|
+
|
|
|
82
|
+def get_data_disk(api, linode_id):
|
|
|
83
|
+ for disk in api.linode_disk_list(LinodeID=linode_id):
|
|
|
84
|
+ if disk['LABEL'].lower() == 'data':
|
|
|
85
|
+ return disk
|
|
|
86
|
+ return None
|
|
|
87
|
+
|
|
|
88
|
+def create_data_disk(api, linode_id, data_disk_size_mb):
|
|
|
89
|
+ disk = api.linode_disk_create(LinodeID=linode_id, Size=data_disk_size_mb, Label="Data", Type="ext4")
|
|
|
90
|
+ return disk['DiskID']
|
|
|
91
|
+
|
|
|
92
|
+def create_swap_disk(api, linode_id, swap_disk_size_mb):
|
|
|
93
|
+ disk = api.linode_disk_create(LinodeID=linode_id, Size=swap_disk_size_mb, Label="Swap", Type="swap")
|
|
|
94
|
+ return disk['DiskID']
|
|
|
95
|
+
|
|
|
96
|
+def create_linode(api, datacenter_abbr, pricing_plan_label):
|
|
|
97
|
+ dcs = get_datacenter_dict(api)
|
|
|
98
|
+ if datacenter_abbr not in dcs:
|
|
|
99
|
+ raise ValueError('Invalid datacenter: ' + datacenter_abbr)
|
|
|
100
|
+
|
|
|
101
|
+ plans = get_pricing_plan_dict(api)
|
|
|
102
|
+ if pricing_plan_label not in plans:
|
|
|
103
|
+ raise ValueError('Invalid pricing plan: ' + pricing_plan_label)
|
|
|
104
|
+
|
|
|
105
|
+ dc_id = dcs[datacenter_abbr]['DATACENTERID']
|
|
|
106
|
+ plan_id = plans[pricing_plan_label]['PLANID']
|
|
|
107
|
+
|
|
|
108
|
+ return api.linode_create(DatacenterID=dc_id, PlanID=plan_id)
|
|
|
109
|
+
|
|
|
110
|
+def deploy_linode_from_stackscript(api, linode_id, configuration_label, stackscript_label, stackscript_args, distribution_label, disk_label, disk_size_mb, root_password, kernel_id=None, extra_disk_ids=[]):
|
|
|
111
|
+ scripts = get_stackscript_dict(api)
|
|
|
112
|
+ script = scripts.get(stackscript_label)
|
|
|
113
|
+ if script is None:
|
|
|
114
|
+ raise ValueError('Invalid stackscript: ' + stackscript_label)
|
|
|
115
|
+
|
|
|
116
|
+ dists = get_distribution_dict(api)
|
|
|
117
|
+ dist = dists.get(distribution_label)
|
|
|
118
|
+ if dist is None:
|
|
|
119
|
+ raise ValueError('Invalid distribution: ' + distribution_label)
|
|
|
120
|
+
|
|
|
121
|
+ if not str(dist['DISTRIBUTIONID']) in str(script['DISTRIBUTIONIDLIST']).split(','):
|
|
|
122
|
+ raise ValueError('The specified StackScript does not support the specified distribution')
|
|
|
123
|
+
|
|
|
124
|
+ if kernel_id is None:
|
|
|
125
|
+ kernel = get_latest_kernel(api, dist['IS64BIT'] != 0)
|
|
|
126
|
+ kernel_id = kernel['KERNELID']
|
|
|
127
|
+
|
|
|
128
|
+ disk = api.linode_disk_createfromstackscript(
|
|
|
129
|
+ LinodeID=linode_id,
|
|
|
130
|
+ StackScriptID=script['STACKSCRIPTID'],
|
|
|
131
|
+ StackScriptUDFResponses=json.dumps(stackscript_args),
|
|
|
132
|
+ DistributionID=dist['DISTRIBUTIONID'],
|
|
|
133
|
+ Label=disk_label,
|
|
|
134
|
+ Size=disk_size_mb,
|
|
|
135
|
+ rootPass=root_password
|
|
|
136
|
+ )
|
|
|
137
|
+ disk_list = ','.join(str(x) for x in [disk['DiskID']] + list(extra_disk_ids))
|
|
|
138
|
+
|
|
|
139
|
+ config = api.linode_config_create(
|
|
|
140
|
+ LinodeID=linode_id,
|
|
|
141
|
+ KernelID=kernel_id,
|
|
|
142
|
+ Label=configuration_label,
|
|
|
143
|
+ DiskList=disk_list,
|
|
|
144
|
+ helper_distro=True,
|
|
|
145
|
+ helper_disableUpdateDB=True,
|
|
|
146
|
+ helper_depmod=True,
|
|
|
147
|
+ devtmpfs_automount=True,
|
|
|
148
|
+ helper_network=True
|
|
|
149
|
+ )
|
|
|
150
|
+
|
|
|
151
|
+ return config
|
|
|
152
|
+
|
|
|
153
|
+def parse_asn1_utctime(value):
|
|
|
154
|
+ if value == '':
|
|
|
155
|
+ return None
|
|
|
156
|
+ else:
|
|
|
157
|
+ matches = re.match(r'^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)Z$', value)
|
|
|
158
|
+ if matches:
|
|
|
159
|
+ year = int(matches.group(1))
|
|
|
160
|
+ year = year + (2000 if year <= 50 else 1900)
|
|
|
161
|
+ return datetime(year, int(matches.group(2)), int(matches.group(3)), int(matches.group(4)), int(matches.group(5)), int(matches.group(6)), tzinfo=utc)
|
|
|
162
|
+ else:
|
|
|
163
|
+ raise ValueError("Invalid ASN1 UTCTime value: {}".format(value))
|
|
|
164
|
+
|
|
|
165
|
+def load_openssl_ca_index(ca_root_dir):
|
|
|
166
|
+ ret = {}
|
|
|
167
|
+ with open(os.path.join(ca_root_dir, 'index.txt'), 'r') as f:
|
|
|
168
|
+ # Later instances of the same certificate name will replace earlier
|
|
|
169
|
+ # ones.
|
|
|
170
|
+ for line in f:
|
|
|
171
|
+ fields = line.rstrip("\r\n").split("\t")
|
|
|
172
|
+
|
|
|
173
|
+ # As per OpenSSL's apps/apps.h
|
|
|
174
|
+ ret[fields[5]] = {
|
|
|
175
|
+ 'type': fields[0],
|
|
|
176
|
+ 'exp_date': parse_asn1_utctime(fields[1]),
|
|
|
177
|
+ 'rev_date': parse_asn1_utctime(fields[2]),
|
|
|
178
|
+ 'serial': fields[3],
|
|
|
179
|
+ 'file': fields[4],
|
|
|
180
|
+ 'name': fields[5],
|
|
|
181
|
+ }
|
|
|
182
|
+ return ret
|
|
|
183
|
+
|
|
|
184
|
+def find_cert_with_common_name(ca_index, common_name):
|
|
|
185
|
+ suffix = '/CN=' + common_name
|
|
|
186
|
+ for key, value in ca_index.items():
|
|
|
187
|
+ if key.endswith(suffix):
|
|
|
188
|
+ return value
|
|
|
189
|
+ return None
|