++hack / list products

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-02-09 11:14:50 +01:00
parent 3b508fc87d
commit 5da6dbb32e
3 changed files with 35 additions and 5 deletions

View File

@ -79,12 +79,19 @@ class DB(object):
return value
@readable_errors
def get_prefix(self, key, as_json=False, **kwargs):
key_range = self._db_clients[0].get_prefix(self.realkey(key), **kwargs)
return key_range
@readable_errors
def set(self, key, value, as_json=False, **kwargs):
if as_json:
value = json.dumps(value)
log.debug("Setting {} = {}".format(self.realkey(key), value))
# FIXME: iterate over clients in case of failure ?
return self._db_clients[0].put(self.realkey(key), value, **kwargs)

View File

@ -9,6 +9,7 @@ from uncloud.hack.mac import MAC
from uncloud.hack.net import VXLANBridge, DNSRA
from uncloud import UncloudException
from uncloud.hack.product import ProductOrder
arg_parser = argparse.ArgumentParser('hack', add_help=False)
#description="Commands that are unfinished - use at own risk")
@ -41,6 +42,7 @@ arg_parser.add_argument('--hackprefix', help="hackprefix, if you need it you kno
# order based commands => later to be shifted below "order"
arg_parser.add_argument('--order', action='store_true')
arg_parser.add_argument('--list-orders', help="List all orders", action='store_true')
arg_parser.add_argument('--product', choices=["dualstack-vm"])
arg_parser.add_argument('--os-image-name', help="Name of OS image (successor to --image)")
arg_parser.add_argument('--os-image-size', help="Size of OS image in GB", type=int, default=10)
@ -48,6 +50,9 @@ arg_parser.add_argument('--os-image-size', help="Size of OS image in GB", type=i
arg_parser.add_argument('--username')
arg_parser.add_argument('--password')
arg_parser.add_argument('--api', help="Run the API")
log = logging.getLogger(__name__)
@ -79,6 +84,7 @@ def order(config):
# create DB entry for VM
vm = VM(config)
vm.product.db_entry["owner"] = config.arguments['username']
vm.product.place_order()
@ -86,9 +92,17 @@ def order(config):
def main(arguments):
config = Config(arguments)
if arguments['api']:
api = API()
api.run()
if arguments['order']:
order(config)
if arguments['list_orders']:
p = ProductOrder(config)
p.list_orders()
if arguments['create_vm']:
vm = VM(config)
vm.create()

View File

@ -24,7 +24,14 @@ import uuid
from uncloud import UncloudException
from uncloud.hack.db import DB
# states
class ProductOrder(object):
def __init__(self, config):
self.config = config
self.db = DB(self.config, prefix="/orders")
def list_orders(self, filter_key=None, filter_regexp_value=None):
for k,m in self.db.get_prefix(""):
print("{} {}".format(k,m))
class Product(object):
@ -54,7 +61,7 @@ class Product(object):
def valid_status(self):
if "status" in self.db_entry:
if self.db_entry["status"] in [ "NEW", "CREATED", "DELETED" ]:
if self.db_entry["status"] in [ "NEW", "SCHEDULED", "CREATED", "DELETED" ]:
return False
return True
@ -63,6 +70,9 @@ class Product(object):
self.db_entry["uuid"] = str(uuid.uuid4())
if not "status" in self.db_entry:
self.db_entry["status"] = "NEW"
if not "owner" in self.db_entry:
self.db_entry["owner"] = "UNKNOWN"
def place_order(self):
""" Schedule creating the product in etcd """
@ -72,9 +82,8 @@ class Product(object):
if not self.db_entry["status"] == "NEW":
raise UncloudException("Cannot re-order product")
self.db.set(self.db_entry["uuid"], str(self))
def __str__(self):
return self.features
return json.dumps(self.db_entry)