in backend/bms_app/services/gce.py [0:0]
def create_instance(project, zone, name, vpc, subnet,
service_account, machine_type, startup_script=None):
# Get the latest bms-control-node image.
compute = discovery.build('compute', 'v1')
image_response = compute.images().getFromFamily(
project='centos-cloud', family='centos-7').execute()
source_disk_image = image_response['selfLink']
# Configure the machine
full_machine_type = f'zones/{zone}/machineTypes/{machine_type}'
config = {
'name': name,
'machineType': full_machine_type,
# Specify the boot disk and the image to use as a source.
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
}
}
],
'networkInterfaces': [{
'network': vpc,
'subnetwork': subnet,
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
# Allow the instance to access cloud storage and logging.
'serviceAccounts': [{
'email': service_account,
'scopes': [
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring.write",
"https://www.googleapis.com/auth/servicecontrol",
"https://www.googleapis.com/auth/service.management.readonly",
"https://www.googleapis.com/auth/trace.append",
"https://www.googleapis.com/auth/cloud-platform",
]
}],
# Metadata is readable from the instance and allows you to
# pass configuration from deployment scripts to instances.
'metadata': {
'items': []
}
}
if startup_script:
config['metadata']['items'].append(
{
'key': 'startup-script',
'value': startup_script
}
)
return compute.instances().insert(
project=project,
zone=zone,
body=config
).execute()