def rebuild_container_pool(): hosts = Host.query.all() for host in hosts: key = 'eru:agent:%s:containers:meta' % host.name rds.delete(key) for c in host.containers.all(): add_container_for_agent(c)
def fix_core(host): containers = Container.get_multi_by_host(host) # 没有的话, 直接销毁重建 if not containers: rds.delete(host._cores_key) _create_cores_on_host(host, host.ncore) return data = {str(i): host.core_share for i in xrange(host.ncore)} for c in containers: cores = c.cores nshare = int(cores.get("nshare", "0")) for e in cores.get("full", []): e.remain = host.core_share data.pop(e.label) for s in cores.get("part", []): s.remain = host.core_share - nshare data[s.label] -= nshare c.cores = cores rds.delete(host._cores_key) if data: rds.zadd(host._cores_key, **data) print "done", host
def kill_container(cid): c = Container.get_by_container_id(cid) if c: c.kill() key = consts.ERU_AGENT_DIE_REASON % c.container_id r = rds.get(key) rds.delete(key) if r is not None: c.set_props({'oom': 1}) current_app.logger.info('Kill container (container_id=%s)', cid[:7]) return {'r': 0, 'msg': consts.OK}
def _bind_container_ip(): rds.publish(pub_agent_vlan_key, '|'.join(values)) for _ in ips: rv = rds.blpop(feedback_key, 15) if rv is None: break succ = rv[1].split('|')[0] if succ == '0': break else: return True rds.delete(feedback_key) return False
def remove_containers(task_id, cids, rmi=False): task = Task.get(task_id) if not task: current_flask.logger.error('Task (id=%s) not found, quit', task_id) return current_flask.logger.info('Task<id=%s>: Start on host %s' % (task_id, task.host.ip)) notifier = TaskNotifier(task) containers = Container.get_multi(cids) container_ids = [c.container_id for c in containers if c] host = task.host version = task.version try: # flag, don't report these flags = {'eru:agent:%s:container:flag' % cid: 1 for cid in container_ids} rds.mset(**flags) for c in containers: remove_container_backends(c) current_flask.logger.info('Task<id=%s>: Container (cid=%s) backends removed', task_id, c.container_id[:7]) appnames = {c.appname for c in containers} publish_to_service_discovery(*appnames) time.sleep(3) dockerjob.remove_host_containers(containers, host) current_flask.logger.info('Task<id=%s>: Containers (cids=%s) removed', task_id, cids) if rmi: try: dockerjob.remove_image(task.version, host) except Exception as e: current_flask.logger.error('Task<id=%s>: Exception (e=%s), fail to remove image', task_id, e) except Exception as e: task.finish_with_result(consts.TASK_FAILED) notifier.pub_fail() current_flask.logger.error('Task<id=%s>: Exception (e=%s)', task_id, e) else: for c in containers: c.delete() task.finish_with_result(consts.TASK_SUCCESS) notifier.pub_success() if container_ids: rds.hdel('eru:agent:%s:containers:meta' % host.name, *container_ids) rds.delete(*flags.keys()) current_flask.logger.info('Task<id=%s>: Done', task_id) if not version.containers.count(): falcon_remove_alarms(version)
def _bind_container_ip_http(task_id, container, ips, nid=None): agent = get_agent(container.host) feedback_key = 'eru:agent:%s:feedback' % task_id ip_list = [(nid or ip.vlan_seq_id, ip.vlan_address) for ip in ips] agent.add_container_vlan(container.container_id, str(task_id), ip_list) for _ in ips: rv = rds.blpop(feedback_key, 15) if rv is None: break succ = rv[1].split('|')[0] if succ == '0': break else: return True rds.delete(feedback_key) return False
def _bind_container_ip_pubsub(task_id, container, ips, nid=None): pub_agent_vlan_key = 'eru:agent:%s:vlan' % container.host.name feedback_key = 'eru:agent:%s:feedback' % task_id values = [task_id, container.container_id] values += ['{0}:{1}'.format(nid or ip.vlan_seq_id, ip.vlan_address) for ip in ips] rds.publish(pub_agent_vlan_key, '|'.join(values)) for _ in ips: rv = rds.blpop(feedback_key, 15) if rv is None: break succ = rv[1].split('|')[0] if succ == '0': break else: return True rds.delete(feedback_key) return False
def _del_falcon(self): rds.delete("eru:falcon:version:%s:expression" % self.id)
def _set_falcon(self, expr_ids): rds.delete("eru:falcon:version:%s:expression" % self.id) for i in expr_ids: rds.sadd("eru:falcon:version:%s:expression" % self.id, i)
def create_containers_with_macvlan_public(task_id, ncontainer, nshare, network_ids, spec_ips=None): """ 执行task_id的任务. 部署ncontainer个容器, 绑定到networks这些子网 """ current_flask.logger.info('Task<id=%s>: Started', task_id) task = Task.get(task_id) if not task: current_flask.logger.error('Task (id=%s) not found, quit', task_id) return if spec_ips is None: spec_ips = [] networks = Network.get_multi(network_ids) notifier = TaskNotifier(task) host = task.host version = task.version entrypoint = task.props['entrypoint'] env = task.props['env'] # use raw image = task.props['image'] cpu_shares = 1024 pub_agent_vlan_key = 'eru:agent:%s:vlan' % host.name feedback_key = 'eru:agent:%s:feedback' % task_id cids = [] for _ in range(ncontainer): try: cid, cname = dockerjob.create_one_container(host, version, entrypoint, env, cores=None, cpu_shares=cpu_shares, image=image) except Exception as e: print e # 同上 continue if spec_ips: ips = [n.acquire_specific_ip(ip) for n, ip in zip(networks, spec_ips)] else: ips = [n.acquire_ip() for n in networks] ips = [i for i in ips if i] ip_dict = {ip.vlan_address: ip for ip in ips} if ips: ident_id = cname.split('_')[-1] values = [str(task_id), cid, ident_id] + ['{0}:{1}'.format(ip.vlan_seq_id, ip.vlan_address) for ip in ips] rds.publish(pub_agent_vlan_key, '|'.join(values)) for _ in ips: # timeout 15s rv = rds.blpop(feedback_key, 15) if rv is None: break # rv is like (feedback_key, 'succ|container_id|vethname|ip') succ, _, vethname, vlan_address = rv[1].split('|') if succ == '0': break ip = ip_dict.get(vlan_address, None) if ip: ip.set_vethname(vethname) else: current_flask.logger.info('Creating container (cid=%s, ips=%s)', cid, ips) c = Container.create(cid, host, version, cname, entrypoint, {}, env, nshare) for ip in ips: ip.assigned_to_container(c) notifier.notify_agent(cid) add_container_for_agent(c) add_container_backends(c) cids.append(cid) # 略过清理工作 continue # 清理掉失败的容器, 释放核, 释放ip current_flask.logger.info('Cleaning failed container (cid=%s)', cid) dockerjob.remove_container_by_cid([cid], host) [ip.release() for ip in ips] # 失败了就得清理掉这个key rds.delete(feedback_key) publish_to_service_discovery(version.name) task.finish_with_result(consts.TASK_SUCCESS, container_ids=cids) notifier.pub_success() current_flask.logger.info('Task<id=%s>: Done', task_id)
dockerjob.remove_host_containers(containers, host) current_flask.logger.info('Task<id=%s>: Containers (cids=%s) removed', task_id, cids) if rmi: dockerjob.remove_image(task.version, host) except Exception, e: task.finish_with_result(consts.TASK_FAILED) notifier.pub_fail() current_flask.logger.error('Task<id=%s>: Exception (e=%s)', task_id, e) else: for c in containers: c.delete() task.finish_with_result(consts.TASK_SUCCESS) notifier.pub_success() if container_ids: rds.srem('eru:agent:%s:containers' % host.name, *container_ids) rds.delete(*flags.keys()) current_flask.logger.info('Task<id=%s>: Done', task_id) @current_app.task() def create_containers_with_macvlan(task_id, ncontainer, nshare, cores, network_ids, spec_ips=None): """ 执行task_id的任务. 部署ncontainer个容器, 占用*_core_ids这些核, 绑定到networks这些子网 """ #TODO support part core current_flask.logger.info('Task<id=%s>: Started', task_id) task = Task.get(task_id) if not task: current_flask.logger.error('Task (id=%s) not found, quit', task_id) return if spec_ips is None:
def cores(self): rds.delete(self._cores_key)
def create_containers_with_macvlan(task_id, ncontainer, nshare, cores, network_ids, spec_ips=None): """ 执行task_id的任务. 部署ncontainer个容器, 占用*_core_ids这些核, 绑定到networks这些子网 """ current_flask.logger.info('Task<id=%s>: Started', task_id) task = Task.get(task_id) if not task: current_flask.logger.error('Task (id=%s) not found, quit', task_id) return if spec_ips is None: spec_ips = [] need_network = bool(network_ids) networks = Network.get_multi(network_ids) notifier = TaskNotifier(task) host = task.host version = task.version entrypoint = task.props['entrypoint'] env = task.props['env'] ports = task.props['ports'] args = task.props['args'] # use raw route = task.props['route'] image = task.props['image'] callback_url = task.props['callback_url'] cpu_shares = int(float(nshare) / host.pod.core_share * 1024) if nshare else 1024 pub_agent_vlan_key = 'eru:agent:%s:vlan' % host.name pub_agent_route_key = 'eru:agent:%s:route' % host.name feedback_key = 'eru:agent:%s:feedback' % task_id cids = [] for fcores, pcores in _iter_cores(cores, ncontainer): cores_for_one_container = {'full': fcores, 'part': pcores} try: cid, cname = dockerjob.create_one_container(host, version, entrypoint, env, fcores+pcores, ports=ports, args=args, cpu_shares=cpu_shares, image=image, need_network=need_network) except Exception as e: # 写给celery日志看 print e host.release_cores(cores_for_one_container, nshare) continue if spec_ips: ips = [n.acquire_specific_ip(ip) for n, ip in zip(networks, spec_ips)] else: ips = [n.acquire_ip() for n in networks] ips = [i for i in ips if i] ip_dict = {ip.vlan_address: ip for ip in ips} if ips: if ERU_AGENT_API == 'pubsub': values = [str(task_id), cid] + ['{0}:{1}'.format(ip.vlan_seq_id, ip.vlan_address) for ip in ips] rds.publish(pub_agent_vlan_key, '|'.join(values)) elif ERU_AGENT_API == 'http': agent = get_agent(host) ip_list = [(ip.vlan_seq_id, ip.vlan_address) for ip in ips] agent.add_container_vlan(cid, str(task_id), ip_list) for _ in ips: # timeout 15s rv = rds.blpop(feedback_key, 15) if rv is None: break # rv is like (feedback_key, 'succ|container_id|vethname|ip') succ, _, vethname, vlan_address = rv[1].split('|') if succ == '0': break ip = ip_dict.get(vlan_address, None) if ip: ip.set_vethname(vethname) if route: rds.publish(pub_agent_route_key, '%s|%s' % (cid, route)) else: current_flask.logger.info('Creating container (cid=%s, ips=%s)', cid, ips) c = Container.create(cid, host, version, cname, entrypoint, cores_for_one_container, env, nshare, callback_url) for ip in ips: ip.assigned_to_container(c) notifier.notify_agent(c) add_container_for_agent(c) add_container_backends(c) cids.append(cid) # 略过清理工作 continue # 清理掉失败的容器, 释放核, 释放ip current_flask.logger.info('Cleaning failed container (cid=%s)', cid) dockerjob.remove_container_by_cid([cid], host) host.release_cores(cores_for_one_container, nshare) [ip.release() for ip in ips] # 失败了就得清理掉这个key rds.delete(feedback_key) publish_to_service_discovery(version.name) task.finish_with_result(consts.TASK_SUCCESS, container_ids=cids) notifier.pub_success() # 有IO, 丢最后面算了 falcon_all_graphs(version) falcon_all_alarms(version) current_flask.logger.info('Task<id=%s>: Done', task_id)
def create_containers_with_macvlan(task_id, ncontainer, nshare, cores, network_ids, spec_ips=None): """ 执行task_id的任务. 部署ncontainer个容器, 占用*_core_ids这些核, 绑定到networks这些子网 """ # TODO support part core current_flask.logger.info("Task<id=%s>: Started", task_id) task = Task.get(task_id) if not task: current_flask.logger.error("Task (id=%s) not found, quit", task_id) return if spec_ips is None: spec_ips = [] networks = Network.get_multi(network_ids) notifier = TaskNotifier(task) host = task.host version = task.version entrypoint = task.props["entrypoint"] env = task.props["env"] # use raw image = task.props["image"] cpu_shares = int(float(nshare) / host.pod.core_share * 1024) if nshare else 1024 pub_agent_vlan_key = "eru:agent:%s:vlan" % host.name feedback_key = "eru:agent:%s:feedback" % task_id cids = [] full_cores, part_cores = cores.get("full", []), cores.get("part", []) for fcores, pcores in izip_longest( chunked(full_cores, len(full_cores) / ncontainer), chunked(part_cores, len(part_cores) / ncontainer), fillvalue=[], ): cores_for_one_container = {"full": fcores, "part": pcores} try: cid, cname = dockerjob.create_one_container( host, version, entrypoint, env, fcores + pcores, cpu_shares, image=image ) except: host.release_cores(cores_for_one_container, nshare) continue if spec_ips: ips = [n.acquire_specific_ip(ip) for n, ip in zip(networks, spec_ips)] else: ips = [n.acquire_ip() for n in networks] ips = [i for i in ips if i] ip_dict = {ip.vlan_address: ip for ip in ips} if ips: ident_id = cname.split("_")[-1] values = [str(task_id), cid, ident_id] + ["{0}:{1}".format(ip.vlan_seq_id, ip.vlan_address) for ip in ips] rds.publish(pub_agent_vlan_key, "|".join(values)) for _ in ips: # timeout 15s rv = rds.blpop(feedback_key, 15) if rv is None: break # rv is like (feedback_key, 'succ|container_id|vethname|ip') succ, _, vethname, vlan_address = rv[1].split("|") if succ == "0": break ip = ip_dict.get(vlan_address, None) if ip: ip.set_vethname(vethname) else: current_flask.logger.info("Creating container (cid=%s, ips=%s)", cid, ips) c = Container.create(cid, host, version, cname, entrypoint, cores_for_one_container, env, nshare) for ip in ips: ip.assigned_to_container(c) notifier.notify_agent(cid) add_container_for_agent(c) add_container_backends(c) cids.append(cid) # 略过清理工作 continue # 清理掉失败的容器, 释放核, 释放ip current_flask.logger.info("Cleaning failed container (cid=%s)", cid) dockerjob.remove_container_by_cid([cid], host) host.release_cores(cores_for_one_container, nshare) [ip.release() for ip in ips] # 失败了就得清理掉这个key rds.delete(feedback_key) publish_to_service_discovery(version.name) task.finish_with_result(consts.TASK_SUCCESS, container_ids=cids) notifier.pub_success() current_flask.logger.info("Task<id=%s>: Done", task_id)