def create_node(self, **kwargs): name = kwargs['name'] # expects fqdn ex: test.com size = kwargs['size'] image = kwargs['image'] auth = kwargs['auth'] dc = None if "location" in kwargs: dc = kwargs["location"].id else: dc = '3' params = {'fqdn': name, 'plan': size.name, 'image': image.id, 'location': dc } ssh_key = None password = None if isinstance(auth, NodeAuthSSHKey): ssh_key = auth.pubkey params['ssh_key'] = ssh_key elif isinstance(auth, NodeAuthPassword): password = auth.password params['password'] = password if not ssh_key and not password: raise HostVirtualException(500, "Need SSH key or Root password") result = self.connection.request(API_ROOT + '/cloud/buy_build', data=json.dumps(params), method='POST').object return self._to_node(result)
def ex_provision_node(self, **kwargs): """ Provision a server on a VR package and get it booted :keyword node: node which should be used :type node: :class:`Node` :keyword image: The distribution to deploy on your server (mandatory) :type image: :class:`NodeImage` :keyword auth: an SSH key or root password (mandatory) :type auth: :class:`NodeAuthSSHKey` or :class:`NodeAuthPassword` :keyword location: which datacenter to create the server in :type location: :class:`NodeLocation` :return: Node representing the newly built server :rtype: :class:`Node` """ node = kwargs['node'] if 'image' in kwargs: image = kwargs['image'] else: image = node.extra['image'] params = { 'mbpkgid': node.id, 'image': image, 'fqdn': node.name, 'location': node.extra['location'], } auth = kwargs['auth'] ssh_key = None password = None if isinstance(auth, NodeAuthSSHKey): ssh_key = auth.pubkey params['ssh_key'] = ssh_key elif isinstance(auth, NodeAuthPassword): password = auth.password params['password'] = password if not ssh_key and not password: raise HostVirtualException(500, "SSH key or Root password is required") try: result = self.connection.request(API_ROOT + '/cloud/server/build', data=json.dumps(params), method='POST').object return bool(result) except HostVirtualException: self.ex_cancel_package(node)
def create_node(self, name, image, size, **kwargs): """ Creates a node Example of node creation with ssh key deployed: >>> from libcloud.compute.base import NodeAuthSSHKey >>> key = open('/home/user/.ssh/id_rsa.pub').read() >>> auth = NodeAuthSSHKey(pubkey=key) >>> from libcloud.compute.providers import get_driver; >>> driver = get_driver('hostvirtual') >>> conn = driver('API_KEY') >>> image = conn.list_images()[1] >>> size = conn.list_sizes()[0] >>> location = conn.list_locations()[1] >>> name = 'markos-dev' >>> node = conn.create_node(name, image, size, auth=auth, >>> location=location) """ dc = None auth = self._get_and_check_auth(kwargs.get('auth')) if not self._is_valid_fqdn(name): raise HostVirtualException( 500, "Name should be a valid FQDN (e.g, hostname.example.com)") # simply order a package first pkg = self.ex_order_package(size) if 'location' in kwargs: dc = kwargs['location'].id else: dc = DEFAULT_NODE_LOCATION_ID # create a stub node stub_node = self._to_node({ 'mbpkgid': pkg['id'], 'status': 'PENDING', 'fqdn': name, 'plan_id': size.id, 'os_id': image.id, 'location_id': dc }) # provisioning a server using the stub node self.ex_provision_node(node=stub_node, auth=auth) node = self._wait_for_node(stub_node.id) if getattr(auth, 'generated', False): node.extra['password'] = auth.password return node
def create_node(self, name, image, size, location=None, auth=None): """ Creates a node Example of node creation with ssh key deployed: >>> from libcloud.compute.base import NodeAuthSSHKey >>> key = open('/home/user/.ssh/id_rsa.pub').read() >>> auth = NodeAuthSSHKey(pubkey=key) >>> from libcloud.compute.providers import get_driver >>> driver = get_driver('hostvirtual') >>> conn = driver('API_KEY') >>> image = conn.list_images()[1] >>> size = conn.list_sizes()[0] >>> location = conn.list_locations()[1] >>> name = 'markos-dev' >>> node = conn.create_node(name, image, size, auth=auth, >>> location=location) """ dc = None auth = self._get_and_check_auth(auth) if not self._is_valid_fqdn(name): raise HostVirtualException( 500, "Name should be a valid FQDN (e.g, hostname.example.com)") # simply order a package first pkg = self.ex_order_package(size) if location: dc = location.id else: dc = DEFAULT_NODE_LOCATION_ID # create a stub node stub_node = self._to_node({ "mbpkgid": pkg["id"], "status": "PENDING", "fqdn": name, "plan_id": size.id, "os_id": image.id, "location_id": dc, }) # provisioning a server using the stub node self.ex_provision_node(node=stub_node, auth=auth) node = self._wait_for_node(stub_node.id) if getattr(auth, "generated", False): node.extra["password"] = auth.password return node
def ex_build_node(self, **kwargs): """ Build a server on a VR package and get it booted @keyword node: node which should be used @type node: L{Node} @keyword image: The distribution to deploy on your server (mandatory) @type image: L{NodeImage} @keyword auth: an SSH key or root password (mandatory) @type auth: L{NodeAuthSSHKey} or L{NodeAuthPassword} @keyword location: which datacenter to create the server in @type location: L{NodeLocation} @rtype: C{bool} """ node = kwargs['node'] if 'image' in kwargs: image = kwargs['image'] else: image = node.extra['image'] params = { 'mbpkgid': node.id, 'image': image, 'fqdn': node.name, 'location': node.extra['location'], } auth = kwargs['auth'] ssh_key = None password = None if isinstance(auth, NodeAuthSSHKey): ssh_key = auth.pubkey params['ssh_key'] = ssh_key elif isinstance(auth, NodeAuthPassword): password = auth.password params['password'] = password if not ssh_key and not password: raise HostVirtualException(500, "Need SSH key or Root password") result = self.connection.request(API_ROOT + '/cloud/server/build', data=json.dumps(params), method='POST').object return bool(result)
def _wait_for_node(self, node_id, timeout=30, interval=5.0): """ @param node_id: ID of the node to wait for. @type node_id: C{int} @param timeout: Timeout (in seconds). @type timeout: C{int} @param interval: How long to wait (in seconds) between each attempt. @type interval: C{float} """ # poll until we get a node for i in range(0, timeout, int(interval)): try: node = self.ex_get_node(node_id) return node except HostVirtualException: time.sleep(interval) raise HostVirtualException(412, 'Timedout on getting node details')
def _wait_for_node(self, node_id, timeout=30, interval=5.0): """ :param node_id: ID of the node to wait for. :type node_id: ``int`` :param timeout: Timeout (in seconds). :type timeout: ``int`` :param interval: How long to wait (in seconds) between each attempt. :type interval: ``float`` :return: Node representing the newly built server :rtype: :class:`Node` """ # poll until we get a node for i in range(0, timeout, int(interval)): try: node = self.ex_get_node(node_id) return node except HostVirtualException: time.sleep(interval) raise HostVirtualException(412, 'Timeout on getting node details')