Execute command after deploy AWS Beanstalk - node.js

I have problem with execute command after deploy, i have some node.js project and script, this script use some bin from node_modules, if i write my command for script in .ebextensions/.config he execute before npm install and return error ("node_modules/.bin/some": No such file or directory). How i can execute command after deploy. Thanks.

I found the following solution
I add to beanstalk config next command:
commands:
create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/some_job.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
cd /var/app/current
export PATH=$PATH:$(ls -td /opt/elasticbeanstalk/node-install/node-* | head -1)/bin
npm run some_script
This commands create(if not exist) folder for post-hooks scripts and adds bash script. Scripts in this folders execute only after npm install, this very important for my problem.
Thanks to this guy http://junkheap.net/blog/2013/05/20/elastic-beanstalk-post-deployment-scripts/

create a file called .ebextensions/post_actions.config:
container_commands:
<name of container_command>:
command: "<command to run>"
this will be executed after the code was extracted, but before it was launched.

A better approach would be to go with the aws platform hooks. Where you can define the postdeploy hooks AWS Patform Hooks
In that inside the project root directory you can add .platform/hooks/postdeploy/
Insdie this path you can create xxx-postdeploy-script.sh. Files here run after the Elastic Beanstalk platform engine deploys the application and proxy server.This is the last deployment workflow step

If you read the AWS ebextensions documentation they mention the execution, specifically where they mention that all commands are executed before the application version is deployed.
"You can use the container_commands key to execute commands for your
container. The commands in container_commands are processed in
alphabetical order by name. They run after the application and web
server have been set up and the application version file has been
extracted, but before the application version is deployed."
If you deploy it for a second time it should work; this is because your application is already unpacked. This however is not a working solution because every new instance that is spawned will error.

Related

From inside a yarn workspace subpackage, run a root-level script

I'm wondering: if your terminal's current working directory is inside a yarn workspace, is there a way to run a yarn script that's defined at the project root without changing the current directory to be outside of a workspace?
For instance, you can run a command for a particular workspace by running yarn workspace workspace-name script-name but is it possible to use that yarn workspace command to target not a subpackage, but the root package itself?
I couldn't find a way to do it with yarn workspace, but you can do it by specifying the current working directory (cwd) when running the root command. Assuming you're running your command from ~/packages/subpackage, you'll need to go back two times with ../..:
yarn --cwd="../.." my-root-script
Scripts that contain a : in their name can be run from anywhere!
For example, your root script called "root:something" can be called from within any workspace by running yarn root:something.
Note that this even works if the : script is not a root script, but a workspace script. See yarn docs.

Jenkins-Publish over SSH plugin: Getting error on npm

I am trying to setup CI/CD pipeline for my node.js application with Jenkins.
What I am trying to do is login to my application server through jenkins and execute the commands on the server with the help of publish over ssh plugin
(https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin).
I've selected the Send Files or Execute commands over SSH option and added the following commands in Exec command section.
cd <project-folder> && git pull origin master && npm install
I am getting an error like npm not found but I try to directly on the server there is no error.
So I believe there's is a permission issue on jenkins but i can't find the solution
Yes, you identified the issue correctly ... Its permission issue ..
I assume you have already added the root login details at "SSH remote hosts" in the "configure system" section of the jenkins.
First pull the GIT repo to the jenkins workspace via web hook settings in github. Hope you have succeeded in this step also
In "Build Environment" select "Execute shell script on remote host using ssh" then use the following
cd <project-folder> (Ex: cd /var/lib/jenkins/workspace/<project-folder>)
npm install
NOTE: you should give the complete server path in the server to avoid permission .. If you are working with localhost then it may work and other very important when you use "Execute shell script on remote host using ssh" you should access via root user only NOT with the cpanel account login.. This will avoid permissions issue.
Once the NPM is install, check the console log for "success" Then do the other commands and suggest to remove the "npm install" as installation multiple times will increase the application compiling time..

How do I change directory to deployment-archive to run gulp?

I am using aws code deploy to deploy a nodejs server to an ec2 instance.
aws code deploy agent downloads bundle to following path:
/opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id/deployment-archive/gulpfile.js
which means my gulpfile.js reside at
/opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id/deployment-archive/gulpfile.js
however any bash command I run will run from
/opt/codedeploy-agent
How do I change directory to dynamically generated
deployment-group-id/deployment-id/deployment-archive
and also install node modules from package.json?
You may use the environment variables as for changing directory
$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/
You can refer this for how to use them.
You can also change the root directory where code deploy adds the deployment-group-id/deployment-id/deployment-archive by changing it from the agent configuration file: https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-agent-configuration.html
By default it's in /opt/codedeploy-agent/deployment-root but you can change that.
Furthermore you can tell CodeDeploy to copy your files over to a different directory in the appspec file by controlling each file's source and destination:
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-files.html
What I understand is you can not run
BeforeInstall from root of your project, you have to move the code to AfterInstall.
To find the full path of root of after install and then navigate from there check this out:
AWS CodeDeploy AfterInstall script is being run from code-deploy agent dir

Openshift action_hook is not executable

I have recently create my first Openshift node application and currently trying to get the build scripts working when I push to the server via git (using action_hook).
I am writing the application using PhpStorm on Windows and have had no problem pushing files to the server.
I have create the following file: .openshift/action_hook/build which contains the following:
#!/bin/bash
webpack --config $OPENSHIFT_DATA_DIR/webpack.config.js
When I push to the server using git, I get the following error:
NOTE: The .openshift/action_hooks/build hook is not executable, to make it executable:
On Windows run: git update-index --chmod=+x .openshift/action_hooks/build
On Linux/OSX run: chmod +x .openshift/action_hooks/build
I have tried the windows command above but when running git ls-tree HEAD to check the file permission, I can see they have not changed:
100644 blob 1b82131d8b9e9c63b765ef328ecbbfb54f0c70aa build
Has anybody managed to get this working on windows?
Thanks a lot

How to install rabbitmq and erlang on centOS without root user?

Can anyone help me with installation?
I have install virtualEnv and trying to install both of these. but not sure it is correct or not.
I know this is an old version, but it worked for me on a different Linux build (Mate OS). Follow the steps in this blog post which I have simplified below.
Download the below
ERLANG from OTP R16B03-1 Source File
RabbitMQ from RabbitMQ Server.tar.gz
Installing Erlang
Extract the ERLANG file
cd to source folder
run $ configure
run $ make
Open Makefile and change /Users/deepkrish/Application/erlang to a suitable directory
The line you are looking for is the below:
# prefix from configure, default is /usr/local (must be an absolute path) prefix = /Users/deepkrish/Application/erlang
Run $ make install
Once Erlang is installed non root user add the erlang/bin to PATH in .bash_profile like below:
export ERLANG=”/Users/deepkrish/Application/erlang/bin”
export PATH=${ERLANG}:${PATH}
Now execute the profile by running `$ source .bash_profile” or log off and login again.
Check $ erl -version This should give you the below:
Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 5.10.4
Installing RabbitMQ
Untar the RabbitMQ.tar file and $ cd to the extracted folder
run $ make
2. This should create a scripts folder. Now change into that. $ cd scripts
Now change the below in the rabbitmq-defaults file. This will change where we run and log rabbitMQ. You can change it to the folder you want to run RabbitMQ from as below
### next line potentially updated in package install steps SYS_PREFIX=~/Application/RabbitMQ
Save and close the file
Now create a directory mkdir -p ../etc/rabbitmq Note that if you don't have access to the /etc directory you can also change it to somewhere else.
./rabbitmq-plugins enable rabbitmq_management
Start rabbitmq server $ ./rabbitmq-server &
I use the below script file to start RabbitMQ server whenever I log on.
#!/bin/sh
cd /home/myusername/myproject/RabbitMQ/rabbitmq-server-3.2.3/scripts
export ERLANG="/home/myusername/myproject/RabbitMQ/erlang/bin"
export PATH=${ERLANG}:${PATH}
./rabbitmq-server &

Resources