图像发生器
image-generator的Python项目详细描述
一个基于python的lxc图像生成器。
它将从一个yaml文件中读取一个配置,启动一个容器 因此,复制并运行特定的脚本,最后创建 LXC图像。
先决条件
sudo apt update && sudo apt install -y python3-pip lxd
安装之后,您需要配置lxd环境。 根据所需的图像和脚本,容器可能需要 Internet连接,因此请确保为 您的容器:
sudo lxd init Name of the storage backend to use (dir or zfs)[default=dir]: Would you like LXD to be available over the network (yes/no)[default=no]? yes Address to bind LXD to (not including port)[default=all]: Port to bind LXD to [default=8443]: Trust password for new clients: Again: Do you want to configure the LXD bridge (yes/no)[default=yes]? Do you want to setup an IPv4 subnet? Yes Default values apply for next questions Do you want to setup an IPv6 subnet? No
安装图像生成器工具
安装方式:
pip3 install image-generator
如何使用
可以用两种方式运行它。single action或 config file。配置文件是一个yaml文件,根目录上包含 要使用某些参数按顺序执行的操作的列表。
每个action都有下面列出的特定参数 章节
配置
您至少已经下载了一个lxc图像,可以在 本地LXC图像存储
lxc image list
如果没有,可以使用 以下命令:
lxc image copy ubuntu:16.04 local:
注意下一步需要的指纹。
配置文件应该如下所示:
connect:url:< The URL with port where to reach lxd engine ># Mandatorytrust-password:< The trust password you have set for the lxd environment ># Mandatorycreate-container:container-name:< The name of the container which will be created > # default:"image-generator"container-image-fingerprint:< The fingeprint of the image which will be used as base image for the container ># Mandatory; you do not need the complete image fingerprint, the one shown by lxc image list is enoughcopy-files:file-tarball:< Path to the tar archive containing all scripts you want to push on the image > # default:"./etc/files.tar"file-dest:< Path where to copy the content of the tar archive on the container ># default /root/files.tarexecute-script:script:< Which script to be executed ># Mandatoryclean-tmp-files:< remove the temporary files used for copying the tarball on the container> # default:False# lxc always assumes you are in /root, thus take care if you use relative paths to the scripts herecreate-image:destination:< Path of the folder where the image will be saved > # default:"/tmp"alias:<additional alias to give to the created image> # default:"Publishedbyimage-generator"name:<name of the result image># generated-image# if the destination does not yet contain the ending tar.gz it will be added automaticallyclean:container:< remove the container used for creating the lxc image> # default:Trueimage-store:< remove the image created from the container from your local image store> # default:True# You can (re)import the images anytime by lxc image import < Your path to the desired image.tar.gz > --alias < Your Alias here >
运行
注意如果在运行图像生成器时遇到此错误:
Traceback (most recent call last): File "/usr/bin/pip3", line 11, in <module> sys.exit(main()) File "/usr/lib/python3/dist-packages/pip/__init__.py", line 215, in main locale.setlocale(locale.LC_ALL, '') File "/usr/lib/python3.5/locale.py", line 594, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting
然后,作为快速修复,您可以运行:
exportLC_ALL=C
查看帮助
image-generator --help usage: image-generator [-h][-f FILE][-d][-action ACTION][-params PARAMS][-dry] optional arguments: -h, --help show this help message and exit -f FILE, --file FILE the file scenario with the action to execute -d, --debug show debug prints -action ACTION The action to execute -params PARAMS The parameters to the action -dry Run dryrun
然后用sudo运行:
sudo image-generator -f <PATH-TO-THE-CONFIGURATION-FILE>
sudo权限是必需的,因为它是 正在提取从lxd下载的图像。
测试它
对于测试,可以通过运行进行干运行:
sudo image-generator -f <PATH-TO-THE-CONFIGURATION-FILE> -dry --debug
它将执行除安装脚本之外的每个步骤,最后 同时删除下载的图像。
卸载
卸载方式:
pip3 uninstall image-generator