您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)利用python怎么將docker-compose.yaml導(dǎo)入到數(shù)據(jù)庫中,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
實現(xiàn)將docker-compose.yaml導(dǎo)入相關(guān)的數(shù)據(jù)庫條目的功能,利用python腳本,yaml、sqlalchemy、json等模塊實現(xiàn)。
docker-compose.yaml
_version: "1" services: webapp: build: context: ./dir dockerfile: Dockerfile-alternate args: buildno: 1
python deme文件。
#!/usr/bin/env python # -*- coding: utf-8 -*- import yaml import traceback from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy import Column, DateTime, Index, Integer, String, Text, text, BigInteger from sqlalchemy.ext.declarative import declarative_base import datetime import json import base64 """ 作用:將docker-compose.yaml導(dǎo)入到數(shù)據(jù)庫的docker-compose表中。 docker-compose表創(chuàng)建語句: CREATE TABLE `docker_compose` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `services` text NOT NULL COMMENT '@desc services', `creator` int(11) DEFAULT '0', `modifier` int(11) DEFAULT '0', `gmt_created` datetime NOT NULL, `gmt_modified` datetime NOT NULL, `_version` varchar(64) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1092 DEFAULT CHARSET=utf8 """ def now(): return datetime.datetime.now() BASE = declarative_base() class DockerCompose(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) def get_session(db_connection): """ db connection api :param db_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) return Session() def load_yaml(filename): try: with open(filename, 'r') as fh: return yaml.safe_load(fh) except (IOError, yaml.YAMLError) as e: raise Exception(u"{}".format(traceback.format_exc())) def import_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() def main(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的編寫
當我們打包好了鏡像的時候,一個一個區(qū)整理的時候,也是很浪費功夫,尤其是我們修改了一些代碼的時候
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: "python3 main.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
看完上述內(nèi)容,你們對利用python怎么將docker-compose.yaml導(dǎo)入到數(shù)據(jù)庫中有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。