bug in codedeploy afterinstall hook? - amazon-web-services

We have autoscaling set up for our symfony3 application. We are using aws codedeploy to deploy to autoscaling instances.
My appspec.yml file
version: 0.0
os: linux
files:
- source: /
destination: /usr/share/nginx/<some_dir>
hooks:
AfterInstall:
- location: post_deploy.sh
timeout: 180
runas: ubuntu
post_deploy.sh
#!/bin/bash
doc_root=/usr/share/nginx/<some_dir>
current_dir=$PWD
cd $doc_root
sudo -E composer install --no-interaction --no-dev --optimize-autoloader
cd $current_dir
and also exported environment variables for parameters.yml file
when we deploy revision, codedeploy succeed in deployment. But when i access my app through browser, nginx error log says:
PHP Fatal error: Uncaught exception 'Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException' with message 'You have requested a non-existent parameter "database.host". Did you mean one of these: "database_host", "database_port"?
Strange thing is that when i run post_deploy.sh script manually by logging in to my server it executes well and no error afterwards.
I don't know how to deal with it.

Try to change database.host to database_host. that what's indicated in the message.

codedeploy doesn't preserve env var in spite of -E option.
So i pass the env var in command itself, like this
sudo SYMFONY_ENV=$SYMFONY_ENV SYMFONY__DATABASE__NAME=$SYMFONY__DATABASE__NAME SYMFONY__DATABASE__USER=$SYMFONY__DATABASE__USER SYMFONY__DATABASE__HOST=$SYMFONY__DATABASE__HOST SYMFONY__DATABASE__PORT=$SYMFONY__DATABASE__PORT SYMFONY__DATABASE__PASSWORD=$SYMFONY__DATABASE__PASSWORD COMPOSER_HOME=$COMPOSER_HOME composer install --no-interaction --no-dev --optimize-autoloader
worked for me.

Related

AWS CodeDeploy error - The deployment failed because a specified file already exists at this location

i am trying to deploy the python application through code-deploy to the ec2 instances
but during deployment i am facing this error
The deployment failed because a specified file already exists at this
location: /home/ubuntu/yello/manage.py
attaching my appsecfile also tried with overwrite but no luck
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/yello
overwrite: true
hooks:
AfterInstall:
- location: script/services.sh
timeout: 300
runas: ubuntu
can anyone help ?
This is a very common error so AWS public docs has a section for the same.
https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-same-files-different-app-name
Basically when creating the deployment, you need to select Overwrite the content option so the deployment does not fail (default behaviour). Details here:
https://docs.aws.amazon.com/codedeploy/latest/userguide/deployments-rollback-and-redeploy.html#deployments-rollback-and-redeploy-content-options
The default behaviour prevents two Deployments from two different CodeDeploy "Applications" writing the same file and possibly causing some sort of conflict.
cd /var/www/your-directory/public/funnel_webhooks/test/
sudo rm -rf clickfunnel.txt
After removing this file please release the change in the code pipeline.
Now you can specify file_exists_behavior in your appspec file. Allowed values are DISALLOW, OVERWRITE, or RETAIN.
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/yello
file_exists_behavior: OVERWRITE

Go command not found while running code deploy hook

Hello I'm trying to create a CodeDeploy deployment for my golang application. I have an autoscaling group that uses a AMI I created that has all the libs I need installed. When I try to run CodeDeploy it exists with an error in my after_install:
LifecycleEvent - AfterInstall
Script - scripts/after_install.sh
[stderr]/opt/codedeploy-agent/deployment-root/a65d9a2e-fddd-471c-8ea1-c018792d00bd/d-4IKP3PP4Y/deployment-archive/scripts/after_install.sh:
line 4: go: command not found
I know go is installed on the server I can verify by sshing into the server and running the go command. Initially I had my after_install hook run as root so that's why I thought it complained about go not being installed.
I updated it to run as ubuntu here is the appspec file
version: 0.0
os: linux
files:
- source: ./
destination: ./home/ubuntu/code
hooks:
AfterInstall:
- location: scripts/after_install.sh
timeout: 180
runas: ubuntu
ApplicationStart:
- location: scripts/application_start.sh
timeout: 180
runas: root
But I still get the error of go command not found. I SSH into the server as ubuntu user and I can clearly see go is installed.
I took it one step further and ran the after_install.sh file and it worked with no errors. What am I doing wrong here?
Just for the extra curious here is my after_install.sh file
#!/bin/bash
cd /home/ubuntu/code/vibeify/cmd/vibeify
go build
If you can use go command without the full installation path only in the interactive shell, check $HOME/.bashrc.
It may depends on OS default settings, but some OS default bashrc file includes scripts that does not load profile in non-interactive shell.
# open $HOME/.bashrc file
# and comment out these lines
case $- in
*i*) ;;
*) return;;
esac

AWS CodeDeploy Issue: Cannot run hooks in appspec file

I have just started working with AWS. I am trying to deploy a nodejs application using codeship and AWS codedeploy. I am successful in deploying the application from codeship to Ec2 instance. But the problem is that I am not able to run the hooks file in appspec.yml. My appspec.yml is given below:
---
version: 0.0
os: linux
files:
- destination: /home/ec2-user/node-project
source: /
hooks:
ApplicationStart:
- location: bin/app-start.sh
runas: root
timeout: 100
In app-start.sh I have:
#!/bin/bash
npm install
The app-start.sh never works and node-modules are never installed. I have also tried to debug in the logs path(/var/log/aws/codedeploy-agent/codedeploy-agent.log) for code-deploy but there is no error and warning.I have also tried multiple things but nothing is working.
The project is successfully installed in Ec2 instance but appspec.yml never launches app-start.sh. Any help would be appreciated.
The issue is that you're moving the files to /home/ec2-user/node-project, which happens before your app-start.sh gets run at the ApplicationStart lifecycle hook. You need to cd into the right directory before running npm install.
Updated ApplicationStart scripts:
#!/bin/bash
cd /home/ec2-user/node-project
npm install
# You'll need to start your application too.
npm start
As an aside, you may want to use the AfterInstall lifecycle hook to run npm install just for organization purposes, but it will have no functional different.

Why won't my AWS CodeDeploy run my script after deployment?

I am using CodeDeploy to deploy my applications to EC2 instances created by an Auto Scaling Group.
The applications deploy fine and are moved to their correct file mapped locations, however my AfterInstallation script is never executed. I can see in the logs that it tries to make the script executable and passes that stage, but it never gets executed.
Here is my appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /opt/lobby
hooks:
AfterInstall:
- location: bootstrap.sh
timeout: 30
runas: root
Here is the script
#!/bin/bash
nohup nodejs lobby.js &
echo "finished"
I do not see the echo printed nor do I see any processes running related to lobby.js. I can verify that this script works by typing ./bootstrap.sh after the deployment and this works fine. This hook should do this for me though.
This needs to be a background task as I will be running multiple applications in this script but only one is displayed now just to get it working.
Edit:
I have referred to http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html#appspec-hooks-server and tried replacing AfterInstall with ValidateService and AfterAllowTraffic (but I'm not using a LoadBalancer)
Question
Why is my AfterInstallation script not getting called, or so it seems?
AfterInstall: AfterInstall script contains the tasks need to be executed after installing the Application.
Example of BeforeInstall script:
#!/bin/bash
cd /home/ubuntu/production/weone-backend
sudo chown -R ubuntu:ubuntu /home/ubuntu/production
sudo NODE_ENV=production nohup nodejs app.js > /dev/null 2> /dev/null < /dev/null &
In the above script, we are changing the ownership of our application folder
& starting application process.
Note: Use “/dev/null 2> /dev/null < /dev/null &” to get out of nohup shell automatically, else your CodeDeploy would get stuck at AfterInstall event.
https://www.oodlestechnologies.com/blogs/AWS-CodeDeploy

Perform actions on server after CircleCI deployment

I have a Django project that I deploy on a server using CircleCI. The server is a basic cloud server, and I can SSH into it.
I set up the deployment section of my circle.yml file, and everything is working fine. I would like to automatically perform some actions on the server after the deployment (such as migrating the database or reloading gunicorn).
I there a way to do that with CircleCI? I looked in the docs but couldn't find anything related to this particular problem. I also tried to put ssh user#my_server_ip after my deployment step, but then I get stuck and cannot perform any action. I can successfully SSH in, but the rest of the commands is not called.
Here is what my ideal circle.yml file would look like:
deployment:
staging:
branch: develop
commands:
- rsync --update ./requirements.txt user#server:/home/user/requirements.txt
- rsync -r --update ./myapp/ user#server:/home/user/myapp/
- ssh user#server
- workon myapp_venv
- cd /home/user/
- pip install -r requirements.txt
I solved the problem by putting a post_deploy.sh file on the server, and putting this line on the circle.yml:
ssh -i ~/.ssh/id_myhost user#server 'post_deploy.sh'
It executes the instructions in the post_deploy.sh file, which is exactly what I wanted.