Created
October 28, 2019 11:06
-
-
Save kpavlovsky/2c18ce607ef40cacd03a198e6969c097 to your computer and use it in GitHub Desktop.
Connect to a remote SSH server with private key in a string, download a bash script and run it with CURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def server_setup(server_id: int): | |
"""Connects to server and runs installation of required software""" | |
server = Server.objects.get(pk=server_id) | |
server.setup_status = SETUP_STATUS.started | |
server.save(update_fields=['setup_status', ]) | |
rsa_key = server.user.private_key | |
print(f"ran server_setup server_id={server_id}") | |
setup_command = "curl -sSL https://app.appliku.com/server_api/initial_installation.sh | bash" | |
try: | |
pkey = paramiko.RSAKey.from_private_key(StringIO(rsa_key)) | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
client.connect(server.ip_address, username='root', pkey=pkey, timeout=5) | |
stdin, stdout, stderr = client.exec_command(setup_command) | |
for line in stdout: | |
print('... ' + line.strip('\n')) | |
client.close() | |
server.setup_status = SETUP_STATUS.finished | |
server.save(update_fields=['setup_status', ]) | |
except: | |
server.setup_status = SETUP_STATUS.error | |
server.save(update_fields=['setup_status', ]) | |
return "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment