Hello, I’m trying to install specific versions of nvm, nodejs with npm for a setup which is quite manual at present.
we have a shell script that works when run locally, but it doesn’t work when run using packer.
usually there are either permission issues, or it says nvm command not found.
are there any suggestions?
the shell script that does it normally is this
sudo -H -u nodejs $(which bash) -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash'
sudo -H -u nodejs $(which bash) -c 'nvm install 8.16.0'
sudo -H -u nodejs $(which bash) -c 'nvm use 8.16.0 ; nvm alias default 8.16.0 ; npm i npm@latest' ## THIS MAY FAIL, IT NEEDS TO BE TESTED
sudo -H -u nodejs $(which bash) -c 'npm i -g pm2@2.10.4'
# As nodejs user - in /home/nodejs
# create *ecosystem.config.js* with the following
touch /home/nodejs/ecosystem.config.js
cat <<EOF >> /home/nodejs/ecosystem.config.js
module.exports = {
/**
* Application configuration section
* http://pm2.keymetrics.io/docs/usage/application-declaration/
*/
apps : [
// First application
{
name : 'unified-api',
script : '/usr/src/app/latest/cluster.js',
watch : ['/usr/src/app/reload/unified-api'],
cwd : '/usr/src/app/latest/',
env: {
COMMON_VARIABLE: 'true',
NODE_ENV: 'production'
}
}
]
};
EOF
"sudo -H -u nodejs $(which bash) -c 'sudo /home/nodejs/.nvm/nvm.sh'",
exit
I’ve tried a number of ansible playbook roles from ansible galaxy however they still seem to have issues.
how can I run commands as a different user, and change all the environment variables so that they refer to the bashrc in the nodejs user before the script is run?
Thanks