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

Implement ssh command

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