-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdva_config_generate.py
466 lines (444 loc) · 14.8 KB
/
dva_config_generate.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env python
'''
github : https://github.com/liangxiao1/mini_utils
This tool is for checking ec2 environment to generate configuration for dva run.
The cfg file is /etc/dva.yaml.
dva repo: https://github.com/RedHatQE/dva
'''
import json
import string
import os
import sys
if sys.version.startswith('2'):
print('Only support run in python3')
sys.exit(1)
import logging
import argparse
import boto3
from botocore.exceptions import ClientError
import shutil
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
#/etc/dva.yaml
credential_file = 'data/dva_key.yaml'
parser = argparse.ArgumentParser(
'Generate configuration for dva running in /etc/dva.yaml')
parser.add_argument('--tag', dest='tag', action='store', default='virtqes1',
help='check resource by tag, default is virtqes1', required=False)
parser.add_argument('--pubkeyfile', dest='pubkeyfile', action='store',
help='specify pub keyfile', required=True)
parser.add_argument('--sshkeyfile', dest='sshkeyfile', action='store',
help='specify private ssh keyfile', required=True)
parser.add_argument('--tokenfile', dest='tokenfile', action='store', default="data/dva_key.yaml",
help='credential file, default data/dva_key.yaml', required=False)
parser.add_argument('--target', dest='target', action='store', default="aws",
help='optional, can be aws or aws-china or aws-us-gov', required=False)
parser.add_argument('--output', dest='output', action='store', default="/tmp/dva.yaml",
help='save output to file name, default is /tmp/dva.yaml', required=False)
parser.add_argument('-d', dest='is_debug', action='store_true', default=False,
help='Run in debug mode', required=False)
args = parser.parse_args()
log = logging.getLogger(__name__)
if args.is_debug:
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s:%(message)s')
else:
logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')
credential_file = args.tokenfile
final_file = args.output
credential_file_format = "aws-us-gov: ['ec2_access_key','ec2_secret_key','subscription_username','subscription_password']"
def vpc_check(vpcid, region):
'''
check whether the vpc's default security group allow ssh connection
'''
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
try:
vpc = ec2.Vpc(vpcid)
log.info("vpc init %s", vpcid)
except Exception as error:
log.info("vpc init error %s, %s", vpci, str(error))
return False
try:
sgs = vpc.security_groups.all()
sg = None
for i in sgs:
log.debug("sg name %s", i.group_name)
if "default" in i.group_name:
sg = i
break
if sg == None:
log.info("No default named security group")
return False
except Exception as error:
log.info("default sg get error: %s", str(error))
return False
try:
sg = ec2.SecurityGroup(sg.id)
ips = sg.ip_permissions
for ip in ips:
ip_ranges = ip['IpRanges']
log.info(ip_ranges)
for ip_range in ip_ranges:
if '0.0.0.0/0' in ip_range['CidrIp']:
log.info("vpc check pass!")
return True
return False
except Exception as error:
log.info("sg init error %s",sg.id)
return False
def igw_create(client, vpcid):
'''
create a new igw and attach to vpc
'''
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
try:
igw_new = client.create_internet_gateway(
DryRun=False
)
igwid = igw_new['InternetGateway']['InternetGatewayId']
log.info("New igw created %s", igwid)
igw = ec2.InternetGateway(igwid)
igw.create_tags(
DryRun=False,
Tags=[
{
'Key': 'Name',
'Value': keyname
},
]
)
igw.attach_to_vpc(
DryRun=False,
VpcId=vpcid
)
return igw
except Exception as err:
if 'Resource.AlreadyAssociated' in str(err):
return igw
log.info(str(err))
return None
def rt_update(client, vpc, igw):
'''
update default route table
'''
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
try:
rts = vpc.route_tables.all()
for i in rts:
for x in i.associations_attribute:
if x['Main']:
log.info("found route table, %s", i.id)
rt = i
#rt = vpc.create_route_table(
# DryRun=False,
#
#)
#log.info("New route table created %s", rt.id)
log.info("Update route table %s", rt.id)
rt.create_tags(
DryRun=False,
Tags=[
{
'Key': 'Name',
'Value': keyname
},
]
)
log.info("tag added")
route = rt.create_route(
DestinationCidrBlock='0.0.0.0/0',
DryRun=False,
GatewayId=igw.id,
)
return rt
except Exception as err:
log.info(str(err))
return None
def sg_update(client, vpc, igw):
'''
update default security group
'''
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
try:
sgs = vpc.security_groups.all()
sg = None
for i in sgs:
log.debug("sg name %s", i.group_name)
if "default" in i.group_name:
sg = i
break
if sg == None:
log.info("No default named security group")
return None
except Exception as error:
log.info("default sg get error: %s", str(error))
return None
try:
#sg = vpc.create_security_group(
# Description='virtqe s1',
# GroupName='default',
# DryRun=True
#)
#log.info("New security group created %s", sg.id)
sg.create_tags(
DryRun=False,
Tags=[
{
'Key': 'Name',
'Value': keyname
},
]
)
log.info("tag added")
response = sg.authorize_ingress(
IpPermissions=[
{
"PrefixListIds": [],
"FromPort": 22,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": 22,
"IpProtocol": "tcp",
"UserIdGroupPairs": [],
"Ipv6Ranges": []
},
{
"PrefixListIds": [],
"FromPort": -1,
"IpRanges": [],
"ToPort": -1,
"IpProtocol": "icmpv6",
"UserIdGroupPairs": [],
"Ipv6Ranges": [
{
"CidrIpv6": "::/0"
}
]
},
{
"PrefixListIds": [],
"FromPort": -1,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": -1,
"IpProtocol": "icmp",
"UserIdGroupPairs": [],
"Ipv6Ranges": []
}
]
)
log.info("Enabled ssh port created %s", sg.id)
return sg
except Exception as err:
log.info(str(err))
return None
def subnet_create(client, vpc):
'''
create a new subnet
'''
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
try:
subnet = vpc.create_subnet(
CidrBlock='192.111.1.0/24',
DryRun=False
)
log.info("New subnet created %s", subnet.id)
subnet.create_tags(
DryRun=False,
Tags=[
{
'Key': 'Name',
'Value': keyname
},
]
)
log.info("tag added")
client.modify_subnet_attribute(
MapPublicIpOnLaunch={
'Value': True
},
SubnetId=subnet.id
)
log.info("enabled ipv4 on launch")
return subnet
except Exception as err:
log.info(str(err))
return None
def vpc_create(client, region):
'''
create a new vpc for test running
'''
log.info("create a new vpc for test running")
try:
vpc_new = client.create_vpc(
CidrBlock='192.111.0.0/16',
AmazonProvidedIpv6CidrBlock=True,
DryRun=False,
InstanceTenancy='default'
)
except Exception as err:
log.info("Failed to create vpc %s", str(err))
vpcid = vpc_new['Vpc']['VpcId']
log.info("New vpc created %s", vpcid)
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
try:
vpc = ec2.Vpc(vpcid)
log.info("vpc init %s", vpcid)
tag = vpc.create_tags(
DryRun=False,
Tags=[
{
'Key': 'Name',
'Value': keyname
},
]
)
log.info("added tag to vpc: %s", keyname)
vpc.modify_attribute(
EnableDnsHostnames={
'Value': True
}
)
log.info("Enabled dns support")
except Exception as error:
log.info(str(error))
return False
igw = igw_create(client, vpcid)
if igw == None:
return vpc
rt = rt_update(client, vpc, igw)
if rt == None:
return vpc
subnet = subnet_create(client, vpc)
if subnet == None:
return vpc
sg = sg_update(client, vpc, igw)
if sg == None:
return vpc
if not os.path.exists(credential_file):
log.error("%s not found, please create it and add your key into it as the following format, multilines support if have" % credential_file)
log.info(credential_file_format)
sys.exit(1)
with open(credential_file,'r') as fh:
keys_data = load(fh, Loader=Loader)
subnet_info = ''
ssh_key_info = ''
keyname = 'virtqe_s1'
try:
ACCESS_KEY = keys_data[args.target][0]
SECRET_KEY = keys_data[args.target][1]
subscription_username = keys_data[args.target][2]
subscription_password = keys_data[args.target][3]
except KeyError:
log.info("%s credential not found", args.target)
sys.exit(1)
default_regions = {"aws-china":"cn-northwest-1","aws":"us-west-2","aws-us-gov":"us-gov-west-1"}
region_list = None
region = default_regions[args.target]
try:
client = boto3.client(
'ec2',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name=region,
)
region_list = client.describe_regions()['Regions']
log.info("Successfully init %s credential in %s", args.target, region)
except ClientError as error:
log.info("Failed to init %s credential in %s", args.target, region)
log.info("Error: %s", error)
log.info("If your credential is correct, you may try to reboot slave node to make it work.")
sys.exit(1)
#region_list = client.describe_regions()['Regions']
regionids = []
for region in region_list:
regionids.append(region['RegionName'])
ssh_key_str = ''
subnet_str = ''
for region in regionids:
client = boto3.client(
'ec2',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name=region,
)
subnet_id = None
subnets = client.describe_subnets()['Subnets']
for subnet in subnets:
if subnet['MapPublicIpOnLaunch']:
vpc_id = subnet['VpcId']
if vpc_check(vpc_id, region):
subnet_id = subnet['SubnetId']
break
if subnet_id is None:
log.info("No ipv4 pub enabed subnets found in region %s", region)
vpc = vpc_create(client, region)
subnets = client.describe_subnets()['Subnets']
for subnet in subnets:
if subnet['MapPublicIpOnLaunch']:
vpc_id = subnet['VpcId']
if vpc_check(vpc_id, region):
subnet_id = subnet['SubnetId']
break
if subnet_id is None:
log.info("Create failed in region %s", region)
log.info("Found existing subnet: %s in region %s", subnet_id, region)
ssh_key_str += ' %s: [%s,%s]\n' % (region, keyname, args.sshkeyfile)
subnet_str += ' %s: [%s]\n' % (region, subnet_id)
log.info('check %s key existing status in %s', keyname, region)
pubkeyfile = args.pubkeyfile
if not os.path.exists(pubkeyfile):
log.info("%s not found!", pubkeyfile)
sys.exit(1)
#keyfile = '/home/xiliang/ec2/dva_keys/virtqe_s1.pub'
with open(pubkeyfile, 'r') as fh:
pubkeystr=fh.readlines()[0]
try:
response = client.import_key_pair(
DryRun=False,
KeyName=keyname,
PublicKeyMaterial=pubkeystr
)
log.info("%s added!", keyname)
except Exception as error:
if 'Duplicate' in str(error):
log.info("%s already exists", keyname)
else:
log.info('%s', error)
ssh_key_str = ssh_key_str.rstrip('\n')
subnet_str = subnet_str.rstrip('\n')
dva_templ = string.Template("""bugzilla: {password: password, user: user@example.com}
cloud_access:
ec2:
ec2_access_key: $ec2_access_key
ec2_secret_key: $ec2_secret_key
ssh:
$ssh_key_info
openstack:
ec2_access_key: AAAAAAAAAAAAAAAAAAAA
ec2_secret_key: B0B0B0B0B0B0B0B0B0B0a1a1a1a1a1a1a1a1a1a1
endpoint: 192.168.0.1
path: /services/Cloud
port: 8773
ssh: [user, /home/user/.pem/openstack.pem]
subscription_manager:
subscription_username: $subscription_username
subscription_password: $subscription_password
subnet:
$subnet_info
#global_setup_script: /tmp/setup_script.sh
""")
final_cfg = dva_templ.substitute(ec2_access_key=ACCESS_KEY, ec2_secret_key=SECRET_KEY,ssh_key_info=ssh_key_str,
subnet_info=subnet_str,subscription_username=subscription_username,subscription_password=subscription_password)
with open(final_file,'wt') as fh:
fh.write(final_cfg)
log.info("New file generated: %s", final_file)