4
4
import configurator
5
5
import sys
6
6
import random
7
+ import logging
7
8
8
9
from libcloud .compute .types import Provider
9
10
from libcloud .compute .providers import get_driver
18
19
NodeState .PENDING : "PENDING" ,
19
20
NodeState .UNKNOWN : "UNKNOWN" }
20
21
22
+ logging .basicConfig (filename = 'aws.log' , level = logging .INFO )
23
+
21
24
def list_resources (driver ):
22
25
23
26
resources = driver .list_nodes ()
@@ -48,45 +51,55 @@ def get_public_ip(driver, name):
48
51
print "Could not find a resource with the id " , name
49
52
return - 1
50
53
51
- def create_security_group (driver , configs ):
54
+ def aws_create_security_group (driver , configs ):
52
55
group_name = configs ["SECURITY_GROUP" ]
53
56
current = driver .ex_list_security_groups ()
54
57
if group_name in current :
55
- print "INFO: Security group: " , group_name , " is already present"
58
+ logging . info ( " Security group: %s is already present", group_name )
56
59
else :
57
- print "INFO: Creating new security group: " , group_name
60
+ logging . info ( " Creating new security group: %s " , group_name )
58
61
res = driver .ex_create_security_group (name = group_name ,description = "Open all ports" )
59
62
if not driver .ex_authorize_security_group (group_name , 0 , 65000 , '0.0.0.0/0' ):
60
- print "INFO: Authorizing ports for security group failed"
63
+ logging . info ( " Authorizing ports for security group failed")
61
64
if not driver .ex_authorize_security_group (group_name , 0 , 65000 , '0.0.0.0/0' , protocol = 'udp' ):
62
- print "INFO: Authorizing ports for security group failed"
63
- print res
65
+ logging . info ( " Authorizing ports for security group failed")
66
+ logging . debug ( "Security group: %s" , str ( res ))
64
67
65
68
def check_keypair (driver , configs ):
66
69
if "AWS_KEYPAIR_NAME" in configs and "AWS_KEYPAIR_FILE" in configs :
67
- print "AWS_KEYPAIR_NAME : " , configs ['AWS_KEYPAIR_NAME' ]
68
- print "AWS_KEYPAIR_FILE : " , configs ['AWS_KEYPAIR_FILE' ]
70
+ logging . debug ( "AWS_KEYPAIR_NAME : %s " , configs ['AWS_KEYPAIR_NAME' ])
71
+ logging . debug ( "AWS_KEYPAIR_FILE : %s " , configs ['AWS_KEYPAIR_FILE' ])
69
72
all_pairs = driver .list_key_pairs ()
70
73
for pair in all_pairs :
71
74
if pair .name == configs ['AWS_KEYPAIR_NAME' ]:
72
- print "INFO: KEYPAIR exists, registered"
75
+ logging . info ( " KEYPAIR exists, registered")
73
76
return 0
74
77
75
- print "INFO : KEYPAIR does not exist. Creating keypair"
78
+ logging . info ( " KEYPAIR does not exist. Creating keypair")
76
79
key_pair = driver .create_key_pair (name = configs ['AWS_KEYPAIR_NAME' ])
77
80
f = open (configs ['AWS_KEYPAIR_FILE' ], 'w' )
78
81
f .write (str (key_pair .private_key ) + '\n ' )
79
82
f .close ()
80
83
os .chmod (configs ['AWS_KEYPAIR_FILE' ], 0600 )
84
+ logging .info ("KEYPAIR created" )
81
85
else :
82
- print "AWS_KEYPAIR_NAME and/or AWS_KEYPAIR_FILE missing"
83
- print "ERROR: Cannot proceed without AWS_KEYPAIR_NAME and AWS_KEYPAIR_FILE"
86
+ logging . error ( "AWS_KEYPAIR_NAME and/or AWS_KEYPAIR_FILE missing" )
87
+ logging . error ( " Cannot proceed without AWS_KEYPAIR_NAME and AWS_KEYPAIR_FILE")
84
88
exit (- 1 )
85
89
86
90
87
91
def start_headnode (driver , configs ):
88
92
userdata = configurator .getstring ("headnode" )
89
93
94
+ # Check if headnode
95
+ nodes = driver .list_nodes ()
96
+ headnode = False
97
+ for node in nodes :
98
+ if node .name == "headnode" and node .state == NodeState .RUNNING :
99
+ headnode = node
100
+ print "INFO: Headnode is RUNNING"
101
+ return 0
102
+
90
103
size = NodeSize (id = configs ['HEADNODE_MACHINE_TYPE' ], name = 'headnode' ,
91
104
ram = None , disk = None , bandwidth = None , price = None , driver = driver )
92
105
image = NodeImage (id = configs ['HEADNODE_IMAGE' ], name = None , driver = driver )
@@ -96,7 +109,9 @@ def start_headnode(driver, configs):
96
109
ex_keyname = configs ['AWS_KEYPAIR_NAME' ],
97
110
ex_securitygroup = configs ['SECURITY_GROUP' ],
98
111
ex_userdata = userdata )
112
+ print "INFO: Waiting for headnode bootup ..."
99
113
driver ._wait_until_running (node , wait_period = 5 , timeout = 240 )
114
+ print "INFO: Headnode active!"
100
115
if node .public_ips :
101
116
print '-' * 51
102
117
print '{0:20} | {1:10} | {2:15}' .format (node .name , NODESTATES [node .state ], node .public_ips [0 ])
@@ -114,10 +129,10 @@ def start_worker(driver, configs, worker_names):
114
129
return - 1
115
130
116
131
# Setup userdata
117
- userdata = configurator .getstring ("headnode " )
132
+ userdata = configurator .getstring ("worker " )
118
133
userdata = userdata .replace ("SET_HEADNODE_IP" , headnode .public_ips [0 ])
119
-
120
- print userdata
134
+ logging . info ( "Headnode connection url : " , headnode . public_ips [ 0 ])
135
+ logging . debug ( "Worker userdata : %s" , userdata )
121
136
122
137
list_nodes = []
123
138
for worker_name in worker_names :
@@ -131,7 +146,7 @@ def start_worker(driver, configs, worker_names):
131
146
ex_securitygroup = configs ['SECURITY_GROUP' ],
132
147
ex_userdata = userdata )
133
148
list_nodes .append (node )
134
- print list_nodes
149
+ logging . info ( "Worker node started : %s" , str ( node ))
135
150
136
151
137
152
def terminate_all_nodes ():
@@ -156,7 +171,7 @@ def init():
156
171
#configurator.pretty_configs(configs)
157
172
driver = get_driver (Provider .EC2_US_WEST_OREGON ) # was EC2
158
173
ec2_driver = driver (configs ['AWSAccessKeyId' ], configs ['AWSSecretKey' ])
159
- create_security_group (ec2_driver , configs )
174
+ aws_create_security_group (ec2_driver , configs )
160
175
check_keypair (ec2_driver , configs )
161
176
return configs ,ec2_driver
162
177
0 commit comments