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

New commands: up, open and which

parent 52b76175
...@@ -53,10 +53,10 @@ def print_status(): ...@@ -53,10 +53,10 @@ def print_status():
""" """
Prints the status of all the virtual machines managed by shipyard Prints the status of all the virtual machines managed by shipyard
""" """
shpyrd_paths = [str(d) for d in Path(os.environ['HOME'], '.shipyard').iterdir() if d.is_dir()] shpyrd_paths = [str(d) for d in XDG_CONFIG_HOME.iterdir() if d.is_dir()]
vgt_db = Path( vgt_db = Path(
os.environ.get('VAGRANT_HOME', str(Path.home())), os.environ.get('VAGRANT_HOME', str(Path.home().joinpath('.vagrant.d'))),
'.vagrant.d', 'data', 'machine-index', 'index' 'data', 'machine-index', 'index'
) )
count = 0 count = 0
if vgt_db.exists(): if vgt_db.exists():
...@@ -125,6 +125,24 @@ def run_playbook(hostname, playbook): ...@@ -125,6 +125,24 @@ def run_playbook(hostname, playbook):
subprocess.run(['vagrant','destroy'], cwd=vagrant_wd) subprocess.run(['vagrant','destroy'], cwd=vagrant_wd)
@only_if_box_exist
def start_vm(hostname):
"""
Starts the vm with the provided hostname
"""
vagrant_wd = XDG_CONFIG_HOME.joinpath(hostname)
subprocess.run(['vagrant','up'], cwd=vagrant_wd)
@only_if_box_exist
def halt(hostname):
"""
Stops the vm with the provided hostname
"""
vagrant_wd = XDG_CONFIG_HOME.joinpath(hostname)
subprocess.run(['vagrant','halt'], cwd=vagrant_wd)
@only_if_box_exist @only_if_box_exist
def invoke_shell(hostname): def invoke_shell(hostname):
""" """
...@@ -134,18 +152,38 @@ def invoke_shell(hostname): ...@@ -134,18 +152,38 @@ def invoke_shell(hostname):
subprocess.run(['vagrant','ssh'], cwd=vagrant_wd) subprocess.run(['vagrant','ssh'], cwd=vagrant_wd)
@only_if_box_exist
def which(hostname):
"""
Print the directory of a vm by it's hostname
"""
vagrant_wd = XDG_CONFIG_HOME.joinpath(hostname)
print(vagrant_wd)
@only_if_box_exist
def gio_open_vm(hostname):
"""
Open the vm directory in nautilis or any other file manager
"""
vagrant_wd = XDG_CONFIG_HOME.joinpath(hostname)
subprocess.run(['gio','open', vagrant_wd])
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='shipyard', prog='shipyard',
description='This utility will create a new virtual machine using vagrant without writing a Vagrant file.', description='This utility will create a new virtual machine using vagrant without writing a Vagrant file.',
) )
parser.add_argument('command', choices=['status', 'create', 'destroy', 'ssh', 'playbook']) 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('--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('--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('--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') parser.add_argument('--playbook', help='Path to the yml file', action='count')
args = parser.parse_args() args = parser.parse_args()
# Ensure XDG_CONFIG_HOME exists
XDG_CONFIG_HOME.mkdir(parents=True, exist_ok=True)
if args.command == 'status': if args.command == 'status':
print_status() print_status()
elif args.command == 'create': elif args.command == 'create':
...@@ -155,16 +193,16 @@ if __name__ == '__main__': ...@@ -155,16 +193,16 @@ if __name__ == '__main__':
print("Shipyard error: the hostname is required to create a vm") print("Shipyard error: the hostname is required to create a vm")
elif args.command == 'destroy': elif args.command == 'destroy':
destroy_box(args.hostname) destroy_box(args.hostname)
elif args.command == 'up':
start_vm(args.hostname)
elif args.command == 'ssh': elif args.command == 'ssh':
invoke_shell(args.hostname) invoke_shell(args.hostname)
elif args.command == 'which':
which(args.hostname)
elif args.command == 'open':
gio_open_vm(args.hostname)
elif args.command == 'playbook': elif args.command == 'playbook':
run_playbook(args.hostname, args.playbook) run_playbook(args.hostname, args.playbook)
else: else:
parser.print_help() parser.print_help()
exit(100) 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