Merge pull request #2 from Ilhasoft/feature/env-file

Feature -env-file
This commit is contained in:
Douglas Paz 2018-12-03 15:38:22 -03:00 committed by GitHub
commit bb34e5ef5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 18 deletions

View file

@ -52,7 +52,7 @@ portainer-cli login douglas d1234
Update stack.
```bash
portainer-cli update_stack id endpoint_id [stack_file]
portainer-cli update_stack id endpoint_id [stack_file] [-env-file]
```
**E.g:**
@ -73,6 +73,7 @@ Where `var` is the environment variable name and `value` is the environment vari
| Flag | Description |
|--|--|
| `-env-file` | Pass env file path, usually `.env` |
| `-p` or `--prune` | Prune services |
| `-c` or `--clear-env` | Clear all environment variables |

View file

@ -13,7 +13,7 @@ try:
except NameError:
FileNotFoundError = IOError
__version__ = '0.2.2'
__version__ = '0.3.0'
logger = logging.getLogger('portainer-cli')
@ -22,10 +22,7 @@ env_arg_regex = r'--env\.(.+)=(.+)'
def env_arg_to_dict(s):
split = re.split(env_arg_regex, s)
return {
'name': split[1],
'value': split[2],
}
return (split[1], split[2],)
class PortainerCLI(object):
@ -119,11 +116,12 @@ class PortainerCLI(object):
self.jwt = jwt
@plac.annotations(
env_file=('Environment Variable file', 'option'),
prune=('Prune services', 'flag', 'p'),
clear_env=('Clear all env vars', 'flag', 'c'),
)
def update_stack(self, id, endpoint_id, stack_file='', prune=False,
clear_env=False, *args):
def update_stack(self, id, endpoint_id, stack_file='', env_file='',
prune=False, clear_env=False, *args):
stack_url = 'stacks/{}?endpointId={}'.format(
id,
endpoint_id,
@ -134,24 +132,53 @@ class PortainerCLI(object):
stack_file_content = open(stack_file).read()
else:
stack_file_content = self.request(
'stacks/{}/file?endpointId={}').format(
'stacks/{}/file?endpointId={}'.format(
id,
endpoint_id,
).json().get('StackFileContent')
env_args = filter(
lambda x: re.match(env_arg_regex, x),
args,
)
).json().get('StackFileContent')
if env_file:
env = {}
for env_line in open(env_file).readlines():
env_line = env_line.strip()
if not env_line \
or env_line.startswith('#') \
or '=' not in env_line:
continue
k, v = env_line.split('=', 1)
k, v = k.strip(), v.strip()
env[k] = v
else:
env_args = filter(
lambda x: re.match(env_arg_regex, x),
args,
)
env = dict(map(
lambda x: env_arg_to_dict(x),
env_args,
))
if not clear_env:
current_env = dict(
map(
lambda x: (x.get('name'), x.get('value'),),
current.get('Env'),
),
)
current_env.update(env)
env = current_env
final_env = list(
map(
lambda x: {'name': x[0], 'value': x[1]},
env.items()
),
)
env = list(map(
lambda x: env_arg_to_dict(x),
env_args,
))
data = {
'Id': id,
'StackFileContent': stack_file_content,
'Prune': prune,
'Env': env if len(env) > 0 or clear_env else current.get('Env'),
'Env': final_env if len(final_env) > 0 else current.get('Env'),
}
logger.debug('update stack data: {}'.format(data))
self.request(
stack_url,
self.METHOD_PUT,