Skip to content

Instantly share code, notes, and snippets.

@Fkawala
Created June 20, 2017 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fkawala/87a8522c724995023314a46a651fe9d1 to your computer and use it in GitHub Desktop.
Save Fkawala/87a8522c724995023314a46a651fe9d1 to your computer and use it in GitHub Desktop.
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Helper methods for working with containers in config."""
USER_DATA_TEMPLATE = \
"""\
#cloud-config
users:
- name: cloudservice
uid: 2000
write_files:
- path: /etc/systemd/system/container.service
permissions: 0644
owner: root
content: |
[Unit]
Description=service that runs the container
[Service]
Restart=always
RestartSec=5
PermissionsStartOnly=true
Environment="HOME=/home/cloudservice"
ExecStartPre=/usr/share/google/dockercfg_update.sh
ExecStart=/usr/bin/docker run --rm -u 2000 --name={name} {env} {port} {image}
ExecStop=/usr/bin/docker stop {name}
ExecStopPost=/usr/bin/docker rm {name}
runcmd:
- systemctl daemon-reload
- systemctl start container.service
"""
def generate_user_data(context):
"""Generates user data given a Template context.
Args:
context: Template context, which must contain dockerImage and port
properties, and an optional dockerEnv property.
Returns:
A user data string.
"""
user_data_parameters = {
'port': '',
'env': '',
'name': context.properties['dockerImage'].split('/')[-1].split('@')[0],
'image': context.properties['dockerImage']}
if 'port' in context.properties and context.properties['port']:
port = context.properties['port']
user_data_parameters['port'] = '-p {0}:{0}'.format(port)
if 'dockerEnv' in context.properties:
docker_env = context.properties['dockerEnv']
environment_variables = " ".join("-e {0}='{1}'".format(k, v)
for k, v in docker_env.iteritems())
user_data_parameters['env'] = environment_variables
return USER_DATA_TEMPLATE.format(**user_data_parameters)
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Creates autoscaled, network LB IGM running specified docker image."""
def GenerateConfig(context):
"""Generate YAML resource configuration."""
# Pull the region out of the zone
region = context.properties['zone'][:context.properties['zone'].rfind('-')]
name = context.env['name']
instance_group_manager_name = name + '-igm'
instance_template_name = name + '-it'
resources = [
{
'name': instance_template_name,
'type': 'instance_template.py',
'properties': {
'port': context.properties['port'] if 'port' in context.properties else None,
'dockerEnv': context.properties['dockerEnv'] if 'dockerEnv' in context.properties else {},
'dockerImage': context.properties['dockerImage'],
'containerImage': context.properties['containerImage'],
'machineType': context.properties['machineType']
}},
{
'name': instance_group_manager_name,
'type': 'compute.v1.instanceGroupManager',
'properties': {
'zone': context.properties['zone'],
'targetSize': context.properties['minSize'],
'baseInstanceName': name,
'instanceTemplate': '$(ref.' + instance_template_name + '.selfLink)'
}
},
{
'name': name + '-as',
'type': 'compute.v1.autoscaler',
'properties': {
'zone': context.properties['zone'],
'target': '$(ref.' + name + '-igm.selfLink)',
'autoscalingPolicy': {
'minNumReplicas': context.properties['minSize'],
'maxNumReplicas': context.properties['maxSize'],
'coolDownPeriodSec': context.properties['coolDownPeriod'],
'cpuUtilization': {
'utilizationTarget': context.properties['utilizationTarget']
}
}
}
}
]
return {'resources': resources}
imports:
- path: deploy.py
- path: instance_template.py
- path: container_helper.py
resources:
- name: match-preprod-ad
type: deploy.py
properties:
zone: us-central1-f
containerImage: cos-stable-58-9334-62-0
dockerImage: gcr.io/<your-project-id>/<your-image>@sha256:<sha256>
minSize: 1
maxSize: 2
utilizationTarget: .9
coolDownPeriod: 300
machineType: f1-micro
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Creates a Container VM with the provided Container manifest."""
from container_helper import generate_user_data
def GenerateConfig(context):
# TODO: remove useless scopes
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"
]
image = 'projects/cos-cloud/global/images/' + \
context.properties['containerImage']
default_network = ''.join(
['https://www.googleapis.com/compute/v1/projects/', context.env['project'],
'/global/networks/default'])
instance_template = {
'name': context.env['name'],
'type': 'compute.v1.instanceTemplate',
'properties': {
'properties': {
'metadata': {
'items': [
{
'key': 'user-data',
'value': generate_user_data(context)
},
{
'key': 'instance-group-name',
'value': context.env['name'] + '-igm'
}]},
'machineType': context.properties['machineType'],
'disks': [
{
'deviceName': 'boot',
'boot': True,
'autoDelete': True,
'mode': 'READ_WRITE',
'type': 'PERSISTENT',
'initializeParams':
{
'sourceImage': image,
'distName': context.env['name'] + '-disk'
}}],
'networkInterfaces': [
{
'accessConfigs': [
{
'name': 'external-nat',
'type': 'ONE_TO_ONE_NAT'
}],
'network': default_network}],
'scheduling': {'preemptible': True},
'serviceAccounts': [
{
# TODO: set your @developer.gserviceaccount.com account.
"email": "",
"scopes": scopes}],
}
}
}
return {'resources': [instance_template]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment