From 50f894a01da7580c0d0d344b3d37a4da080fd443 Mon Sep 17 00:00:00 2001 From: pycook Date: Mon, 11 Nov 2019 11:27:43 +0800 Subject: [PATCH] add command init-cache --- api/Dockerfile | 9 ++++++--- api/commands/click_cmdb.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 api/commands/click_cmdb.py diff --git a/api/Dockerfile b/api/Dockerfile index 9f11108..b1b65c5 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -2,7 +2,7 @@ # maybe use: cd .. FROM centos:7.6.1810 AS cmdb-api -LABEL description="Python2.7.5,cmdb" +LABEL description="Python3.7.5,cmdb" RUN mkdir -p /data/apps/cmdb /data/apps/cmdb/logs @@ -12,6 +12,9 @@ WORKDIR /data/apps/cmdb RUN yum install -y epel-release && yum clean all\ && yum install -y python-pip \ - && pip install --no-cache-dir -r docs/requirements.txt + && pip install --no-cache-dir -r docs/requirements.txt \ + && cp ./api/settings.py.example ./api/settings.py \ + && sed -i "s#{user}:{password}@127.0.0.1:3306/{db}#cmdb:123456@mysql:3306/cmdb#g" api/settings.py \ + && sed -i "s/127.0.0.1/redis/g" api/settings.py -CMD ["bash", "-c", "cp api/settings.py.example api/settings.py && sed -i 's#{user}:{password}@127.0.0.1:3306/{db}#:@127.0.0.1:3306/cmdb#g' api/settings.py && flask run"] +CMD ["bash", "-c", "flask run"] diff --git a/api/commands/click_cmdb.py b/api/commands/click_cmdb.py new file mode 100644 index 0000000..443a465 --- /dev/null +++ b/api/commands/click_cmdb.py @@ -0,0 +1,24 @@ +# -*- coding:utf-8 -*- + + +import json + +import click +from flask.cli import with_appcontext + +import api.lib.cmdb.ci +from api.extensions import rd +from api.models.cmdb import CI + + +@click.command() +@with_appcontext +def init_cache(): + cis = CI.get_by(to_dict=False) + for ci in cis: + m = api.lib.cmdb.ci.CIManager() + ci = m.get_ci_by_id_from_db(ci.id, need_children=False, use_master=False) + if rd.get([ci.id]): + return + rd.delete(ci.id) + rd.add({ci.id: json.dumps(ci)})