| 1 |
# Copyright 2015 Google Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import re |
| 16 |
|
| 17 |
import cmds as cmds_lib |
| 18 |
import validate |
| 19 |
|
| 20 |
class Service(object): |
| 21 |
|
| 22 |
def validateCmds(self, root, path): |
| 23 |
cmds = validate.array(root, path, validate.is_any_type({'string', 'object'}), []) |
| 24 |
for i, cmd in enumerate(cmds): |
| 25 |
cmd_path = path + [i] |
| 26 |
if isinstance(cmd, basestring): |
| 27 |
# Any string will do for validation purposes. |
| 28 |
pass |
| 29 |
elif isinstance(cmd, dict): |
| 30 |
kinds = {'CopyFile', 'LiteralFile', 'EnsureDir'} |
| 31 |
kind = validate.path_val(root, cmd_path + ['kind'], validate.is_any_value(kinds)) |
| 32 |
if kind == 'CopyFile': |
| 33 |
fields = {'owner', 'group', 'dirPermissions', 'filePermissions', 'from', 'to'} |
| 34 |
for f in fields: |
| 35 |
validate.path_val(root, cmd_path + [f], 'string') |
| 36 |
validate.obj_only(root, cmd_path, fields | {'kind'}) |
| 37 |
elif kind == 'LiteralFile': |
| 38 |
fields = {'owner', 'group', 'filePermissions', 'content', 'to'} |
| 39 |
for f in fields: |
| 40 |
validate.path_val(root, cmd_path + [f], 'string') |
| 41 |
validate.obj_only(root, cmd_path, fields | {'kind'}) |
| 42 |
elif cmd['kind'] == 'EnsureDir': |
| 43 |
fields = {'owner', 'group', 'dirPermissions', 'dir'} |
| 44 |
for f in fields: |
| 45 |
validate.path_val(root, cmd_path + [f], 'string') |
| 46 |
validate.obj_only(root, cmd_path, fields | {'kind'}) |
| 47 |
else: |
| 48 |
raise RuntimeError('Internal error: %s' % kind) |
| 49 |
else: |
| 50 |
raise RuntimeError('Internal error: %s' % type(cmd)) |
| 51 |
|
| 52 |
def validateImage(self, root, path): |
| 53 |
# Superclasses override this method and validate specific image attributes. |
| 54 |
# Byt here we can do the cmds. |
| 55 |
self.validateCmds(root, path + ['cmds']) |
| 56 |
|
| 57 |
def children(self, service): |
| 58 |
for child_name in sorted(service.keys()): |
| 59 |
if child_name in {'environment', 'infrastructure', 'outputs'}: |
| 60 |
continue |
| 61 |
yield child_name, service[child_name] |
| 62 |
|
| 63 |
def validateInfrastructure(self, root, service_name, path): |
| 64 |
infrastructure = validate.path_val(root, path, 'object', {}) |
| 65 |
for rtype in sorted(infrastructure.keys()): |
| 66 |
resources = validate.path_val(root, path + [rtype], 'object', {}) |
| 67 |
for resource_name in sorted(resources.keys()): |
| 68 |
if not resource_name.startswith(service_name): |
| 69 |
validate.err(path, |
| 70 |
'Expected "%s" to be prefixed by "%s"' % (resource_name, service_name), |
| 71 |
'Resource names must be prefixed by the name of the service defining them') |
| 72 |
|
| 73 |
def validateService(self, root, path): |
| 74 |
validate.path_val(root, path + ['outputs'], validate.is_string_map, {}) |
| 75 |
self.validateInfrastructure(root, self.fullName(path), path + ['infrastructure']) |
| 76 |
|
| 77 |
def fullName(self, path): |
| 78 |
return '-'.join(path) |
| 79 |
|
| 80 |
def preprocess(self, service): |
| 81 |
return { |
| 82 |
'environment': service.get('environment', 'default'), |
| 83 |
'infrastructure': service.get('infrastructure',{}), |
| 84 |
'outputs': service.get('outputs', {}), |
| 85 |
} |
| 86 |
|
| 87 |
def compileStartupScript(self, cmds, bootCmds): |
| 88 |
lines = [] |
| 89 |
lines.append('#!/usr/bin/env bash') |
| 90 |
lines.append('if [ ! -r /micromanage_instance_initialized ] ; then') |
| 91 |
for cmd in cmds: |
| 92 |
lines += cmds_lib.compile_command_to_bash(cmd) |
| 93 |
lines.append('touch /micromanage_instance_initialized') |
| 94 |
lines.append('fi') |
| 95 |
for cmd in bootCmds: |
| 96 |
lines += cmds_lib.compile_command_to_bash(cmd) |
| 97 |
return '\n'.join(lines) |
| 98 |
|