This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
201 lines (170 loc) · 6.73 KB
/
controller.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Controller program to interact with AWS EMR and run PySpark pipeline.
import argparse
from subprocess import call
import boto3
from settings import *
emr_client = boto3.client('emr')
def start_cluster(cluster_name, instance_count, master_type, slave_type, src_folder):
src_path = REMOTE_SRC + src_folder
response = emr_client.run_job_flow(
Name=cluster_name,
LogUri=LOG_URI,
ReleaseLabel=EMR_RELEASE_LABEL,
Instances={
'MasterInstanceType': master_type,
'SlaveInstanceType': slave_type,
'InstanceCount': instance_count,
'KeepJobFlowAliveWhenNoSteps': True,
'TerminationProtected': False,
'Ec2SubnetId': EC2_SUBNET_ID,
},
Applications=[
{'Name': 'Spark'},
{'Name': 'Hadoop'},
{'Name': 'Zeppelin'},
{'Name': 'Ganglia'},
],
BootstrapActions=[
{
'Name': 'Upgrade Pip',
'ScriptBootstrapAction': {
'Path': src_path + 'config/upgrade_pip.sh',
},
},
{
'Name': 'Config PySpark Runtime',
'ScriptBootstrapAction': {
'Path': src_path + 'config/sync_node.sh',
'Args': [src_folder, BASE_FOLDER],
},
},
{
'Name': 'Copy Slave SSH Key',
'ScriptBootstrapAction': {
'Path': 's3://elasticmapreduce/bootstrap-actions/run-if',
'Args': [
'instance.isMaster=false',
'cat %sconfig/id_rsa.pub >> /home/hadoop/.ssh/authorized_keys' % LOCAL_SRC,
],
},
},
],
Steps=[
{
'Name': 'Setup Debugging',
'ActionOnFailure': 'TERMINATE_CLUSTER',
'HadoopJarStep': {
'Jar': 'command-runner.jar',
'Args': ['state-pusher-script'],
},
},
],
Configurations=[
{
'Classification': 'spark-env',
'Configurations': [
{
'Classification': 'export',
'Configurations': [],
'Properties': {
'PYTHONPATH': '$PYTHONPATH:' + LOCAL_SRC,
},
}
],
'Properties': {},
},
],
VisibleToAllUsers=True,
JobFlowRole=JOBFLOW_ROLE,
ServiceRole=SERVICE_ROLE,
)
return response['JobFlowId']
def add_script_step_to_cluster(cluster_id, script, argument):
response = emr_client.add_job_flow_steps(
JobFlowId=cluster_id,
Steps=[
{
'Name': script.split('/')[-1],
'ActionOnFailure': 'CONTINUE',
'HadoopJarStep': {
'Jar': 'command-runner.jar',
'Args': [script] + argument,
},
},
],
)
return response['StepIds']
def add_pyspark_step_to_cluster(cluster_id, job_file, argument):
spark_submit_command = [
'spark-submit',
'--deploy-mode', 'cluster', '--master', 'yarn',
'--conf', 'spark.yarn.submit.waitAppCompletion=true',
]
if SPARK_SUBMIT_PACKAGES:
spark_submit_command += ['--packages', ','.join(SPARK_SUBMIT_PACKAGES)]
spark_submit_command += [LOCAL_SRC + job_file] + argument.split(' ')
response = emr_client.add_job_flow_steps(
JobFlowId=cluster_id,
Steps=[
{
'Name': job_file.split('/')[-1],
'ActionOnFailure': 'CONTINUE',
'HadoopJarStep': {
'Jar': 'command-runner.jar',
'Args': spark_submit_command,
},
},
],
)
return response['StepIds']
def stop_cluster(cluster_id):
response = emr_client.terminate_job_flows(
JobFlowIds=[cluster_id]
)
return response
def start(args):
print('Start an EMR cluster')
cluster_id = start_cluster(args.cluster_name, args.instance_count, args.master_type, args.slave_type,
args.src_folder)
print('Cluster id: %s' % cluster_id)
def step(args):
print('Add PySpark step (%s) to EMR cluster %s' % (args.job_file, args.cluster_id))
step_ids = add_pyspark_step_to_cluster(args.cluster_id, args.job_file, args.argument)
print('Step ids: %s' % step_ids)
def stop(args):
print('Stop EMR cluster %s' % args.cluster_id)
response = stop_cluster(args.cluster_id)
print(response)
def push(args):
destination = REMOTE_SRC + args.src_folder
print('Push code to %s' % destination)
call(['aws', 's3', 'sync', '--delete', '.', destination])
if args.cluster_id:
add_script_step_to_cluster(args.cluster_id, LOCAL_SRC + 'config/sync_cluster.sh',
[args.src_folder, BASE_FOLDER])
def main():
parser = argparse.ArgumentParser(description='AWS EMR Controller')
subparsers = parser.add_subparsers()
subparser1 = subparsers.add_parser('start', description='Start an EMR cluster.')
subparser1.add_argument('-c', '--instance_count', default=3, help='instance count, including master')
subparser1.add_argument('-m', '--master_type', default='m4.xlarge', help='type of master instance')
subparser1.add_argument('-s', '--slave_type', default='m4.xlarge', help='type of slave instance')
subparser1.add_argument('-n', '--cluster_name', help='cluster name', required=True)
subparser1.add_argument('-r', '--src_folder', help='source folder', required=True)
subparser1.set_defaults(func=start)
subparser2 = subparsers.add_parser('step', description='Add a PySpark step to EMR cluster.')
subparser2.add_argument('-j', '--cluster_id', help='cluster id', required=True)
subparser2.add_argument('-f', '--job_file', help='pyspark job file', required=True)
subparser2.add_argument('-a', '--argument', help='argument', default='')
subparser2.set_defaults(func=step)
subparser3 = subparsers.add_parser('stop', description='Stop an EMR cluster.')
subparser3.add_argument('-j', '--cluster_id', help='cluster id', required=True)
subparser3.set_defaults(func=stop)
subparser4 = subparsers.add_parser('stop', description='Push code to S3 and cluster.')
subparser4.add_argument('-r', '--src_folder', help='source folder', required=True)
subparser4.add_argument('-j', '--cluster_id', help='cluster id')
subparser4.set_defaults(func=push)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()