使用 Packer 创建 AMI 时出现间歇性 apt-get 安装问题

Beh*_*ang 8 server apt aws amazon-ec2 14.04

我正在尝试基于ami-3b054701ap-southeast-2区域创建一个 AMI 。Packer 配置有一个简单的配置器:

#!/bin/bash -e

info() {
    echo -e "[INFO] $1\n"
}

exec_cmd() {
    echo -e "\n[INFO] $1\n"
    eval $1
}

exec_cmd "apt-get update"

exec_cmd "apt-get -y upgrade"

exec_cmd "apt-get -y install build-essential git zip unzip wget default-jre"
Run Code Online (Sandbox Code Playgroud)

这有时没有问题,但有时会因此错误而失败:

ami: [INFO] apt-get -y install build-essential git zip unzip wget default-jre
ami:
ami: Reading package lists... Done
ami: Building dependency tree
ami: Reading state information... Done
ami: Package build-essential is not available, but is referred to by another package.
ami: This may mean that the package is missing, has been obsoleted, or
ami: is only available from another source
ami:
ami: Package default-jre is not available, but is referred to by another package.
ami: This may mean that the package is missing, has been obsoleted, or
ami: is only available from another source
ami:
ami: Package zip is not available, but is referred to by another package.
ami: This may mean that the package is missing, has been obsoleted, or
ami: is only available from another source
ami:
ami: E: Package 'build-essential' has no installation candidate
ami: E: Package 'zip' has no installation candidate
ami: E: Package 'default-jre' has no installation candidate
Run Code Online (Sandbox Code Playgroud)

我浪费了一整天的时间来寻找避免这种情况的方法。Stack Exchange 上有一个建议的解决方案,但它更像是一种破解而不是解决方案。有没有办法防止此错误间歇性发生?

这是 Packer 的完整输出

==> ami: Prevalidating AMI Name...
==> ami: Inspecting the source AMI...
==> ami: Creating temporary keypair: packer 55d33324-e319-bd49-24c5-c06a18b4fe09
==> ami: Launching a source AWS instance...
    ami: Instance ID: i-8b78cd55
==> ami: Waiting for instance (i-8b78cd55) to become ready...
==> ami: Waiting for SSH to become available...
==> ami: Connected to SSH!
==> ami: Provisioning with shell script: scripts/provision.sh
    ami:
    ami: [INFO] apt-get update
    ami:
    ami: Ign http://security.ubuntu.com trusty-security InRelease
    ami: Ign http://archive.ubuntu.com trusty InRelease
    ami: Get:1 http://security.ubuntu.com trusty-security Release.gpg [933 B]
    ami: Ign http://archive.ubuntu.com trusty-updates InRelease
    ami: Get:2 http://security.ubuntu.com trusty-security Release [63.5 kB]
    ami: Hit http://archive.ubuntu.com trusty Release.gpg
    ami: Get:3 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
    ami: Hit http://archive.ubuntu.com trusty Release
    ami: Get:4 http://archive.ubuntu.com trusty-updates Release [63.5 kB]
    ami: Get:5 http://security.ubuntu.com trusty-security/main amd64 Packages [333 kB]
    ami: Get:6 http://security.ubuntu.com trusty-security/restricted amd64 Packages [8875 B]
    ami: Hit http://archive.ubuntu.com trusty/main amd64 Packages
    ami: Get:7 http://security.ubuntu.com trusty-security/universe amd64 Packages [114 kB]
    ami: Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
    ami: Hit http://archive.ubuntu.com trusty/universe amd64 Packages
    ami: Get:8 http://security.ubuntu.com trusty-security/multiverse amd64 Packages [3686 B]
    ami: Hit http://archive.ubuntu.com trusty/multiverse amd64 Packages
    ami: Get:9 http://security.ubuntu.com trusty-security/main Translation-en [181 kB]
    ami: Hit http://archive.ubuntu.com trusty/main Translation-en
    ami: Hit http://security.ubuntu.com trusty-security/multiverse Translation-en
    ami: Hit http://archive.ubuntu.com trusty/multiverse Translation-en
    ami: Hit http://security.ubuntu.com trusty-security/restricted Translation-en
    ami: Hit http://archive.ubuntu.com trusty/restricted Translation-en
    ami: Get:10 http://security.ubuntu.com trusty-security/universe Translation-en [66.6 kB]
    ami: Hit http://archive.ubuntu.com trusty/universe Translation-en
    ami: Get:11 http://archive.ubuntu.com trusty-updates/main amd64 Packages [605 kB]
    ami: Get:12 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [15.3 kB]
    ami: Get:13 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [308 kB]
    ami: Get:14 http://archive.ubuntu.com trusty-updates/multiverse amd64 Packages [11.9 kB]
    ami: Get:15 http://archive.ubuntu.com trusty-updates/main Translation-en [292 kB]
    ami: Hit http://archive.ubuntu.com trusty-updates/multiverse Translation-en
    ami: Hit http://archive.ubuntu.com trusty-updates/restricted Translation-en
    ami: Get:16 http://archive.ubuntu.com trusty-updates/universe Translation-en [163 kB]
    ami: Fetched 2232 kB in 16s (134 kB/s)
    ami: Reading package lists... Done
    ami:
    ami: [INFO] apt-get -y upgrade
    ami:
    ami: Reading package lists... Done
    ami: Building dependency tree
    ami: Reading state information... Done
    ami: Calculating upgrade... Done
    ami: The following packages have been kept back:
    ami: linux-headers-generic linux-headers-virtual linux-image-virtual
    ami: linux-virtual
    ami: 0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.
    ami:
    ami: [INFO] apt-get -y install build-essential git zip unzip wget default-jre
    ami:
    ami: Reading package lists... Done
    ami: Building dependency tree
    ami: Reading state information... Done
    ami: Package build-essential is not available, but is referred to by another package.
    ami: This may mean that the package is missing, has been obsoleted, or
    ami: is only available from another source
    ami:
    ami: Package default-jre is not available, but is referred to by another package.
    ami: This may mean that the package is missing, has been obsoleted, or
    ami: is only available from another source
    ami:
    ami: Package zip is not available, but is referred to by another package.
    ami: This may mean that the package is missing, has been obsoleted, or
    ami: is only available from another source
    ami:
    ami: E: Package 'build-essential' has no installation candidate
    ami: E: Package 'zip' has no installation candidate
    ami: E: Package 'default-jre' has no installation candidate
==> ami: Terminating the source AWS instance...
==> ami: No AMIs to cleanup
Run Code Online (Sandbox Code Playgroud)

小智 14

最有可能的是,您在使用 cloud-init 时遇到了竞争条件。这个封隔器问题提供了一些见解,我已经成功地尝试了 OP 给出的以下建议:

echo "waiting 180 seconds for cloud-init to update /etc/apt/sources.list"
timeout 180 /bin/bash -c \
  'until stat /var/lib/cloud/instance/boot-finished 2>/dev/null; do echo waiting ...; sleep 1; done'
Run Code Online (Sandbox Code Playgroud)


use*_*932 6

就我而言,将文本添加/usr/bin/cloud-init status --wait &&到该execute_command人前面解决了问题,

json 文件:

"provisioners" : [
    {
        "type" : "shell",
        "script" : "./setup.sh",
        "execute_command" : "/usr/bin/cloud-init status --wait && \
            sudo -E -S sh '{{ .Path }}'"
    }
]
Run Code Online (Sandbox Code Playgroud)


小智 0

我已经要求服务器团队看一下这个问题,当他们找到答案时,我可以说 StackExchange 上的解决方案涉及“哈希和不匹配”问题,这些问题在您的输出中看不到。

由于某种原因,存档元数据似乎与您要求安装的软件包(位于 main 中)不匹配,但 apt 知道其他软件包依赖于它们,因此它告诉您,它不认为你也疯了。;-)