aws-codedeploy-agent icon indicating copy to clipboard operation
aws-codedeploy-agent copied to clipboard

CodeDeploy agent install fails with Could not get lock /var/lib/dpkg/lock

Open chriskinsman opened this issue 8 years ago • 11 comments

I am trying to install the agent on Ubuntu 16.04 LTS

Like this:

wget https://aws-codedeploy-us-west-2.s3.amazonaws.com/latest/install chmod +x ./install sudo ./install auto

It fails to install. Looking at /tmp/codedeploy-agent.update.log I see that it is failing to install gdebi with this message:

Could not get lock /var/lib/dpkg/lock

This is running during cloud-init.

If I run it manually once cloud-init is done it installs fine.

chriskinsman avatar Jan 05 '17 22:01 chriskinsman

I think I worked around this.

If I manually do a:

apt-get update apt-get install -y -q gdebi-core

prior to running the codedeploy agent install it runs to completion.

chriskinsman avatar Jan 05 '17 23:01 chriskinsman

Even with this I get occasional failures like this from /tmp/codedeploy-agent.update.log

I, [2017-01-08T20:09:18.898661 #2373] INFO -- : Downloading package from bucket aws-codedeploy-us-west-2 and key releases/codedeploy-agent_1.0-1.1067_all.deb... I, [2017-01-08T20:09:19.004921 #2373] INFO -- : Executing /usr/bin/gdebi -n -o Dpkg::Options::=--force-c onfdef -o Dkpg::Options::=--force-confold /tmp/codedeploy-agent_1.0-1.1067_all.tmp-20170108-2373-n4ed0f.de b...

^MReading state information... Done E, [2017-01-08T20:09:22.220315 #2373] ERROR -- : Error installing /tmp/codedeploy-agent_1.0-1.1067_all.tmp-20170108-2373-n4ed0f.deb.

chriskinsman avatar Jan 08 '17 20:01 chriskinsman

The host agent has auto-update functions, and it'll periodically check from the s3 bucket, and see if there is a new version of host agent has been released or not. The codedeploy-agent.update.log is mainly for this logging information. Are you running the host agent on one of the EC2 instances? If it's public, do you mind share with me the instance AMI? The log doesn't provide much information, I'd like to try and see if I can reproduce. Or is there any more logs before/after the ERROR message?

Thanks, Binbin

feverLu avatar Jan 18 '17 22:01 feverLu

This is a new Ubuntu 16.04 LTS instance installing codedeploy-agent during cloud-init.

It does not currently have the agent on it.

Is the host agent the codedeploy-agent or something different?

chriskinsman avatar Jan 19 '17 04:01 chriskinsman

Hi,

The install script and the updater download a deb and try to install it. So something else is already running some apt operations, the installation will fail with the "could not get lock" error.

As far as as the updater is concerned, if it fails to install the deb, it will try again an hour later. But as far as failures in the cloud init script is concerned, you need to find out what is holding the lock on the /var/lib/dpkg/lock

Thanks, Amartya Datta Gupta

amartyag avatar Jan 25 '17 00:01 amartyag

This is the initial install. So when it fails there is nothing to run and update later.

The lock is not taken by anything in my script. It must be something that Ubuntu 16.04 is updating behind the schenes or something pre-installed in the AMI updating.

Finally got past the issue with this trick:

until service codedeploy-agent status >/dev/null 2>&1; do
   sleep 60
    rm -f install
    wget https://aws-codedeploy-us-west-2.s3.amazonaws.com/latest/install
    chmod +x ./install
    sudo ./install auto
    service codedeploy-agent restart
done

Seems like I shouldn't have to do the above. The install script should just handle it.

chriskinsman avatar Feb 04 '17 02:02 chriskinsman

This is a good suggestion. I will add a feature backlog for the install script to wait out the locks on the package management. Thanks a lot for the input!

Thanks, Amartya

amartyag avatar Mar 15 '17 20:03 amartyag

@amartyag is this feature added?, I'm having the exact same problem with Ubuntu 16.04 I will try @chriskinsman suggestion

libert-xyz avatar Jun 24 '17 14:06 libert-xyz

just fyi: stumbled across a similar issue. Could only be solved by following instructions @chriskinsman provided. Took me 3 hours to figure out sigh :)

madsem avatar Nov 14 '17 14:11 madsem

This is also an issue when running aws opsworks register, which installs the opsworks agent (very similar process as the codedeploy agent). I think a better approach is to check the lock file before running the installer (avoids the download and all the other stuff the script does before it actually runs dpkg).

Here's my bash function that checks the lock file before running the installer:

#!/bin/bash

function wait_for_dpkg_lock {
  # check for a lock on dpkg (another installation is running)
  lsof /var/lib/dpkg/lock > /dev/null
  dpkg_is_locked="$?"
  if [ "$dpkg_is_locked" == "0" ]; then
    echo "Waiting for another installation to finish"
    sleep 5
    wait_for_dpkg_lock
  fi
}

wait_for_dpkg_lock

# now it's safe to run the codedeploy install script or `aws opsworks register`

dknell avatar Mar 22 '18 03:03 dknell

is this not fixed yet???

alok2k5ingh avatar Mar 02 '22 07:03 alok2k5ingh