-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfigurator.py
55 lines (40 loc) · 1.45 KB
/
configurator.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
#!/usr/bin/env python
import logging
import uuid
import json
import boto
import boto.ec2
def _read_conf(config_file):
cfile = open(config_file, 'r').read()
config = {}
for line in cfile.split('\n'):
if line.startswith('#') or not line :
continue
temp = line.split('=')
config[temp[0]] = temp[1].strip('\r')
return config
def load_confs(conf_file):
configs = _read_conf(conf_file)
if not configs['AWS_CREDENTIALS_FILE']:
print "No creds file found"
exit(-1)
creds = []
with open(configs['AWS_CREDENTIALS_FILE']) as cred_file:
creds = json.load(cred_file)
configs['AWSAccessKeyId'] = creds['AccessKey']['AccessKeyId']
configs['AWSSecretKey'] = creds['AccessKey']['SecretAccessKey']
return configs
def init():
configs = load_confs("configs")
regions = boto.ec2.regions()
logging.debug("AWS region : ", configs['AWS_REGION'])
if configs['AWS_REGION'] not in [x.name for x in regions]:
print configs['AWS_REGION']
conn = boto.ec2.connect_to_region(configs['AWS_REGION'],
aws_access_key_id=configs['AWSAccessKeyId'],
aws_secret_access_key=configs['AWSSecretKey'])
if conn == None :
print "[INFO] : Region name could be incorrect"
print "[ERROR]: Failed to connect to region, exiting"
exit(-1)
return configs, conn