def reboot(self, api): """Reboot this VM.""" info("Rebooting VM {}".format(self['id'])) with catch_fault(): res = api.hosting.vm.reboot(APIKEY, self['id']) ope = Operation(**res) return ope
def stop(self, api): """Stop this VM.""" info("Stopping VM {}".format(self['id'])) with catch_fault(): res = api.hosting.vm.stop(APIKEY, self['id']) ope = Operation(**res) return ope
def filter(cls, api, datacenter_id): """Select an image by filtering on his name.""" images = {k: v for k, v in cls.list(api).items() if v['datacenter_id'] == datacenter_id} for k, v in images.items(): print(k, v) while len(images) > 1: _ = "an id or a keyword, as we still have {} possible disk images." keyword = ask_string(_.format(len(images)), '') try: keyword = int(keyword) if keyword not in images: warning("{} is not a valid id".format(keyword)) else: image = images[keyword] break except ValueError: filtered = {k: v for k, v in images.items() if keyword in v['label']} if not len(filtered): warning("No label contains \"{}\"".format(keyword)) continue elif len(filtered) is 1: image = filtered.popitem()[1] break else: images = filtered for _, v in images.items(): print(v) continue info('Image selected :') print(image) return image['disk_id']
def disk_detach(self, api, disk_id): """Detach a disk from this VM.""" with catch_fault(): disk = Disk(api.hosting.disk.info(APIKEY, int(disk_id))) info('Disk({}) found'.format(disk_id)) res = api.hosting.vm.disk_detach(APIKEY, self['id'], disk['id']) return Operation(**res)
def delete(self, api): """Delete this VM.""" info("Deleting VM {}".format(self['id'])) with catch_fault(): res = api.hosting.vm.delete(APIKEY, self['id']) ope = Operation(**res) return ope
def __init__(self): with catch_fault(): super().__init__() self.account = Account(self.api) welcome(self.account) # Map this type of objects to a self variable self.disks, self.images, self.ips = None, None, None self.ifaces, self.operations, self.vms = None, None, None self.stored_objects = { Disk: self.disks, Image: self.images, Ip: self.ips, Iface: self.ifaces, Operation: self.operations, VM: self.vms, } for i in [Disk, Image, Ip, Iface, Operation, VM]: self.stored_objects[i] = i.list(self.api) info("{} loaded.".format(i.__name__))
def command_handler(self, line, ttype): """Parse the line and run the selected method on the given type.""" tokens = split(line) # No arguments : print out available actions if len(tokens) is 0: info("Possible actions are : {}".format(' '.join(ttype.all_token))) return # Class action : execute it if tokens[0] in ttype.class_token: res = getattr(ttype, tokens[0])(self.api) print_iter(res) # Instance action : we need an id elif tokens[0] in ttype.instance_token: try: obj_id = int(tokens[1]) except ValueError: warning("Bad input.") return except IndexError: warning("'{}' is not a complete command".format(tokens)) return try: obj = self.stored_objects[ttype][obj_id] except KeyError as exc: warning("Unknow id: {}".format(exc)) return try: ope = getattr(obj, tokens[0])(self.api, *tokens[2:]) print(ope) except TypeError as exc: warning("Bad arguments : {}".format(exc)) # Refresh internal data, except for read-only commands. if ttype in self.stored_objects \ and tokens[0] not in ['count', 'info', 'list']: debug('refreshing {}'.format(ttype.__name__)) self.stored_objects[ttype] = ttype.list(self.api) else: warning("Unknow command : {}.".format(tokens[0]))
def connect(self, api): """Automatically start an ssh connection.""" info("Starting SSH session...") with catch_fault(): infos = api.hosting.vm.info(APIKEY, self['id']) ifaces = infos['ifaces'] ips = [] for iface in ifaces: for _ in iface['ips']: ips.append(_['ip']) if len(ips) is 0: error("No address IP for this VM (?!?)") return elif len(ips) > 1: info("We have {} possible IP :".format(len(ips))) for _, addr in enumerate(ips): bold("#{} : {}".format(_, addr)) ip_addr = ips[ask_int('an ip id', choices=range(len(ips)))] else: ip_addr = ips.pop() login = ask_string('login') info("Running 'ssh {}@{}'".format(login, ip_addr)) call(['ssh', '{}@{}'.format(login, ip_addr)])
def count(cls, api): """Get the number of existing VM.""" info("Counting VM") with catch_fault(): res = api.hosting.vm.count(APIKEY) return "VM count: {}".format(res)
def info(self, api): """Get info about this disk.""" info("Info about Disk {}".format(self['id'])) with catch_fault(): res = api.hosting.disk.info(APIKEY, self['id']) return Disk(**res)
def count(cls, api): """Count the number of existing disks.""" info("Counting Disk") with catch_fault(): res = api.hosting.disk.count(APIKEY) return "Disk count: {}".format(res)
def info(self, api): """Get info about this Interface.""" info("Info about Interface {}".format(self['id'])) with catch_fault(): res = api.hosting.iface.info(APIKEY, self['id']) return Iface(**res)
def info(self, api): """Get info about this disk image.""" info("Info about Image {}".format(self['id'])) with catch_fault(): res = api.hosting.image.info(APIKEY, self['id']) return Image(**res)
def count(cls, api): """Get the number of existing IPs.""" info("Counting IPs") with catch_fault(): res = api.hosting.ip.count(APIKEY) return "IP count: {}".format(res)
def info(self, api): """Get info about this VM.""" info("Info about VM {}".format(self['id'])) with catch_fault(): res = api.hosting.vm.info(APIKEY, self['id']) return VirtualMachine(**res)
def info(self, api): """Get info about this IP.""" info("Info about IP {}".format(self['id'])) with catch_fault(): res = api.hosting.ip.info(APIKEY, self['id']) return Ip(**res)
def info(self, api): """Get info about this Operation.""" info("Info about Operation {}".format(self['id'])) with catch_fault(): res = api.operation.info(APIKEY, self['id']) return Operation(**res)
def count(cls, api): """Get the number of existing Operation.""" info("Counting Operation") with catch_fault(): res = api.operation.count(APIKEY) return "Operation count: {}".format(res)