Commit 56f5cd34 by Czémán Arnold

Initial commit: add arguments parsing and minimal shell

parents
*.sw?
*.pyc
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2017 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
import argparse
from shell import OCCIShell
from parsers import VMParser, TemplateParser, NetworkParser
top_parser = argparse.ArgumentParser()
group = top_parser.add_mutually_exclusive_group()
group.add_argument("--json", action="store_true",
help="Show output in JSON format.")
group.add_argument("--pretty-json", action="store_true",
help="Show output in prettified JSON format.")
top_parser.add_argument("command", nargs="?", default="shell")
top_parser.add_argument("command_args", type=unicode, nargs=argparse.REMAINDER)
args = top_parser.parse_args()
parsers = {
"vm": VMParser(),
"template": TemplateParser(),
"network": NetworkParser(),
}
if args.command == "shell":
try:
OCCIShell().cmdloop()
except KeyboardInterrupt: # Ctrl + C
print("")
pass
else:
parser = parsers.get(args.command)
if parser:
parser.parse_args(args.command_args)
else:
top_parser.print_usage()
# -*- coding: utf-8 -*-
# Copyright 2017 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
import argparse
class ResourceParser(argparse.ArgumentParser):
description = None
name = None
def __init__(self, *args, **kwargs):
super(ResourceParser, self).__init__(
add_help=False, description=self.description, prog=self.name,
*args, **kwargs)
self.subparsers = self.add_subparsers(
parser_class=argparse.ArgumentParser)
list_parser = self.subparsers.add_parser("list")
list_parser.add_argument("--filter", type=unicode)
class VMParser(ResourceParser):
description = "VM operations"
name = "vm"
def __init__(self, *args, **kwargs):
super(VMParser, self).__init__(*args, **kwargs)
commands = ("start", "stop", "forced_start", "forced_stop",
"reboot", "forced_reboot", "destroy", "renew")
for cmd in commands:
sub_parser = self.subparsers.add_parser(cmd)
self.add_id_arguments(sub_parser)
create_parser = self.subparsers.add_parser("create")
create_group = create_parser.add_mutually_exclusive_group(required=True)
create_group.add_argument("-t", "--template-id", type=int)
create_group.add_argument("--template-name", type=int)
self.add_vm_arguments(create_parser)
modify_parser = self.subparsers.add_parser("modify")
self.add_id_arguments(modify_parser)
self.add_vm_arguments(modify_parser)
self.add_disk_parser()
self.add_nic_parser()
def add_vm_arguments(self, parser):
parser.add_argument("-n", "--vm-name", type=unicode)
parser.add_argument("-c", "--cpu", type=int)
parser.add_argument("-m", "--memory", type=int)
parser.add_argument("-p", "--priotiry", type=unicode,
choices=("idle", "normal", "server", "realtime"))
def add_id_arguments(self, parser, name=None):
prefix = name + "-" if name else ""
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--%sname" % prefix, type=unicode)
group.add_argument("--%sid" % prefix, type=int)
def add_subresource_parser(self, name):
parser = self.subparsers.add_parser(name)
parsers = parser.add_subparsers()
list_parser = parsers.add_parser("list")
list_parser.add_argument("--filter", type=unicode)
destroy_parser = parsers.add_parser("destroy")
self.add_id_arguments(destroy_parser, name)
show_parser = parsers.add_parser("show")
self.add_id_arguments(show_parser, name)
return parsers
def add_disk_parser(self):
parsers = self.add_subresource_parser("disk")
create_parser = parsers.add_parser("create")
self.add_id_arguments(create_parser, "vm")
create_parser.add_argument("--disk-name", type=unicode)
create_parser.add_argument("--size", type=int)
def add_nic_parser(self):
parsers = self.add_subresource_parser("nic")
create_parser = parsers.add_parser("create")
self.add_id_arguments(create_parser, "vm")
self.add_id_arguments(create_parser, "network")
class TemplateParser(ResourceParser):
description = "Template operations"
name = "template"
class NetworkParser(ResourceParser):
description = "Network operations"
name = "network"
# -*- coding: utf-8 -*-
# Copyright 2017 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
import cmd
from parsers import VMParser, NetworkParser, TemplateParser
def parser(parser_class):
def top_wrapper(func):
def wrapper(self, arg):
try:
args = parser_class().parse_args(arg.split())
return func(self, args)
except SystemExit:
pass
return wrapper
return top_wrapper
class OCCIShell(cmd.Cmd):
intro = ('Wellcome to the CIRCLE cloud shell.'
' Type help or ? to list commands.\n')
prompt = '(CIRCLE) '
def do_print(self, arg):
print(arg)
@parser(VMParser)
def do_vm(self, arg):
print("vm %s" % unicode(arg))
@parser(TemplateParser)
def do_template(self, arg):
print("template %s" % unicode(arg))
@parser(NetworkParser)
def do_network(self, arg):
print("network %s" % unicode(arg))
def do_exit(self, arg):
exit()
def do_quit(self, arg):
exit()
def precmd(self, line):
if line == "EOF": # Ctrl + D
print("")
exit()
return line
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 sign in to comment