示例#1
0
def GetNetworkInterfaces(context):
    """Extracts the network interfaces to be used in the VM creation."""
    props = context.properties

    networks = props.setdefault(NETWORKS, DEFAULT_NETWORKS)
    subnetworks = props.get(SUBNETWORKS, [])
    external_ips = props.get(EXTERNAL_IPS, DEFAULT_EXTERNAL_IPS)

    network_interfaces = []
    for i in range(len(networks)):
        name = 'Interface %d' % i
        network_interface = {
            'network': common.MakeNetworkComputeLink(context, networks[i]),
            'name': name,
        }

        if subnetworks and i < len(subnetworks) and subnetworks[i]:
            network_interface['subnetwork'] = common.MakeSubnetworkComputeLink(
                context, subnetworks[i])

        if i < len(external_ips) and external_ips[i] == 'EPHEMERAL':
            network_interface['accessConfigs'] = [{
                'name':
                '%s %s' % (name, default.EXTERNAL),
                'type':
                default.ONE_NAT,
            }]

        network_interfaces.append(network_interface)

    return network_interfaces
def GetNetworkInterfaces(context):
  """Extracts the network interfaces to be used in the VM creation."""
  props = context.properties

  networks = props.setdefault(NETWORKS, DEFAULT_NETWORKS)
  subnetworks = props.get(SUBNETWORKS, [])
  external_ips = props.get(EXTERNAL_IPS, DEFAULT_EXTERNAL_IPS)

  network_interfaces = []
  for i in range(len(networks)):
    name = 'Interface %d' % i
    network_interface = {
        'network': common.MakeNetworkComputeLink(context, networks[i]),
        'name': name,
    }

    if subnetworks and i < len(subnetworks) and subnetworks[i]:
      network_interface['subnetwork'] = common.MakeSubnetworkComputeLink(
          context, subnetworks[i])

    ip_value = external_ips[i] if i < len(external_ips) else 'NONE'
    is_static_ip = VALID_IP_RE.match(ip_value) is not None
    if ip_value != 'EPHEMERAL' and ip_value != 'NONE' and not is_static_ip:
      raise common.Error(
          ('External IP value "%s" is invalid. Valid values '
           'are: a valid IP Address, EPHEMERAL or NONE.') % ip_value)

    if ip_value == 'EPHEMERAL' or is_static_ip:
      access_config = {
          'name': '%s %s' % (name, default.EXTERNAL),
          'type': default.ONE_NAT,
      }

      if is_static_ip:
        access_config[default.NAT_IP] = ip_value

      network_interface['accessConfigs'] = [access_config]

    network_interfaces.append(network_interface)

  return network_interfaces
示例#3
0
def AddServiceEndpointIfNeeded(context):
    """If the endpoint property is present, it will add a service endpoint."""
    prop = context.properties
    if ENDPOINT_NAME not in prop:
        return []
    network = common.MakeNetworkComputeLink(context, prop[default.NETWORKS][0])
    reference = "$(ref." + MakeVMName(context) + ".name)"
    address = common.MakeFQHN(context, reference)
    name = prop[ENDPOINT_NAME]
    resource = [{
        "name": name,
        "type": default.ENDPOINT,
        "properties": {
            "addresses": [{
                "address": address
            }],
            "dnsIntegration": {
                "networks": [network]
            },
        },
    }]
    return resource
def AddServiceEndpointIfNeeded(context):
    """If the endpoint property is present, it will add a service endpoint."""
    prop = context.properties
    if ENDPOINT_NAME not in prop:
        return []
    network = common.MakeNetworkComputeLink(context, prop[default.NETWORKS][0])
    reference = '$(ref.' + MakeVMName(context) + '.name)'
    address = common.MakeFQHN(context, reference)
    name = prop[ENDPOINT_NAME]
    resource = [{
        'name': name,
        'type': default.ENDPOINT,
        'properties': {
            'addresses': [{
                'address': address
            }],
            'dnsIntegration': {
                'networks': [network]
            }
        }
    }]
    return resource
示例#5
0
def GenerateConfig(context):
  """Generates deployment configuration """

  CheckParameters(context)

  suffix = context.properties['discriminator']

  hc_name = 'hc-' + suffix
  fw_name = 'hc-fw-' + suffix
  rt_config_name = 'config-' + suffix

  project_id = context.properties['projectId']
  network_project_id = context.properties['networkProjectId']

  if (network_project_id == ''):
    network_project_id = project_id

  region = context.properties['region']
  nat_gw_tag = context.properties['nat-gw-tag']

  network = common.MakeNetworkComputeLink(context, context.properties['network'])
  subnetwork = common.MakeSubnetworkComputeLink(context, context.properties['subnetwork'])

  sourceImage =  GlobalComputeUrl('debian-cloud', 'images', 'family/debian-9')

  config = {'resources': []}

  # A health check to be used by managed instance groups
  healthCheck = {
      'name': hc_name,
      'type': 'compute.v1.httpHealthCheck',
      'properties': {
          'port': 80,
          'requestPath': '/health-check',
          'healthyThreshold': 1,
          'unhealthyThreshold': 3,
          'checkIntervalSec': 10
      }
  }
  config['resources'].append(healthCheck)

  # Firewall rule that allows the health check to work. See
  # https://cloud.google.com/compute/docs/load-balancing/health-checks#health_check_source_ips_and_firewall_rules.
  fwRule = {
      'name': fw_name,
      'type': 'compute.v1.firewall',
      'properties': {
          'network': network,
          'sourceRanges': ['209.85.152.0/22', '209.85.204.0/22', '35.191.0.0/16', '130.211.0.0/22'],
          'targetTags': [nat_gw_tag],
          'allowed': [{
              'IPProtocol': 'TCP',
              'ports': [80]
          }]
      }
  }
  config['resources'].append(fwRule)

  # Runtime config is used to coordinate waiters and make sure that NAT gateway VMs are up before trying
  # to add routes pointing to these VMs
  rtConfig = {
      'name': rt_config_name,
      'type': 'runtimeconfig.v1beta1.config',
      'properties': {
          'config':  rt_config_name
      }
  }
  config['resources'].append(rtConfig)

  # Create a NAT gateway for each zone specified in zones property
  i = 1
  # for zone in context.properties['zones']:
  zones = [context.properties['zone']]
  for zone in zones:
    nat_gateway_vm = {
        # the same zone can be specified multiple times, so adding a counter for uniquness
        'name': 'nat-' +  str(i) + '-' + zone + '-' + suffix,
        'type': 'single-nat-gateway.py',
        'properties': {
            'projectId': project_id,
            'discriminator': context.properties['discriminator'],
            'region': region,
            'zone': zone,
            'machineType': context.properties['machineType'],
            'image': sourceImage,
            'diskType': context.properties['diskType'],
            'diskSizeGb': context.properties['diskSizeGb'],
            'nat-gw-tag': context.properties['nat-gw-tag'],
            'nated-vm-tag': context.properties['nated-vm-tag'],
            'routePriority': context.properties['routePriority'],
            'startupScript': context.properties['startupScript'],
            'network': network,
            'subnetwork': subnetwork,
            'healthCheck': '$(ref.' + hc_name + '.selfLink)',
            'runtimeConfig': '$(ref.' + rt_config_name + '.name)',
            'runtimeConfigName': rt_config_name
        }
    }
    config['resources'].append(nat_gateway_vm)
    i += 1

  return config