前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >harbor源码分析之配置文件生成脚本解析(三)

harbor源码分析之配置文件生成脚本解析(三)

原创
作者头像
暮雨
修改2018-10-23 11:13:32
6420
修改2018-10-23 11:13:32
举报
文章被收录于专栏:云端漫步云端漫步

脚本骨架

在harbor第二篇中介绍了安装脚本install.sh,在一个脚本中有一step是执行prepare脚本.这个脚本主要是用来生成配置文件的.在本篇中,将对这一脚本进行展开.

通过对脚本简单的分析,可以得出以下的模型

prepare
prepare

通过这个脚本分析,得到如上的模型

根据程序提供的模版文件和用户配置的参数文件harbor.cfg,生成各个组件启动的环境变量配置文件文件以及nginx的配置文件.

代码语言:txt
复制
common tree
.
└── templates
    ├── adminserver
    │   └── env
    ├── db
    │   └── env
    ├── jobservice
    │   ├── app.conf
    │   └── env
    ├── nginx
    │   ├── nginx.http.conf
    │   ├── nginx.https.conf
    │   ├── notary.server.conf
    │   └── notary.upstream.conf
    ├── notary
    │   ├── mysql-initdb.d
    │   │   ├── initial-notaryserver.sql
    │   │   └── initial-notarysigner.sql
    │   ├── notary-signer-ca.crt
    │   ├── notary-signer.crt
    │   ├── notary-signer.key
    │   ├── server-config.json
    │   ├── signer-config.json
    │   └── signer_env
    ├── registry
    │   ├── config.yml
    │   └── root.crt
    └── ui
        ├── app.conf
        ├── env
        └── private_key.pem

脚本中的代码都为了这样的一个结果进行处理的.下边对代码一些内容进行展开.

脚本解析

python版本判断

代码语言:txt
复制
import sys
if sys.version_info[0] == 2:
    print "version 2"
if sys.version_info[0] == 3:
    print "version 3"

删除文件

代码语言:javascript
复制
import sys
import os

def delfile(src):
    if os.isfile(src):
        os.remove(src)
    elif os.isdir(src):
        for item in src:
            itemsrc = os.path.join(src, item)
            delfile(itemsrc)

delfile(os.path.join(os.path.dirname(__file__), "test"))

命令行参数

代码语言:python
复制
import argparse  

parser = argparse.ArgumentParser() 
parser.add_argument('--conf', dest='cfgfile',default=base_dir+'/hello.cfg',type=str,help="the path of Harbor configuration file") 
parser.add_argument('--with-notary', dest='notary_mode',default=False, action='store_true', help="the Harbor instance is to be deployed with notary") 
args = parser.parse_args() 
print args.cfgfile

配置文件

代码语言:javascript
复制
import StringIO
import ConfigParser
import os
from io import open

conf = StringIO.StringIO()
conf.write("[configuration]\n")
conf.write(open(args.cfgfile).read())
conf.seek(0, os.SEEK_SET)
rcp = ConfigParser.RawConfigParser()
rcp.readfp(conf)

模版文件

代码语言:python
复制
def render(src, dest, **kw):
    t = Template(open(src, 'r').read())
    with open(dest, 'w') as f:
        f.write(t.substitute(**kw))
    print("Generated configuration file: %s" % dest)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 脚本骨架
  • 脚本解析
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档