Commit 5b298e33 authored by Noe Nieto's avatar Noe Nieto 💬

Implement ssh command

parent c0b57c33
...@@ -26,6 +26,18 @@ end ...@@ -26,6 +26,18 @@ end
""" """
def only_if_box_exist(func):
def wrapper(*args, **kwargs):
hostname = args[0]
dot_shipyard = Path(os.environ['HOME'], '.shipyard')
vagrant_wd = dot_shipyard.joinpath(hostname)
if vagrant_wd.exists():
func(*args, **kwargs)
else:
print("Shipyard error: no vm managed by me has the hostname '{}'".format(hostname))
exit(1)
return wrapper
def print_status(): def print_status():
""" """
...@@ -71,26 +83,35 @@ def create_box(hostname, ram, cpus): ...@@ -71,26 +83,35 @@ def create_box(hostname, ram, cpus):
subprocess.run(['vagrant','provision'], cwd=vagrant_wd) subprocess.run(['vagrant','provision'], cwd=vagrant_wd)
@only_if_box_exist
def destroy_box(hostname): def destroy_box(hostname):
""" """
Destroy a box, or all Destroy a box, or all
""" """
dot_shipyard = Path(os.environ['HOME'], '.shipyard') dot_shipyard = Path(os.environ['HOME'], '.shipyard')
vagrant_wd = dot_shipyard.joinpath(hostname) vagrant_wd = dot_shipyard.joinpath(hostname)
if vagrant_wd.exists(): subprocess.run(['vagrant','destroy'], cwd=vagrant_wd)
# Execute vagrant with the generated vagrantfile shutil.rmtree(vagrant_wd)
subprocess.run(['vagrant','destroy'], cwd=vagrant_wd)
shutil.rmtree(vagrant_wd)
else: @only_if_box_exist
print("Shipyard error: no vm managed by me has the hostname '{}'".format(hostname)) def run_playbook(hostname, playbook):
exit(0) """
run a playbook against one of the boxes
"""
dot_shipyard = Path(os.environ['HOME'], '.shipyard')
vagrant_wd = dot_shipyard.joinpath(hostname)
subprocess.run(['vagrant','destroy'], cwd=vagrant_wd)
def run_playbook(name, playbook): @only_if_box_exist
def invoke_shell(hostname):
""" """
run a playbook against one of the boxes run a playbook against one of the boxes
""" """
print('Running playbook {} for box {}'.format(playbook, name)) dot_shipyard = Path(os.environ['HOME'], '.shipyard')
vagrant_wd = dot_shipyard.joinpath(hostname)
subprocess.run(['vagrant','ssh'], cwd=vagrant_wd)
if __name__ == '__main__': if __name__ == '__main__':
...@@ -115,9 +136,9 @@ if __name__ == '__main__': ...@@ -115,9 +136,9 @@ if __name__ == '__main__':
elif args.command == 'destroy': elif args.command == 'destroy':
destroy_box(args.hostname) destroy_box(args.hostname)
elif args.command == 'ssh': elif args.command == 'ssh':
invoke_shell() invoke_shell(args.hostname)
elif args.command == 'playbook': elif args.command == 'playbook':
run_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