summaryrefslogtreecommitdiff
path: root/icktool
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-04-08 10:28:21 +0300
committerLars Wirzenius <liw@liw.fi>2018-04-08 13:10:38 +0300
commitc4ce61b47d0ded28a4ef881f578b310339eaa2ae (patch)
tree240023515757748d418ab98f673c89abee61a694 /icktool
parent896d176ef803c096ec8d197bb961c7367d862bcb (diff)
downloadick2-c4ce61b47d0ded28a4ef881f578b310339eaa2ae.tar.gz
Add: icktool trigger, status, show-log, show-latest-log
Diffstat (limited to 'icktool')
-rwxr-xr-xicktool108
1 files changed, 108 insertions, 0 deletions
diff --git a/icktool b/icktool
index 6d0b4e2..79fcb48 100755
--- a/icktool
+++ b/icktool
@@ -125,6 +125,16 @@ class Icktool(cliapp.Application):
version = api.get_version()
self._prettyson(version)
+ def cmd_trigger(self, args):
+ project_name = args[0]
+ pipeline_name = args[1]
+
+ token = self._new_token()
+ api = self._new_api()
+ api.set_token(token)
+ result = api.trigger(project_name, pipeline_name)
+ self._prettyson(result)
+
def cmd_make_it_so(self, argv):
obj = self._read_object()
@@ -156,6 +166,76 @@ class Icktool(cliapp.Application):
objs = api.show('/' + kind)
self._prettyson(objs)
+ def cmd_status(self, args):
+ token = self._new_token()
+ api = self._new_api()
+ api.set_token(token)
+
+ rows = []
+ projects = api.show('/projects')['projects']
+ projects = sorted(projects, key=lambda p: p.get('project'))
+
+ builds = api.show('/builds')['builds']
+
+ for project in projects:
+ pipeline_names = sorted(project['pipelines'])
+ for pipeline_name in pipeline_names:
+ build = self._get_latest_build(
+ project['project'], pipeline_name, builds)
+
+ if build is None:
+ build = {
+ 'build_id': 'never',
+ 'log': 'none',
+ 'status': 'n/a',
+ }
+
+ status = api.get_build_status(
+ project['project'], pipeline_name)
+
+ row = {
+ 'project': project['project'],
+ 'pipeline': pipeline_name,
+ 'build_id': build['build_id'],
+ 'status': status['status'],
+ 'build_status': build['status'],
+ 'log': build['log'],
+ }
+ rows.append(row)
+ self._pretty_table(
+ rows, ['project', 'pipeline', 'status', 'build_status', 'log'])
+
+ def _get_latest_build(self, project_name, pipeline_name, builds):
+ wanted = [
+ b for b in builds
+ if b['project'] == project_name and b['pipeline'] == pipeline_name
+ ]
+ if wanted:
+ return wanted[-1]
+ return None
+
+ def cmd_show_latest_log(self, args):
+ token = self._new_token()
+ api = self._new_api()
+ api.set_token(token)
+
+ project_name = args[0]
+ builds = api.show('/builds')['builds']
+ project_builds = [b for b in builds if b['project'] == project_name]
+ if project_builds:
+ b = project_builds[-1]
+ log = api.get_log(b['build_id'])
+ self.output.write(log.decode('UTF-8'))
+
+ def cmd_show_log(self, args):
+ token = self._new_token()
+ api = self._new_api()
+ api.set_token(token)
+
+ build_id = args[0]
+ log = api.get_log(build_id)
+ self.output.write(log.decode('UTF-8'))
+
def _new_api(self):
api = ick2.ControllerClient()
api.set_verify_tls(self.settings['verify-tls'])
@@ -192,6 +272,34 @@ class Icktool(cliapp.Application):
json.dump(obj, self.output, indent=4, sort_keys=True)
self.output.write('\n')
+ def _pretty_table(self, rows, columns):
+ headings = {
+ column: column
+ for column in columns
+ }
+
+ widths = {
+ column: 0
+ for column in columns
+ }
+
+ for row in [headings] + rows:
+ for column in columns:
+ widths[column] = max(widths[column], len(str(row[column])))
+
+ underlines = {
+ column: '-' * widths[column]
+ for column in columns
+ }
+
+ for row in [headings, underlines] + rows:
+ self.output.write(
+ '{}\n'.format(self._pretty_row(widths, row, columns)))
+
+ def _pretty_row(self, widths, row, columns):
+ parts = ['%*s' % (widths[c], row[c]) for c in columns]
+ return ' | '.join(parts)
+
class Command: