mirror of
https://github.com/Hopiu/portainer-cli.git
synced 2026-05-21 04:51:54 +00:00
Fix update_stack request url mount, add env-file param in update_stack, fix clear-env in update_stack
This commit is contained in:
parent
5dae82b37d
commit
ddcc8e7887
1 changed files with 43 additions and 16 deletions
|
|
@ -22,10 +22,7 @@ env_arg_regex = r'--env\.(.+)=(.+)'
|
||||||
|
|
||||||
def env_arg_to_dict(s):
|
def env_arg_to_dict(s):
|
||||||
split = re.split(env_arg_regex, s)
|
split = re.split(env_arg_regex, s)
|
||||||
return {
|
return (split[1], split[2],)
|
||||||
'name': split[1],
|
|
||||||
'value': split[2],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class PortainerCLI(object):
|
class PortainerCLI(object):
|
||||||
|
|
@ -119,11 +116,12 @@ class PortainerCLI(object):
|
||||||
self.jwt = jwt
|
self.jwt = jwt
|
||||||
|
|
||||||
@plac.annotations(
|
@plac.annotations(
|
||||||
|
env_file=('Environment Variable file', 'option'),
|
||||||
prune=('Prune services', 'flag', 'p'),
|
prune=('Prune services', 'flag', 'p'),
|
||||||
clear_env=('Clear all env vars', 'flag', 'c'),
|
clear_env=('Clear all env vars', 'flag', 'c'),
|
||||||
)
|
)
|
||||||
def update_stack(self, id, endpoint_id, stack_file='', prune=False,
|
def update_stack(self, id, endpoint_id, stack_file='', env_file='',
|
||||||
clear_env=False, *args):
|
prune=False, clear_env=False, *args):
|
||||||
stack_url = 'stacks/{}?endpointId={}'.format(
|
stack_url = 'stacks/{}?endpointId={}'.format(
|
||||||
id,
|
id,
|
||||||
endpoint_id,
|
endpoint_id,
|
||||||
|
|
@ -134,24 +132,53 @@ class PortainerCLI(object):
|
||||||
stack_file_content = open(stack_file).read()
|
stack_file_content = open(stack_file).read()
|
||||||
else:
|
else:
|
||||||
stack_file_content = self.request(
|
stack_file_content = self.request(
|
||||||
'stacks/{}/file?endpointId={}').format(
|
'stacks/{}/file?endpointId={}'.format(
|
||||||
id,
|
id,
|
||||||
endpoint_id,
|
endpoint_id,
|
||||||
).json().get('StackFileContent')
|
)
|
||||||
env_args = filter(
|
).json().get('StackFileContent')
|
||||||
lambda x: re.match(env_arg_regex, x),
|
if env_file:
|
||||||
args,
|
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 = {
|
data = {
|
||||||
'Id': id,
|
'Id': id,
|
||||||
'StackFileContent': stack_file_content,
|
'StackFileContent': stack_file_content,
|
||||||
'Prune': prune,
|
'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(
|
self.request(
|
||||||
stack_url,
|
stack_url,
|
||||||
self.METHOD_PUT,
|
self.METHOD_PUT,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue