-
Notifications
You must be signed in to change notification settings - Fork 1
/
aoi
executable file
·84 lines (67 loc) · 3.22 KB
/
aoi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import argparse
import os
from sys import argv, exit
# Local imports.
import config
def launch(*args, **kwargs):
cmd = "./" + kwargs.pop('command')
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.execvp(cmd, (cmd, kwargs.pop('hostname'), kwargs.pop('image')))
def init(*args, **kwargs):
cmd = "./" + kwargs.pop('command')
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.execvp(cmd, [cmd])
def delete(*args, **kwargs):
cmd = "./" + kwargs.pop('command')
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.execvp(cmd, [cmd, kwargs.pop('hostname')])
def get_ip(*args, **kwargs):
cmd = "./" + kwargs.pop('command')
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.execvp(cmd, [cmd, kwargs.pop('hostname')])
def get_mac(*args, **kwargs):
cmd = "./" + kwargs.pop('command')
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.execvp(cmd, [cmd, kwargs.pop('hostname')])
def ssh(*args, **kwargs):
cmd = "./" + kwargs.pop('command')
os.chdir(os.path.dirname(os.path.realpath(__file__)))
argv = [cmd] + list(args)
os.execvp(cmd, argv)
parser = argparse.ArgumentParser(prog='aoi')
# parser.add_argument('--foo', action='store_true', help='foo help')
# subparsers = parser.add_subparsers(help='sub-command help')
subparsers = parser.add_subparsers()
parser_launch = subparsers.add_parser('launch', help='Launch a cloud image.')
parser_launch.add_argument('image', type=str, help='Image to launch. (Example: xenial)')
parser_launch.add_argument('hostname', type=str, help='Instance hostname to create.')
parser_launch.set_defaults(func=launch, command="aoi-launch")
parser_init = subparsers.add_parser('init', help='Initialize aoi for the first time.')
parser_init.set_defaults(func=init, command="aoi-init")
parser_delete = subparsers.add_parser('delete', help='Delete a running instance.')
parser_delete.add_argument('hostname', type=str, help='Instance hostname to delete.')
parser_delete.set_defaults(func=delete, command="aoi-delete")
parser_get_ip = subparsers.add_parser('get-ip', help='Get the IP address of a running instance.')
parser_get_ip.add_argument('hostname', type=str, help='Instance hostname whose IP address to get.')
parser_get_ip.set_defaults(func=get_ip, command="aoi-get-ip-via-arp")
parser_get_mac = subparsers.add_parser('get-mac', help='Get the MAC address for an instance.')
parser_get_mac.add_argument('hostname', type=str, help='Instance hostname whose IP address to get.')
parser_get_mac.set_defaults(func=get_mac, command="aoi-get-mac")
parser_config = subparsers.add_parser('config', help="Configure aoi.")
# Call out to the config module to add the subparser parameters.
config.configure_subparser(parser_config)
parser_config.set_defaults(func=config.show_command)
parser_ssh = subparsers.add_parser('ssh', help='Open an SSH session to a running instance.')
parser_ssh.set_defaults(func=ssh, command="aoi-ssh")
# parse some argument lists
if __name__ == '__main__':
out, unknown = parser.parse_known_args(argv[1:])
kwargs = vars(out)
func = kwargs.get('func', None)
if func is not None:
del kwargs['func']
func(*unknown, **vars(out))
else:
parser.print_help()
exit(1)