instack

instack是一个用来在当前系统中执行diskimage-builder格式的elements的工具。elements最初是在diskimage-builder中出现的,diskimage-builder是一个制作镜像的工具,因为定制镜像,需要安装各种各样的东西,elements就是它抽象出来的一个个功能的集合,这有点类似于ansible中role的概念,需要在镜像中加入什么功能,那么指定相应的element就可以了,diskimage-builder按顺序执行elements中的脚本,从而定制镜像,因此elements是可以分发的,每个人都可以写自己的elements,然后供别人使用。

这种抽象机制非常不错,因此也被应用到tripleo中,但是并没有使用diskimage-builder来执行elements,而是使用instack来执行,instack底层又是使用的dib-run-parts工具来执行的,并且加上了自己的一些逻辑。在每一个element中,都按照约定定义了相同的hook,如extra-data, pre-install, install, post-install,在每一个hook中放置了一些脚本,这些脚本的名称上都进行了编号,在instack执行elements时,先把所有elements中相同hook中的脚本放到同一个hook下,然后由dib-run-parts去依次执行每一个hook中的脚本,编号靠前的就先执行,通过这种机制,每个elements可以控制自己hook中的脚本执行的顺序,举例如下:

比如有两个elements,每个elements有两个hook,每个hook又有两个脚本:

  1. .
  2. ├── element1
  3. ├── hook1
  4. ├── 01-scritp
  5. └── 03-scritp
  6. └── hook2
  7. ├── 06-script
  8. └── 08-script
  9. └── element2
  10. ├── hook1
  11. ├── 02-script
  12. └── 04-script
  13. └── hook2
  14. ├── 05-script
  15. └── 07-script

在instack执行hook之前,先要进行合并,如下:

  1. .
  2. ├── hook1
  3. ├── 01-scritp
  4. ├── 02-scritp
  5. ├── 03-scritp
  6. └── 04-scritp
  7. └── hook2
  8. ├── 05-scritp
  9. ├── 06-scritp
  10. ├── 07-scritp
  11. └── 08-scritp

然后使用dib-run-parts依次去执行某个hook下的脚本,instack可以指定执行哪些hook,没有被指定的将会被跳过。instack是一个相对底层的工具,在tripleo中,被封装在instack-undercloud中,在部署undercloud时被用到。

instack使用方法如下:

  1. [stack@undercloud ~]$ instack -h
  2. usage: instack [-h] [-e [ELEMENT [ELEMENT ...]]]
  3. [-p ELEMENT_PATH [ELEMENT_PATH ...]] [-k [HOOK [HOOK ...]]]
  4. [-b [BLACKLIST [BLACKLIST ...]]]
  5. [-x [EXCLUDE_ELEMENT [EXCLUDE_ELEMENT ...]]] [-j JSON_FILE]
  6. [-d] [-i] [--dry-run] [--no-cleanup] [-l LOGFILE]
  7. Execute diskimage-builder elements on the current system.
  8. optional arguments:
  9. -h, --help show this help message and exit
  10. -e [ELEMENT [ELEMENT ...]], --element [ELEMENT [ELEMENT ...]]
  11. element(s) to execute
  12. -p ELEMENT_PATH [ELEMENT_PATH ...], --element-path ELEMENT_PATH [ELEMENT_PATH ...]
  13. element path(s) to search for elements (ELEMENTS_PATH
  14. environment variable will take precedence if defined)
  15. -k [HOOK [HOOK ...]], --hook [HOOK [HOOK ...]]
  16. hook(s) to execute for each element
  17. -b [BLACKLIST [BLACKLIST ...]], --blacklist [BLACKLIST [BLACKLIST ...]]
  18. script names, that if found, will be blacklisted and
  19. not run
  20. -x [EXCLUDE_ELEMENT [EXCLUDE_ELEMENT ...]], --exclude-element [EXCLUDE_ELEMENT [EXCLUDE_ELEMENT ...]]
  21. element names that will be excluded from running even
  22. if they are listed as dependencies
  23. -j JSON_FILE, --json-file JSON_FILE
  24. read runtime configuration from json file
  25. -d, --debug Debugging output
  26. -i, --interactive If set, prompt to continue running after a failed
  27. script.
  28. --dry-run Dry run only, don't actually modify system, prints out
  29. what would have been run.
  30. --no-cleanup Do not cleanup tmp directories
  31. -l LOGFILE, --logfile LOGFILE
  32. Logfile to log all actions

可以在命令行中直接指定elements,和相应的hook来执行,如下:

  1. sudo -E instack \
  2. -p /usr/share/tripleo-image-elements /usr/share/diskimage-builder/elements \
  3. -e fedora base keystone mariadb \
  4. -k extra-data pre-install install post-install \
  5. -b 15-remove-grub 10-cloud-init 05-fstab-rootfs-label

也可以将这些选项全都配置在一个json格式的配置文件中,直接指定这些配置文件就可以了,如下:

  1. sudo -E instack \
  2. -p /usr/share/tripleo-image-elements /usr/share/diskimage-builder/elements \
  3. -j /usr/share/instack-undercloud/json-files/centos-7-undercloud-packages.json

centos-7-undercloud-packages.json文件的内容如下:

  1. [
  2. {
  3. "name": "Installation",
  4. "element": [
  5. "install-types",
  6. "undercloud-install",
  7. "enable-packages-install",
  8. "element-manifest",
  9. "puppet-stack-config"
  10. ],
  11. "hook": [
  12. "extra-data",
  13. "pre-install",
  14. "install",
  15. "post-install"
  16. ],
  17. "exclude-element": [
  18. "pip-and-virtualenv",
  19. "os-collect-config",
  20. "svc-map",
  21. "pip-manifest",
  22. "package-installs",
  23. "pkg-map",
  24. "puppet",
  25. "cache-url",
  26. "dib-python",
  27. "os-svc-install",
  28. "install-bin"
  29. ],
  30. "blacklist": [
  31. "99-refresh-completed"
  32. ]
  33. }
  34. ]