Example #1
0
def create_instance(sdk, folder_id,  zone, name, subnet_id):
    image_service = sdk.client(ImageServiceStub)
    source_image = image_service.GetLatestByFamily(
        GetImageLatestByFamilyRequest(
            folder_id='standard-images',
            family='debian-9'))
    subnet_id = subnet_id or get_subnet(sdk, folder_id, zone)
    instance_service = sdk.client(InstanceServiceStub)
    operation = instance_service.Create(CreateInstanceRequest(
        folder_id=folder_id,
        name=name,
        resources_spec=ResourcesSpec(
            memory=2 * 2**30,
            cores=1,
            core_fraction=0),
        zone_id=zone,
        platform_id='standard-v1',
        boot_disk_spec=AttachedDiskSpec(
            auto_delete=True,
            disk_spec=AttachedDiskSpec.DiskSpec(
                type_id='network-hdd',
                size=20 * 2 ** 30,
                image_id=source_image.id)),
        network_interface_specs=[
            NetworkInterfaceSpec(
                subnet_id=subnet_id,
                primary_v4_address_spec=PrimaryAddressSpec(
                    one_to_one_nat_spec=OneToOneNatSpec(
                        ip_version=IPV4,
                    ))),
        ],
    ))
    print('Creating initiated')
    return operation
def _get_attached_disk_spec(disk_type,
                            disk_size,
                            image_id=None,
                            snapshot_id=None):
    return (AttachedDiskSpec(
        auto_delete=True,
        disk_spec=AttachedDiskSpec.DiskSpec(
            type_id=disk_type, size=disk_size, image_id=image_id),
    ) if image_id else AttachedDiskSpec(
        auto_delete=True,
        disk_spec=AttachedDiskSpec.DiskSpec(
            type_id=disk_type, size=disk_size, snapshot_id=snapshot_id),
    ))
def _get_secondary_disk_specs(secondary_disks):
    return list(
        map(
            lambda disk: AttachedDiskSpec(
                auto_delete=disk.get("autodelete", True),
                disk_spec=AttachedDiskSpec.DiskSpec(
                    description=disk.get("description"),
                    type_id=disk["type"],
                    size=disk["size"],
                ),
            ),
            secondary_disks,
        ))
Example #4
0
def create_instance_async(sdk, name, memory, cores, disk_size,
                          share=100, spot=True, disk_type=YC_HDD, image=YC_UBUNTU_1604,
                          subnet=YC_SUBNET, folder=YC_FOLDER, platform=YC_PLATFORM,
                          user=SSH_USER, key=SSH_KEY):

    folder = find_folder(sdk, folder)
    subnet = find_subnet(sdk, folder, subnet)
    image = find_image(sdk, folder, image)
    metadata = dict(generate_metadata(user, key))

    service = sdk.client(InstanceServiceStub)
    return service.Create(
        CreateInstanceRequest(
            folder_id=folder.id,
            name=name,
            zone_id=subnet.zone_id,
            platform_id=platform,

            resources_spec=ResourcesSpec(
                memory=memory * GB,
                cores=cores,
                core_fraction=share
            ),

            metadata=metadata,

            boot_disk_spec=AttachedDiskSpec(
                auto_delete=True,
                disk_spec=AttachedDiskSpec.DiskSpec(
                    type_id=disk_type,
                    size=disk_size * GB,
                    image_id=image.id
                )
            ),

            network_interface_specs=[
                NetworkInterfaceSpec(
                    subnet_id=subnet.id,
                    primary_v4_address_spec=PrimaryAddressSpec(
                        one_to_one_nat_spec=OneToOneNatSpec(
                            ip_version=IPV4,
                        )
                    )
                ),
            ],

            scheduling_policy=SchedulingPolicy(
                preemptible=spot
            )
        ))