mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 04:53:36 +00:00
droplets: Use bash script instead of cloud-config as user data.
We use cloud-config for setting up the SSH keys and executing some commands. When cloud-config sets the SSH key it doesn't override the existing keys. So we need to set the SSH keys manually using a command instead. This means we no longer require cloud config. We can instead pass a bash script as the user data instead of cloud-config. I also included a command to set the SSH key of the root.
This commit is contained in:
@@ -96,15 +96,13 @@ def exit_if_droplet_exists(my_token: str, username: str, recreate: bool) -> None
|
|||||||
return
|
return
|
||||||
print("...No droplet found...proceeding.")
|
print("...No droplet found...proceeding.")
|
||||||
|
|
||||||
def set_user_data(username: str, userkeys: List[Dict[str, Any]]) -> str:
|
def set_user_data(username: str, userkey_dicts: List[Dict[str, Any]]) -> str:
|
||||||
print("Setting cloud-config data, populated with GitHub user's public keys...")
|
print("Setting cloud-config data, populated with GitHub user's public keys...")
|
||||||
ssh_authorized_keys = ""
|
userkeys = [userkey_dict["key"] for userkey_dict in userkey_dicts]
|
||||||
|
ssh_keys = "\n".join(userkeys)
|
||||||
|
|
||||||
# spaces here are important here - these need to be properly indented under
|
setup_root_ssh_keys = "printf '{keys}' > /root/.ssh/authorized_keys".format(keys=ssh_keys)
|
||||||
# ssh_authorized_keys:
|
setup_zulipdev_ssh_keys = "printf '{keys}' > /home/zulipdev/.ssh/authorized_keys".format(keys=ssh_keys)
|
||||||
for key in userkeys:
|
|
||||||
ssh_authorized_keys += "\n - {}".format(key['key'])
|
|
||||||
# print(ssh_authorized_keys)
|
|
||||||
|
|
||||||
# We pass the hostname as username.zulipdev.org to the DigitalOcean API.
|
# We pass the hostname as username.zulipdev.org to the DigitalOcean API.
|
||||||
# But some droplets (eg on 18.04) are created with with hostname set to just username.
|
# But some droplets (eg on 18.04) are created with with hostname set to just username.
|
||||||
@@ -121,23 +119,20 @@ def set_user_data(username: str, userkeys: List[Dict[str, Any]]) -> str:
|
|||||||
server_repo_setup = setup_repo.format(username, "zulip")
|
server_repo_setup = setup_repo.format(username, "zulip")
|
||||||
python_api_repo_setup = setup_repo.format(username, "python-zulip-api")
|
python_api_repo_setup = setup_repo.format(username, "python-zulip-api")
|
||||||
|
|
||||||
cloudconf = """
|
cloudconf = """\
|
||||||
#cloud-config
|
#!/bin/bash
|
||||||
users:
|
|
||||||
- name: zulipdev
|
|
||||||
ssh_authorized_keys:{ssh_authorized_keys}
|
|
||||||
runcmd:
|
|
||||||
- {hostname_setup}
|
|
||||||
- su -c '{server_repo_setup}' zulipdev
|
|
||||||
- su -c '{python_api_repo_setup}' zulipdev
|
|
||||||
- su -c 'git config --global core.editor nano' zulipdev
|
|
||||||
- su -c 'git config --global pull.rebase true' zulipdev
|
|
||||||
power_state:
|
|
||||||
mode: reboot
|
|
||||||
condition: True
|
|
||||||
""".format(ssh_authorized_keys=ssh_authorized_keys, hostname_setup=hostname_setup,
|
|
||||||
server_repo_setup=server_repo_setup, python_api_repo_setup=python_api_repo_setup)
|
|
||||||
|
|
||||||
|
{setup_zulipdev_ssh_keys}
|
||||||
|
{setup_root_ssh_keys}
|
||||||
|
{hostname_setup}
|
||||||
|
su -c '{server_repo_setup}' zulipdev
|
||||||
|
su -c '{python_api_repo_setup}' zulipdev
|
||||||
|
su -c 'git config --global core.editor nano' zulipdev
|
||||||
|
su -c 'git config --global pull.rebase true' zulipdev
|
||||||
|
""".format(setup_root_ssh_keys=setup_root_ssh_keys,
|
||||||
|
setup_zulipdev_ssh_keys=setup_zulipdev_ssh_keys,
|
||||||
|
hostname_setup=hostname_setup,
|
||||||
|
server_repo_setup=server_repo_setup, python_api_repo_setup=python_api_repo_setup)
|
||||||
print("...returning cloud-config data.")
|
print("...returning cloud-config data.")
|
||||||
return cloudconf
|
return cloudconf
|
||||||
|
|
||||||
@@ -251,7 +246,7 @@ if __name__ == '__main__':
|
|||||||
exit_if_droplet_exists(my_token=api_token, username=args.username, recreate=args.recreate)
|
exit_if_droplet_exists(my_token=api_token, username=args.username, recreate=args.recreate)
|
||||||
|
|
||||||
# set user_data
|
# set user_data
|
||||||
user_data = set_user_data(username=args.username, userkeys=public_keys)
|
user_data = set_user_data(username=args.username, userkey_dicts=public_keys)
|
||||||
|
|
||||||
# create droplet
|
# create droplet
|
||||||
ip_address = create_droplet(my_token=api_token,
|
ip_address = create_droplet(my_token=api_token,
|
||||||
|
|||||||
Reference in New Issue
Block a user