Skip to content

Commit

Permalink
Merge pull request #197 from glasnt/topic/failed-deployment
Browse files Browse the repository at this point in the history
Add error code on deployment errors, add deploy logs
  • Loading branch information
glasnt authored Jul 21, 2017
2 parents ab7f172 + 663564f commit 675b07c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pip install divio-cli

# Using the CLI

See [Divio Support: How to use the Divio command-line interface](http://support.divio.com/local-development/divio-local/how-to-use-the-divio-command-line-interface)
See [Divio Support: How to use the Divio command-line interface](http://support.divio.com/local-development/divio-shell/divio-cli-reference)

# Releasing the binary

Expand Down
5 changes: 5 additions & 0 deletions divio_cli/api_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ class DeployProjectProgressRequest(JsonResponse, APIRequest):
method = 'GET'


class DeployLogRequest(JsonResponse, APIRequest):
url = 'api/v1/website/{website_id}/deploy-log/{stage}/'
method = 'GET'


class DeployProjectRequest(JsonResponse, APIRequest):
url = '/api/v1/website/{website_id}/deploy/'
method = 'POST'
Expand Down
10 changes: 10 additions & 0 deletions divio_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ def project_deploy(obj, stage):
obj.client.deploy_project_or_get_progress(website_id, stage)


@project.command(name='deploy-log')
@click.argument('stage', default='test')
@click.pass_obj
def project_deploy_log(obj, stage):
"""View last deployment log"""
check_project_context(obj.project)
website_id = obj.project['id']
obj.client.show_deploy_log(website_id, stage)


@project.command(name='dashboard')
@click.pass_obj
def project_dashboard(obj):
Expand Down
46 changes: 37 additions & 9 deletions divio_cli/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ def get_projects(self):
request = api_requests.ProjectListRequest(self.session)
return request()

def show_deploy_log(self, website_id, stage):
project_data = self.get_project(website_id)
# If we have tried to deploy before, there will be a log
if project_data["{}_status".format(stage)]["last_deployment"]["status"]:
deploy_log = self.get_deploy_log(website_id, stage)
task_id = "Deploy Log {}".format(deploy_log["task_id"])
output = task_id + "\n" + deploy_log["output"]
click.echo_via_pager(output)
else:
click.secho('No {} server deployed yet, no log available.'.format(stage), fg='yellow')

def deploy_project_or_get_progress(self, website_id, stage):
def fmt_progress(data):
if not data:
Expand Down Expand Up @@ -134,23 +145,33 @@ def fmt_progress(data):
with click.progressbar(
length=100, show_percent=True,
show_eta=False, item_show_func=fmt_progress) as bar:
progress_percent = 0
while response['is_deploying']:
response = self.deploy_project_progress(website_id, stage)
bar.current_item = progress = response['deploy_progress']
if (
'main_percent' in progress and
'extra_percent' in progress
):
bar.update(
# update the difference of the current percentage
# to the new percentage
progress['main_percent'] +
progress['extra_percent'] -
bar.pos
)
# update the difference of the current percentage
# to the new percentage
progress_percent = (progress['main_percent'] +
progress['extra_percent'] -
bar.pos)
bar.update(progress_percent)
sleep(3)
bar.current_item = 'Done'
bar.update(100)
if response['last_deployment']['status'] == 'failure':
bar.current_item = 'error'
bar.update(progress_percent)

raise click.ClickException(
"\nDeployment failed. Please run 'divio project deploy-log {}' " \
"to get more informaton".format(stage)
)
else:
bar.current_item = 'Done'
bar.update(100)

except KeyboardInterrupt:
click.secho('Disconnected')

Expand All @@ -170,6 +191,13 @@ def deploy_project(self, website_id, stage):
)
return request()

def get_deploy_log(self, website_id, stage):
request = api_requests.DeployLogRequest(
self.session,
url_kwargs={'website_id': website_id, 'stage': stage},
)
return request()

def get_project(self, website_id):
request = api_requests.ProjectDetailRequest(
self.session,
Expand Down

0 comments on commit 675b07c

Please sign in to comment.