Problemas intermitentes do apt-get install ao criar uma AMI usando o Packer

2

Estou tentando criar uma AMI com base em ami-3b054701 na região ap-southeast-2 . A configuração do Packer possui um provisionador simples:

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

Isso às vezes funciona sem problemas, mas às vezes falha com esse erro:

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

Eu perdi um dia inteiro para encontrar uma maneira de evitar isso. Existe uma solução sugerida no Stack Exchange mas é mais um hack do que uma solução. Existe uma maneira de evitar que esse erro ocorra de forma intermitente?

Aqui está a saída completa do 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
    
por Behrang 19.08.2015 / 14:11

3 respostas

2

Provavelmente, você se deparou com uma condição de corrida com o cloud-init. Esta edição do empacotador fornece algumas ideias, e eu tentei a seguinte sugestão dada pelo OP com sucesso:

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'
    
por Wil Tan 10.04.2016 / 19:31
0

Pedi à equipe do servidor para dar uma olhada nisso e, enquanto eles resolvem uma resposta, posso dizer que a solução no StackExchange se refere a problemas de "Incompatibilidade de soma de hash" que não são vistos em sua saída.

Por alguma razão, os metadados do arquivo não parecem corresponder aos pacotes que você está pedindo para instalar (que estão no main), mas o apt sabe que outros pacotes dependem deles, então está dizendo que dizer isso não acha que você é louco também. ; -)

    
por rcj 23.08.2015 / 18:51
0

Isso ainda parece ser um problema. Resolvi isso criando uma cópia da imagem padrão do Ubuntu e colocando um wrapper do apt-get nele que aguardará as instalações existentes serem concluídas antes de continuar (em vez de falhar).

Etapas para resolver

  1. Faça login no console da AWS e navegue até o painel do EC2.
  2. Crie uma nova instância com a imagem do Ubuntu que você deseja.
  3. SSH na nova instância e coloque o novo script apt-get (veja abaixo) em /usr/local/sbin/apt-get
  4. Torne-o executável: sudo chmod 755 /usr/local/sbin/apt-get
  5. Verifique se /usr/local/sbin/ está na frente da variável PATH em /etc/environment
  6. Logout da máquina.
  7. De volta aos contras da AWS
    1. Pare a instância do ES2: Ações > Estado da Instância > Pare
    2. Crie uma nova imagem nesta instância: Ações > Imagem > Criar imagem
  8. Use a AMI gerada para todas as novas instâncias.

Script: apt-get

Script adaptado deste

#!/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 "$@"
    
por Jeremy Gillick 24.05.2017 / 21:43