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

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

我正在尝试根据区域创建 AMI。Packerami-3b054701配置ap-southeast-2有一个简单的配置程序:

#!/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"

有时这可以正常工作,但有时会失败并出现以下错误:

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

我浪费了整整一天的时间才找到避免这种情况的方法。有一个建议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

答案1

最有可能的是,您遇到了 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'

答案2

就我的情况来说,在前面加上这段文字/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 }}'"
    }
]

答案3

这似乎仍然是一个问题。我通过创建库存 Ubuntu 映像的副本并在其上放置一个 apt-get 包装器来解决这个问题,该包装器将等待现有安装完成后再继续(而不是失败)。

解决步骤

  1. 登录 AWS 控制台并导航到 EC2 仪表板。
  2. 使用您想要的 Ubuntu 映像创建一个新实例。
  3. 通过 SSH 进入新实例并将新apt-get脚本(见下文)放在/usr/local/sbin/apt-get
  4. 使其可执行:sudo chmod 755 /usr/local/sbin/apt-get
  5. 确保/usr/local/sbin/位于 PATH 变量的前面/etc/environment
  6. 退出机器。
  7. 回顾 AWS 的弊端
    1. 停止 ES2 实例:操作 > 实例状态 > 停止
    2. 从此实例创建新图像:操作 > 图像 > 创建图像
  8. 对所有新实例使用生成的 AMI。

脚本:apt-get

剧本改编自此

#!/bin/bash

#
# Wait for the package manager locks to be released before trying to call apt-get
#

i=0
tput sc

while fuser /var/lib/dpkg/lock >/dev/null 2>&1 || fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do
    case $(($i % 4)) in
        0 ) j="-" ;;
        1 ) j="\\" ;;
        2 ) j="|" ;;
        3 ) j="/" ;;
    esac
    tput rc
    echo -en "\r[$j] Waiting for other software managers to finish..."
    sleep 0.5
    ((i=i+1))
done

/usr/bin/apt-get "$@"

答案4

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

由于某种原因,档案元数据似乎与您要求安装的软件包(位于主软件包中)不匹配,但 apt 知道其他软件包依赖于它们,所以它告诉您这一点,表明它也不认为您疯了。;-)

相关内容