80 lines
2.3 KiB
Python
Executable File
80 lines
2.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
from argparse import ArgumentParser
|
|
from args_utils import dir_arg_type
|
|
from base import BaseCLi
|
|
from constants import PORTAINER_HOST_URLS, PORTAINER_STACK_CLI_ACTIONS
|
|
from portainer_host import PortainerHost
|
|
|
|
CLI_DESCRIPTION='''
|
|
This cli is meant to be used to execute whoel LCM flow of
|
|
Portainer host stacks. Portainer host can be supplied separately,
|
|
or by the tag.
|
|
'''
|
|
|
|
class PortainerStackCLi(BaseCLi):
|
|
|
|
def configure_args(self, parser: ArgumentParser):
|
|
parser.add_argument(
|
|
'-a',
|
|
'--action',
|
|
help='Action to be performed on Portainer host',
|
|
required=True,
|
|
choices=[action['value'] for action in PORTAINER_STACK_CLI_ACTIONS.values()]
|
|
)
|
|
parser.add_argument(
|
|
'-s',
|
|
'--stack-dir-path',
|
|
help='Directory with the stack related files',
|
|
type=dir_arg_type
|
|
)
|
|
parser.add_argument(
|
|
'-i',
|
|
'--stack-id',
|
|
help='Id of the stack in Portainer (see list)',
|
|
type=int
|
|
)
|
|
parser.add_argument(
|
|
'-n',
|
|
'--hostname',
|
|
help='Portainer hostname to be used',
|
|
required=True,
|
|
type=str,
|
|
choices=PORTAINER_HOST_URLS.keys()
|
|
)
|
|
|
|
|
|
def process_args(self, parser: ArgumentParser):
|
|
args = parser.parse_args()
|
|
required_args = {
|
|
'stack-dir-path': args.stack_dir_path,
|
|
'stack-id': args.stack_id,
|
|
}
|
|
|
|
for action in PORTAINER_STACK_CLI_ACTIONS.values():
|
|
if not (args.action == action['value'] and action['arg_required']):
|
|
continue
|
|
|
|
for arg_name, arg_value in required_args.items():
|
|
if action['arg_name'] == arg_name and not arg_value:
|
|
parser.print_help()
|
|
self.error(f'Argument --{arg_name} is required.')
|
|
return args
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
parser = ArgumentParser(description=CLI_DESCRIPTION)
|
|
self.configure_args(parser)
|
|
self.args = self.process_args(parser)
|
|
|
|
def start(self):
|
|
host = PortainerHost(PORTAINER_HOST_URLS[self.args.hostname])
|
|
print(host.list_stacks()[0])
|
|
|
|
|
|
def main():
|
|
cli = PortainerStackCLi()
|
|
cli.start()
|
|
|
|
if __name__ == "__main__":
|
|
main() |