-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtool
executable file
·155 lines (132 loc) · 5.08 KB
/
tool
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
#!/usr/bin/env node
const { spawnSync } = require('child_process');
const { writeFileSync } = require('fs');
const region = 'us-east-1';
const stackName = 'EmrExperiment1';
const deploymentBucketName = 'emr-experiment-bucket';
const ec2KeyName = 'EmrExperimentEc2KeyPair';
const ec2KeyLocalFilename = 'key.pem';
const yargs = require('yargs');
const argv = yargs
.showHelpOnFail(true)
.demandCommand(1, '')
.command('create-key', 'Create SSH key', () => {}, () => {
shell(`aws ec2 create-key-pair \
--key-name ${ec2KeyName} \
--query \'KeyMaterial\' \
--output text >${ec2KeyLocalFilename}`);
shell(`chmod 400 ${ec2KeyLocalFilename}`);
})
.command('delete-key', 'Delete SSH key', () => {}, () => {
shell(`aws ec2 delete-key-pair \
--key-name ${ec2KeyName}`);
shell(`rm ${ec2KeyLocalFilename}`);
})
.command('deploy', 'Deploy everything', () => {}, () => {
console.log('Deploying');
shell(`aws s3 mb s3://${deploymentBucketName} --region ${region}`);
shell(`aws s3 cp \
build/libs/aws-emr-experiment-1.0-SNAPSHOT.jar \
s3://${deploymentBucketName} \
--region ${region}`);
shell(`aws s3 cp \
sample.csv \
s3://${deploymentBucketName}/sample/ \
--region ${region}`);
shell(`aws s3 cp \
dummy.q \
s3://${deploymentBucketName} \
--region ${region}`);
shell(`aws cloudformation package \
--template-file cf.yml \
--s3-bucket ${deploymentBucketName} \
--output-template-file _packaged.yml`);
shell(`aws cloudformation deploy \
--template-file _packaged.yml \
--stack-name ${stackName} \
--capabilities CAPABILITY_NAMED_IAM \
--region ${region} \
--parameter-overrides \
DeploymentBucketName=${deploymentBucketName} \
Ec2KeyName=${ec2KeyName}`);
})
.command('undeploy', 'Undeploy everything', () => {}, () => {
console.log('Undeploying');
shell(`aws cloudformation delete-stack --stack-name ${stackName} --region ${region}`);
shell(`aws cloudformation wait stack-delete-complete --stack-name ${stackName} --region ${region}`);
shell(`aws s3 rm s3://${deploymentBucketName} --recursive`);
shell(`aws s3 rb s3://${deploymentBucketName} --force`);
})
.command('submit-jar', 'Submit a dummy custom JAR step', () => {}, () => {
console.log('Submitting a step');
writeFileSync('step.json', JSON.stringify([{
'Name': 'MyDummyCustomJarStep',
'Type': 'CUSTOM_JAR',
'Jar': `s3://${deploymentBucketName}/aws-emr-experiment-1.0-SNAPSHOT.jar`,
'MainClass': 'me.loki2302.EmrHandler',
'Args': [ 'argOne', 'argTwo', 'argThree' ],
'Properties': 'myCustomSystemPropertyOne=theValueForIt,myCustomSystemPropertyTwo=hahaha'
}]));
const clusterId = getStackOutput('ClusterId');
shell(`aws emr add-steps \
--cluster-id ${clusterId} \
--steps file://./step.json`);
})
.command('submit-hive', 'Submit a Hive step', () => {}, () => {
console.log('Submitting a Hive step');
const outputS3Url = `s3://${deploymentBucketName}/script-output/`;
writeFileSync('step.json', JSON.stringify([{
'Name': 'MyDummyHiveStep',
'Type': 'HIVE',
'Args': [
'-f', `s3://${deploymentBucketName}/dummy.q`,
'-d', `INPUT=s3://${deploymentBucketName}/sample/`,
'-d', `OUTPUT=${outputS3Url}` ]
}]));
shell(`aws s3 rm ${outputS3Url} --recursive`);
const clusterId = getStackOutput('ClusterId');
shell(`aws emr add-steps \
--cluster-id ${clusterId} \
--steps file://./step.json`);
})
.command('ssh', 'Generate a command that SSHes to EMR master (do $(./tool ssh))', () => {}, () => {
const clusterId = getStackOutput('ClusterId');
console.log(`aws emr ssh --cluster-id ${clusterId} --key-pair-file ./${ec2KeyLocalFilename}`);
})
.help()
.demandCommand()
.strict()
.argv;
function shell(command) {
console.log(command);
const result = spawnSync(command, [], {
shell: '/bin/bash',
stdio: 'inherit'
});
if(result.error) {
throw result.error;
}
if(result.status != 0) {
throw 'status != 0';
}
return result;
}
function getShellOutput(command) {
const result = spawnSync(command, [], {
shell: '/bin/bash'
});
if(result.error) {
throw result.error;
}
if(result.status != 0) {
throw 'status != 0';
}
return result.stdout.toString().trim();
}
function getStackOutput(outputKey) {
return getShellOutput(`aws cloudformation describe-stacks \
--stack-name ${stackName} \
--query 'Stacks[0].Outputs[?OutputKey==\`'${outputKey}'\`].OutputValue' \
--output text \
--region ${region}`);
}