aws / aws-codedeploy-agent

Host Agent for AWS CodeDeploy
https://aws.amazon.com/codedeploy
Apache License 2.0
329 stars 187 forks source link

Running composer in build.sh #67

Closed dbclkclk closed 8 years ago

dbclkclk commented 8 years ago

Hi:

I'm trying to run composer in my before deployment to my httpd server. This is my configuration:

 version: 0.0
 os: linux 
 files:
   - source: .
     destination: /var/www/html/ 
 permissions:
   - object: ./build.sh 
     mode: 644

hooks:
   BeforeInstall:
     - location: ./build.sh
       timeout: 300

However, I'm getting this error:

     2016-06-07 22:37:20 WARN  [codedeploy-agent(2469)]:        InstanceAgent::Plugins::CodeDeployPlugin::HookExecutor: Script at specified location: ./build.sh is   not executable.  Trying to make it executable.
     2016-06-07 22:37:21 INFO  [codedeploy-agent(2469)]: Version file found in /opt/codedeploy-a     gent/.version.

    2016-06-07 22:37:21 INFO  [codedeploy-agent(2469)]: [Aws::CodeDeployCommand::Client 200  0.03208 0 retries] put_host_command_complete(command_status:"Failed",diagnostics: {format:"JSON",payload:"{\"error_code\":4,\"script_name\":\"./build.sh\",\"message\":\"Script at  specified location: ./build.sh failed with exit code 1\",\"log\":\"LifecycleEvent - BeforeInstall\\nScript -  ./build.sh\\n[stderr]Running composer as root/super user is highly discouraged as packages, plugins  and scripts cannot always be trusted\\n[stderr]Composer could not find a composer.json file in    /opt/codedeploy-agent\\n[stderr]To initialize a project, please create a composer.json file as   described in the https://getcomposer.org/ \\\"Getting Started\\\" section\\n[stderr]sudo:   vendor/bin/phing: command not   found\\n\"}"},host_command_identifier:"WyJjb20uYW1hem9uLmFwb2xsby5kZXBsb3ljb250cm9sLm  RvbWFpbi5Ib3N0Q29tbWFuZElkZW50aWZpZXIiLHsiZGVwbG95bWVudElkIjoiQ29kZURlcGxveS91c y13ZXN0LTIvUHJvZC9hcm46YXdzOnNkczp1cy13ZXN0LTI6ODYyODgzMjM3MDgwOmRlcGxveW1l bnQvZC1CVkwzQUxRMkciLCJob3N0SWQiOiJhcm46YXdzOmVjMjp1cy13ZXN0LTI6ODYyODgzMjM 3MDgwOmluc3RhbmNlL2ktZmE1MTk4MjEiLCJjb21tYW5kTmFtZSI6IkJlZm9yZUluc3RhbGwiLCJjb2 1tYW5kUG9zaXRpb24iOjMsImNvbW1hbmRBdHRlbXB0IjoxfV0=")

   2016-06-07 22:37:21 ERROR [codedeploy-agent(2469)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Error during perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location: ./build.sh failed with exit code 1 - /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:153:in `execute_script'
   /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:110:in `block (2 levels) in execute'

/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:98:in each' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:98:inblock in execute' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:168:in create_script_log_file_if_needed' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:96:inexecute' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:134:in block (3 levels) in map' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:125:ineach' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:125:in block (2 levels) in map' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:63:inexecute_command' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:132:in process_command' /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:65:inperform' /opt/codedeploy-agent/lib/instance_agent/agent/base.rb:28:in run' /opt/codedeploy-agent/lib/instance_agent/runner/child.rb:38:inblock in run' /opt/codedeploy-agent/lib/instance_agent/runner/child.rb:55:in with_error_handling' /opt/codedeploy-agent/lib/instance_agent/runner/child.rb:37:inrun' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/child.rb:70:in block in run_with_error_handling' /opt/codedeploy-agent/lib/instance_agent/runner/child.rb:55:inwith_error_handling' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/child.rb:69:in run_with_error_handling' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/child.rb:33:inblock in start' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/child.rb:22:in loop' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/child.rb:22:instart' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:202:in block in spawn_child' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:200:infork' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:200:in spawn_child' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:192:inblock in spawn_children' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:191:in times' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:191:inspawn_children' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:134:in start' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:37:inblock in start' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:36:in fork' /opt/codedeploy-agent/vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb:36:instart' /opt/codedeploy-agent/bin/codedeploy-agent:37:in block (2 levels) in <main>' /opt/codedeploy-agent/vendor/gems/gli-2.5.6/lib/gli/command_support.rb:130:incall' /opt/codedeploy-agent/vendor/gems/gli-2.5.6/lib/gli/command_support.rb:130:in execute' /opt/codedeploy-agent/vendor/gems/gli-2.5.6/lib/gli/app_support.rb:262:inblock in call_command' /opt/codedeploy-agent/vendor/gems/gli-2.5.6/lib/gli/app_support.rb:275:in call' /opt/codedeploy-agent/vendor/gems/gli-2.5.6/lib/gli/app_support.rb:275:incall_command' /opt/codedeploy-agent/vendor/gems/gli-2.5.6/lib/gli/app_support.rb:69:in run' /opt/codedeploy-agent/bin/codedeploy-agent:84:in

' 2016-06-07 22:37:31 INFO [codedeploy-agent(2469)]: Version file found in /opt/codedeploy- agent/.version. 2016-06-07 22:38:31 INFO [codedeploy-agent(2469)]: [Aws::CodeDeployCommand::Client 200 60.100514 0 retries] poll_host_command(host_identifier:"arn:aws:ec2:us-west-2:862883237080:instance/i-fa519821")

This is my build.sh

 #!/bin/bash
   echo pwd
  sudo composer install
 sudo vendor/bin/phing www
amartyag commented 8 years ago

Hi, Looks like composer is trying to find the json file in pwd (which looks like /opt/codedeploy-agent). You can check the stdout of your script in: /opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id/logs/scripts.log

You can modify the script like this, which might solve the issue:

!/bin/bash

echo pwd cd path_to_composer_json sudo composer install sudo vendor/bin/phing www

dbclkclk commented 8 years ago

@amartyag To try to * cd path_to_composer_json* , that's within my actually deploy directory. My question is, isn't the path /deployment-group-id/deployment-id/, especially deployment-id generated dynamically? I dont want to have a case when on each deploy, a deployment-id folder is generated and I can't reference the correct composer.json file.

feverLu commented 8 years ago

deployment-id is different for each deployment(as a unique id), it shows on the deployment page console too. Every time, a deployment-id folder will be generated, but your default directory will be /opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id, so actually just change to the relative directory inside your deployment archive will work.

dbclkclk commented 8 years ago

@feverLu I dont think I get you. If this is being automated and I have a build.sh script that runs on each deploy, how do I know from my build.sh script what's the current named folder the project was deployed in (if this is something random)? Example:


 #!/bin/bash
cd  /deployment-root/deployment-group-[`[HOW DO I KNOW WHAT ID IT IS AT DEPLOY TIME WITHOUT FINDING THIS OUT FROM CONSOLE AND CHANGING THIS EVERY TIME`]/deployment-[`HOW DO I KNOW WHAT ID IT IS AT DEPLOY TIME WITHOUT FINDING THIS OUT FROM CONSOLE AND CHANGING THIS EVERY TIME`]
  sudo composer install
 sudo vendor/bin/phing www

Make sense?

dbclkclk commented 8 years ago

Also please note, I mentioned this in my previous comment:

 /deployment-[`HOW DO I KNOW WHAT ID IT IS AT DEPLOY TIME WITHOUT FINDING THIS OUT FROM CONSOLE AND CHANGING THIS EVERY TIME I DEPLOY`]
dbclkclk commented 8 years ago

I guess this isn't possible

bdashrad commented 8 years ago

@dbclkclk This doesn't look like a bug, but rather a misconfiguration. Have a look at https://github.com/awslabs/aws-codedeploy-samples/ for examples.

If I were you, instead of trying to guess the directory, I would put my build script in a standard location and set the permissions of the file correctly before calling in in appspec.yml.

Your tree could look something like this:

deploy/
├── appspec.yml
├── deploy_hooks
│   └── build.sh
├── composer
    ├── composer.json
└── html
    ├── index.php
    └── some_content

And appspec.yml should look like:

version: 0.0
os: linux
files:
  - source: html/
    destination: /var/www/html
  - source: composer/
    destination: /opt/composer
permissions:
  - object: /var/www/html
    owner: www-data
    group: www-data
    mode: 744
    type:
      - file
hooks:
  BeforeInstall:
    - location: deploy_hooks/build.sh
      runas: root

Then point your build.sh to the location you moved the files to:

build.sh

#!/bin/bash
echo pwd
cd /opt/composer
composer install
vendor/bin/phing www
ambareesha commented 8 years ago

You can get the current deployment-id and deployment-group-id from the environment variables: https://blogs.aws.amazon.com/application-management/post/Tx1PX2XMPLYPULD/Using-CodeDeploy-Environment-Variables

You can use these to build up your absolute path.