Ejemplo n.º 1
0
def parse_health(hc_str):
    err_msg = ("Invalid healthcheck argument '%s'. healthcheck arguments"
               " must be of the form --healthcheck <cmd='command',"
               "interval=time,retries=integer,timeout=time>, and the unit "
               "of time is s(seconds), m(minutes), h(hours).") % hc_str
    keys = ["cmd", "interval", "retries", "timeout"]
    health_info = {}
    for kv_str in hc_str[0].split(","):
        try:
            k, v = kv_str.split("=", 1)
            k = k.strip()
            v = v.strip()
        except ValueError:
            raise apiexec.CommandError(err_msg)
        if k in keys:
            if health_info.get(k):
                raise apiexec.CommandError(err_msg)
            elif k in ['interval', 'timeout']:
                health_info[k] = _convert_healthcheck_para(v, err_msg)
            elif k == "retries":
                health_info[k] = int(v)
            else:
                health_info[k] = v
        else:
            raise apiexec.CommandError(err_msg)
    return health_info
Ejemplo n.º 2
0
def parse_mounts(mounts):
    err_msg = ("Invalid mounts argument '%s'. mounts arguments must be of "
               "the form --mount source=<volume>,destination=<path>. Or use "
               "--mount size=<size>,destination=<path> to create a new volume "
               "and mount to the container")
    parsed_mounts = []
    for mount in mounts:
        keys = ["source", "destination", "size"]
        mount_info = {}
        for mnt in mount.split(","):
            try:
                k, v = mnt.split("=", 1)
                k = k.strip()
                v = v.strip()
            except ValueError:
                raise apiexec.CommandError(err_msg % mnt)
            if k in keys:
                if mount_info.get(k):
                    raise apiexec.CommandError(err_msg % mnt)
                mount_info[k] = v
            else:
                raise apiexec.CommandError(err_msg % mnt)

        if not mount_info.get('destination'):
            raise apiexec.CommandError(err_msg % mnt)

        if not mount_info.get('source') and not mount_info.get('size'):
            raise apiexec.CommandError(err_msg % mnt)

        parsed_mounts.append(mount_info)
    return parsed_mounts
Ejemplo n.º 3
0
def _convert_healthcheck_para(time, err_msg):
    int_pattern = r'^\d+$'
    time_pattern = r'^\d+(s|m|h)$'
    ret = 0
    if re.match(int_pattern, time):
        ret = int(time)
    elif re.match(time_pattern, time):
        if time.endswith('s'):
            ret = int(time.split('s')[0])
        elif time.endswith('m'):
            ret = int(time.split('m')[0]) * 60
        elif time.endswith('h'):
            ret = int(time.split('h')[0]) * 3600
    else:
        raise apiexec.CommandError(err_msg)
    return ret
Ejemplo n.º 4
0
def parse_mounts(mounts):
    err_msg = ("Invalid mounts argument '%s'. mounts arguments must be of "
               "the form --mount source=<volume>,destination=<path>, "
               "or use --mount size=<size>,destination=<path> to create "
               "a new volume and mount to the container, "
               "or use --mount type=bind,source=<file>,destination=<path> "
               "to inject file into a path in the container.")
    parsed_mounts = []
    for mount in mounts:
        keys = ["source", "destination", "size", "type"]
        mount_info = {}
        for mnt in mount.split(","):
            try:
                k, v = mnt.split("=", 1)
                k = k.strip()
                v = v.strip()
            except ValueError:
                raise apiexec.CommandError(err_msg % mnt)
            if k in keys:
                if mount_info.get(k):
                    raise apiexec.CommandError(err_msg % mnt)
                mount_info[k] = v
            else:
                raise apiexec.CommandError(err_msg % mnt)

        if not mount_info.get('destination'):
            raise apiexec.CommandError(err_msg % mount)

        if not mount_info.get('source') and not mount_info.get('size'):
            raise apiexec.CommandError(err_msg % mount)

        type = mount_info.get('type', 'volume')
        if type not in ('volume', 'bind'):
            mnt = "type=%s" % type
            raise apiexec.CommandError(err_msg % mnt)

        if type == 'bind':
            # TODO(hongbin): handle the case that 'source' is a directory
            filename = mount_info.pop('source')
            with open(filename, 'rb') as file:
                mount_info['source'] = file.read()

        parsed_mounts.append(mount_info)
    return parsed_mounts
Ejemplo n.º 5
0
def parse_nets(ns):
    err_msg = ("Invalid nets argument '%s'. nets arguments must be of "
               "the form --nets <network=network, v4-fixed-ip=ip-addr,"
               "v6-fixed-ip=ip-addr, port=port-uuid>, "
               "with only one of network, or port specified.")
    nets = []
    for net_str in ns:
        net_info = {
            "network": "",
            "v4-fixed-ip": "",
            "v6-fixed-ip": "",
            "port": ""
        }
        for kv_str in net_str.split(","):
            try:
                k, v = kv_str.split("=", 1)
                k = k.strip()
                v = v.strip()
            except ValueError:
                raise apiexec.CommandError(err_msg % net_str)
            if k in net_info:
                if net_info[k]:
                    raise apiexec.CommandError(err_msg % net_str)
                net_info[k] = v
            else:
                raise apiexec.CommandError(err_msg % net_str)

        if net_info['v4-fixed-ip'] and not netutils.is_valid_ipv4(
                net_info['v4-fixed-ip']):
            raise apiexec.CommandError("Invalid ipv4 address.")

        if net_info['v6-fixed-ip'] and not netutils.is_valid_ipv6(
                net_info['v6-fixed-ip']):
            raise apiexec.CommandError("Invalid ipv6 address.")

        if bool(net_info['network']) == bool(net_info['port']):
            raise apiexec.CommandError(err_msg % net_str)

        nets.append(net_info)
    return nets