一种包装openshift命令行工具以实现openshift模板的可重复自动部署的工具

ocdeployer的Python项目详细描述


ocdeployer

生成状态

一种包装openshift命令行工具以实现openshift模板可重复自动部署的工具。允许您更有效地基于模板重新创建环境。给定一组openshift模板,您可以创建一个简单的配置,允许您:

  • 重复地将相同的模板部署到不同的openshift项目中
  • 通过"阶段"定义它们的部署顺序
  • (可选)等待所有部署达到"活动"状态/等待生成成功,然后继续下一阶段
  • 定义应将哪些"图像"导入项目
  • 定义服务所依赖的机密,并从本地目录或openshift中的其他项目导入它们
  • 将模板拆分为"服务集",并部署所有集或特定集
  • 定义依赖项(例如:服务集"a"需要服务集"b")
  • 创建环境文件,这些文件定义了应在模板处理时设置的参数,因此您可以将相同的模板部署到不同的环境中
  • 在部署时指定多个环境文件并合并它们
  • 将openshift模板与jinja2模板一起使用
  • 如果需要更精细的控制,请在python中创建自定义的预部署/部署/部署后脚本
  • 快速扩展模板中定义的资源请求/限制。

需要openshift命令行工具(OC命令)

在部署之前,您应该登录到您的项目:

$oc登录https://api.myopenshift--token=********

入门

详细信息

ocdeployer依赖4条信息:

  • 一个模板目录(默认值:./templates)--它包含了openshift yaml/json模板以及特殊的配置文件(名为_cfg.yml)。您可以将模板拆分为称为服务集的文件夹,并在每个文件夹中定义一个负责部署特定服务集的cfg.yml。基本配置yml定义了所有服务集的部署顺序,以及所有服务所依赖的应导入的任何"全局"机密/映像。
  • 自定义脚本目录(默认值:./custom)。这是可选的。python脚本可以放在这里,可以在特定服务集的预部署/部署/部署后阶段运行。
  • 机密目录(默认值:./secrets)。这是可选的。包含机密或机密列表的openshift yaml文件可以放在这里。需要导入机密的服务集可以使用此目录中的机密。
  • 一个或多个环境文件。这是可选的。定义应传递给模板的参数。您可以在部署时在cli上指定多个环境文件,这些值将被合并。

请参阅示例以更好地了解所有这些配置是如何组合在一起的,并获取有关ocdeployer在运行部署时执行的操作的更多详细信息。

安装和使用

安装

您可以使用以下命令从pypi安装此软件包:

$ pip install ocdeployer

或者,如果您是从源安装,请安装到虚拟环境中:

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt

用法

(venv) $ ocdeployer -h
Usage: ocdeployer [OPTIONS] COMMAND [ARGS]...

  Deploys components to a given cluster. NOTE: You need the openshift cli
  tool ('oc') installed and to login to your openshift cluster before
  running the tool.

Options:
  -h, --help  Show this message and exit.

Commands:
  cache        Used for updating or deleting local template cache
  deploy       Deploy to project
  list-routes  List routes currently in the project
  list-sets    List service sets available in template dir
  process      Process templates but do not deploy
  wipe         Delete everything from project

部署命令

deploy命令用于将模板推入openshift。

命令用法:

(venv) $ ocdeployer deploy -h
Usage: ocdeployer deploy [OPTIONS] DST_PROJECT

  Deploy to project

Options:
  -a, --all                   Deploy all service sets
  -s, --sets TEXT             Comma,separated,list of specific service set
                              names to deploy
  -p, --pick TEXT             Pick a single component from a service set and
                              deploy that.  E.g. '-p myset/myvm'
  -k, --skip TEXT             Comma,separated,list of service_set/service_name
                              to skip
  -e, --env-file TEXT         Path to parameters config file (default: None).
                              Use this option multiple times to concatenate
                              config files
  -t, --template-dir TEXT     Template directory (default 'templates')
  --scale-resources FLOAT     Factor to scale configured cpu/memory resource
                              requests/limits by
  -f, --no-confirm            Do not prompt for confirmation
  --secrets-local-dir TEXT    Import secrets from local files in a directory
                              (default 'secrets')
  --secrets-src-project TEXT  Openshift project to import secrets from
                              (default: secrets)
  -i, --ignore-requires       Ignore the 'requires' statement in config files
                              and deploy anyway
  -u, --custom-dir TEXT       Specify custom deploy scripts directory (default
                              'custom')
  -l, --label TEXT            Adds a label to each deployed resource.  E.g.
                              '-l app=test'
  -h, --help                  Show this message and exit.

过程命令

使用process查看模板数据而不实际部署它。流程dep有非常相似的选项loy,但它不会将任何配置推送到openshift,而是使用jinja2和openshift模板(即,oc process)解析模板,替换给定的变量/项目名称等,然后将生成的配置打印到stdout或保存结果模板文件到目录。

擦除命令

使用wipe从项目中删除对象。它实际上运行以下命令,删除所有对象或具有特定标签的对象:

oc delete all [--all or --selector mylabel=myvalue]
oc delete configmap [--all or --selector mylabel=myvalue]
oc delete secret [--all or --selector mylabel=myvalue]
oc delete pvc [--all or --selector mylabel=myvalue]

列出路由命令

使用列出路由仅打印项目中活动路由的URL

列表设置命令

使用列表集只需打印模板目录中可用于部署的服务集的名称。

缓存命令

cache命令提供了一种快捷方式,可将git模板存储库存储在本地应用程序缓存文件夹中。如果缓存已初始化且文件夹存在,ocdeployer将此文件夹用作其默认位置,而不是当前工作目录。

该实现使用appdirscache文件夹,因此…

  • 默认模板目录变成:
    • Linux:/home/<;用户名>;/.cache/ocdeployer/templates
    • Mac:/users/username/library/application support/ocdeployer/templates
  • 默认脚本目录变成:
    • Linux:/home/<;用户名>;/.cache/ocdeployer/custom
    • Mac:/users/username/library/application support/ocdeployer/custom
  • 默认机密目录变成:
    • Linux:/home/<;用户名>;/.cache/ocdeployer/secrets
    • Mac:/users/username/library/application support/ocdeployer/secrets

注意这些默认值仅在存在ocdeployer的缓存目录时使用。


模板配置

解释模板配置工作原理的最佳方法是描述配置服务集的过程。

创建服务集的指南
  1. 模板目录中为您的服务集创建一个新目录,例如"myservice"

  2. 在此目录中为您的服务添加openshift yaml文件。这些文件应该是openshift模板文件,其中包含运行服务所需的所有资源(除了机密和图像流,我们稍后将讨论…)。这通常是像buildconfigdeploymentconfig服务路由等内容。

  3. 在您的目录中创建一个"_cfg.yml"文件。此配置文件的内容解释如下:

    requires:# Here you can list other service sets that need to be deployed before this one can.# Deployment will fail when processing this file if we see the required service set has# not yet been deployed.-"myotherservice"secrets:# Lists which secrets these services rely on; they will be imported# A secret is only imported once, so if other services rely on the same secret# it won't be imported again.-"mysecret"# Indicates that there is a pre_deploy/post_deploy/deploy method defined for this# service set that should be usedcustom_deploy_logic:true# Indicates how long post-deploy logic should take before timeout in sec# A null value can be handled differently depending on the post deploy logic,# but it is recommended that it means there is "no waiting" that will occurpost_deploy_timeout:300images:# Lists the images these services require# key: the name used in the openshift config# value: the full image "pull uri" to pass to 'oc import-image'## We check to make sure that an image with this name exists before importing it.# If it already exists, it will not be re-imported.cp-kafka:"confluentinc/cp-kafka"cp-zookeeper:"confluentinc/cp-zookeeper"nginx-stable-openshift:"docker.io/mhuth/nginx-stable-openshift"python-36-centos7:"centos/python-36-centos7"postgresql-95-rhel7:"registry.access.redhat.com/rhscl/postgresql-95-rhel7"deploy_order:# Lists the order in which components should be deployedstage0:# A stage deploys a set of components at a time# By default, at the end of each stage, we wait for all new DeploymentConfig's # to reach "active", and build configurations to successfully build, unless you set 'wait' to False as seen below...# Stages are sorted by their name, and processed.wait:Falsecomponents:-"zookeeper"stage1:# You can specify a wait timeout. By default, 300sec is usedtimeout:600components:# Components lists the templates that should be deployed in this stage.# They are created in openshift in the same order they are listed.# A component can be a template file, or it can be a folder of more templates-"kafka"-"inventory-db"stage2:components:-"insights-inventory"-"upload-service"
  4. 如果将"自定义部署逻辑"设置为true,请阅读下面的"自定义部署逻辑"。

  5. 将服务文件夹名称添加到templates目录中的base_cfg.yml中。请记住,部署顺序指定每个服务集的部署顺序。因此,如果您的服务集依赖于首先部署的其他服务,请适当订购它!

  6. 运行"ocdeployer list sets"(ocdeployer列表集),您将看到新组件作为可部署服务集列出。

自定义部署逻辑

默认情况下,不运行pre_deploy/post_deploy,部署逻辑由ocdeployer.deploy.deploy_components方法处理。因此,除非您正在做一些复杂的事情,并且需要额外的"python脚本"来处理您的(前/后)部署逻辑,否则您不需要担心自定义逻辑(而且很少需要重新编写主部署逻辑本身)

但是假设您希望在部署组件之前或在部署组件之后执行一些任务ts.自定义脚本对此很有用。

您可以在服务集的_cfg.yml中将自定义部署逻辑设置为true。然后,您应该在项目的custom目录中创建一个自定义部署脚本,该脚本的名称与您的服务集匹配——例如deploy myservice.py。在这个脚本中,您可以定义3个方法:

defpre_deploy(project_name,template_dir,variables_for_component):
  • 项目名称:字符串,正在部署到的项目的名称
  • 模板目录:string,正在部署的服务集模板目录的完整路径
  • variables\u for_component:dict,keys是服务集中的每个组件名,values是另一个dict,由从env.yml文件中解析的变量组成。
defdeploy(project_name,template_dir,components,variables_for_component,wait,timeout,resources_scale_factor,label):
  • 项目名称:字符串,正在部署到的项目的名称
  • 模板目录:string,正在部署的服务集模板目录的完整路径
  • 组件:字符串列表,正在部署的服务集中的组件名称
  • variables\u for_component:dict,keys是服务集中的每个组件名,values是另一个dict,由从env.yml文件中解析的变量组成。
  • wait:布尔值,用于确定部署逻辑是否应等待deploymentconfig和buildconfig等操作"完成"(分别为"活动"或"成功生成"。
  • 超时:int,超时前等待多长时间
  • 资源缩放因子:float,运行ocdeployer时传递给--scale resources的值
  • 标签:string,部署时附加到open shift中每个对象的标签
defpost_deploy(processed_templates,project_name,template_dir,variables_for_component):
  • 已处理的模板:dict,其中包含已部署的每个组件的已处理模板信息--keys:模板名称,vals:ocdeployer.templates.template的实例
  • 项目名称:字符串,正在部署到的项目的名称
  • 模板目录:string,正在部署的服务集模板目录的完整路径
  • variables\u for_component:dict,keys是服务集中的每个组件名,values是另一个dict,由从env.yml文件中解析的变量组成。

在编写自定义部署逻辑时,ocdeployer.common中的大部分代码可能对您很有用(例如用于运行oc命令的oc方法)。

示例1

假设在部署组件之后,您希望在您推送的任何生成配置上触发生成(实际上,默认情况下,只要您的服务集\u cfg.yml--中的舞台上wait不是false而是play along,这种情况在每个舞台之后都会发生。

您可以定义如下所示的部署后方法:

$ pip install ocdeployer
0

示例2

假设项目更新中的任何configmaps都希望触发部署的新卷展栏。您可以通过在部署之前跟踪configmap的状态并将其与部署之后的状态进行比较来完成此操作。

$ pip install ocdeployer
1

秘密

默认情况下,ocdeployer将尝试从openshift中的项目secrets导入机密,并在/secrets本地目录中查找机密。您还可以使用--secrets src project将机密从openshift中的其他项目复制到项目中,或使用--secrets local dir从目录中的openshift配置文件加载机密。

任何放置在secrets local dir中的.yaml/.json文件都将被解析,并从中提取和导入机密。 文件可以包含单个机密或资源列表

一个EXAmpe secret.yaml配置:

$ pip install ocdeployer
2

如何从项目导出机密,以便以后与--secrets local dir一起使用?

您可以使用以下命令导出所有内容:

$ pip install ocdeployer
3

或导出单个机密对象:

$ pip install ocdeployer
4

在下一个项目部署中使用机密文件:

$ pip install ocdeployer
5

环境文件

默认情况下,ocdeployer在部署时将以下参数传递给模板:

  • "namespace"对应于在cli上选择的项目名称。
  • "secrets_project"对应于在cli上选择的secrets src project(默认值:"secrets")

您还可以定义一个包含更多自定义变量信息的"环境"文件。下面是一个示例:

$ pip install ocdeployer
6

这允许您在全局级别、在"每个服务集"级别或在"服务集内的每个组件"级别定义变量。您也可以在"更细粒度级别"中重写同名变量。如果openshift模板的"参数"部分没有定义变量,则在处理时将跳过该变量。这允许您在全局级别定义变量, 但不一定要在每个模板中将每个变量定义为参数。

使用-e--env file命令行选项在运行时选择环境文件,例如:

$ pip install ocdeployer
7

您可以定义多个环境文件并在部署时合并它们。例子:

环境1.yaml

$ pip install ocdeployer
8

环境2.yaml

$ pip install ocdeployer
9

运行以下命令:

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt
0

结果env1.yaml和env2.yaml被合并。由于env2在后面的列表中列出,此文件中的任何匹配参数项都将覆盖env1的参数项。结果是一个值文件,看起来像:

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt
1

常用

列出可供部署的服务集:

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt
2

从"mysecretsprroject"部署平台、引擎和导入机密的示例:

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt
3

您可以使用--scale resources标志来缩放所有资源的CPU/内存请求/限制:

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt
4

这将使模板中任何配置的资源请求/限制乘以所需的因子。如果 您尚未配置请求/限制,将不会对其进行缩放。如果按0缩放,则资源 配置将从所有项中完全删除,从而导致默认的kubernetes限制 要启动的范围。

使用以下工具删除项目中的所有内容(这意味着几乎所有内容,因此请小心):

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt
5

您还可以删除与特定标签匹配的所有内容:

$ python3 -m venv .venv
$ . .venv/bin/activate
(venv) $ pip install -r requirements.txt
6

已知问题/需要改进

  • 脚本当前在退出前检查以确保部署已移动到"活动"状态,但是,它们不会修复任何"挂起"或"卡住"的生成/部署。很快就要开始了…
  • 目前没有办法"强制"配置,即删除模板中未列出的内容。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java Spring REST API JSON在postman中显示,但在测试中未“看到”   java如何以编程方式检查旋转设备?   java力穷举开关   amazon web services DynamoDB从Java更新列表失败   java Android studio IDE更改波斯注释字体   java如何将postgresql添加到OpenShift Spring应用程序?   java RecycleView分页是否在末尾添加新项?   如何通过java获取移动设备信息?   java为什么Apache Commons StringEscapeUtils escapeXML10不能在groovy中工作?   函数式编程java curry现有的静态函数   java与JList的合作