Skip to content

Commit

Permalink
Add a thebacknd destroy-self subcommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
noteed committed May 10, 2024
1 parent 931e005 commit 3eadef9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 18 additions & 0 deletions lib/thebacknd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ def destroy_all_droplets():
}


def destroy_self(vm_id, vm_killcode):
has_killcode = verify_killcode(vm_id, vm_killcode)
if has_killcode:
do_client.droplets.destroy_by_tag(tag_name=vm_id)
return {"destroyed": vm_id}
else:
return {}


def cli():
"""
Drive the above functions from the command-line (i.e. locally instead of
Expand Down Expand Up @@ -195,6 +204,10 @@ def run_destroy_all():
r = destroy_all_droplets()
pprint.pp(r)

def run_destroy_self(vm_id, vm_killcode):
r = destroy_self(vm_id, vm_killcode)
pprint.pp(r)

parser = argparse.ArgumentParser(description="Ephemeral virtual machines in one command.")
subparsers = parser.add_subparsers(dest='command', help='Available commands')

Expand All @@ -209,6 +222,11 @@ def run_destroy_all():
parser_destroy_all = subparsers.add_parser('destroy-all', help='Destroy all virtual machines')
parser_destroy_all.set_defaults(func=lambda args: run_destroy_all())

parser_destroy_self = subparsers.add_parser('destroy-self', help='Destroy a virtual machine given a killcode')
parser_destroy_self.add_argument("--vm-id", type=str, help="Specify a virtual machine ID.")
parser_destroy_self.add_argument("--killcode", type=str, help="Specify a killcode.")
parser_destroy_self.set_defaults(func=lambda args: run_destroy_self(args.vm_id, args.killcode))

args = parser.parse_args()
if hasattr(args, 'func'):
args.func(args)
Expand Down
8 changes: 2 additions & 6 deletions packages/thebacknd/destroy-self/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ def main(event):
# TODO test for .vm_id and .killcode attrs.
vm_id = event["vm_id"]
vm_killcode = event["vm_killcode"]
has_killcode = thebacknd.verify_killcode(vm_id, vm_killcode)
if has_killcode:
thebacknd.do_client.droplets.destroy_by_tag(tag_name=vm_id)
return {"destroyed": vm_id}
else:
return {}
r = thebacknd.destroy_self(vm_id, vm_killcode)
return r

0 comments on commit 3eadef9

Please sign in to comment.