Commit e80a99ae authored by Noe Nieto's avatar Noe Nieto 💬
Browse files

Implement destroy command

parent bb4f8f4c
...@@ -5,6 +5,7 @@ import argparse ...@@ -5,6 +5,7 @@ import argparse
import tempfile import tempfile
import subprocess import subprocess
import json import json
import shutil
from pathlib import Path from pathlib import Path
vagrant_template = """ vagrant_template = """
...@@ -24,6 +25,8 @@ Vagrant.configure("2") do |config| ...@@ -24,6 +25,8 @@ Vagrant.configure("2") do |config|
end end
""" """
def print_status(): 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
...@@ -68,14 +71,20 @@ def create_box(hostname, ram, cpus): ...@@ -68,14 +71,20 @@ def create_box(hostname, ram, cpus):
subprocess.run(['vagrant','provision'], cwd=vagrant_wd) subprocess.run(['vagrant','provision'], cwd=vagrant_wd)
def destroy_box(name, all=False): def destroy_box(hostname):
""" """
Destroy a box, or all Destroy a box, or all
""" """
if all: dot_shipyard = Path(os.environ['HOME'], '.shipyard')
print('Destroy all boxes') 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: else:
print('Destroying box {} ...'.format(name)) print("Shipyard error: no vm managed by me has the hostname '{}'".format(hostname))
exit(0)
def run_playbook(name, playbook): def run_playbook(name, playbook):
""" """
...@@ -104,7 +113,7 @@ if __name__ == '__main__': ...@@ -104,7 +113,7 @@ if __name__ == '__main__':
else: else:
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) destroy_box(args.hostname)
elif args.command == 'ssh': elif args.command == 'ssh':
invoke_shell() invoke_shell()
elif args.command == 'playbook': elif args.command == 'playbook':
......
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