Beispiel #1
0
def delete_node(driver, instance_name, zone):
    node = utils.find_instance(driver, instance_name, zone)
    if node:
        driver.destroy_node(node)
        return {"stopped_instance": instance_name}
    else:
        return {"error": f"instance {instance_name} not found"}
Beispiel #2
0
def get_node(driver, instance_name, zone):
    node = utils.find_instance(driver, instance_name, zone)
    if node:
        return {
            "name": node.name,
            "creationTimestamp": node.extra['creationTimestamp'],
            "image": node.extra['image'],
            "public_ips": node.public_ips
        }
    else:
        return {}
Beispiel #3
0
def create_instance(driver,
                    user_id,
                    sig_server_addr,
                    sig_server_port,
                    zone='us-central1-b',
                    tags=[],
                    branch='aosp-master',
                    target='aosp_cf_x86_phone-userdebug'):
    """Creates a new Cuttlefish instance and launches it.
       Does not store runtime data in external GCP disk."""

    target = target.replace('_', '-')
    instance_name = f'halyard-{user_id}'
    image_family = f'halyard-{branch}-{target}'

    try:
        driver.ex_get_image_from_family(image_family)
    except:
        utils.fatal_error(f'Image family {image_family} does not exist.\n \
            New base images can be created using the `create_base_image` endpoint.'
                          )

    # Stops execution if instance already exists
    instance = utils.find_instance(driver, instance_name, zone)
    if instance:
        utils.fatal_error(f'Instance {instance_name} already exists.')

    build_node = driver.create_node(instance_name,
                                    'n1-standard-4',
                                    None,
                                    location=zone,
                                    ex_image_family=image_family,
                                    ex_service_accounts=[{
                                        'scopes': ['storage-ro']
                                    }],
                                    ex_disk_size=30,
                                    ex_tags=tags)

    utils.wait_for_instance(instance_name, zone)

    print('successfully created new instance', instance_name)

    launch_cvd(instance_name, zone, sig_server_addr, sig_server_port, False)

    return {"name": instance_name}
Beispiel #4
0
def create_base_image(
        driver,
        source_image_family='debian-10',
        source_image_project='debian-cloud',
        repository_url='https://github.com/google/android-cuttlefish.git',
        repository_branch='main',
        build_branch='aosp-master',
        build_target='aosp_cf_x86_phone-userdebug',
        build_id='',
        build_instance='halyard-build',
        build_zone='europe-west4-a',
        tags=[],
        dest_image='',
        dest_family='',
        image_disk='halyard-image-disk',
        respin=False):
    """Creates new base image that holds Cuttlefish packages and Android build artifacts"""

    # SETUP

    build_node = utils.find_instance(driver, build_instance, build_zone)
    if build_node:
        driver.destroy_node(build_node)
        print('successfully deleted', build_instance)

    build_volume = utils.find_disk(driver, image_disk, build_zone)
    if build_volume:
        driver.destroy_volume(build_volume)
        print('successfully deleted', image_disk)

    # BUILD INSTANCE CREATION

    build_volume = driver.create_volume(30,
                                        image_disk,
                                        location=build_zone,
                                        ex_image_family=source_image_family)

    print('built', source_image_family, 'disk')

    gpu_type = 'nvidia-tesla-p100-vws'
    gpu = utils.find_gpu(driver, gpu_type, build_zone)
    if not gpu:
        utils.fatal_error(f'Please use a zone with {gpu_type} GPUs available')

    build_node = driver.create_node(build_instance,
                                    'n1-standard-16',
                                    None,
                                    location=build_zone,
                                    ex_image_family=source_image_family,
                                    ex_accelerator_type=gpu_type,
                                    ex_on_host_maintenance='TERMINATE',
                                    ex_accelerator_count=1,
                                    ex_service_accounts=[{
                                        'scopes': ['storage-ro']
                                    }],
                                    ex_disk_size=30,
                                    ex_tags=tags)
    print('successfully created', build_instance)

    utils.wait_for_instance(build_instance, build_zone)

    driver.attach_volume(build_node, build_volume)

    src_files = ['create_base_image_gce.sh', 'download_artifacts.sh']
    src_files = [PATH + '/' + file for file in src_files]
    src = ' '.join(list(map(str, src_files)))

    os.system(f'gcloud compute scp {src} {build_instance}: \
        --zone={build_zone}')

    # IMAGE CREATION

    os.system(f'gcloud compute ssh --zone={build_zone} \
        {build_instance} -- ./create_base_image_gce.sh \
        {repository_url} {repository_branch} \
        {build_branch} {build_target} {build_id}')

    dest_names = get_dest_names(build_branch, build_target, build_id,
                                build_instance, build_zone, dest_image,
                                dest_family)

    dest_image = dest_names['dest_image']
    dest_family = dest_names['dest_family']

    try:
        build_image = driver.ex_get_image(dest_image)
    except:
        build_image = None

    if build_image:
        if respin:
            driver.ex_delete_image(build_image)
        else:
            utils.fatal_error(f'''Image {dest_image} already exists.
            (To replace run with flag --respin)''')

    driver.destroy_node(build_node)

    driver.ex_create_image(
        dest_image,
        build_volume,
        ex_licenses=[
            'https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx'
        ],
        family=dest_family)

    print(f'Created image {dest_image} in {dest_family} family')

    driver.destroy_volume(build_volume)

    return {"name": dest_image, "family": dest_family}
Beispiel #5
0
def create_or_restore_instance(driver,
                               user_id,
                               sig_server_addr,
                               sig_server_port,
                               zone='us-central1-b',
                               tags=[],
                               branch='aosp-master',
                               target='aosp_cf_x86_phone-userdebug'):
    """Restores instance with existing user disk and original base image.
       Creates a new instance with latest image if user disk doesn't exist.
       Stores runtime data in external GCP disk.
       Launches Cuttlefish if creation is successful."""

    # SETUP
    target = target.replace('_', '-')
    instance_name = f'halyard-{user_id}'
    disk_name = f'halyard-user-{user_id}'
    image_family = f'halyard-{branch}-{target}'

    # Stops execution if instance already exists
    instance = utils.find_instance(driver, instance_name, zone)
    if instance:
        utils.fatal_error(f'Instance {instance_name} already exists.')

    # Looks for existing user disk
    user_disk = utils.find_disk(driver, disk_name, zone)
    if user_disk:
        base_image = get_base_image_from_labels(user_disk)
    else:
        user_disk = driver.create_volume(30,
                                         disk_name,
                                         location=zone,
                                         image='blank-halyard')
        base_image = None

    # CREATE INSTANCE

    # If existing user, use original base image
    if base_image:
        try:
            driver.ex_get_image(base_image)
        except:
            utils.fatal_error(f'Image {base_image} does not exist.')

        new_instance = driver.create_node(instance_name,
                                          'n1-standard-4',
                                          base_image,
                                          location=zone,
                                          ex_service_accounts=[{
                                              'scopes': ['storage-ro']
                                          }],
                                          ex_disk_size=30,
                                          ex_tags=tags)

    # If new user, use image family
    else:
        try:
            img = driver.ex_get_image_from_family(image_family)
        except:
            utils.fatal_error(
                f'Image in family {image_family} does not exist.')

        set_base_image_labels(driver, user_disk, img.name, branch, target)

        new_instance = driver.create_node(instance_name,
                                          'n1-standard-4',
                                          None,
                                          location=zone,
                                          ex_image_family=image_family,
                                          ex_service_accounts=[{
                                              'scopes': ['storage-ro']
                                          }],
                                          ex_disk_size=30,
                                          ex_tags=tags)

    # ATTACH USER DISK AND LAUNCH

    utils.wait_for_instance(instance_name, zone)
    print('successfully created new instance', instance_name)

    driver.attach_volume(new_instance, user_disk)
    print(f'attached {disk_name} to {instance_name}')

    os.system(f'gcloud compute ssh --zone={zone} {instance_name} -- \
        sudo mkdir /mnt/user_data')
    os.system(f'gcloud compute ssh --zone={zone} {instance_name} -- \
        sudo mount /dev/sdb /mnt/user_data')
    os.system(f'gcloud compute ssh --zone={zone} {instance_name} -- \
        sudo chmod -R 777 /mnt/user_data')
    # FIXME : should assign specific user permissions

    launch_cvd(instance_name, zone, sig_server_addr, sig_server_port)

    return {"name": instance_name}