5

I am running Ubuntu on an EC2 instance and trying to set up code deploy to run scripts to provision the deployment in appspec.yml. However, these hook scripts do not seem to be running. When I check the code deploy error logs, I see the error message InstanceAgent::Plugins::CodeDeployPlugin::HookExecutor: Script at specified location: scripts/applicationstart.sh is not executable. Trying to make it executable.

This is very confusing because my scripts are very simple and I am not sure why they would not be accepted. Here is where they are called in the appspec.yml file:

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu/teller-install
hooks:
  BeforeInstall:
    - location: scripts/beforeinstall.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/applicationstart.sh
      timeout: 300
      runas: root

Here are the two scripts I am calling form this:

#!/bin/bash

cd /home/ubuntu/teller-install/Server && npm install
exit 0

And the second:

    #!/bin/bash

pm2 start /home/ubuntu/teller-install/Server/app.js
exit 0

The deployment succeeds but the scripts are not run.

Here is the error message i get in the codedeploy-agent log file:

    2017-06-01 23:59:01 WARN  [codedeploy-agent(3504)]: InstanceAgent::Plugins::CodeDeployPlugin::HookExecutor: Script at specified location: scripts/applicationstart.sh is not executable.  Trying to make it executable.
2017-06-01 23:59:01 INFO  [codedeploy-agent(3504)]: Version file found in /opt/codedeploy-agent/.version.
2017-06-01 23:59:01 INFO  [codedeploy-agent(3504)]: [Aws::CodeDeployCommand::Client 200 0.028596 0 retries] put_host_command_complete(command_status:"Succeeded",diagnostics:{format:"JSON",payload:"{\"error_code\":0,\"script_name\":\"\",\"message\":\"Succeeded\",\"log\":\"\"}"},host_command_identifier:"WyJjb20uYW1hem9uLmFwb2xsby5kZXBsb3ljb250cm9sLmRvbWFpbi5Ib3N0Q29tbWFuZElkZW50aWZpZXIiLHsiZGVwbG95bWVudElkIjoiQ29kZURlcGxveS91cy1lYXN0LTEvUHJvZC9hcm46YXdzOnNkczp1cy1lYXN0LTE6MDEwNzAyMDY3ODAwOmRlcGxveW1lbnQvZC1LWFU2WjQ0UU0iLCJob3N0SWQiOiJhcm46YXdzOmVjMjp1cy1lYXN0LTE6MDEwNzAyMDY3ODAwOmluc3RhbmNlL2ktMGNkMzQ3OThlNDdiYzE3MjkiLCJjb21tYW5kTmFtZSI6IkFwcGxpY2F0aW9uU3RhcnQiLCJjb21tYW5kUG9zaXRpb24iOjYsImNvbW1hbmRBdHRlbXB0IjoxfV0=")  

Here is the source code with the files I am talking aboout.

https://github.com/SamKirkiles/Teller

random_0620
  • 1,636
  • 5
  • 23
  • 44

2 Answers2

7

I think there's a bit of a misconception here. Understandable so, because CodeDeploy does a poor job of explaining it.

The warning ... is not executable. Trying to make it executable. is a bit of a red herring. See the next line in OP's snippet, where it says that it did indeed successfully set the script to be executable: put_host_command_complete(command_status:"Succeeded" ...

So you don't have to set permissions on your hooks scripts at all - CodeDeploy takes care of that for you.

Note that there's an additional, annoying peculiarity. CodeDeploy doesn't execute your hooks scripts from your chosen code location (e.g., /srv/my-cool-app/scripts/beforeinstall.sh). It actually makes a copy of your entire deployment and stores it in /opt/codedeploy-agent/deployment-root/[some-guid]/[newest-folder]/deployment-archive/. This is where it runs the hooks scripts from. You can see proof of this by going to that folder, and checking the permissions on your scripts there - the ones that have been run by the CodeDeploy agent will be 777.

What's more, while you can put a permissions section in your appspec.yml file, it requires absolute paths, which again would only allow you to edit your deploy location, not the /opt location where the scripts are actually run from.

jameslol
  • 1,829
  • 16
  • 18
  • This is pure madness. Running something like `touch random-file-name` from inside one of your scripts will not create a file in the deployment destination and it won't create it either in the /opt/... path – Marco Aurélio Deleu Nov 21 '20 at 18:30
1

Ok so after a lot of frustration I solved the problem. First of all, I had to change the permissions on my scripts with chmod 777 scriptname.sh but you're going to want to give them tighter permissions than that in reality. This got rid of that annoying error message.

Then the reason the scripts were not working was becasue I had my script where I installed dependencies in before install hook and that is calling it before any of the files are present. I had to move the dependency installation into after install and everything is working perfectly. Hope that helps anyone with a similar problem.

random_0620
  • 1,636
  • 5
  • 23
  • 44