0

I am trying to setup CI using AWS CodeDeploy and CircleCI. Right now I am stuck at the step where AWS CodeDeploy should copy stuff into EC2 and run scripts. But somehow CircleCI tells me something is wrong. Does anyone know what might be happening? Thanks.

the appspec.yml is:

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu
hooks:
  BeforeInstall:
    - location: scripts/setup.sh
      timeout: 3800
      runas: root
  ApplicationStart:
    - location: scripts/start.sh
      timeout: 3800
      runas: root

and setup.sh is:

#!/bin/bash
sudo apt-get install nodejs npm
npm install

in the above code I also tried only apt-get install nodejs npm but it's still nor working.

the error message in /var/log/aws/codedeploy-agent/codedeploy-agent.log is as follows:

2015-10-22 08:02:54 ERROR [codedeploy-agent(1314)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Error during 
 perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location:
./scripts/setup.sh run as user root failed with exit code 127 - /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:150:in `execute_script'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:107:in `block (2 levels) in execute'
......
Shih-Min Lee
  • 9,350
  • 7
  • 37
  • 67

2 Answers2

1

Exit code 127 generally means that the OS couldn't find something required to execute the command. In this case it could be either the script wasn't at the expected path or /bin/bash doesn't exist (unlikely).

Check that the archive being produced by your build process is actually putting your scripts in the archive where your appspec expects them. scripts/setup.sh needs to be in that exact path within your archive.

You can also look at what the agent actually got by checking the deployment archive for your deployment: /opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id/deployment-archive to make sure the archive is being extracted correctly.

Jonathan Turpie
  • 1,343
  • 10
  • 16
  • right now the situation is when I take away the hooks part in appspec.yml I can see all the files are extracted to the instance. (just it won't run) But if I keep the hooks part in there I won't see anything being extracted somehow.. – Shih-Min Lee Oct 22 '15 at 23:56
  • BeforeInstall run after teh new archive is downloaded but before the `files` section of the appspec is executed. So the files won't be copied anywhere if it failed on a BeforeInstall script. What you need to check is that the script is actually in the archive bundle at the path specified in the appspec. – Jonathan Turpie Oct 23 '15 at 19:20
  • I downloaded the revision and extracted the files and I can see all the scripts are inside the folder. My guess is something with script running permission because when I do `sudo apt-get install nodejs` it would still have permission issues even if I specify `runas: root` in the circle.yml – Shih-Min Lee Oct 24 '15 at 03:20
  • I also updated permission from git so all the downloaded code has 755 permission. somehow it still says `InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Error during perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location: scripts/setup.sh run as user root failed with exit code 127 - /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:150:in `execute_script'` – Shih-Min Lee Oct 26 '15 at 03:32
1

Do the following steps for the debugging:

  • in the CodeDeploy error log /var/log/aws/codedeploy-agent/codedeploy-agent.log there is a line that says Error during perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location: scripts/setup.sh failed with exit code 1. So from the error log I know the problems might be from this script.

  • In the above mentioned script setup.sh, put something like this at the beginning of the script:

exec 3>&1 4>&2 trap 'exec 2>&4 1>&3' 0 1 2 3 exec 1>/home/ubuntu/out.log 2>&1

This logs the entire error outputs for you.

  • Permission issues

It's also possible that EC2 failed to execute those scripts, you need to make sure those files have at least 755 permissions when copied to your instance. So you need to specify 755 file mode for your scripts.

How to change the File Mode on GitHub?

Also in appspec.yml you need can specify a runas directive. Could be ubuntu or root or whatever that gives you the correct permission.

  • miscellaneous

Some pitfalls on deploying like when you do sudo apt-get install nodejs there will be intermediate steps that ask if you want to install packages and used disk spaces and you have to type Y or N to proceed installation. those scripts would hang there and timeout resulting in failed deployment. So instead you do

sudo apt-get -y install nodejs npm

Or in your setup.sh script maybe you have

chmod -R 777 public

but it's possible CodeDeploy is executing this code in a folder that's different than your project root. So make sure all the paths are valid.

Community
  • 1
  • 1
Shih-Min Lee
  • 9,350
  • 7
  • 37
  • 67