介绍
今天就跟大家聊聊有关利用python怎么将docker-compose.yaml导入到数据库中,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
实现将docker-compose.yaml导入相关的数据库条目的功能,利用python脚本,yaml sqlalchemy, json等模块实现。
docker-compose。yaml
_version:,“1”; 服务: webapp才能: ,才能构建: ,,,背景:。/dir ,,,dockerfile: Dockerfile-alternate ,,,arg游戏: ,,,,buildno:, 1
python同类群文件。
# !/usr/bin/env python #,- *安康;编码:utf-8 - * - , import yaml import 回溯 得到sqlalchemy import  create_engine 得到sqlalchemy.orm import  sessionmaker 得到sqlalchemy import 列,,DateTime,,指数,整数,字符串,,文本,文本、先导入BigInteger。 得到sqlalchemy.ext.declarative import  declarative_base import datetime import json import base64 , “““ 作用:将docker-compose.yaml导入到数据库的docker-compose表中。 docker-compose表创建语句: ,CREATE TABLE “docker_compose”, ( ,“id”bigint (20), unsigned NOT NULL AUTO_INCREMENT, ,“服务”text NOT NULL COMMENT & # 39; @desc 服务# 39; ,“创造者”int (11), DEFAULT & # 39; 0 & # 39; ,修饰符的int (11), DEFAULT & # 39; 0 & # 39; ,‘gmt_created datetime NOT 空, ,‘gmt_modified datetime NOT 空, ,‘_version varchar (64), DEFAULT 空, ,PRIMARY KEY (id) ),引擎=InnoDB AUTO_INCREMENT=1092, DEFAULT CHARSET=utf8 “““ , , def 现在(): return 才能;datetime.datetime.now () , , 时间=BASE declarative_base () , , class DockerCompose(基础): 时间=__tablename__ 才能;& # 39;docker_compose& # 39; , id 才能=,列(BigInteger, primary_key=True) services 才能=,列(文本,nullable=False) _version 才能=,列(字符串(64)) creator 才能=,列(整数,,server_default=文本(“& # 39;0 & # 39;“)) modifier 才能=,列(整数,,server_default=文本(“& # 39;0 & # 39;“)) gmt_created 才能=,列(DateTime, nullable=False) gmt_modified 才能=,列(DateTime, nullable=False) , , def get_session (db_connection): “才能”;“ db 才能;connection  api :才能param db_connection: ,,:返回: “才能”;“ 时间=DB_URL 才能;& # 39;mysql + pymysql://% & # 39;, %, (db_connection,) engine 才能=,create_engine (DB_URL, pool_size=25日,pool_recycle=3600,,回声=False) Session 才能=,sessionmaker(绑定=引擎) return 才能;会话() , , def load_yaml(文件名): 尝试才能: ,,,with 开放(文件名,& # 39;" # 39;),as fh: ,,,,,return yaml.safe_load(跳频) except 才能;(IOError,那么,yaml.YAMLError), as e: , ,,,raise 例外(u" {}“.format (traceback.format_exc ())) , , def import_engine_compose (instance_config, dbConnection): 时间=services 才能;instance_config[& # 39;服务# 39;】 _version 才能=,instance_config.get (& # 39; _version& # 39;) 时间=session 才能;get_session (dbConnection) , 时间=docker_compose 才能;DockerCompose ( ,,,gmt_created=datetime.datetime.now () .strftime (“Y % - % - % d % H: % m: % S"), ,,,gmt_modified=datetime.datetime.now () .strftime (“Y % - % - % d % H: % m: % S"), ,,,的创造者=999, ,,,修饰符=999, ,,,_version=_version, ,,,服务=json.dumps(服务) ,,) , session.add才能(docker_compose) session.commit才能() , , def 主要(dbConnection, filepath=癲ocker_compose.yaml"): 时间=docker_compose_config 才能;load_yaml (filepath) import_engine_compose才能(docker_compose_config, dbConnection) , , if __name__ ==, & # 39; __main__ # 39;: dbConnection =,才能“xxx" , 时间=DB_CONNECTION 才能;base64.b64decode (dbConnection) 主要的才能(DB_CONNECTION)
<强>补充:docker-compose。yaml的编写强>
当我们打包好了镜像的时候,一个一个区整理的时候,也是很浪费功夫,尤其是我们修改了一些代码的时候