diff --git a/Pipfile b/Pipfile index ee3b61b..63057b9 100644 --- a/Pipfile +++ b/Pipfile @@ -12,6 +12,3 @@ validators = ">=0.12.2" "flake8" = "*" setuptools = "*" wheel = "*" - -[requires] -python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock deleted file mode 100644 index b973aae..0000000 --- a/Pipfile.lock +++ /dev/null @@ -1,124 +0,0 @@ -{ - "_meta": { - "hash": { - "sha256": "2e28e1ee11aba53785a7bee2ec9b68d004035f148a7655dbdc0e537eeab9c2e3" - }, - "pipfile-spec": 6, - "requires": { - "python_version": "3.7" - }, - "sources": [ - { - "name": "pypi", - "url": "https://pypi.org/simple", - "verify_ssl": true - } - ] - }, - "default": { - "certifi": { - "hashes": [ - "sha256:339dc09518b07e2fa7eda5450740925974815557727d6bd35d319c1524a04a4c", - "sha256:6d58c986d22b038c8c0df30d639f23a3e6d172a05c3583e766f4c0b785c0986a" - ], - "version": "==2018.10.15" - }, - "chardet": { - "hashes": [ - "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", - "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" - ], - "version": "==3.0.4" - }, - "decorator": { - "hashes": [ - "sha256:2c51dff8ef3c447388fe5e4453d24a2bf128d3a4c32af3fabef1f01c6851ab82", - "sha256:c39efa13fbdeb4506c476c9b3babf6a718da943dab7811c206005a4a956c080c" - ], - "version": "==4.3.0" - }, - "idna": { - "hashes": [ - "sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e", - "sha256:684a38a6f903c1d71d6d5fac066b58d7768af4de2b832e426ec79c30daa94a16" - ], - "version": "==2.7" - }, - "plac": { - "hashes": [ - "sha256:879d3009bee474cc96b5d7a4ebdf6fa0c4931008ecb858caf09eed9ca302c8da", - "sha256:b03f967f535b3bf5a71b191fa5eb09872a5cfb1e3b377efc4138995e10ba36d7" - ], - "index": "pypi", - "version": "==1.0.0" - }, - "requests": { - "hashes": [ - "sha256:99dcfdaaeb17caf6e526f32b6a7b780461512ab3f1d992187801694cba42770c", - "sha256:a84b8c9ab6239b578f22d1c21d51b696dcfe004032bb80ea832398d6909d7279" - ], - "index": "pypi", - "version": "==2.20.0" - }, - "six": { - "hashes": [ - "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9", - "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb" - ], - "version": "==1.11.0" - }, - "urllib3": { - "hashes": [ - "sha256:41c3db2fc01e5b907288010dec72f9d0a74e37d6994e6eb56849f59fea2265ae", - "sha256:8819bba37a02d143296a4d032373c4dd4aca11f6d4c9973335ca75f9c8475f59" - ], - "version": "==1.24" - }, - "validators": { - "hashes": [ - "sha256:172ac45f7d1944ce4beca3c5c53ca7c83e9759e39fd3fedc1cf28e2130268706" - ], - "index": "pypi", - "version": "==0.12.2" - } - }, - "develop": { - "flake8": { - "hashes": [ - "sha256:7253265f7abd8b313e3892944044a365e3f4ac3fcdcfb4298f55ee9ddf188ba0", - "sha256:c7841163e2b576d435799169b78703ad6ac1bbb0f199994fc05f700b2a90ea37" - ], - "index": "pypi", - "version": "==3.5.0" - }, - "mccabe": { - "hashes": [ - "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", - "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" - ], - "version": "==0.6.1" - }, - "pycodestyle": { - "hashes": [ - "sha256:682256a5b318149ca0d2a9185d365d8864a768a28db66a84a2ea946bcc426766", - "sha256:6c4245ade1edfad79c3446fadfc96b0de2759662dc29d07d80a6f27ad1ca6ba9" - ], - "version": "==2.3.1" - }, - "pyflakes": { - "hashes": [ - "sha256:08bd6a50edf8cffa9fa09a463063c425ecaaf10d1eb0335a7e8b1401aef89e6f", - "sha256:8d616a382f243dbf19b54743f280b80198be0bca3a5396f1d2e1fca6223e8805" - ], - "version": "==1.6.0" - }, - "wheel": { - "hashes": [ - "sha256:9fa1f772f1a2df2bd00ddb4fa57e1cc349301e1facb98fbe62329803a9ff1196", - "sha256:d215f4520a1ba1851a3c00ba2b4122665cd3d6b0834d2ba2816198b1e3024a0e" - ], - "index": "pypi", - "version": "==0.32.1" - } - } -} diff --git a/portainer_cli/__init__.py b/portainer_cli/__init__.py index 424f4f3..fd90d84 100755 --- a/portainer_cli/__init__.py +++ b/portainer_cli/__init__.py @@ -1,13 +1,19 @@ #!/usr/bin/env python -import logging -import plac -import json +import os import re +import logging +import json +import plac import validators -from pathlib import Path from requests import Request, Session -__version__ = '0.1.0' + +try: + FileNotFoundError +except NameError: + FileNotFoundError = IOError + +__version__ = '0.2.1' logger = logging.getLogger('portainer-cli') @@ -22,7 +28,7 @@ def env_arg_to_dict(s): } -class PortainerCLI: +class PortainerCLI(object): COMMAND_CONFIGURE = 'configure' COMMAND_LOGIN = 'login' COMMAND_REQUEST = 'request' @@ -50,7 +56,7 @@ class PortainerCLI: def base_url(self, value): if not validators.url(value): raise Exception('Insert a valid base URL') - self._base_url = value if value.endswith('/') else f'{value}/' + self._base_url = value if value.endswith('/') else '{}/'.format(value) self.persist() @property @@ -68,16 +74,20 @@ class PortainerCLI: logger.debug('using local configuration file') return '.portainer-cli.json' logger.debug('using user configuration file') - return Path.joinpath(Path.home(), '.portainer-cli.json') + return os.path.join( + os.path.expanduser('~'), + '.portainer-cli.json', + ) def persist(self): data = { 'base_url': self.base_url, 'jwt': self.jwt, } - logger.info(f'persisting configuration: {data}') + logger.info('persisting configuration: {}'.format(data)) data_file = open(self.data_path, 'w+') data_file.write(json.dumps(data)) + logger.info('configuration persisted in: {}'.format(self.data_path)) def load(self): try: @@ -85,7 +95,7 @@ class PortainerCLI: except FileNotFoundError: return data = json.loads(data_file.read()) - logger.info(f'configuration loaded: {data}') + logger.info('configuration loaded: {}'.format(data)) self._base_url = data.get('base_url') self._jwt = data.get('jwt') @@ -103,7 +113,7 @@ class PortainerCLI: ) r = response.json() jwt = r.get('jwt') - logger.info(f'logged with jwt: {jwt}') + logger.info('logged with jwt: {}'.format(jwt)) self.jwt = jwt @plac.annotations( @@ -112,15 +122,20 @@ class PortainerCLI: ) def update_stack(self, id, endpoint_id, stack_file='', prune=False, clear_env=False, *args): - stack_url = f'stacks/{id}?endpointId={endpoint_id}' + stack_url = 'stacks/{}?endpointId={}'.format( + id, + endpoint_id, + ) current = self.request(stack_url).json() stack_file_content = '' if stack_file: stack_file_content = open(stack_file).read() else: stack_file_content = self.request( - f'stacks/{id}/file?endpointId={endpoint_id}').json().get( - 'StackFileContent') + 'stacks/{}/file?endpointId={}').format( + id, + endpoint_id, + ).json().get('StackFileContent') env_args = filter( lambda x: re.match(env_arg_regex, x), args, @@ -145,7 +160,10 @@ class PortainerCLI: printc=('Print response content', 'flag', 'p'), ) def request(self, path, method=METHOD_GET, data='', printc=False): - url = f'{self.base_url}api/{path}' + url = '{}api/{}'.format( + self.base_url, + path, + ) session = Session() request = Request(method, url) prepped = request.prepare() @@ -154,13 +172,13 @@ class PortainerCLI: try: json.loads(data) prepped.body = data - except Exception as e: + except Exception: prepped.body = json.dumps(data) prepped.headers['Content-Length'] = len(prepped.body) if self.jwt: - prepped.headers['Authorization'] = f'Bearer {self.jwt}' + prepped.headers['Authorization'] = 'Bearer {}'.format(self.jwt) response = session.send(prepped) - logger.debug(f'request response: {response.content}') + logger.debug('request response: {}'.format(response.content)) response.raise_for_status() if printc: print(response.content.decode())