-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone_kata.py
executable file
·145 lines (124 loc) · 5.02 KB
/
clone_kata.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
#!/usr/bin/env python3
import click
import os
from fabric import Connection
from dataclasses import dataclass
import csv
from instances import all_instances
from summon import read_regions_config, read_aws_defaults
@dataclass
class KataMachine:
region_name: str
host_ip: str
url: str
def read_classroom_file(file, running_instances):
result = []
reader = csv.DictReader(file)
for row in reader:
url = row["url"]
region_name = row["region"]
matching_instances = [machine for machine in running_instances if machine.url.strip() == url.strip()]
if matching_instances:
instance = matching_instances[0]
machine = KataMachine(url=url, region_name=region_name, host_ip=instance.ip_address)
result.append(machine)
return result
def clone_kata_commandline(kata):
return f"""
sudo -u typist -- sh -c '\
cd /home/typist
mkdir -p katas;\
cd katas;\
git clone {kata}
'\
"""
@click.command()
@click.option(
"--kata",
help="the kata to clone",
default="https://github.com/emilybache/starter.git",
prompt=f"what kata repo?"
)
@click.option(
"--region-name",
help=f"the aws region name (default eu-north-1): {', '.join(read_regions_config().keys())}",
default="eu-north-1"
)
@click.option(
"--aws-profile",
default="default",
help="the aws profile, if you don't use the default"
)
@click.option(
"--host_ip",
help="the ip address",
)
@click.option(
"--coach",
help="add the kata to all running instances owned by this person",
)
@click.option(
"--classroom",
help="add the kata to all running instances in this classroom file",
)
def clone_kata(kata, region_name, aws_profile, host_ip, coach, classroom):
commandline = clone_kata_commandline(kata)
aws_defaults = read_aws_defaults(profile_name=aws_profile)
url_stem = aws_defaults["url_stem"]
if region_name == "all":
all_regions = read_regions_config(profile_name=aws_profile).keys()
for region_name in all_regions:
clone_to_machines_in_region(aws_profile, classroom, coach, host_ip, region_name, url_stem, commandline)
else:
clone_to_machines_in_region(aws_profile, classroom, coach, host_ip, region_name, url_stem, commandline)
def clone_to_machines_in_region(aws_profile, classroom, coach, host_ip, region_name, url_stem, commandline):
machines = determine_machines_to_update(aws_profile, classroom, coach, host_ip, region_name, url_stem)
logging.getLogger().info(f"will clone kata to machines: {[m.url for m in machines]}")
run_commandline_on_machines(commandline, machines, aws_profile)
def run_commandline_on_machines(commandline, machines, aws_profile):
for m in machines:
try:
print(f"will run remote commands on {m}")
c = connect_to_machine(m, aws_profile)
c.run(commandline)
except Exception as e:
print("unexpected problem running remote commands on machine ", m, e)
def connect_to_machine(machine, aws_profile):
key_name = read_regions_config(profile_name=aws_profile)[machine.region_name]["key_name"]
key_file = os.path.expanduser(f"~/.ssh/{key_name}.pem")
c = Connection(host=machine.host_ip.strip(), user='ubuntu', connect_kwargs={
"key_filename": key_file,
}, )
return c
def determine_machines_to_update(aws_profile, classroom, coach, host_ip, region_name, url_stem):
logging.getLogger().info(f"finding machines to clone to in region {region_name}")
running_instances = [machine for machine in all_instances(region_name, aws_profile) if
machine.state.strip() == "running"]
if not running_instances:
logging.getLogger().error(f"No running instances found in region {region_name}")
return []
if host_ip:
instance = [machine for machine in running_instances if host_ip in machine.ip_address]
if not instance:
print(f"ERROR: host ip {host_ip} is not running")
machines = []
else:
machines = [KataMachine(host_ip=host_ip, region_name=region_name, url=instance[0].url)]
elif coach:
running_instances = [machine
for machine in running_instances
if machine.coach.strip() == coach]
machines = [KataMachine(host_ip=machine.ip_address, region_name=region_name, url=machine.url)
for machine in running_instances]
elif classroom:
with open(classroom, 'r', newline="", encoding="utf-8") as f:
machines = read_classroom_file(f, running_instances)
else:
machines = [KataMachine(host_ip=machine.ip_address, region_name=region_name, url=machine.url)
for machine in running_instances
if url_stem in machine.url]
return machines
if __name__ == "__main__":
import logging
logging.basicConfig(level=logging.INFO)
clone_kata()