def create_public(group_name, pod_name, appname): """参数同private, 只是不能指定需要的核心数量""" data = request.get_json() if data.get('raw', ''): vstr = consts.RAW_VERSION_PLACEHOLDER else: vstr = data['version'] group, pod, application, version = validate_instance(group_name, pod_name, appname, vstr) networks = Network.get_multi(data.get('networks', [])) spec_ips = data.get('spec_ips', []) ncontainer = int(data['ncontainer']) appconfig = version.appconfig if not data['entrypoint'] in appconfig.entrypoints: current_app.logger.error('Entrypoint not in app.yaml (entry=%s, name=%s, version=%s)', data['entrypoint'], appname, version.short_sha) raise EruAbortException(consts.HTTP_BAD_REQUEST, 'Entrypoint %s not in app.yaml' % data['entrypoint']) ts, keys = [], [] with rds.lock('%s:%s' % (group_name, pod_name)): hosts = pod.get_free_public_hosts(ncontainer) for host in itertools.islice(itertools.cycle(hosts), ncontainer): t = _create_task(consts.TASK_CREATE, version, host, 1, {}, 0, networks, spec_ips, data['entrypoint'], data['env'], image=data.get('image', '')) if not t: continue ts.append(t.id) keys.append(t.result_key) return {'r':0, 'msg': 'ok', 'tasks': ts, 'watch_keys': keys}
def create_private(group_name, pod_name, appname): """ncore: 需要的核心数, 可以是小数, 例如1.5个""" data = request.get_json() if data.get('raw', ''): vstr = consts.RAW_VERSION_PLACEHOLDER else: vstr = data['version'] group, pod, application, version = validate_instance(group_name, pod_name, appname, vstr) # TODO check if group has this pod core_require = int(float(data['ncore']) * pod.core_share) # 是说一个容器要几个核... ncore = core_require / pod.core_share nshare = core_require % pod.core_share ncontainer = int(data['ncontainer']) networks = Network.get_multi(data.get('networks', [])) spec_ips = data.get('spec_ips', []) appconfig = version.appconfig # 指定的host, 如果没有则按照编排分配host hostname = data.get('hostname', '') host = hostname and Host.get_by_name(hostname) or None if host and not (host.group_id == group.id and host.pod_id == pod.id): current_app.logger.error('Host must belong to pod/group (hostname=%s, pod=%s, group=%s)', host, pod_name, group_name) raise EruAbortException(consts.HTTP_BAD_REQUEST, 'Host must belong to this pod and group') if not data['entrypoint'] in appconfig.entrypoints: current_app.logger.error('Entrypoint not in app.yaml (entry=%s, name=%s, version=%s)', data['entrypoint'], appname, version.short_sha) raise EruAbortException(consts.HTTP_BAD_REQUEST, 'Entrypoint %s not in app.yaml' % data['entrypoint']) ts, keys = [], [] with rds.lock('%s:%s' % (group_name, pod_name)): host_cores = group.get_free_cores(pod, ncontainer, ncore, nshare, spec_host=host) if not host_cores: current_app.logger.error('Not enough cores (name=%s, version=%s, ncore=%s)', appname, version.short_sha, data['ncore']) raise EruAbortException(consts.HTTP_BAD_REQUEST, 'Not enough core resources') for (host, container_count), cores in host_cores.iteritems(): t = _create_task(consts.TASK_CREATE, version, host, container_count, cores, nshare, networks, spec_ips, data['entrypoint'], data['env'], image=data.get('image', '')) if not t: continue host.occupy_cores(cores, nshare) ts.append(t.id) keys.append(t.result_key) return {'r': 0, 'msg': 'ok', 'tasks': ts, 'watch_keys': keys}
def create_public(group_name, pod_name, appname): data = request.get_json() vstr = data['version'] group, pod, _, version = validate_instance(group_name, pod_name, appname, vstr) ports = data.get('ports', []) args = data.get('args', []) callback_url = data.get('callback_url', '') if callback_url and not is_strict_url(callback_url): abort(400, 'callback_url must starts with http:// or https://') networks = Network.get_multi(data.get('networks', [])) spec_ips = data.get('spec_ips', []) appconfig = version.appconfig ncontainer = int(data['ncontainer']) if not ncontainer: abort(400, 'ncontainer must be > 0') entrypoint = data['entrypoint'] if entrypoint not in appconfig.entrypoints: abort(400, 'Entrypoint %s not in app.yaml' % entrypoint) ts, keys = [], [] with rds.lock('%s:%s' % (group_name, pod_name)): hosts = pod.get_free_public_hosts(ncontainer) for host in itertools.islice(itertools.cycle(hosts), ncontainer): t = _create_task( version, host, 1, {}, 0, networks, ports, args, spec_ips, data['entrypoint'], data['env'], image=data.get('image', ''), callback_url=callback_url, ) if not t: continue ts.append(t.id) keys.append(t.result_key) return {'r':0, 'msg': 'ok', 'tasks': ts, 'watch_keys': keys}
def create_public(group_name, pod_name, appname): """参数同private, 只是不能指定需要的核心数量""" data = request.get_json() if data.get("raw", ""): vstr = consts.RAW_VERSION_PLACEHOLDER else: vstr = data["version"] group, pod, application, version = validate_instance(group_name, pod_name, appname, vstr) networks = Network.get_multi(data.get("networks", [])) spec_ips = data.get("spec_ips", []) ncontainer = int(data["ncontainer"]) appconfig = version.appconfig if not data["entrypoint"] in appconfig.entrypoints: current_app.logger.error( "Entrypoint not in app.yaml (entry=%s, name=%s, version=%s)", data["entrypoint"], appname, version.short_sha ) raise EruAbortException(consts.HTTP_BAD_REQUEST, "Entrypoint %s not in app.yaml" % data["entrypoint"]) ts, keys = [], [] with rds.lock("%s:%s" % (group_name, pod_name)): hosts = pod.get_free_public_hosts(ncontainer) for host in itertools.islice(itertools.cycle(hosts), ncontainer): t = _create_task( consts.TASK_CREATE, version, host, 1, {}, 0, networks, spec_ips, data["entrypoint"], data["env"], image=data.get("image", ""), ) if not t: continue ts.append(t.id) keys.append(t.result_key) return {"r": 0, "msg": "ok", "tasks": ts, "watch_keys": keys}
def create_private(group_name, pod_name, appname): data = request.get_json() vstr = data['version'] group, pod, _, version = validate_instance(group_name, pod_name, appname, vstr) # TODO check if group has this pod ncore, nshare = pod.get_core_allocation(float(data['ncore'])) ports = data.get('ports', []) args = data.get('args', []) strategy = data.get('strategy', 'average') callback_url = data.get('callback_url', '') if callback_url and not is_strict_url(callback_url): abort(400, 'callback_url must start with http:// or https://') ncontainer = int(data['ncontainer']) if not ncontainer: abort(400, 'ncontainer must be > 0') networks = Network.get_multi(data.get('networks', [])) spec_ips = data.get('spec_ips', []) appconfig = version.appconfig entrypoint = data['entrypoint'] if entrypoint not in appconfig.entrypoints: abort(400, 'Entrypoint %s not in app.yaml' % entrypoint) hostname = data.get('hostname', '') host = hostname and Host.get_by_name(hostname) or None if host and not (host.group_id == group.id and host.pod_id == pod.id): abort(400, 'Host must belong to this pod and group') ts, keys = [], [] with rds.lock('%s:%s' % (group_name, pod_name)): host_cores = _get_strategy(strategy)(group, pod, ncontainer, ncore, nshare, host) if not host_cores: abort(400, 'Not enough core resources') for (host, container_count), cores in host_cores.iteritems(): t = _create_task( version, host, container_count, cores, nshare, networks, ports, args, spec_ips, entrypoint, data['env'], image=data.get('image', ''), callback_url=callback_url, ) if not t: continue host.occupy_cores(cores, nshare) ts.append(t.id) keys.append(t.result_key) return {'r': 0, 'msg': 'ok', 'tasks': ts, 'watch_keys': keys}
def _(*args, **kwargs): ags = inspect.getargspec(f) kw = dict(zip(ags.args, args)) kw.update(kwargs) with rds.lock(fmt.format(**kw)): return f(*args, **kwargs)
def create_private(group_name, pod_name, appname): """ncore: 需要的核心数, 可以是小数, 例如1.5个""" data = request.get_json() if data.get("raw", ""): vstr = consts.RAW_VERSION_PLACEHOLDER else: vstr = data["version"] group, pod, application, version = validate_instance(group_name, pod_name, appname, vstr) # TODO check if group has this pod core_require = int(float(data["ncore"]) * pod.core_share) # 是说一个容器要几个核... ncore = core_require / pod.core_share nshare = core_require % pod.core_share ncontainer = int(data["ncontainer"]) networks = Network.get_multi(data.get("networks", [])) spec_ips = data.get("spec_ips", []) appconfig = version.appconfig # 指定的host, 如果没有则按照编排分配host hostname = data.get("hostname", "") host = hostname and Host.get_by_name(hostname) or None if host and not (host.group_id == group.id and host.pod_id == pod.id): current_app.logger.error( "Host must belong to pod/group (hostname=%s, pod=%s, group=%s)", host, pod_name, group_name ) raise EruAbortException(consts.HTTP_BAD_REQUEST, "Host must belong to this pod and group") if not data["entrypoint"] in appconfig.entrypoints: current_app.logger.error( "Entrypoint not in app.yaml (entry=%s, name=%s, version=%s)", data["entrypoint"], appname, version.short_sha ) raise EruAbortException(consts.HTTP_BAD_REQUEST, "Entrypoint %s not in app.yaml" % data["entrypoint"]) ts, keys = [], [] with rds.lock("%s:%s" % (group_name, pod_name)): host_cores = group.get_free_cores(pod, ncontainer, ncore, nshare, spec_host=host) if not host_cores: current_app.logger.error( "Not enough cores (name=%s, version=%s, ncore=%s)", appname, version.short_sha, data["ncore"] ) raise EruAbortException(consts.HTTP_BAD_REQUEST, "Not enough core resources") for (host, container_count), cores in host_cores.iteritems(): t = _create_task( consts.TASK_CREATE, version, host, container_count, cores, nshare, networks, spec_ips, data["entrypoint"], data["env"], image=data.get("image", ""), ) if not t: continue host.occupy_cores(cores, nshare) ts.append(t.id) keys.append(t.result_key) return {"r": 0, "msg": "ok", "tasks": ts, "watch_keys": keys}
def create_private(group_name, pod_name, appname): """ncore: 需要的核心数, 可以是小数, 例如1.5个""" data = request.get_json() vstr = data['version'] group, pod, application, version = validate_instance(group_name, pod_name, appname, vstr) # TODO check if group has this pod core_require = int(float(data['ncore']) * pod.core_share) # 是说一个容器要几个核... ncore = core_require / pod.core_share nshare = core_require % pod.core_share ports = data.get('ports', []) args = data.get('args', []) ncontainer = int(data['ncontainer']) networks = Network.get_multi(data.get('networks', [])) spec_ips = data.get('spec_ips', []) entrypoint = data['entrypoint'] appconfig = version.appconfig strategy = data.get('strategy', 'average') # 指定的host, 如果没有则按照编排分配host hostname = data.get('hostname', '') host = hostname and Host.get_by_name(hostname) or None if host and not (host.group_id == group.id and host.pod_id == pod.id): current_app.logger.error('Host must belong to pod/group (hostname=%s, pod=%s, group=%s)', host, pod_name, group_name) raise EruAbortException(consts.HTTP_BAD_REQUEST, 'Host must belong to this pod and group') if not entrypoint in appconfig.entrypoints: current_app.logger.error('Entrypoint not in app.yaml (entry=%s, name=%s, version=%s)', entrypoint, appname, version.short_sha) raise EruAbortException(consts.HTTP_BAD_REQUEST, 'Entrypoint %s not in app.yaml' % entrypoint) route = appconfig.entrypoints[entrypoint].get('network_route', '') ts, keys = [], [] with rds.lock('%s:%s' % (group_name, pod_name)): if strategy == 'average': host_cores = average_schedule(group, pod, ncontainer, ncore, nshare, spec_host=host) elif strategy == 'centralized': host_cores = centralized_schedule(group, pod, ncontainer, ncore, nshare, spec_host=host) else: raise EruAbortException(consts.HTTP_BAD_REQUEST, 'strategy %s not supported' % strategy) if not host_cores: current_app.logger.error('Not enough cores (name=%s, version=%s, ncore=%s)', appname, version.short_sha, data['ncore']) raise EruAbortException(consts.HTTP_BAD_REQUEST, 'Not enough core resources') for (host, container_count), cores in host_cores.iteritems(): t = _create_task( version, host, container_count, cores, nshare, networks, ports, args, spec_ips, route, data['entrypoint'], data['env'], image=data.get('image', ''), ) if not t: continue host.occupy_cores(cores, nshare) ts.append(t.id) keys.append(t.result_key) return {'r': 0, 'msg': 'ok', 'tasks': ts, 'watch_keys': keys}