def _resource_info(self, type, id): try: obj = self.connection.request('hosting.%s.info' % type, int(id)) return obj.object except Exception: e = sys.exc_info()[1] raise GandiException(1003, e)
def ex_snapshot_disk(self, disk, name=None): """ Specific method to make a snapshot of a disk :param disk: Disk which should be used :type disk: :class:`GandiDisk` :param name: Name which should be used :type name: ``str`` :rtype: ``bool`` """ if not disk.extra.get("can_snapshot"): raise GandiException(1021, "Disk %s can't snapshot" % disk.id) if not name: suffix = datetime.today().strftime("%Y%m%d") name = "snap_%s" % (suffix) op = self.connection.request( "hosting.disk.create_from", { "name": name, "type": "snapshot" }, int(disk.id), ) if self._wait_operation(op.object["id"]): return True return False
def ex_snapshot_disk(self, disk, name=None): """ Specific method to make a snapshot of a disk @param disk: Disk which should be used @type disk: L{GandiDisk} @param name: Name which should be used @type name: C{str} @rtype: C{bool} """ if not disk.extra.get('can_snapshot'): raise GandiException(1021, 'Disk %s can\'t snapshot' % disk.id) if not name: suffix = datetime.today().strftime('%Y%m%d') name = 'snap_%s' % (suffix) op = self.connection.request( 'hosting.disk.create_from', { 'name': name, 'type': 'snapshot', }, int(disk.id), ) if self._wait_operation(op.object['id']): return True return False
def _node_info(self, id): try: obj = self.connection.request('vm.info', int(id)) return obj except Exception: e = sys.exc_info()[1] raise GandiException(1003, e) return None
def list_images(self, location=None): try: if location: filtering = {'datacenter_id': int(location.id)} else: filtering = {} images = self.connection.request('image.list', filtering) return [self._to_image(i) for i in images] except Exception, e: raise GandiException(1011, e)
def list_images(self, location=None): try: if location: filtering = {'datacenter_id': int(location.id)} else: filtering = {} images = self.connection.request('hosting.image.list', filtering) return [self._to_image(i) for i in images.object] except Exception: e = sys.exc_info()[1] raise GandiException(1011, e)
def destroy_node(self, node): vm = self._node_info(node.id) if vm['state'] == 'running': # Send vm_stop and wait for accomplish op_stop = self.connection.request('hosting.vm.stop', int(node.id)) if not self._wait_operation(op_stop.object['id']): raise GandiException(1010, 'vm.stop failed') # Delete op = self.connection.request('hosting.vm.delete', int(node.id)) if self._wait_operation(op.object['id']): return True return False
def ex_snapshot_disk(self, disk, name=None): """Specific method to make a snapshot of a disk""" if not disk.extra.get('can_snapshot'): raise GandiException(1021, "Disk %s can't snapshot" % disk.id) if not name: suffix = datetime.today().strftime("%Y%m%d") name = "snap_%s" % (suffix) op = self.connection.request( 'disk.create_from', { 'name': name, 'type': 'snapshot', }, int(disk.id), ) if self._wait_operation(op['id']): return True return False
def list_images(self, location=None): """ Return a list of image objects. :keyword location: Which data center to filter a images in. :type location: :class:`NodeLocation` :return: List of GCENodeImage objects :rtype: ``list`` of :class:`GCENodeImage` """ try: if location: filtering = {"datacenter_id": int(location.id)} else: filtering = {} images = self.connection.request("hosting.image.list", filtering) return [self._to_image(i) for i in images.object] except Exception as e: raise GandiException(1011, e)
def destroy_node(self, node): """ Destroy a node. :param node: Node object to destroy :type node: :class:`Node` :return: True if successful :rtype: ``bool`` """ vm = self._node_info(node.id) if vm["state"] == "running": # Send vm_stop and wait for accomplish op_stop = self.connection.request("hosting.vm.stop", int(node.id)) if not self._wait_operation(op_stop.object["id"]): raise GandiException(1010, "vm.stop failed") # Delete op = self.connection.request("hosting.vm.delete", int(node.id)) if self._wait_operation(op.object["id"]): return True return False
def create_node(self, **kwargs): """ Create a new Gandi node @keyword name: String with a name for this new node (required) @type name: C{str} @keyword image: OS Image to boot on node. (required) @type image: L{NodeImage} @keyword location: Which data center to create a node in. If empty, undefined behavior will be selected. (optional) @type location: L{NodeLocation} @keyword size: The size of resources allocated to this node. (required) @type size: L{NodeSize} @keyword login: user name to create for login on machine (required) @type login: C{str} @keyword password: password for user that'll be created (required) @type password: C{str} @keyword inet_family: version of ip to use, default 4 (optional) @type inet_family: C{int} @rtype: L{Node} """ if kwargs.get('login') is None or kwargs.get('password') is None: raise GandiException( 1020, 'login and password must be defined for node creation') location = kwargs.get('location') if location and isinstance(location, NodeLocation): dc_id = int(location.id) else: raise GandiException( 1021, 'location must be a subclass of NodeLocation') size = kwargs.get('size') if not size and not isinstance(size, NodeSize): raise GandiException(1022, 'size must be a subclass of NodeSize') # If size name is in INSTANCE_TYPE we use new rating model instance = INSTANCE_TYPES.get(size.id) cores = instance['cpu'] if instance else int(size.id) src_disk_id = int(kwargs['image'].id) disk_spec = { 'datacenter_id': dc_id, 'name': 'disk_%s' % kwargs['name'] } vm_spec = { 'datacenter_id': dc_id, 'hostname': kwargs['name'], 'login': kwargs['login'], 'password': kwargs['password'], # TODO : use NodeAuthPassword 'memory': int(size.ram), 'cores': cores, 'bandwidth': int(size.bandwidth), 'ip_version': kwargs.get('inet_family', 4), } # Call create_from helper api. Return 3 operations : disk_create, # iface_create,vm_create (op_disk, op_iface, op_vm) = self.connection.request('hosting.vm.create_from', vm_spec, disk_spec, src_disk_id).object # We wait for vm_create to finish if self._wait_operation(op_vm['id']): # after successful operation, get ip information # thru first interface node = self._node_info(op_vm['vm_id']) ifaces = node.get('ifaces') if len(ifaces) > 0: ips = ifaces[0].get('ips') if len(ips) > 0: node['ip'] = ips[0]['ip'] return self._to_node(node) return None
def create_node( self, name, size, image, location=None, login=None, password=None, inet_family=4, keypairs=None, ): """ Create a new Gandi node :keyword name: String with a name for this new node (required) :type name: ``str`` :keyword image: OS Image to boot on node. (required) :type image: :class:`NodeImage` :keyword location: Which data center to create a node in. If empty, undefined behavior will be selected. (optional) :type location: :class:`NodeLocation` :keyword size: The size of resources allocated to this node. (required) :type size: :class:`NodeSize` :keyword login: user name to create for login on machine (required) :type login: ``str`` :keyword password: password for user that'll be created (required) :type password: ``str`` :keyword inet_family: version of ip to use, default 4 (optional) :type inet_family: ``int`` :keyword keypairs: IDs of keypairs or Keypairs object :type keypairs: list of ``int`` or :class:`.KeyPair` :rtype: :class:`Node` """ keypairs = keypairs or [] if not login and not keypairs: raise GandiException( 1020, "Login and password or ssh keypair " "must be defined for node creation", ) if location and isinstance(location, NodeLocation): dc_id = int(location.id) else: raise GandiException( 1021, "location must be a subclass of NodeLocation") if not size and not isinstance(size, NodeSize): raise GandiException(1022, "size must be a subclass of NodeSize") keypair_ids = [ k if isinstance(k, int) else k.extra["id"] for k in keypairs ] # If size name is in INSTANCE_TYPE we use new rating model instance = INSTANCE_TYPES.get(size.id) cores = instance["cpu"] if instance else int(size.id) src_disk_id = int(image.id) disk_spec = {"datacenter_id": dc_id, "name": "disk_%s" % name} vm_spec = { "datacenter_id": dc_id, "hostname": name, "memory": int(size.ram), "cores": cores, "bandwidth": int(size.bandwidth), "ip_version": inet_family, } if login and password: vm_spec.update({ "login": login, "password": password } # TODO : use NodeAuthPassword ) if keypair_ids: vm_spec["keys"] = keypair_ids # Call create_from helper api. Return 3 operations : disk_create, # iface_create,vm_create (op_disk, op_iface, op_vm) = self.connection.request("hosting.vm.create_from", vm_spec, disk_spec, src_disk_id).object # We wait for vm_create to finish if self._wait_operation(op_vm["id"]): # after successful operation, get ip information # thru first interface node = self._node_info(op_vm["vm_id"]) ifaces = node.get("ifaces") if len(ifaces) > 0: ips = ifaces[0].get("ips") if len(ips) > 0: node["ip"] = ips[0]["ip"] return self._to_node(node) return None
def destroy_node(self, node, cascade=False): """ Destroy a node. :param node: Node object to destroy :type node: :class:`Node` :param cascade: Deletes the node's disks and ifaces if set to true :type cascade: ``bool`` :return: True if successful :rtype: ``bool`` """ vm = self._node_info(node.id) if vm['state'] == 'running': # Send vm_stop and wait for accomplish op_stop = self.connection.request('hosting.vm.stop', int(node.id)) if not self._wait_operation(op_stop.object['id']): raise GandiException(1010, 'vm.stop failed') # Get interfaces and ips associated with the node ifaces = vm.get('ifaces') ifaces_objects = [] disks = vm.get('disks') disks_objects = [] for iface in ifaces: ips = iface.get('ips') if iface['type'] == 'public': # public ips are auto deleted when vm is deleted ?? ifaces_objects.append(self._to_iface(iface)) for address in ips: if address['ip'] not in node.public_ips: node.public_ips.append(address['ip']) elif iface['type'] == 'private': # get iface object if iface type is private ifaces_objects.append(self._to_iface(iface)) for address in ips: node.private_ips.append(address['ip']) for disk in disks: disks_objects.append(self._to_disk(disk)) # Delete the node op = self.connection.request('hosting.vm.delete', int(node.id)) if self._wait_operation(op.object['id']): if cascade: # Delete private interfaces if cascade requested # first interface is always deleted at vm suppression. for iface in ifaces_objects[1:]: op = self.connection.request('hosting.iface.delete', int(iface.id)) self._wait_operation(op.object['id']) # Delete disks if cascade requested # system disk is deleted with vm for disk in disks_objects[1:]: if not disk.extra['is_boot_disk']: op = self.connection.request('hosting.disk.delete', int(disk.id)) self._wait_operation(op.object['id']) return True return False
def _node_info(self, id): try: obj = self.connection.request('vm.info', int(id)) return obj except Exception, e: raise GandiException(1003, e)
def create_node(self, **kwargs): """ Create a new Gandi node :keyword name: String with a name for this new node (required) :type name: ``str`` :keyword image: OS Image to boot on node. (required) :type image: :class:`NodeImage` :keyword location: Which data center to create a node in. If empty, undefined behavior will be selected. (optional) :type location: :class:`NodeLocation` :keyword size: The size of resources allocated to this node. (required) :type size: :class:`NodeSize` :keyword login: user name to create for login on machine (required) :type login: ``str`` :keyword password: password for user that'll be created (required) :type password: ``str`` :keyword interfaces: list of interfaces to attach to the vm :type interfaces: ``dict`` :keyword inet_family: version of ip to use, default 4 (optional) :type inet_family: ``int`` :keyword keypairs: IDs of keypairs or Keypairs object :type keypairs: list of ``int`` or :class:`.KeyPair` :keyword farm: Name of the farm this node belongs to (optional) :type farm: ``str`` :rtype: :class:`Node` """ if not kwargs.get('login') and not kwargs.get('keypairs'): raise GandiException( 1020, "Login and password or ssh keypair " "must be defined for node creation") location = kwargs.get('location') if location and isinstance(location, NodeLocation): dc_id = int(location.id) else: raise GandiException( 1021, 'location must be a subclass of NodeLocation') size = kwargs.get('size') if not size and not isinstance(size, NodeSize): raise GandiException(1022, 'size must be a subclass of NodeSize') keypairs = kwargs.get('keypairs', []) keypair_ids = [ k if isinstance(k, int) else k.extra['id'] for k in keypairs ] # If size name is in INSTANCE_TYPE we use new rating model instance = INSTANCE_TYPES.get(size.id) cores = instance['cpu'] if instance else int(size.id) src_disk_id = int(kwargs['image'].id) disk_spec = { 'datacenter_id': dc_id, 'name': 'disk_%s' % kwargs['name'], 'size': int(size.disk) * 1024 } vm_spec = { 'datacenter_id': dc_id, 'hostname': kwargs['name'], 'memory': int(size.ram), 'cores': cores, 'bandwidth': int(size.bandwidth), 'ip_version': kwargs.get('inet_family', 4), } ifaces = kwargs.get('interfaces', {}) public_interfaces = ifaces.get('publics', []) private_interfaces = ifaces.get('privates', []) vlans = self.ex_list_vlans() only_private = False if ifaces != {}: if len(public_interfaces) > 0: public_ipv4 = public_interfaces[0].get('ipv4', None) if public_ipv4: def_iface_version = kwargs.get('inet_family', 4) else: def_iface_version = kwargs.get('inet_family', 6) public_interfaces.pop(0) vm_def_iface_options = {'ip_version': def_iface_version} vm_spec.update(vm_def_iface_options) elif len(private_interfaces) > 0: vlan_name = private_interfaces[0].get('vlan') ipv4 = private_interfaces[0].get('ipv4', None) if vlan_name: vlan = self._get_by_name(vlan_name, vlans) iface = self.ex_create_interface(location=location, vlan=vlan, ip_address=ipv4) vm_spec.update({'iface_id': int(iface.id)}) private_interfaces.pop(0) only_private = True else: raise GandiException( 1021, "'interfaces' not empty but no 'publics' or \ 'privates' defined") else: # default one ipv6 public interfaces def_iface_version = kwargs.get('inet_family', 4) vm_def_iface_options = {'ip_version': def_iface_version} vm_spec.update(vm_def_iface_options) if kwargs.get('farm'): vm_spec.update({ 'farm': kwargs['farm'], }) if kwargs.get('login') and kwargs.get('password'): vm_spec.update({ 'login': kwargs['login'], 'password': kwargs['password'], # TODO : use NodeAuthPassword }) if keypair_ids: vm_spec['keys'] = keypair_ids # Call create_from helper api. Return 3 operations : disk_create, # iface_create,vm_create if only_private: print vm_spec (op_disk, op_vm) = self.connection.request('hosting.vm.create_from', vm_spec, disk_spec, src_disk_id).object else: (op_disk, op_iface, op_vm) = self.connection.request('hosting.vm.create_from', vm_spec, disk_spec, src_disk_id).object # We wait for vm_create to finish if self._wait_operation(op_vm['id']): # after successful creation, get the new vm node = self._node_info(op_vm['vm_id']) # create and attach optional interfaces for iface in public_interfaces: ipv4_address = iface.get('ipv4', None) if ipv4_address: ip_version = 4 else: ip_version = 6 iface = self.ex_create_interface(location=location, ip_version=ip_version) if iface: self.ex_node_attach_interface(node=self._to_node(node), iface=iface) for iface in private_interfaces: ip_address = iface.get('ipv4', None) ip_version = 4 if ip_address and ip_address == 'auto': ip_address = None vlan = self._get_by_name(iface.get('vlan', None), vlans) iface = self.ex_create_interface(location=location, ip_version=ip_version, ip_address=ip_address, vlan=vlan) if iface: self.ex_node_attach_interface(node=self._to_node(node), iface=iface) ifaces = node.get('ifaces') if len(ifaces) > 0: ips = ifaces[0].get('ips') if len(ips) > 0: node['ip'] = ips[0]['ip'] return self._to_node(node) return None
def _resource_info(self, type, id): try: obj = self.connection.request("hosting.%s.info" % type, int(id)) return obj.object except Exception as e: raise GandiException(1003, e)
def create_node(self, **kwargs): """ Create a new Gandi node :keyword name: String with a name for this new node (required) :type name: ``str`` :keyword image: OS Image to boot on node. (required) :type image: :class:`NodeImage` :keyword location: Which data center to create a node in. If empty, undefined behavior will be selected. (optional) :type location: :class:`NodeLocation` :keyword size: The size of resources allocated to this node. (required) :type size: :class:`NodeSize` :keyword login: user name to create for login on machine (required) :type login: ``str`` :keyword password: password for user that'll be created (required) :type password: ``str`` :keyword inet_family: version of ip to use, default 4 (optional) :type inet_family: ``int`` :keyword keypairs: IDs of keypairs or Keypairs object :type keypairs: list of ``int`` or :class:`.KeyPair` :rtype: :class:`Node` """ if not kwargs.get('login') and not kwargs.get('keypairs'): raise GandiException(1020, "Login and password or ssh keypair " "must be defined for node creation") location = kwargs.get('location') if location and isinstance(location, NodeLocation): dc_id = int(location.id) else: raise GandiException( 1021, 'location must be a subclass of NodeLocation') size = kwargs.get('size') if not size and not isinstance(size, NodeSize): raise GandiException( 1022, 'size must be a subclass of NodeSize') keypairs = kwargs.get('keypairs', []) keypair_ids = [ k if isinstance(k, int) else k.extra['id'] for k in keypairs ] # If size name is in INSTANCE_TYPE we use new rating model instance = INSTANCE_TYPES.get(size.id) cores = instance['cpu'] if instance else int(size.id) src_disk_id = int(kwargs['image'].id) disk_spec = { 'datacenter_id': dc_id, 'name': 'disk_%s' % kwargs['name'] } vm_spec = { 'datacenter_id': dc_id, 'hostname': kwargs['name'], 'memory': int(size.ram), 'cores': cores, 'bandwidth': int(size.bandwidth), 'ip_version': kwargs.get('inet_family', 4), } if kwargs.get('login') and kwargs.get('password'): vm_spec.update({ 'login': kwargs['login'], 'password': kwargs['password'], # TODO : use NodeAuthPassword }) if keypair_ids: vm_spec['keys'] = keypair_ids # Call create_from helper api. Return 3 operations : disk_create, # iface_create,vm_create (op_disk, op_iface, op_vm) = self.connection.request( 'hosting.vm.create_from', vm_spec, disk_spec, src_disk_id ).object # We wait for vm_create to finish if self._wait_operation(op_vm['id']): # after successful operation, get ip information # thru first interface node = self._node_info(op_vm['vm_id']) ifaces = node.get('ifaces') if len(ifaces) > 0: ips = ifaces[0].get('ips') if len(ips) > 0: node['ip'] = ips[0]['ip'] return self._to_node(node) return None