如何在 cloud-init 中的用户脚本之前执行一些命令

如何在 cloud-init 中的用户脚本之前执行一些命令

我正在尝试使用多部分配置运行一些脚本和配置。

Content-Type: multipart/mixed; boundary="MIMEBOUNDARY"
MIME-Version: 1.0
--MIMEBOUNDARY
Content-Disposition: attachment; filename="cloud-config.cfg"
Content-Transfer-Encoding: 7bit
Content-Type: text/cloud-config
Mime-Version: 1.0

package_upgrade: true
packages:
  - jq
  - docker

runcmd:
  - service docker start

--MIMEBOUNDARY
Content-Disposition: attachment; filename="00-rancher_server_install"
Content-Transfer-Encoding: 7bit
Content-Type: text/x-shellscript
Mime-Version: 1.0

#!/bin/bash

# Pull required images
docker pull rancher/server:v1.0.2

--MIMEBOUNDARY--

问题在于脚本的执行顺序... - 首先 yum 安装脚本 - 然后是 bash 脚本 - 然后是runcmd脚本

那么当执行用户脚本命令时,docker 服务还没有启动

我在 EC2 上使用 Amazon Linux AMI,它是 CentOS...

这是云初始化日志

Cloud-init v. 0.7.6 running 'modules:config' at Tue, 21 Jun 2016 01:30:44 +0000. Up 25.32 seconds.
Loaded plugins: priorities, update-motd, upgrade-helper
Metadata Cache Created
Loaded plugins: priorities, update-motd, upgrade-helper
No packages needed for security; 26 packages available
Resolving Dependencies
Loaded plugins: priorities, update-motd, upgrade-helper
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:1.11.1-1.2.amzn1 will be installed
--> Processing Dependency: xfsprogs for package: docker-1.11.1-1.2.amzn1.x86_64
---> Package jq.x86_64 0:1.5-1.2.amzn1 will be installed
--> Processing Dependency: jq-libs(x86-64) = 1.5-1.2.amzn1 for package: jq-1.5-1.2.amzn1.x86_64
--> Processing Dependency: oniguruma for package: jq-1.5-1.2.amzn1.x86_64
--> Processing Dependency: libjq.so.1()(64bit) for package: jq-1.5-1.2.amzn1.x86_64
--> Processing Dependency: libonig.so.2()(64bit) for package: jq-1.5-1.2.amzn1.x86_64
--> Running transaction check
---> Package jq-libs.x86_64 0:1.5-1.2.amzn1 will be installed
---> Package oniguruma.x86_64 0:5.9.1-3.1.2.amzn1 will be installed
---> Package xfsprogs.x86_64 0:3.2.2-2.20.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch         Version                  Repository          Size
================================================================================
Installing:
 docker          x86_64       1.11.1-1.2.amzn1         amzn-updates        17 M
 jq              x86_64       1.5-1.2.amzn1            amzn-updates        44 k
Installing for dependencies:
 jq-libs         x86_64       1.5-1.2.amzn1            amzn-updates       121 k
 oniguruma       x86_64       5.9.1-3.1.2.amzn1        amzn-main          149 k
 xfsprogs        x86_64       3.2.2-2.20.amzn1         amzn-main          1.7 M

Transaction Summary
================================================================================
Install  2 Packages (+3 Dependent packages)

Total download size: 19 M
Installed size: 60 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                               11 MB/s |  19 MB  00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : oniguruma-5.9.1-3.1.2.amzn1.x86_64                           1/5
  Installing : jq-libs-1.5-1.2.amzn1.x86_64                                 2/5
  Installing : xfsprogs-3.2.2-2.20.amzn1.x86_64                             3/5
  Installing : docker-1.11.1-1.2.amzn1.x86_64                               4/5
  Installing : jq-1.5-1.2.amzn1.x86_64                                      5/5
  Verifying  : docker-1.11.1-1.2.amzn1.x86_64                               1/5
  Verifying  : oniguruma-5.9.1-3.1.2.amzn1.x86_64                           2/5
  Verifying  : jq-1.5-1.2.amzn1.x86_64                                      3/5
  Verifying  : jq-libs-1.5-1.2.amzn1.x86_64                                 4/5
  Verifying  : xfsprogs-3.2.2-2.20.amzn1.x86_64                             5/5

Installed:
  docker.x86_64 0:1.11.1-1.2.amzn1           jq.x86_64 0:1.5-1.2.amzn1

Dependency Installed:
  jq-libs.x86_64 0:1.5-1.2.amzn1        oniguruma.x86_64 0:5.9.1-3.1.2.amzn1
  xfsprogs.x86_64 0:3.2.2-2.20.amzn1

Complete!
Cloud-init v. 0.7.6 running 'modules:final' at Tue, 21 Jun 2016 01:31:55 +0000. Up 96.28 seconds.
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon. Is the docker daemon running on this host?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Jun 21 01:31:55 cloud-init[2485]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/00-rancher_server_install [1]
Starting cgconfig service: [  OK  ]
Starting docker:    .[  OK  ]
Jun 21 01:31:56 cloud-init[2485]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Jun 21 01:31:56 cloud-init[2485]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Cloud-init v. 0.7.6 finished at Tue, 21 Jun 2016 01:31:56 +0000. Datasource DataSourceEc2.  Up 97.56 seconds

答案1

来自 irc #cloud-init 的 @smoser 帮助了我

查看/var/lib/cloud/instance/scripts/ 您会看到一个文件被命名runcmd ,并且多部分中的其他文件都由它们的文件名命名。

该目录中的脚本按 C 语言环境排序顺序运行(就像通过 runparts 运行一样)

因此,如果您将所添加的脚本的名称更改为, filename="00-rancher_server_install" 那么 filename="zz-rancher_server_install" 就很好了。

相关内容