def _get_host(id_or_name): if id_or_name.isdigit(): host = Host.get(id_or_name) else: host = Host.get_by_name(id_or_name) if not host: abort(404, 'Host %s not found' % id_or_name) return host
def create_private(): data = request.get_json() pod, _, version = _get_instances(**data) 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 = [ipam.get_pool(n) for n in 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 task_ids, watch_keys = [], [] host_cores = _get_strategy(strategy)(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) task_ids.append(t.id) watch_keys.append(t.result_key) return {'tasks': task_ids, 'watch_keys': watch_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 rm_containers(group_name, pod_name, appname): data = request.get_json() group, pod, application, version = validate_instance(group_name, pod_name, appname, data['version']) host = Host.get_by_name(data['host']) # 直接拿前ncontainer个吧, 反正他们都等价的 containers = host.get_containers_by_version(version)[:int(data['ncontainer'])] try: cids = [c.id for c in containers] task_props = {'container_ids': cids} task = Task.create(code.TASK_REMOVE, version, host, task_props) remove_containers.apply_async( args=(task.id, cids, False), task_id='task:%d' % task.id ) return {'r': 0, 'msg': 'ok', 'task': task.id, 'watch_key': task.result_key} except Exception, e: logger.exception(e) return {'r': 1, 'msg': str(e), 'task': None, 'watch_key': None}
def _get_host(id_or_name): if id_or_name.isdigit(): return Host.get(id_or_name) return Host.get_by_name(id_or_name)
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 list_host_containers(host_name): host = Host.get_by_name(host_name) if not host: raise EruAbortException(code.HTTP_NOT_FOUND, 'Host %s not found' % host_name) containers = host.list_containers(g.start, g.limit) return {'r': 0, 'msg': code.OK, 'containers': containers}
def cure_host(host_name): host = Host.get_by_name(host_name) if host: host.cure() return {'r': 0, 'msg': code.OK}
def kill_host(host_name): host = Host.get_by_name(host_name) if host: host.kill() return {'r': 0, 'msg': code.OK}
def get_host_by_name(host_name): host = Host.get_by_name(host_name) if not host: raise EruAbortException(code.HTTP_NOT_FOUND, 'Host %s not found' % host_name) return host
def _get_host(id_or_name): host = Host.get(id_or_name) or Host.get_by_name(id_or_name) if not host: abort(404, 'Host %s not found' % id_or_name) return host
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}