Ejemplo n.º 1
0
def get_image_name(encrypted_image_name, name):
    if encrypted_image_name:
        return encrypted_image_name

    nonce = make_nonce()
    # Replace nonce in image name
    m = re.match('(.+)\-encrypted\-', name)
    if m:
        encrypted_image_name = append_suffix(m.group(1),
                                             '-encrypted-%s' % (nonce, ),
                                             GCE_NAME_MAX_LENGTH)
    else:
        encrypted_image_name = append_suffix(name, '-encrypted-%s' % (nonce, ),
                                             GCE_NAME_MAX_LENGTH)
    return encrypted_image_name
Ejemplo n.º 2
0
def get_name_from_image(image):
    name = append_suffix(
        image.name,
        get_encrypted_suffix(),
        max_length=AMI_NAME_MAX_LENGTH
    )
    return name
Ejemplo n.º 3
0
def _get_updated_image_name(image_name, session_id):
    """ Generate a new name, based on the existing name of the encrypted
    image and the session id.

    :return the new name
    """
    # Replace session id in the image name.
    m = re.match('(.+) \(encrypted (\S+)\)', image_name)
    suffix = ' (encrypted %s)' % session_id
    if m:
        encrypted_ami_name = util.append_suffix(
            m.group(1), suffix, max_length=128)
    else:
        encrypted_ami_name = util.append_suffix(
            image_name, suffix, max_length=128)
    return encrypted_ami_name
Ejemplo n.º 4
0
    def test_append_suffix(self):
        """ Test that we append the suffix and truncate the original name.
        """
        name = 'Boogie nights are always the best in town'
        suffix = ' (except Tuesday)'
        encrypted_name = util.append_suffix(
            name, suffix, max_length=128)
        self.assertTrue(encrypted_name.startswith(name))
        self.assertTrue(encrypted_name.endswith(suffix))

        # Make sure we truncate the original name when it's too long.
        name += ('X' * 100)
        encrypted_name = util.append_suffix(
            name, suffix, max_length=128)
        self.assertEqual(128, len(encrypted_name))
        self.assertTrue(encrypted_name.startswith('Boogie nights'))
Ejemplo n.º 5
0
    def test_append_suffix(self):
        """ Test that we append the suffix and truncate the original name.
        """
        name = 'Boogie nights are always the best in town'
        suffix = ' (except Tuesday)'
        encrypted_name = util.append_suffix(
            name, suffix, max_length=128)
        self.assertTrue(encrypted_name.startswith(name))
        self.assertTrue(encrypted_name.endswith(suffix))

        # Make sure we truncate the original name when it's too long.
        name += ('X' * 100)
        encrypted_name = util.append_suffix(
            name, suffix, max_length=128)
        self.assertEqual(128, len(encrypted_name))
        self.assertTrue(encrypted_name.startswith('Boogie nights'))
Ejemplo n.º 6
0
def launch(log, gce_svc, image_id, instance_name, zone, delete_boot, instance_type, network, subnetwork, metadata={}):
    if not instance_name:
        instance_name = 'brkt' + '-' + str(uuid.uuid4().hex)

    snap_name = append_suffix(instance_name, '-snap', 64)
    log.info("Creating guest root disk from snapshot")
    gce_svc.disk_from_snapshot(zone, image_id, snap_name)
    gce_svc.wait_for_disk(zone, snap_name)
    log.info("Starting instance")
    guest_disk = gce_svc.get_disk(zone, snap_name)
    guest_disk['autoDelete'] = True
    gce_svc.run_instance(zone=zone,
                         name=instance_name,
                         image=image_id,
                         disks=[guest_disk],
                         metadata=metadata,
                         delete_boot=delete_boot,
                         network=network,
                         subnet=subnetwork,
                         instance_type=instance_type)
    gce_svc.wait_instance(instance_name, zone)
    log.info("Instance %s (%s) launched successfully" % (instance_name,
             gce_svc.get_instance_ip(instance_name, zone)))

    return instance_name
Ejemplo n.º 7
0
def get_image_name(encrypted_image_name, name):
    if encrypted_image_name:
        return encrypted_image_name

    nonce = make_nonce()
    # Replace nonce in image name
    m = re.match('(.+)\-encrypted\-', name)
    if m:
        encrypted_image_name = append_suffix(
                m.group(1),
                '-encrypted-%s' % (nonce,),
                GCE_NAME_MAX_LENGTH)
    else:
        encrypted_image_name = append_suffix(
                name,
                '-encrypted-%s' % (nonce,),
                GCE_NAME_MAX_LENGTH)
    return encrypted_image_name
Ejemplo n.º 8
0
def _get_updated_image_name(image_name, session_id):
    """ Generate a new name, based on the existing name of the encrypted
    image and the session id.

    :return the new name
    """
    # Replace session id in the image name.
    m = re.match('(.+) \(encrypted (\S+)\)', image_name)
    suffix = ' (encrypted %s)' % session_id
    if m:
        encrypted_ami_name = util.append_suffix(m.group(1),
                                                suffix,
                                                max_length=128)
    else:
        encrypted_ami_name = util.append_suffix(image_name,
                                                suffix,
                                                max_length=128)
    return encrypted_ami_name
Ejemplo n.º 9
0
def get_description_from_image(image):
    if image.description:
        suffix = SUFFIX_ENCRYPTED_IMAGE % {'image_id': image.id}
        description = append_suffix(
            image.description, suffix, max_length=255)
    else:
        description = DEFAULT_DESCRIPTION_ENCRYPTED_IMAGE % {
            'image_id': image.id
        }
    return description
Ejemplo n.º 10
0
def launch(
    log,
    gce_svc,
    image_id,
    instance_name,
    zone,
    delete_boot,
    instance_type,
    network,
    subnetwork,
    metadata={},
    ssd_disks=0,
):
    if not instance_name:
        instance_name = "brkt" + "-" + str(uuid.uuid4().hex)

    snap_name = append_suffix(instance_name, "-snap", 64)
    log.info("Creating guest root disk from snapshot")
    gce_svc.disk_from_snapshot(zone, image_id, snap_name)
    gce_svc.wait_for_disk(zone, snap_name)
    log.info("Starting instance")
    guest_disk = gce_svc.get_disk(zone, snap_name)
    guest_disk["autoDelete"] = True
    disks = [guest_disk]
    for x in range(ssd_disks):
        ssd_disk = gce_svc.create_ssd_disk(zone)
        disks.append(ssd_disk)
    gce_svc.run_instance(
        zone=zone,
        name=instance_name,
        image=image_id,
        disks=disks,
        metadata=metadata,
        delete_boot=delete_boot,
        network=network,
        subnet=subnetwork,
        instance_type=instance_type,
    )
    gce_svc.wait_instance(instance_name, zone)
    log.info("Instance %s (%s) launched successfully" % (instance_name, gce_svc.get_instance_ip(instance_name, zone)))

    return instance_name