Example #1
0
    def add(self, keypair):
        """
        Add a keypair
        """
        LOG.info('add(keypair=%s)', keypair)

        # Generate a new keypair if the request doesn't contain a public key
        if 'public_key' in keypair:
            public_key = keypair['public_key']
            private_key = None
        else:
            key = RSA.gen_key(1024, 65537, callback=lambda: None)
            public_key = 'ssh-rsa %s' % b64encode(key.pub()[1])
            private_key = key.as_pem(cipher=None)

        # Calculate the key fingerprint
        fp_plain = md5(b64decode(public_key.split()[1])).hexdigest()
        fp = ':'.join(a + b for (a, b) in zip(fp_plain[::2], fp_plain[1::2]))

        # Create the new keypair in the database
        new_keypair = DB.keypairs.create(name=keypair['name'], fingerprint=fp,
                                         public_key=public_key)

        if private_key is None:
            return utils.sanitize(new_keypair, KEYPAIRS_INFO)

        new_keypair['private_key'] = private_key
        return utils.sanitize(new_keypair, KEYPAIRS_INFO + ('private_key', ))
Example #2
0
File: images.py Project: tpot/dwarf
    def list(self, detail=True):
        """
        List all images
        """
        LOG.info('list(detail=%s)', detail)

        images = DB.images.list()
        if detail:
            return utils.sanitize(images, IMAGES_DETAIL)
        else:
            return utils.sanitize(images, IMAGES_INFO)
Example #3
0
    def list(self, detail=True):
        """
        List all images
        """
        LOG.info("list(detail=%s)", detail)

        images = DB.images.list()
        if detail:
            return utils.sanitize(images, IMAGES_DETAIL)
        else:
            return utils.sanitize(images, IMAGES_INFO)
Example #4
0
    def list(self, detail=True):
        """
        List all flavors
        """
        LOG.info('list(detail=%s)', detail)

        flavors = DB.flavors.list()
        if detail:
            return utils.sanitize(flavors, FLAVORS_DETAIL)
        else:
            return utils.sanitize(flavors, FLAVORS_INFO)
Example #5
0
    def list(self, detail=True):
        """
        List all servers
        """
        LOG.info('list(detail=%s)', detail)

        servers = []
        for s in DB.servers.list():
            servers.append(self._extend(s))
        if detail:
            return utils.sanitize(servers, SERVERS_DETAIL)
        else:
            return utils.sanitize(servers, SERVERS_INFO)
Example #6
0
    def create(self, image_fh, image_md):
        """
        Create a new image
        """
        LOG.info('create(image_md=%s)', image_md)

        # Create a new image in the database
        image_md['status'] = 'SAVING'
        image = DB.images.create(**image_md)
        image_id = image['id']

        # Copy the image and calculate its MD5 sum
        image_file = os.path.join(CONF.images_dir, image_id)
        with open(image_file, 'wb') as fh:
            d = md5()
            while True:
                buf = image_fh.read(4096)
                if not buf:
                    break
                d.update(buf)
                fh.write(buf)
        md5sum = d.hexdigest()

        # Update the image database entry
        image = DB.images.update(id=image_id,
                                 checksum=md5sum,
                                 location='file://%s' % image_file,
                                 status='ACTIVE')

        return utils.sanitize(image, IMAGES_INFO)
Example #7
0
 def show(self, image_id):
     """
     Show image details
     """
     LOG.info('show(image_id=%s)', image_id)
     image = DB.images.show(id=image_id)
     return utils.sanitize(image, IMAGES_INFO)
Example #8
0
    def create(self, image_fh, image_md):
        """
        Create a new image
        """
        LOG.info('create(image_md=%s)', image_md)

        # Create a new image in the database
        image_md['status'] = 'SAVING'
        image = DB.images.create(**image_md)
        image_id = image['id']

        # Copy the image and calculate its MD5 sum
        image_file = os.path.join(CONF.images_dir, image_id)
        with open(image_file, 'wb') as fh:
            d = md5()
            while True:
                buf = image_fh.read(4096)
                if not buf:
                    break
                d.update(buf)
                fh.write(buf)
        md5sum = d.hexdigest()

        # Update the image database entry
        image = DB.images.update(id=image_id, checksum=md5sum,
                                 location='file://%s' % image_file,
                                 status='ACTIVE')

        return utils.sanitize(image, IMAGES_INFO)
Example #9
0
 def show(self, image_id):
     """
     Show image details
     """
     LOG.info('show(image_id=%s)', image_id)
     image = DB.images.show(id=image_id)
     return utils.sanitize(image, IMAGES_INFO)
Example #10
0
    def list(self):
        """
        List all images
        """
        LOG.info('list()')

        images = DB.images.list()
        return utils.sanitize(images, IMAGES_INFO)
Example #11
0
    def show(self, flavor_id):
        """
        Show flavor details
        """
        LOG.info('show(flavor_id=%s)', flavor_id)

        flavor = DB.flavors.show(id=flavor_id)
        return utils.sanitize(flavor, FLAVORS_DETAIL)
Example #12
0
    def list(self):
        """
        List all images
        """
        LOG.info('list()')

        images = DB.images.list()
        return utils.sanitize(images, IMAGES_INFO)
Example #13
0
    def update(self, image_id, image_md):
        """
        Update image metadata
        """
        LOG.info('update(image_id=%s, image_md=%s)', image_id, image_md)

        image = DB.images.update(id=image_id, **image_md)
        return utils.sanitize(image, IMAGES_INFO)
Example #14
0
    def list(self):
        """
        List all keypairs
        """
        LOG.info('list()')

        keypairs = DB.keypairs.list()
        return utils.sanitize(keypairs, KEYPAIRS_INFO)
Example #15
0
    def create(self, flavor):
        """
        Create a flavor
        """
        LOG.info('create(flavor=%s)', flavor)

        new_flavor = DB.flavors.create(**flavor)
        return utils.sanitize(new_flavor, FLAVORS_DETAIL)
Example #16
0
    def show(self, server_id):
        """
        Show server details
        """
        LOG.info('show(server_id=%s)', server_id)

        server = DB.servers.show(id=server_id)
        return utils.sanitize(self._extend(server), SERVERS_DETAIL)
Example #17
0
    def update(self, image_id, image_md):
        """
        Update image metadata
        """
        LOG.info('update(image_id=%s, image_md=%s)', image_id, image_md)

        image = DB.images.update(id=image_id, **image_md)
        return utils.sanitize(image, IMAGES_INFO)
Example #18
0
    def boot(self, server):
        """
        Boot a new server
        """
        LOG.info('boot(server=%s)', server)

        name = server['name']
        image_id = server['imageRef']
        flavor_id = server['flavorRef']
        key_name = server.get('key_name', None)

        # Sanity checks, will throw exceptions if they fail
        IMAGES.exists(image_id)
        FLAVORS.exists(flavor_id)
        if key_name is not None:
            KEYPAIRS.exists(key_name)

        # Create a new server in the database
        server = DB.servers.create(name=name, image_id=image_id,
                                   flavor_id=flavor_id, key_name=key_name,
                                   status='BUILDING')
        server_id = server['id']

        # Generate some more server properties and update the database
        mac_address = _generate_mac()
        server = DB.servers.update(id=server_id, mac_address=mac_address)

        # Extend the server details
        server = self._extend(server)

        # Create the server directory
        basepath = os.path.join(CONF.instances_dir, server_id)
        os.makedirs(basepath)

        # Create the server disk images
        _create_disks(server)

        # Finally boot the server
        VIRT.boot_server(server)

        # Update the status of the server
        server = DB.servers.update(id=server_id, status='NETWORKING')

        # Start a task to wait for the server to get its DHCP IP address
        task.start(server_id, 2, 60/2, [True], self._wait_for_ip, server)

        return utils.sanitize(server, SERVERS_DETAIL)