-
Notifications
You must be signed in to change notification settings - Fork 37
/
aws_startup.py
45 lines (41 loc) · 1.57 KB
/
aws_startup.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
import argparse
import urlparse
import os
import ConfigParser
import subprocess
from requests import Session
ZONE_TO_ID = {
'eu-west-1a': 'a',
'eu-west-1b': 'b',
'eu-west-1c': 'c'
}
cur_dir = os.path.dirname(__file__)
parser = argparse.ArgumentParser(description='------ AWS Startup Script ------')
parser.add_argument('api_dest', type=str, help='Destination to database')
params = parser.parse_args()
api_ini_file_path = os.path.join(cur_dir, 'etc/openprocurement.api.ini')
session = Session()
resp = session.get('http://169.254.169.254/latest/meta-data/placement/availability-zone')
if resp.status_code == 200:
zone = resp.text
zone_suffix = ZONE_TO_ID.get(zone, '')
if zone_suffix:
domain = '{}.{}'.format(zone_suffix, params.api_dest)
else:
domain = params.api_dest
if os.path.isfile(api_ini_file_path):
config = ConfigParser.ConfigParser()
config.read([api_ini_file_path])
for k in ['couchdb.url', 'couchdb.admin_url']:
value = config.get('app:api', k)
url = urlparse.urlparse(value)
if url.username:
url = url._replace(netloc='{}:{}@{}:{}'.format(url.username, url.password,
domain, url.port))
else:
url = url._replace(netloc='{}:{}'.format(domain, url.port))
config.set('app:api', k, url.geturl())
if zone_suffix:
config.set('app:api', 'id', zone_suffix)
with open(api_ini_file_path, 'wb') as configfile:
config.write(configfile)