Exemplo n.º 1
0
def readCreateNetworkParameters(network):
    network_name = list(network.keys())[0]
    network_params = network[network_name]

    cmd = []
    cmd_opt = []
    cmd_list = []
    cmd_map = []

    for confkey, confval in network_params.items():
        if confkey in DOCKER_NETWORK_MAP_OPTIONS or confkey in DOCKER_NETWORK_MAPEQUAL_OPTIONS:
            if isinstance(confval, dict):
                for ck, cv in confval.items():
                    if confkey in DOCKER_NETWORK_MAPEQUAL_OPTIONS:
                        cmd_map.append("--%s=\"%s\"=\"%s\"" %
                                       (confkey, ck, safeQuote(cv)))
                    else:
                        cmd_map.append("--%s=%s:%s" %
                                       (confkey, ck, safeQuote(cv)))
                continue
        if confkey in DOCKER_NETWORK_SINGLE_OPTIONS:
            cmd_opt.append("--%s=%s" % (confkey, safeQuote(confval)))
        elif confkey in DOCKER_NETWORK_BOOL_OPTIONS:
            if confval is not None and (confval is True or confval.lower()
                                        in ["true", "yes"] or confval == 1):
                cmd_opt.append("--%s" % confkey)
        elif confkey in DOCKER_NETWORK_LIST_OPTIONS:
            if not isinstance(confval, (str, list, tuple)):
                return Fail(
                    InvalidConfigError(
                        "[%s] Malformed option '%s': Should be string, number or list."
                        % (network_name, confkey)))
            confval = ensureList(confval)
            for i, lv in enumerate(confval):
                cmd_list.append("--%s=%s" % (confkey, safeQuote(lv)))
        else:
            return Fail(
                InvalidConfigError("[%s] Unkown option '%s'." %
                                   (network_name, confkey)))

    for part in cmd_opt:
        cmd.append(part)
    for part in cmd_list:
        cmd.append(part)
    for part in cmd_map:
        cmd.append(part)

    cmd.append("--label %s.name=\"%s\"" %
               (DOCKWRKR_LABEL_DOMAIN_NETWORK, network_name))
    cmd.append("--label %s.managed=1" % DOCKWRKR_LABEL_DOMAIN)
    cmd.append(network_name)

    cmdline = ""
    for i, part in enumerate(cmd):
        if i > 0:
            cmdline += " "
        cmdline += part

    return OK(cmdline)
Exemplo n.º 2
0
 def getContainerDependencies(self, container):
     node = {}
     node['name'] = container
     deps = []
     config = self.getContainerConfig(container)
     if "link" in config:
         for link in ensureList(config['link']):
             deps.append(link.partition(':')[0])
     node['deps'] = deps
     return node
Exemplo n.º 3
0
 def getContainerDependencies(self, container):
     node = {}
     node['name'] = container
     deps = []
     config = self.getContainerConfig(container)
     if not config:
         raise InvalidContainerError(
             "Container named '%s' is listed as a dependency but it is never defined."
             % container)
     if "link" in config:
         for link in ensureList(config['link']):
             deps.append(link.partition(':')[0])
     node['deps'] = deps
     return node
Exemplo n.º 4
0
def readCreateParameters(container,
                         config,
                         basePath=None,
                         networks=None,
                         asList=False):

    cconf = config.copy()

    # Autostart is only used by dockwrkr, we do not want to pass it to docker
    if 'autostart' in cconf:
        del cconf['autostart']

    if 'net' in cconf:
        if networks and cconf['net'] in networks:
            readNetworkExists({cconf['net']: networks[cconf['net']]}) \
                .bind(createNetwork)

    cconf['name'] = container

    if 'image' not in cconf:
        return Fail(
            InvalidConfigError("[%s] Container has no 'image' defined." %
                               (container)))
    image = cconf['image']
    del cconf['image']

    command = []
    if 'command' in cconf:
        command = ensureList(cconf['command'])
        del cconf['command']

    extra_flags = []
    if 'extra-flags' in cconf:
        extra_flags = ensureList(cconf['extra-flags'])
        del cconf['extra-flags']

    cmd = []
    cmd_opt = []
    cmd_list = []
    cmd_map = []

    for confkey, confval in cconf.items():
        if confkey in DOCKER_MAP_OPTIONS or confkey in DOCKER_MAPEQUAL_OPTIONS:
            if isinstance(confval, dict):
                for ck, cv in confval.items():
                    if confkey in DOCKER_MAPEQUAL_OPTIONS:
                        cmd_map.append("--%s=%s=%s" %
                                       (confkey, ck, safeQuote(cv)))
                    else:
                        cmd_map.append("--%s=%s:%s" %
                                       (confkey, ck, safeQuote(cv)))
                continue
        if confkey == "volume":
            for ck in ensureList(confval):
                if ck.find(':'):
                    (path, sep, path_map) = ck.partition(':')
                    path = expandLocalPath(path, basePath=basePath)
                    cmd_map.append(
                        "--%s=%s:%s" %
                        (confkey, safeQuote(path), safeQuote(path_map)))
            continue
        if confkey in DOCKER_SINGLE_OPTIONS:
            cmd_opt.append("--%s=%s" % (confkey, safeQuote(confval)))
        elif confkey in DOCKER_BOOL_OPTIONS:
            if confval is not None and (confval is True or confval.lower()
                                        in ["true", "yes"] or confval == 1):
                cmd_opt.append("--%s=%s" % (confkey, "true"))
            elif confval is not None and (confval is False or confval.lower()
                                          in ["false", "no"] or confval == 0):
                cmd_opt.append("--%s=%s" % (confkey, "false"))
            else:
                return Fail(
                    InvalidConfigError(
                        "[%s] Invalid value for option '%s' : Must be true or false."
                        % (container, confkey)))
        elif confkey in DOCKER_LIST_OPTIONS:
            if not isinstance(confval, (str, list, tuple)):
                return Fail(
                    InvalidConfigError(
                        "[%s] Malformed option '%s': Should be string, number or list."
                        % (container, confkey)))
            confval = ensureList(confval)
            for i, lv in enumerate(confval):
                cmd_list.append("--%s=%s" % (confkey, safeQuote(lv)))
        else:
            return Fail(
                InvalidConfigError("[%s] Unknown option '%s'." %
                                   (container, confkey)))

    for part in cmd_opt:
        cmd.append(part)
    for part in cmd_list:
        cmd.append(part)
    for part in cmd_map:
        cmd.append(part)
    for extra in extra_flags:
        cmd.append(extra)

    cmd.append("--label")
    cmd.append("%s.name=\"%s\"" % (DOCKWRKR_LABEL_DOMAIN, container))
    cmd.append("--label")
    cmd.append("%s.managed=1" % DOCKWRKR_LABEL_DOMAIN)
    cmd.append(image)

    for part in command:
        cmd.append(part)

    if asList:
        return OK(cmd)

    cmdline = ""
    for i, part in enumerate(cmd):
        if i > 0:
            cmdline += " "
        cmdline += part

    return OK(cmdline)