Commit ee75ea30 authored by Noe Nieto's avatar Noe Nieto 💬

Now the hostname parameter is implied

parent e00557df
......@@ -29,6 +29,7 @@ end
XDG_CONFIG_HOME = Path(os.environ['HOME'],'.config','ldh_developer','shipyard')
def only_if_box_exist(func):
def wrapper(*args, **kwargs):
hostname = args[0]
......@@ -160,6 +161,7 @@ def which(hostname):
vagrant_wd = XDG_CONFIG_HOME.joinpath(hostname)
print(vagrant_wd)
@only_if_box_exist
def gio_open_vm(hostname):
"""
......@@ -168,41 +170,60 @@ def gio_open_vm(hostname):
vagrant_wd = XDG_CONFIG_HOME.joinpath(hostname)
subprocess.run(['gio','open', vagrant_wd])
COMMANDS = {
'status': "Prints the status of all the VM's managed by shipyard",
'create': 'Creates a new VM using vagrant and libvirt (KVM)',
'up': 'Start VM',
'destroy': 'Destroy the VM and do cleanup',
'ssh': 'Connect to the vm using SSH',
'playbook': 'Run a playbook against a machine',
'which': 'Print the path to the Vagrantfile',
'open': 'Show the working directory of the VM using the file manager.'
}
if __name__ == '__main__':
parser = argparse.ArgumentParser(
cmd_parser = argparse.ArgumentParser(
prog='shipyard',
description='This utility will create a new virtual machine using vagrant without writing a Vagrant file.',
description='This utility helps you mange virtual machines with Vagrant without dealing with configuration details',
)
parser.add_argument('command', choices=['status', 'create', 'up', 'destroy', 'ssh', 'playbook', 'which', 'open'])
parser.add_argument('--hostname', help='This is the hostname for the new box',)
parser.add_argument('--ram', help='Hoy much RAM for this box (default is 512)', default=512)
parser.add_argument('--cpus', help='Hoy many CPUs for this box (default is 1)', default=1)
parser.add_argument('--playbook', help='Path to the yml file', action='count')
args = parser.parse_args()
cmd_parser.add_argument('command', choices=COMMANDS,)
cmd_args = cmd_parser.parse_args(sys.argv[1:2])
# Ensure XDG_CONFIG_HOME exists
XDG_CONFIG_HOME.mkdir(parents=True, exist_ok=True)
if args.command == 'status':
if cmd_args.command == 'status':
print_status()
elif args.command == 'create':
if args.hostname:
create_box(args.hostname, args.ram, args.cpus)
else:
print("Shipyard error: the hostname is required to create a vm")
elif args.command == 'destroy':
destroy_box(args.hostname)
elif args.command == 'up':
start_vm(args.hostname)
elif args.command == 'ssh':
invoke_shell(args.hostname)
elif args.command == 'which':
which(args.hostname)
elif args.command == 'open':
gio_open_vm(args.hostname)
elif args.command == 'playbook':
run_playbook(args.hostname, args.playbook)
sys.exit()
sub_parser = argparse.ArgumentParser(prog='shipyard',)
if cmd_args.command == 'create':
sub_parser.usage = 'shipyard create <hostname> [--ram RAM] [--cpus CPUS]'
sub_parser.add_argument('hostname', help='This is the hostname for the new the box')
sub_parser.add_argument('--ram', help='Hoy much RAM for this box (default is 512)', default=512)
sub_parser.add_argument('--cpus', help='Hoy many CPUs for this box (default is 1)', default=1)
sub_args = sub_parser.parse_args(sys.argv[2:])
create_box(sub_args.hostname, sub_args.ram, sub_args.cpus)
sys.exit()
sub_parser.add_argument('hostname', help='This is the hostname of the box')
if cmd_args.command == 'playbook':
sub_parser.add_argument('--playbook', help='Path to the yml file', required=True)
sub_args = sub_parser.parse_args(sys.argv[2:])
run_playbook(sub_args.hostname, sub_args.playbook)
sys.exit()
sub_args = sub_parser.parse_args(sys.argv[2:])
if cmd_args.command == 'up':
start_vm(sub_args.hostname)
elif cmd_args.command == 'destroy':
destroy_box(sub_args.hostname)
elif cmd_args.command == 'ssh':
invoke_shell(sub_args.hostname)
elif cmd_args.command == 'which':
which(sub_args.hostname)
elif cmd_args.command == 'open':
gio_open_vm(sub_args.hostname)
else:
parser.print_help()
cmd_parser.print_help()
exit(100)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment