def _transform_envvar(_envvar): _envvar = _envvar.split("=", 1) length = len(_envvar) if length == 2: return {'key': _envvar[0], 'value': _envvar[1]} else: raise BadParameter("Environment variable '%s' does not match with 'KEY=VALUE'." " Example: ENVVAR=foo" % _envvar[0])
def _format_link(_link): link_regexp = re.compile(r'^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)?:[a-zA-Z0-9_-]+$') match = link_regexp.match(_link) if match: temp = _link.split(":", 1) return {target: temp[0], 'name': temp[1]} raise BadParameter("Link variable argument %s does not match with (service_name[.stack_name]:alias)." " Example: mysql:db" % _link)
def _get_port_dict(_port): if isinstance(_port, int) and 0 <= _port < 65535: port_spec = { 'protocol': 'tcp', 'inner_port': '%d' % _port, 'published': False } return port_spec raise BadParameter("expose port %s is not a valid port number" % _port)
def load_stack_file(name, stackfile, stack=None): if not stack: stack = tutum.Stack.create() else: name = stack.name if not stackfile: filematch = 0 if os.path.exists("tutum.yml"): stackfile = "tutum.yml" filematch += 1 if os.path.exists("tutum.yaml"): stackfile = "tutum.yaml" filematch += 1 if os.path.exists("tutum.json"): stackfile = "tutum.json" filematch += 1 if filematch == 0: raise BadParameter( "Cannot find stack file. Are you in the right directory?") elif filematch > 1: raise BadParameter( "More than one stack file was found in the path. " "Please specify which one you'd like to use with -f <filename>" ) with open(stackfile, 'r') as f: content = yaml.load(f.read()) services = [] if content: for k, v in content.items(): v.update({"name": k}) services.append(v) if not name: name = os.path.basename(os.getcwd()) services = inject_env_var(services) data = {'name': name, 'services': services} for k, v in list(data.items()): setattr(stack, k, v) else: raise BadParameter("Bad format of the stack file: %s" % stackfile) return stack
def get_services_from_stackfiles(name, stackfiles): services_dict = {} for stackfile in stackfiles: with open(stackfile, 'r') as f: content = yaml.load(f.read()) try: interpolated_content = interpolate_environment_variables( content, 'service') except Exception as e: raise BadParameter("Bad format of the stack file(%s): %s" % (stackfile, e)) if interpolated_content: for k, v in interpolated_content.items(): v.update({"name": k}) services_dict[k] = v else: raise BadParameter("Bad format of the stack file: %s" % stackfile) services = inject_env_var(services_dict.values()) data = {'name': name, 'services': services} return data
def _get_port_dict(_port): port_regexp = re.compile('^([0-9]{1,5}:)?([0-9]{1,5})(/tcp|/udp)?$') match = port_regexp.match(_port) if bool(match): outer_port = match.group(1) inner_port = match.group(2) protocol = match.group(3) if protocol is None: protocol = "tcp" else: protocol = protocol[1:] port_spec = {'protocol': protocol, 'inner_port': inner_port, 'published': True} if outer_port is not None: port_spec['outer_port'] = outer_port[:-1] return port_spec raise BadParameter("publish port %s does not match with '[host_port:]container_port[/protocol]'." " E.g: 80:80/tcp" % _port)
def parse_volume(volume): bindings = [] if not volume: return bindings for vol in volume: binding = {} terms = vol.split(":") if len(terms) == 1: binding["container_path"] = terms[0] elif len(terms) == 2: binding["host_path"] = terms[0] binding["container_path"] = terms[1] elif len(terms) == 3: binding["host_path"] = terms[0] binding["container_path"] = terms[1] if terms[2].lower() == 'ro': binding["rewritable"] = False else: raise BadParameter('Bad volume argument %s. Format: "[host_path:]/container_path[:permission]"' % vol) bindings.append(binding) return bindings