Pathing issues with using ./ in a Rakefile on Windows - javascript

I have the following Rakefile...
namespace :dev do
desc "Execute my-bash-script."
task :done do
sh "./bin/my-bash-script.sh" # <-- Error on this line
end
end
Which I execute successfully on my Mac with rake dev:done
When I run the command on my Windows machine however, I receive a Command failed with status (127) error on the sh "./bin/my-bash-script.sh" line.
I figured there was a pathing issue with using ./ so I tried replacing ./ in the Rakefile with #{File.dirname(__FILE__)} but am still receiving the same error.
What am I doing incorrectly?

Under windows, passing forward slashes in a path to the shell does not work.
You need to replace all '/' with '\' in the command string.

Related

How to Change Default Directory in Node.JS

I am trying to change the default folder in node.js. I went to the following link in the node.js documentation:
https://nodejs.org/api/process.html#process_process_chdir_directory
I then generated the following code in a .js file:
console.log('Starting directory: ${process.cwd()}');
try {
process.chdir('C:\Users\HalvorSD\node-party');
console.log('New directory: ${process.cwd()}');
} catch (err) {
console.error('chdir: ${err}');
}
I get the error thrown in my console. The directory does exist so that's not the problem. Is my directory formatting incorrect or what's my issue?
I am trying to change the default from C:/Windows/System32/ to what I have above. Any help would be much appreciated.
JavaScript uses \ for String escape sequences. Use \\ for a literal backslash:
process.chdir('C:\\Users\\HalvorSD\\node-party');
Alternatively use path.join for cross-platform paths:
const path = require('path')
process.chdir(path.join('C', 'Users', 'HalvorSD', 'node-party'));
If you are going to change default directory for "Node.js command prompt" every time, when you launch it, then (Windows case)
go the directory where NodeJS was installed
find file nodevars.bat
open it with editor as administrator
change the default path in the row which looks like
if "%CD%\"=="%~dp0" cd /d "%HOMEDRIVE%%HOMEPATH%"
with your path. It could be for example
if "%CD%\"=="%~dp0" cd /d "c://MyDirectory/"
if you mean to change directory once when you launched "Node.js command prompt", then execute the following command in the Node.js command prompt:
cd c:/MyDirectory/

Running a bash script before startup in an NGINX docker container

I'm trying to run a javascript app on localhost:8000 using docker. Part of what I would like to do is swap out some config files based on the docker run command, I'd like to pass an environment variable into the container so that the bash script can use that as a parameter.
What my dockerfile is looking like is this:
FROM nginx
COPY . /usr/share/nginx/html
CMD ["bash","/usr/share/nginx/html/runfile.sh"]
And the bash script looks like this:
#!/bin/bash
if [ "$SECURITY_VERSION" = "OPENAM" ]; then
sed -i -e 's/localhost/openam/g' authConfig.js
fi
docker run -p 8000:80 missioncontrol:latest -e SECURITY_VERSION="TEST"
Docker gives me an exception saying -e exec command not found.
However if I change the dockerfile to use ENTRYPOINT instead of CMD, the -e flag works but the webserver does not start up.
Is there something I'm missing here? Is the ENTRYPOINT being overriden or something?
EDIT:
So I've updated my dockerfile to use ENTRYPOINT ["bash","/usr/share/nginx/html/runfile.sh", ";", " nginx -g daemon off;"]
But the docker container still shuts down. Is there something I'm missing?
NGINX 1.19 has a folder /docker-entrypoint.d on the root where place startup scripts executed by thedocker-entrypoint.sh script. You can also read the execution on the log.
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will
attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in
/docker-entrypoint.d/
/docker-entrypoint.sh: Launching
[..........]
/docker-entrypoint.sh: Configuration complete; ready for start up
For my future self and everybody else, this is how you can set up variable substitution at startup (for nginx, may also work for other images):
I've also wrote a more in depth blog post about it: https://danielhabenicht.github.io/docker/angular/2019/02/06/angular-nginx-runtime-variables.html
Dockerfile:
FROM nginx
ENV TEST="Hello variable"
WORKDIR /etc/nginx
COPY ./substituteEnv.sh ./substituteEnv.sh
# Execute the subsitution script and pass the path of the file to replace
ENTRYPOINT ["./substituteEnv.sh", "/usr/share/nginx/html/index.html"]
CMD ["nginx", "-g", "daemon off;"]
subsitute.sh: (same as #Daniel West's answer)
#!/bin/bash
if [[ -z $1 ]]; then
echo 'ERROR: No target file given.'
exit 1
fi
#Substitute all environment variables defined in the file given as argument
envsubst '\$TEST \$UPSTREAM_CONTAINER \$UPSTREAM_PORT' < $1 > $1
# Execute all other paramters
exec "${#:2}"
Now you can run docker run -e TEST="set at command line" -it <image_name>
The catch was the WORKDIR, without it the nginx command wouldn't be executed. If you want to apply this to other containers be sure to set the WORKDIR accordingly.
If you want to do the substitution recursivly in multiple files this is the bash script you are looking for:
# Substitutes all given environment variables
variables=( TEST )
if [[ -z $1 ]]; then
echo 'ERROR: No target file or directory given.'
exit 1
fi
for i in "${variables[#]}"
do
if [[ -z ${!i} ]]; then
echo 'ERROR: Variable "'$i'" not defined.'
exit 1
fi
echo $i ${!i} $1
# Variables to be replaced should have the format: ${TEST}
grep -rl $i $1 | xargs sed -i "s/\${$i}/${!i}/Ig"
done
exec "${#:2}"
I know this is late but I found this thread while searching for a solution so thought I'd share.
I had the same issue. Your ENTRYPOINT script should also include exec "$#"
#!/bin/sh
set -e
envsubst '\$CORS_HOST \$UPSTREAM_CONTAINER \$UPSTREAM_PORT' < /srv/api/default.conf > /etc/nginx/conf.d/default.conf
exec "$#"
That will mean the startup CMD from the nginx:alpine container will run. The above script will inject the specified environment variables into a config file. By doing this in runtime yo can override the environment variables.
Update the CMD line as below in the your dockerfile. Please note that if runfile.sh does not succeed (exit 0; inside it) then the next nginx command will not be executed.
FROM nginx
COPY . /usr/share/nginx/html
CMD /usr/share/nginx/html/runfile.sh && nginx -g 'daemon off;'
nginx docker file is using a CMD commnd to start the server on the base image you use. When you use the CMD command in your dockerfile you overwrite the one in their image. As it is mentioned in the dockerfile documentation:
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
NginX image has docker-entrypoint.d included and on container start will look for any scripts located in there. You can add your custom scripts during docker build. I also found that if you are using alpine image, bash is not installed, so you can add it yourself by running:
RUN apk update
RUN apk upgrade
RUN apk add bash
sample DockerFile:
FROM nginx:alpine
EXPOSE 443
EXPOSE 80
RUN apk update
RUN apk upgrade
RUN apk add bash
COPY ["my-script.sh", "/docker-entrypoint.d/my-script.sh"]
RUN chown nginx:nginx /docker-entrypoint.d/my-script.sh
USER nginx
In order to limit scope execution of your custom script script, it's highly recommended to run your container as a non-privileged user.
nginx container already defines ENTRYPOINT. If you define also CMD it will combine them both like 'ENTRYPOINT CMD' in such way that CMD becomes argument of ENTRYPOINT. That is why you need to redefine ENTRYPOINT to get it working.
Usually ENTRYPOINT is defined in such way, that if you also pass CMD, it will be executed by ENTRYPOINT script. However this might not be case with every container.

Error message from "jspm install jquery"

I am working through the tutorial on the jspm.io site
https://github.com/jspm/jspm-cli/wiki/Getting-Started
All works fine until I get to item 3, where I try to execute
jspm install jquery
and I get this error message
warn Error on getOverride for jspm:github, retrying (2).
ReferenceError: ui is not defined
at c:\Projects\Project1\node_modules\jspm\node_modules\jspm-registry\registry.js:157:5
nodejs is v0.12.0
npm is 2.5.1
jspm is 0.14.0
and this is on Windows 8.1
Does anyone have any clue what is causing this?
This looks like it was because there was an error while jspm was trying to create the local registry clone. Ensure you have git installed as git on your machine. Otherwise it may be a permissions issue.
This was a logging bug though - have fixed it with an update to the registry, so that the error should be slightly more useful next time if you update jspm.
I was getting a similar error with jspm but my problem was actually in how nodejs child_process.exec was calling the git command.
child_process.exec was running
C:\Windows\system32\cmd.exe /s /c "git clone --depth=1 github.com/jspm/registry.git .
However cmd.exe was still auto running commands set in the registry first. In my case the command changing the working folder. So the cwd was being overridden.
Check your registry settings for:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
If there is a command in there to set the drive of working folder it will cause the above error.
Also
With your working folder as c:\, try ruuning the following nodejs code:
var exec = require('child_process').exec;
exec('dir', { cwd: 'C:/windows/fonts' }, function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
});
If it does not list the contents of the fonts folder then your problem is more likely with child_process.exec in node

Node.js with Webstorm -

I've followed this example to try and add a record to MongoDB database.
When trying to run ./server/server.js I get the following message (at 2:22 in video):
"C:\Program Files (x86)\JetBrains\WebStorm 9.0.1\bin\runnerw.exe" "C:\Program Files\nodejs\npm" server.js
CreateProcess failed with error 193 (no message available)
Process finished with exit code 0
The node.exe path is right but don't know why I'm getting this error and can't find information on debugging it.
I got the same error while using WebStorm with Babel as the Node executable. After re-installing the dependencies, the correct path to the babel-node executable was node_modules\.bin\babel-node.cmd.

Ubuntu 8.04 Hardy and node.js upstart script

I am trying to write an upstart script for my ubuntu machine, which is version 8.04 "Hardy". I have followed the instructions on this site: upstart for node.js but it seems like these instructions are for a current version of ubuntu.
I noticed that the /etc/init directory does not exist on my machine, first I tried putting the script in the /etc/init.d directory and then I created the /etc/init dir and placed it there.
I will post my upstart script below (which is basically the same as from the website above with some path changes), but when I run start jobname, I just get an error "start: Unknown job: jobname". So then I changed the script around to a slimmed down version, posted below, and still I get the same result.
For now, I am using the 'nohup' command to run my node server but I would like a more permanent solution.
Please, any help?
SCRIPT 1:
description "node.js chat server"
author "iandev ith3"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
script
# Not sure why $HOME is needed, but we found that it is:
export HOME="/root"
exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script
post-start script
# optionally put a script here that will notifiy you node has (re)started
# /root/bin/hoptoad.sh "node.js has started!"
end script
SCRIPT 2:
description "node.js chat server"
author "iandev ith3"
script
exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script
Just use Forever. https://github.com/indexzero/forever
From looking at the website you provided I'd say that the /etc/init was just a typo and it should be /etc/init.d/. Some things you may want to check:
executable flag on your scripts. With most versions of Ubuntu executable files show up green when running 'ls' from the command line. If you want to check if your file is executable run 'ls -l /etc/init.d/YOUR_SCRIPT' from the command line. You will see something like this:
-rwxr-xr-x 1 root root 1342 2010-09-16 10:13 YOUR_SCRIPT
The x's mean that it is executable.
To set the executable flag if it is not set, run chmod u+x YOUR_SCRIPT
I'm pretty sure for older versions of ubuntu you need to have the script in /etc/rc.d/rc3.d or /etc/rc3.d. What linux does is run through rc0.d to rc5.d and execute every script in there. From what it looks like, ubuntu is moving away from this to something simpler so if you have rc directories you may need to edit your script a little.
Anyway I think i'm getting a little over complicated here. Check your executable flag and if you have rc directories and we'll move on from there.
May not be the best thing to start a process with sudo, but here's what I have setup on my local pc:
#!upstart
description "node.js server"
author "alessio"
start on startup
stop on shutdown
script
export HOME="/ubuntu"
exec sudo -u ubuntu /usr/bin/node /home/ubuntu/www/test.js 2>&1 >> /var/log/node.log
end script
Hope this helps.

Categories