-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path__init__.py
More file actions
161 lines (132 loc) · 4.81 KB
/
__init__.py
File metadata and controls
161 lines (132 loc) · 4.81 KB
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
import json
from jsonpath_rw import parse
from celery.canvas import Signature
import requests
import cumulus
from cumulus.tasks.job import terminate_job
from cumulus.constants import JobState
from girder_client import GirderClient, HttpError
CHECKIP_URL = 'http://checkip.amazonaws.com/'
def terminate_jobs(task, client, cluster, jobs):
for job in jobs:
task.logger.info('Terminating job %s' % job['_id'])
# Fetch the latest job info
job_url = 'jobs/%s' % job['_id']
job = client.get(job_url)
# Update the status to terminating
body = {
'status': JobState.TERMINATING
}
client.patch(job_url, data=json.dumps(body))
terminate_job(
cluster, job, log_write_url=None,
girder_token=task.taskflow.girder_token)
def get_cluster_job_output_dir(cluster):
job_output_dir \
= parse('config.jobOutputDir').find(cluster)
if job_output_dir:
job_output_dir = job_output_dir[0].value
else:
job_output_dir = None
return job_output_dir
def create_girder_client(girder_api_url, girder_token):
client = GirderClient(apiUrl=girder_api_url)
client.token = girder_token
return client
def create_ec2_cluster(task, cluster, profile, ami):
machine_type = cluster['machine']['id']
nodeCount = cluster['clusterSize']-1
launch_spec = 'ec2'
# Look up the external IP of the deployment to user for firewall rules
r = requests.get(CHECKIP_URL)
r.raise_for_status()
source_ip = '%s/32' % r.text.strip()
extra_rules = [{
'proto': 'tcp',
'from_port': 9000,
'to_port': 9000,
'cidr_ip': source_ip
}]
task.logger.info('Using source ip: %s' % source_ip)
launch_params = {
'master_instance_type': machine_type,
'master_instance_ami': ami,
'node_instance_count': nodeCount,
'node_instance_type': machine_type,
'node_instance_ami': ami,
'gpu': cluster['machine']['gpu'],
'source_cidr_ip': source_ip,
'extra_rules': extra_rules
}
provision_spec = 'gridengine/site'
provision_params = {
'ansible_ssh_user': 'ubuntu'
}
body = {
'type': 'ec2',
'name': cluster['name'],
'config': {
'launch': {
'spec': launch_spec,
'params': launch_params
},
'provision': {
'spec': provision_spec
}
},
'profileId': cluster['profileId']
}
client = create_girder_client(
task.taskflow.girder_api_url, task.taskflow.girder_token)
try:
cluster = client.post('clusters', data=json.dumps(body))
except HttpError as he:
task.logger.exception(he.responseText)
raise
msg = 'Created cluster: %s' % cluster['_id']
task.taskflow.logger.info(msg)
task.logger.info(msg)
# Now save cluster id in metadata
task.taskflow.set_metadata('cluster', cluster)
task.logger.info('Starting cluster.')
body = {
'status': 'launching'
}
client.patch('clusters/%s' % cluster['_id'], data=json.dumps(body))
secret_key = profile['secretAccessKey']
log_write_url = '%s/clusters/%s/log' % (task.taskflow.girder_api_url,
cluster['_id'])
provision_params['cluster_state'] = 'running'
launch_params['cluster_state'] = 'running'
girder_token = task.taskflow.girder_token
cumulus.ansible.tasks.cluster.start_cluster(
launch_spec, provision_spec, cluster, profile, secret_key,
launch_params, provision_params, girder_token, log_write_url,
master_name='head')
# Get the update to date cluster
cluster = client.get('clusters/%s' % cluster['_id'])
# Add the passphrase if there is one. We need to do this as the clusters endpoints will not
# expose it for security reasons.
passphrase = parse('ssh.passphrase').find(profile)
if passphrase:
passphrase = passphrase[0].value
cluster['config']['ssh']['passphrase'] = passphrase
return cluster
@cumulus.taskflow.task
def setup_cluster(task, *args,**kwargs):
cluster = kwargs['cluster']
if '_id' in cluster:
task.taskflow.logger.info('We are using an existing cluster: %s' % cluster['name'])
else:
task.taskflow.logger.info('We are creating an EC2 cluster.')
task.logger.info('Cluster name %s' % cluster['name'])
kwargs['machine'] = cluster.get('machine')
ami = kwargs.get('ami')
profile = kwargs.get('profile')
cluster = create_ec2_cluster(task, cluster, profile, ami)
task.logger.info('Cluster started.')
# Call any follow on task
if 'next' in kwargs:
kwargs['cluster'] = cluster
next = Signature.from_dict(kwargs['next'])
next.delay(*args, **kwargs)