-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.py
62 lines (50 loc) · 1.79 KB
/
parsers.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
import click
import sys
from subprocess import check_output
import string
import random
import yaml
letters = string.ascii_lowercase
@click.group()
def cli():
pass
@click.command()
@click.option('--job', '-j', help='Script to run', required=False,
default="create_ec2_instance.sh")
@click.option('--aws', '-a', help='Instance Flavour', required=False, default="t3.micro")
@click.option('--docker', '-d', help='Docker Image', required=True)
@click.option('--entrypoint_args', '-e', help='Entry Point', required=True)
def run(job, aws, docker, entrypoint_args):
cmd_args = list(["bash", job, "entry_point=%s" % entrypoint_args.split()])
cluster = ''.join(random.choice(letters) for i in range(10)) + '-cluster'
policy = ''.join(random.choice(letters) for i in range(10)) + '-policy'
config = ''.join(random.choice(letters) for i in range(10)) + '-config'
instance = aws
create_docker_compose(docker, entrypoint_args)
cmd_args.append("cluster=%s" % cluster)
cmd_args.append("policy=%s" % policy)
cmd_args.append("config=%s" % config)
cmd_args.append("instance=%s" % instance)
try:
print("Command: %s" % ' '.join(cmd_args))
output = str(check_output(cmd_args), 'utf-8')
for out in output.split('\n'):
print(out)
except Exception as e:
print("Some exception occured due to error %s" % str(e))
def create_docker_compose(docker, entrypoint):
entrypoint = entrypoint.split()
compose = {
'version': "1",
'services': {
'cli': {
'image': docker,
'command': entrypoint
}
}
}
with open('docker-compose.yml', 'w') as compose_file:
yaml.dump(compose, compose_file)
cli.add_command(run)
if __name__ == "__main__":
cli()