Start up script to re-start the NodeJS app is not working - google-cloud-platform

I'm trying to use the startup-script meta to automatically start my server when my instance resets.
The startup script runs, but the actual command I need doesn't work. What am I doing wrong in this script?
#! /bin/bash
echo "testing" > /tmp/test-script.txt # This WORKS
cd /../ && npm start # This does NOT work

If you look in your package.json file, it says "start": "command" and that is the command that is run when you use the npm start command. The command should be changed in package.json: "start": "node ./pathToStartUpScripts". For example in an Express.js project the start command would be "node ./bin/www" as that is where all the application start up scripts are written.

Related

Sagemaker lifecycle config: could not find conda environment conda_python3

the below script should run a notebook called prepTimePreProcessing whenever a AWS notebook instance starts runing.
however I am getting "could not find conda environment conda_python3" error from the lifecycle config file.
set -e
ENVIRONMENT=python3
NOTEBOOK_FILE="/home/ec2-user/SageMaker/prepTimePreProcessing.ipynb"
echo "Activating conda env"
source /home/ec2-user/anaconda3/bin/activate "$ENVIRONMENT"
echo "Starting notebook"
nohup jupyter nbconvert --to notebook --inplace --ExecutePreprocessor.timeout=600 --ExecutePreprocessor.kernel_name=python3 --execute "$NOTEBOOK_FILE" &
Any help whould be appreciated.
Assuming no environment problems, if you open a terminal in the instance in use and run:
conda env list
the result should also contain this line:
python3 /home/ec2-user/anaconda3/envs/python3
After that, you can create a .sh script inside /home/ec2-user/SageMaker containing all the code to run. This way it also becomes versionable by being a persisted file in the instance space and not inside an external configuration.
The on-start.sh/on-create.sh (from this point I will simply call it script.sh) file becomes trivially:
# PARAMETERS
ENVIRONMENT=python3
# conda env
source /home/ec2-user/anaconda3/bin/activate "$ENVIRONMENT";
echo "'$ENVIRONMENT' env activated"
In the lifecycle config, on the other hand, just write a few lines to invoke the previously created script.sh:
#!/bin/bash
set -e
SETUP_FILE=/home/ec2-user/SageMaker/script.sh
echo "Run setup script"
sh "$SETUP_FILE"
echo "Setup completed!"
Extra
If you want to add a safety check so that the .sh file is read correctly regardless of line breaks, I would also add a conversion:
#!/bin/bash
set -e
SETUP_FILE=/home/ec2-user/SageMaker/script.sh
# convert script to unix format
echo "Converting setup script into unix format"
sudo yum -y install dos2unix > /dev/null 2>&1
dos2unix "$SETUP_FILE" > /dev/null 2>&1
echo "Run setup script"
sh "$SETUP_FILE"
echo "Setup completed!"

pm2 crash with too many unstable restarts (16). Stopped. "errored" in python

I have created a script that can run the python django application. I run this script using pm2.
I do pm2 start scripts.sh, it works properly but after some time my application doesn't work and displays an error like this
the runtime process for the instance running on port 37001 has unexpectedly quit**
I show the log using pm2 logs, it displays an error like this
script.sh had too many unstable restarts (16). Stopped. "errored"
How to resolved it? Can anyone help me?
I had the same issue and resolved with:
pm2 kill
rm -rf node_modules
npm i
pm2 start index.js
sudo shutdown -r now
Primarily I have created a configuration file using the following command:
cd ~
pm2 init
sudo nano ecosystem.config.js
Then copy and paste the following code into the newly created ecosystem.config.js file (note: change your project location in cwd section):
apps: [
{
name: 'my-site',
cwd: ' /home/your-name/your-project-directory',
script: 'npm',
args: 'start',
env: {
NODE_PUBLIC_APP: 'NODE_PUBLIC_APP', // for example
},
},
// optionally a second project
],};
After that before running the app using pm2 I did the following:
pm2 kill
sudo rm -rf node_modules
npm i
Then I run the application in watch mode, so that I can see what is going on using pm2 log later. To run the app in watch mode do the following:
pm2 start npm --name "AnyAppName" -- run start --watch
And finally I see the logs what causes the error by running the following command:
pm2 log
Hope this helps someone in the future like me.

Codeship deploy custom script error: "bash: npm: command not found bash: pm2: command not found"

I m trying to automatically run "npm install" and "pm2 restart all" whenever codeship deployed my codes onto DigitalOcean.
This is the custom script:
rsync -avz -e "ssh" ~/clone/ root#IP:/opt/projectname
ssh root#IP 'cd /opt/projectname/; npm install; pm2 restart all'
The rsync works. Codes will be deployed onto the correct folder on DigitalOcean.
However, the second line fails. Error:
bash: npm: command not found
bash: pm2: command not found
Why?

How do I run concurrent scripts when webpack watch fires?

Background
I currently have an npm script that looks something like
"dev":"yarn install && concurrently -k \"npm run webpack\" \"cd dist/ && node App.js\" \"npm run test\" \"npm run lint\""
Logically this runs webpack, starts the app, lints, and tests in parallel.
npm webpack in that script has --watch set
Note: this is for dev.
The Problems
This tries to run the app before it has webpacked
This won't re-run the app when webpack repacks due to watch
The Goal
Run npm run webpack once
When it outputs (meaning the watch fired and finished) run the other three commands
when something crashes inform me, don't waste time running stuff that won't work, but try again when I fix the file.
The Real Problem
I don't know what I don't know. I suspect the real answer will be in the webpack config itself potentially, or there's a better tool than concurrently/watch for my use case, or the core idea on how I've designed this is just crazy. Maybe I want to create a devServer.js that uses webpack dev middleware to serve these instead? how would that pull in linting and testing then?
I don't know what a beautiful version of this build would look like.
What I Really Need
A great tutorial/guide/blog post about how this 'Should' go.
Here's what I would do; perhaps there's a better way:
"scripts": {
"dev": "yarn install && concurrently -k \"npm run webpack\" \"npm run watch\"",
"watch": "onchange \"dist/**/" -- concurrently -k \"cd dist/ && node App.js\" \"npm run test\" \"npm run lint\""
}
This uses onchange. npm run dev starts webpack and onchange in parallel. onchange monitors for any file changes in dist/ and runs your tasks when any files change.
The limitation of this approach is that your tasks will not run until files change in dist. You can work around this by deleting dist/ before running webpack. (Use rimraf to do this in a cross-platform way.) Example:
"dev": "yarn install && rimraf dist && concurrently -k \"npm run webpack\" \"npm run watch\""
You can just use rm -rf dist if you don't care about Windows support.

How can I write a Groovy Jenkinsfile for a Django application to run my tests?

I have recently started using Jenkins and I am wanting to use Multibranch Pipelines so I can test the various feature branches in my project.
The project is using django 1.8. So far my Jenkinsfile looks like this and fails in the testing stage as django can not find my settings file even though it is there:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get the code from a GitHub repository
git credentialsId: 'mycredentials', url: 'https://github.com/<user>/<project>/'
// Mark the code build 'stage'....
stage 'Build'
env.WORKSPACE = pwd()
sh 'virtualenv --python=python34 venv'
sh 'source venv/bin/activate'
sh 'pip install -r requirements.txt'
env.DJANGO_SETTINGS_MODULE = "<appname>.settings.jenkins"
// Start the tests
stage 'Test'
sh 'python34 manage.py test --keepdb'
}
venv/bin/activate does no more than setting up proper environmental paths.
You can do it on your own by adding at the beginning assuming that env.WORKSPACE is your project directory:
env.PATH="${env.WORKSPACE}/venv/bin:/usr/bin:${env.PATH}"
Later, if you want to call virtualenved python, just need to prepend it with specified path like here:
stage 'Test'
sh "${env.WORKSPACE}/venv/bin/python34 manage.py test --keepdb'
Or to call pip
sh "${env.WORKSPACE}/venv/bin/pip install -r requirements.txt"