Skip to content

Instantly share code, notes, and snippets.

@kpavlovsky
Created September 28, 2020 05:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kpavlovsky/5aa7527ffb25d94173ae9972dcfd53ae to your computer and use it in GitHub Desktop.
Run the deployment: Celery, Paramiko
def run_deployment(deployment: Deployment):
"""Runs deployment as 'app' user with separate commands passing commands via base64 scripts"""
server = deployment.application.server # type: Server
rsa_key = server.user.private_key # type: str
deployment.log(f"ran run_deployment_v2 for deployment {deployment.id}")
username = 'app'
deployment.log(f'??? Connecting to {username}@{server.ip_address}')
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=username, pkey=pkey, timeout=5)
build_script = base64_command(make_build_script(deployment=deployment))
deployment.stage = DEPLOYMENT_STAGE.build
deployment.save(update_fields=['stage', ])
stdin, stdout, stderr = client.exec_command(build_script)
for line in stdout:
deployment.log('... ' + line.strip('\n'))
for line in stderr:
deployment.log('!!! ' + line.strip('\n'))
last_exit_code = stdout.channel.recv_exit_status()
if last_exit_code == 0:
deployment_script = base64_command(make_deployment_script(deployment=deployment))
deployment.stage = DEPLOYMENT_STAGE.deploy
deployment.save(update_fields=['stage', ])
stdin, stdout, stderr = client.exec_command(deployment_script)
for line in stdout:
deployment.log('... ' + line.strip('\n'))
for line in stderr:
deployment.log('!!! ' + line.strip('\n'))
last_exit_code = stdout.channel.recv_exit_status()
if last_exit_code == 0:
if not deployment.application.skip_release_command:
release_script = base64_command(make_release_script(deployment=deployment))
deployment.stage = DEPLOYMENT_STAGE.release
deployment.save(update_fields=['stage', ])
stdin, stdout, stderr = client.exec_command(release_script)
for line in stdout:
deployment.log('... ' + line.strip('\n'))
for line in stderr:
deployment.log('!!! ' + line.strip('\n'))
last_exit_code = stdout.channel.recv_exit_status()
if last_exit_code == 0:
cleanup_script = base64_command(make_cleanup_script())
deployment.stage = DEPLOYMENT_STAGE.cleanup
deployment.save(update_fields=['stage', ])
stdin, stdout, stderr = client.exec_command(cleanup_script)
for line in stdout:
deployment.log('... ' + line.strip('\n'))
for line in stderr:
deployment.log('!!! ' + line.strip('\n'))
last_exit_code = stdout.channel.recv_exit_status()
client.close()
deployment.log(f"Connection closed to the server. Exit code: {last_exit_code}")
if last_exit_code == 0:
deployment.status = DEPLOYMENT_STATUS.deployed
else:
deployment.status = DEPLOYMENT_STATUS.failed
deployment.log(f'Deployment finished with exit_code: {last_exit_code}')
deployment.finished_dt = arrow.now().datetime
deployment.save()
except AuthenticationException as e:
deployment.log(f"!!! Authentication failed: {e}")
deployment.status = DEPLOYMENT_STATUS.failed
deployment.finished_dt = arrow.now().datetime
message = str(e)
exception_type = type(e).__name__
deployment.error_message = f"{exception_type}: {message}"
deployment.save()
return f"Failed {str(e)}"
except SSHException as e:
deployment.log(f"!!! SSHException: {e}")
deployment.status = DEPLOYMENT_STATUS.failed
deployment.finished_dt = arrow.now().datetime
message = str(e)
exception_type = type(e).__name__
deployment.error_message = f"{exception_type}: {message}"
deployment.save()
return f"Failed {str(e)}"
except Exception as e:
deployment.status = DEPLOYMENT_STATUS.failed
deployment.finished_dt = arrow.now().datetime
message = str(e)
exception_type = type(e).__name__
deployment.error_message = f"{exception_type}: {message}"
deployment.log(f"!!! Exception: {exception_type}: {message}")
deployment.save()
return f"Failed {str(e)}"
return "Succeeded"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment