From 4398cd017019b92a21686fef08579eb991e7b1e9 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 16 Dec 2017 20:04:02 +0200 Subject: Add: icktool put-blob and get-blob --- icktool | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'icktool') diff --git a/icktool b/icktool index 165c8b3..bb76aaa 100755 --- a/icktool +++ b/icktool @@ -56,6 +56,7 @@ types = [ 'logs', ] + class Icktool(cliapp.Application): _default_scopes = [ @@ -63,6 +64,8 @@ class Icktool(cliapp.Application): 'uapi_work_post', 'uapi_projects_id_pipelines_id_get', 'uapi_projects_id_pipelines_id_put', + 'uapi_blobs_id_get', + 'uapi_blobs_id_put', ] + scopes_for_types(types) def add_settings(self): @@ -285,6 +288,25 @@ class Icktool(cliapp.Application): self._report(code, 200, text) self.output.write(text) + def cmd_get_blob(self, args): + blob_id = args[0] + blob_api = self._new_blob_api() + status_code, blob = blob_api.get(blob_id) + if status_code == 200: + filename = self.settings['output'] or '/dev/stdout' + with open(filename, 'wb') as f: + f.write(blob) + else: + sys.exit('Error: {}'.format(status_code)) + + def cmd_put_blob(self, args): + blob_id = args[0] + blob = sys.stdin.read() + blob_api = self._new_blob_api() + code, text = blob_api.put(blob_id, blob) + if code != 200: + sys.exit(text) + def _new_token(self): scopes = self.settings['scope'] cmd = self.settings['token-private-key-cmd'] @@ -306,6 +328,14 @@ class Icktool(cliapp.Application): api.set_verify(self.settings['verify-tls']) return api + def _new_blob_api(self): + token = self.settings['token'] or self._new_token() + api = BlobAPI() + api.set_token(token) + api.set_url(self.settings['controller']) + api.set_verify(self.settings['verify-tls']) + return api + def _new_rc(self, path, field_name): api = self._new_api() return ResourceCommands(path, api, field_name) @@ -410,6 +440,47 @@ class API: return r.status_code, r.text +class BlobAPI: + + def __init__(self): + self._url = None + self._token = None + self._verify = True + + def set_url(self, url): + self._url = url + + def set_token(self, token): + self._token = token + + def set_verify(self, verify): + self._verify = verify + + def get(self, blob_id): + assert self._url is not None + assert self._token is not None + + full_url = '{}/blobs/{}'.format(self._url, blob_id) + headers = { + 'Authorization': 'Bearer {}'.format(self._token), + } + r = requests.get(full_url, headers=headers, verify=self._verify) + return r.status_code, r.content + + def put(self, blob_id, blob): + assert self._url is not None + assert self._token is not None + + full_url = '{}/blobs/{}'.format(self._url, blob_id) + headers = { + 'Authorization': 'Bearer {}'.format(self._token), + 'Content-Type': 'application/octet-stream', + } + r = requests.put( + full_url, data=blob, headers=headers, verify=self._verify) + return r.status_code, r.text + + class ResourceCommands: def __init__(self, path, api, name_field): -- cgit v1.2.1