1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #!/usr/bin/env python3
- import argparse
- import os
- import common
- container_name = kwargs['repo_short_id']
- container_hostname = kwargs['repo_short_id']
- image_name = kwargs['repo_short_id']
- target_dir = '/root/{}'.format(kwargs['repo_short_id'])
- docker = ['sudo', 'docker']
- def create(args):
- self.sh.run_cmd(docker + ['build', '-t', image_name, '.', LF])
- # --privileged for KVM:
- # https://stackoverflow.com/questions/48422001/launching-qemu-kvm-from-inside-docker-container
- self.sh.run_cmd(
- docker +
- [
- 'create', LF,
- '--hostname', container_hostname, LF,
- '-i', LF,
- '--name', container_name, LF,
- '--net', 'host', LF,
- '--privileged', LF,
- '-t', LF,
- '-w', target_dir, LF,
- '-v', '{}:{}'.format(os.getcwd(), target_dir), LF,
- image_name,
- ]
- )
- def destroy(args):
- stop(args)
- self.sh.run_cmd(docker + ['rm', container_name, LF])
- self.sh.run_cmd(docker + ['rmi', image_name, LF])
- def sh(args):
- start(args)
- if args:
- sh_args = args
- else:
- sh_args = ['bash']
- self.sh.run_cmd(
- docker + ['exec', '-i', '-t', container_name] +
- sh_args +
- [LF],
- )
- def start(args):
- self.sh.run_cmd(docker + ['start', container_name, LF])
- def stop(args):
- self.sh.run_cmd(docker + ['stop', container_name, LF])
- cmd_action_map = {
- 'create': lambda args: create(args),
- 'DESTROY': lambda args: destroy(args),
- 'sh': lambda args: sh(args),
- 'start': lambda args: start(args),
- 'stop': lambda args: stop(args),
- }
- parser = argparse.ArgumentParser()
- parser.add_argument('cmd', choices=cmd_action_map)
- parser.add_argument('args', nargs='*')
- self.add_dry_run_argument(parser)
- args = parser.parse_args()
- self.setup_dry_run_arguments(args)
- cmd_action_map[kwargs['cmd']](kwargs['args'])
|