-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_setup.py
67 lines (52 loc) · 2.13 KB
/
cluster_setup.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
"""Script creates the cluster and retrieves the cluster's endpoint."""
import boto3
import time
import configparser
config = configparser.ConfigParser()
config.optionxform = str
config.read('dwh.cfg')
redshift = boto3.client(
'redshift', region_name='us-west-2',
aws_access_key_id=config.get('AWS', 'KEY'),
aws_secret_access_key=config.get('AWS', 'SECRET')
)
def create_cluster():
"""Create the cluster and set its properties."""
try:
redshift.create_cluster(
ClusterType=config.get('DWH', 'CLUSTER_TYPE'),
NodeType=config.get('DWH', 'NODE_TYPE'),
NumberOfNodes=int(config.get('DWH', 'NUM_NODES')),
DBName=config.get('CLUSTER', 'DB_NAME'),
ClusterIdentifier=config.get('DWH', 'CLUSTER_IDENTIFIER'),
MasterUsername=config.get('CLUSTER', 'DB_USER'),
MasterUserPassword=config.get('CLUSTER', 'DB_PASSWORD'),
IamRoles=[config.get('IAM_ROLE', 'ARN')]
)
print('Cluster has been created successfully!')
except Exception as e:
print(e)
def get_endpoint():
"""
Retrieve the endpoint once the cluster is made available.
Stores the endpoint value as the host in the config file.
"""
create_cluster()
ClusterProps = redshift.describe_clusters(
ClusterIdentifier=config.get(
'DWH', 'CLUSTER_IDENTIFIER'))['Clusters'][0]
cluster_status = ClusterProps['ClusterStatus']
print('Waiting for the cluster to be made available...')
while cluster_status == 'creating':
time.sleep(10)
cluster_status = redshift.describe_clusters(
ClusterIdentifier=config.get('DWH', 'CLUSTER_IDENTIFIER')
)['Clusters'][0]['ClusterStatus']
if cluster_status == 'available':
break
print('Cluster is now available!')
cluster_endpoint = redshift.describe_clusters(
ClusterIdentifier=config.get('DWH', 'CLUSTER_IDENTIFIER')
)['Clusters'][0]['Endpoint']['Address']
config.set('CLUSTER', 'HOST', cluster_endpoint)
config.write(open('dwh.cfg', 'w'))