Gitiles
blob: 0212b660a629e9be94237181a3d2e0e9ad053fe9 [file] [
log] [
blame]
CONF_MARKER = "### wmf-laptop-sre - do not edit below this point ###\n"
def check_key(ssh_dir: Path, env: str) -> str:
Checks if a key with the standard name exists.
Returns the full path to the key"""
keyname = ssh_dir.joinpath(f"id_wmf_{env}")
# Now if the key exists, we'll just return its value
raise ValueError(f"ssh key {keyname} not found.")
def update_ssh_config(ssh_dir: Path, keys: Dict):
"""Update the WMF section of the ssh configuration"""
with open("/usr/share/wmf-laptop-sre/ssh-client-config", "r") as fh:
ssh_config_tpl = fh.read()
# Now let's get the variables to substitute.
user = input("Please provide your production/cloud shell username: ")
ssh_config_tpl.replace("USERNAME", user)
.replace("PRODUCTION_KEY", os.path.basename(keys["prod"]))
.replace("WMCS_KEY", os.path.basename(keys["cloud"]))
# Now let's replace the old config.
configfile = ssh_dir.joinpath("config")
print("Patching the ssh configuration")
with configfile.open("r") as fh:
with configfile.open("w") as fh:
def start_systemd(env: str):
"""Enable the systemd service if needed"""
svcname = f"ssh-agent@wmf-{env}.service"
subprocess.check_call(f"systemctl --user is-active {svcname} > /dev/null", shell=True)
except subprocess.CalledProcessError:
print(f"Enabling {svcname}")
subprocess.check_call(f"systemctl --user start {svcname}", shell=True)
def make_known_hosts_dir(ssh_dir: Path):
"""Create the known_hosts directory if not present"""
kh = ssh_dir.joinpath("known_hosts.d")
print(f"Creating {kh} if non-existent")
kh.mkdir(parents=True, exist_ok=True)
ssh_dir = Path.home().joinpath(".ssh")
keyname = check_key(ssh_dir, env)
# Now check if the user already has an ssh config,
# In that case just paste the wmf config below it.
update_ssh_config(ssh_dir, keys)
make_known_hosts_dir(ssh_dir)
# Now let's ensure the services are up and running.
if __name__ == "__main__":