Ejemplo n.º 1
0
    def _Create(self):
        """Create a ProfitBricks VM instance."""

        # Grab ssh pub key to inject into new VM
        with open(self.ssh_public_key) as f:
            public_key = f.read().rstrip('\n')

        # Find an Ubuntu image that matches our location
        self.image = util.ReturnImage(self.header, self.location)

        # Create server POST body
        new_server = {
            'properties': {
                'name': self.name,
                'ram': self.ram,
                'cores': self.cores,
                'availabilityZone': self.zone
            },
            'entities': {
                'volumes': {
                    'items': [{
                        'properties': {
                            'size': self.boot_volume_size,
                            'name': 'boot volume',
                            'image': self.image,
                            'type': self.boot_volume_type,
                            'sshKeys': [public_key],
                            'availabilityZone': self.availability_zone
                        }
                    }]
                },
                'nics': {
                    'items': [{
                        'properties': {
                            'name': 'nic1',
                            'lan': self.lan_id
                        }
                    }]
                }
            }
        }

        # Build Server URL
        url = '%s/datacenters/%s/servers' % (PROFITBRICKS_API, self.dc_id)

        # Provision Server
        r = util.PerformRequest('post', url, self.header, json=new_server)
        logging.info('Creating VM: %s' % self.name)

        # Parse Required values from response
        self.server_status = r.headers['Location']
        response = r.json()
        self.server_id = response['id']

        # The freshly created server will be in a locked and unusable
        # state for a while, and it cannot be deleted or modified in
        # this state. Wait for the action to finish and check the
        # reported result.
        if not self._WaitUntilReady(self.server_status):
            raise errors.Error('VM creation failed, see log.')
    def _PostCreate(self):
        """Get the instance's public IP address."""

        # Build URL
        url = '%s/datacenters/%s/servers/%s?depth=5' % (
            PROFITBRICKS_API, self.dc_id, self.server_id)

        # Perform Request
        r = util.PerformRequest('get', url, self.header)
        response = r.json()
        nic = response['entities']['nics']['items'][0]
        self.ip_address = nic['properties']['ips'][0]
    def _WaitUntilReady(self, status_url):
        """Returns true if the ProfitBricks resource is ready."""

        # Poll resource for status update
        logging.info('Polling ProfitBricks resource.')
        r = util.PerformRequest('get', status_url, self.header)
        response = r.json()
        status = response['metadata']['status']

        # Keep polling resource until a "DONE" state is returned
        if status != 'DONE':
            raise Exception  # Exception triggers vm_util.Retry to go again

        return True
    def _DeleteDependencies(self):
        """Delete a data center and LAN."""

        # Build URL
        url = '%s/datacenters/%s' % (PROFITBRICKS_API, self.dc_id)

        # Make call to delete data center
        logging.info('Deleting Datacenter: %s' % self.dc_id)
        r = util.PerformRequest('delete', url, self.header)

        # Check to make sure deletion has finished
        delete_status = r.headers['Location']
        if not self._WaitUntilReady(delete_status):
            raise errors.Error('Data center deletion failed, see log.')
    def _Delete(self):
        """Delete a ProfitBricks VM."""

        # Build URL
        url = '%s/datacenters/%s/servers/%s' % (PROFITBRICKS_API, self.dc_id,
                                                self.server_id)

        # Make call
        logging.info('Deleting VM: %s' % self.server_id)
        r = util.PerformRequest('delete', url, self.header)

        # Check to make sure deletion has finished
        delete_status = r.headers['Location']
        if not self._WaitUntilReady(delete_status):
            raise errors.Error('VM deletion failed, see log.')