python 如何把docker-compose.yaml导入到数据库相关条目里
实现将docker-compose.yaml导入相关的数据库条目的功能,利用python脚本,yaml、sqlalchemy、json等模块实现。
docker-compose.yaml
_version:"1" services: webapp: build: context:./dir dockerfile:Dockerfile-alternate args: buildno:1
pythondeme文件。
#!/usr/bin/envpython #-*-coding:utf-8-*- importyaml importtraceback fromsqlalchemyimportcreate_engine fromsqlalchemy.ormimportsessionmaker fromsqlalchemyimportColumn,DateTime,Index,Integer,String,Text,text,BigInteger fromsqlalchemy.ext.declarativeimportdeclarative_base importdatetime importjson importbase64 """ 作用:将docker-compose.yaml导入到数据库的docker-compose表中。 docker-compose表创建语句: CREATETABLE`docker_compose`( `id`bigint(20)unsignedNOTNULLAUTO_INCREMENT, `services`textNOTNULLCOMMENT'@descservices', `creator`int(11)DEFAULT'0', `modifier`int(11)DEFAULT'0', `gmt_created`datetimeNOTNULL, `gmt_modified`datetimeNOTNULL, `_version`varchar(64)DEFAULTNULL, PRIMARYKEY(`id`) )ENGINE=InnoDBAUTO_INCREMENT=1092DEFAULTCHARSET=utf8 """ defnow(): returndatetime.datetime.now() BASE=declarative_base() classDockerCompose(BASE): __tablename__='docker_compose' id=Column(BigInteger,primary_key=True) services=Column(Text,nullable=False) _version=Column(String(64)) creator=Column(Integer,server_default=text("'0'")) modifier=Column(Integer,server_default=text("'0'")) gmt_created=Column(DateTime,nullable=False) gmt_modified=Column(DateTime,nullable=False) defget_session(db_connection): """ dbconnectionapi :paramdb_connection: :return: """ DB_URL='mysql+pymysql://%s'%(db_connection,) engine=create_engine(DB_URL,pool_size=25,pool_recycle=3600,echo=False) Session=sessionmaker(bind=engine) returnSession() defload_yaml(filename): try: withopen(filename,'r')asfh: returnyaml.safe_load(fh) except(IOError,yaml.YAMLError)ase: raiseException(u"{}".format(traceback.format_exc())) defimport_engine_compose(instance_config,dbConnection): services=instance_config['services'] _version=instance_config.get('_version') session=get_session(dbConnection) docker_compose=DockerCompose( gmt_created=datetime.datetime.now().strftime("%Y-%m-%d%H:%M:%S"), gmt_modified=datetime.datetime.now().strftime("%Y-%m-%d%H:%M:%S"), creator=999, modifier=999, _version=_version, services=json.dumps(services) ) session.add(docker_compose) session.commit() defmain(dbConnection,filepath="docker_compose.yaml"): docker_compose_config=load_yaml(filepath) import_engine_compose(docker_compose_config,dbConnection) if__name__=='__main__': dbConnection="xxx" DB_CONNECTION=base64.b64decode(dbConnection) main(DB_CONNECTION)
补充:docker-compose.yaml的编写
当我们打包好了镜像的时候,一个一个区整理的时候,也是很浪费功夫,尤其是我们修改了一些代码的时候
version:"2" services: mysql: image:mysql:mysql hostname:mysql ports: -"3306:3306" volumes: -"/data/mysql:/var/lib/mysql" restart:always backend: image:backend:0.5 command:"python3main.py" hostname:backend volumes: -"/data/files:/app/files" network_mode:"host" depends_on: -mysql restart:always frontend: image:frontend:0.5 hostname:frontend volumes: -"/Users/gaven/Desktop/anji/frontend/web/nginx.conf:/etc/nginx/nginx.conf" -"/Users/gaven/Desktop/anji/frontend/web/default.conf:/etc/nginx/conf.d/default.conf" ports: -"80:80" network_mode:"host" restart:always
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。