Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding NODE_CMD option as env variable for specifying run command. #359

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

pacostas
Copy link
Contributor

@pacostas pacostas commented Sep 29, 2022

Two new env variables have been added for specifying on how to start the Node.js application.

  • NODE_CMD
  • INIT_WRAPPER

These two env variables can be used as follow, assuming you already have a build image created with s2i:

  • docker run -e NODE_CMD="node server.js" -e INIT_WRAPPER=true node-16-rhel9

By using NODE_CMD env variable:

  • We avoid running npm, which results in having several pros

In addition to NODE_CMD env variable, you can combine it with the INIT_WRAPPER env variable which wraps/runs the value of NODE_CMD variable with this script. That way we achieve:

  • reaping zombie processes
  • Properly forwarding signals such as (SIGINT, SIGTERM) to the node.js application inside the container.

@pacostas
Copy link
Contributor Author

#120

@pacostas
Copy link
Contributor Author

#275

@mhdawson
Copy link
Member

@pacostas I think we should add this for 18 and 18-minimal as well.

@pacostas In addition to being able to run with docker run -e NODE_CMD="node server.js". We'd also want to be able to use when building a container with S2i or docker. We should probably find the right place in the documentation to add some explanation of how to use it those three cases. Just let me know if you have an questions about that.

@hhorak thanks for the pointer to the init-wrapper.

@pacostas
Copy link
Contributor Author

pacostas commented Oct 17, 2022

The commit 72896d2 is not very descriptive, what it does is adding the init-wrapper on node-18. It is also tested.

@pacostas
Copy link
Contributor Author

@mhdawson Thanks for indicating on adding NODE_CMD in the documentation. I added it on the readme page of each node version under the NPM_RUN variable. 2e3958e

Below is the text I've added for documenting the NODE_CMD variable, feel free to review it, by adding a comment here and I'll update it in all the places it is being used.

NODE_CMD
When specified (e.g.Specify NODE_CMD="node server.js") the NODE_CMD is executed by the init-wrapper script, which handles reaping zombie processes and signal forwarding (SIGINT, SIGTERM) to Node.js application.

@pacostas
Copy link
Contributor Author

@mhdawson 06f9b3d d404db7 cf13d07

I've added these commits, with docs about the init-wrapper and with some examples.

18/README.md Outdated Show resolved Hide resolved
18/README.md Outdated Show resolved Hide resolved
18/README.md Outdated Show resolved Hide resolved
@@ -313,6 +316,47 @@ Below is an example _package.json_ file with the _main_ attribute and _start_ sc
#### Note:
`oc rsync` is only available in versions 3.1+ of OpenShift.

## init-wrapper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also mention the INIT_WRAPPER env variables in the Environment variables for Source-to-Image above even if they only refer to this later section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, exaclty, forgot to add it. Added it on commit a59f33d

echo "exec $NODE_CMD"
fi
elif [ "$INIT_WRAPPER" == true ]; then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank space?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it on commit a59f33d

18/s2i/bin/run Outdated
if [ -f "server.js" ]; then
start_file="server.js"
elif [ -f "index.js" ]; then
start_file="main.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a typo should be index.js ? Makes me wonder if there is anywhere we could add some testing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also are these three files documented in the documentation somewhere else? Otherwise we might want to add that to the documentation as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wondered if we could pull what npm start would have used from package.json ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a typo should be index.js ? Makes me wonder if there is anywhere we could add some testing?

Fixed it on commit a59f33d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wondered if we could pull what npm start would have used from package.json ?

Yes we can do that with bash code, working on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also are these three files documented in the documentation somewhere else? Otherwise we might want to add that to the documentation as well.

We only mention the presence of these files when we describe the init-wrapper functionality. I dont think we should mention something more because is specific to the scope of how init-wrapper works - looks for the files.

18/README.md Show resolved Hide resolved
@mhdawson
Copy link
Member

@pacostas I'm looking for the part which enables INIT_WRAPPER to be used in the build part of S2i. I assume that somehow INIT_WRAPPER being set in env during built must result in INIT_WRAPPER being set in the built in environment variables for the resulting container but I can't find the part that would make that happen?

@pacostas
Copy link
Contributor Author

@pacostas I'm looking for the part which enables INIT_WRAPPER to be used in the build part of S2i. I assume that somehow INIT_WRAPPER being set in env during built must result in INIT_WRAPPER being set in the built in environment variables for the resulting container but I can't find the part that would make that happen?

@mhdawson I think you mean this one? https://github.com/pacostas/s2i-nodejs-container/blob/a59f33d729564d7509b56680cfd84a7406f9a5ca/18/README.md?plain=1#L263-L275

@mhdawson
Copy link
Member

@pacostas what I meant were the changes that allow

s2i -e INIT_WRAPPER=true build . buildImage node-app
docker run node-app

to work? Or were there none needed specifically for that?

@pacostas
Copy link
Contributor Author

@pacostas what I meant were the changes that allow

s2i -e INIT_WRAPPER=true build . buildImage node-app
docker run node-app

to work? Or were there none needed specifically for that?

@mhdawson Exactly, the s2i propagates by itself the env variables that have been set on the build process to the output image. So none changes had to be done for that.

@mhdawson
Copy link
Member

@pacostas thanks for helping me understand that.

@mhdawson
Copy link
Member

@pacostas did you built a containers to test/try out in the build process? If is it something you can give me access to in order to experiment?

@pacostas
Copy link
Contributor Author

@mhdawson sure

cd ./s2i-nodejs-container
git remote add pacostas https://github.com/pacostas/s2i-nodejs-container.git
git fetch --all
git checkout command-option-on-14-and-16

# the name of the branch is a bit wrong, it should also include the number 18
 
cd 16

# Create the builder image 
docker build -f Dockerfile.fedora . -t fedora16build

# Create the app image
s2i -e INIT_WRAPPER=true build https://github.com/pacostas/hello-node-16.git fedora16build myapp16fedora

# run the the app image 
docker run myapp16fedora

@pacostas
Copy link
Contributor Author

I also wondered if we could pull what npm start would have used from package.json ?

Yes we can do that with bash code, working on it.

@mhdawson
by looking at the npm start documentation https://docs.npmjs.com/cli/v9/commands/npm-start?v=true I tried to reproduce the start command and I ended up with below implementation which will replace the current one of this pr https://github.com/pacostas/s2i-nodejs-container/blob/a59f33d729564d7509b56680cfd84a7406f9a5ca/18/s2i/bin/run#L31-L45

elif [ "$INIT_WRAPPER" == true ]; then

    package_json_start=$(cat package.json | grep "start" | sed -r 's/(.*)"start"([^:]*[^"]*")(.*)",/\3/')
    package_json_main=$(cat package.json | grep "main" | sed -r 's/(.*)"main"([^:]*[^"]*")(.*)",/\3/')

    if [ -n "$package_json_start" ]; then
        start_command=$package_json_start
    elif [ -n $package_json_main ]; then
        start_command="node ."
    elif [ -f "server.js" ]; then
        start_command="node server.js"
    else
        echo "Failed to find file for starting the Node.js application"
        exit 1
    fi

    echo "launching via init wrapper..."
    exec ${STI_SCRIPTS_PATH}/init-wrapper $start_command
else

@mhdawson
Copy link
Member

mhdawson commented Dec 2, 2022

@pacostas what you have for start looks reasonable to me.

@lholmquist since I'm out next week could you do the final review once @pacostas integrates the latest change?

14-minimal/README.md Outdated Show resolved Hide resolved
Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the one small typo I mentioned has been fixed.

@mhdawson
Copy link
Member

@pacostas just one small typo I noticed during my final review otherwise looks good !

@mhdawson
Copy link
Member

One other thing to check @pacostas should we add a test for this feature in - https://github.com/sclorg/s2i-nodejs-container/blob/master/test/run

@phracek
Copy link
Member

phracek commented Jan 2, 2023

[test-all]

@pacostas
Copy link
Contributor Author

pacostas commented Jan 3, 2023

One other thing to check @pacostas should we add a test for this feature in - https://github.com/sclorg/s2i-nodejs-container/blob/master/test/run

@mhdawson
Thank you for the URL, although I'm not sure how tests work and how to write them on this codebase. But theoretically, to test this implementation I would go with 2 tests:

  1. unit testing the run_node function https://github.com/pacostas/s2i-nodejs-container/blob/0bc965615e69639e0cb76ebd63e5f03990f15166/18/s2i/bin/run#L18 where in that case this would require refactoring it to the point where the run_node would return a string (and later on another function executing that string). That way we ensure that if statements work properly on each PR.
  2. running the https://github.com/pacostas/s2i-nodejs-container/blob/0bc965615e69639e0cb76ebd63e5f03990f15166/18/s2i/bin/run#L48 command as we do with https://github.com/pacostas/s2i-nodejs-container/blob/0bc965615e69639e0cb76ebd63e5f03990f15166/18/s2i/bin/run#L51 for all the tests.

I wouldn't go with integration tests because we would need more tests to have the same coverage, as with above 2 tests.

Also, we can avoid unit tests if there is no place for that type of tests in the project. IMO the logic inside run_node function is very simple and small, to sacrifice time and effort for adding unit tests on the project just for testing one simple function.

bottom line: IMO just running all the tests with exec ${STI_SCRIPTS_PATH}/init-wrapper $start_command as we do with exec npm run -d $NPM_RUN , we are good.

@phracek
Copy link
Member

phracek commented Nov 6, 2023

@pacostas Can you please have a look why tests for RHEL7-14 are failing?

The logs are here: http://artifacts.osci.redhat.com/testing-farm/13d9c6fd-6bff-4a4d-8bd0-67f979a3144e/

@phracek
Copy link
Member

phracek commented Nov 6, 2023

[test-openshift]

@pacostas pacostas force-pushed the command-option-on-14-and-16 branch 2 times, most recently from 5d13280 to 3a47b01 Compare November 10, 2023 15:04
@pacostas
Copy link
Contributor Author

@pacostas Can you please have a look why tests for RHEL7-14 are failing?

The logs are here: http://artifacts.osci.redhat.com/testing-farm/13d9c6fd-6bff-4a4d-8bd0-67f979a3144e/

@phracek On rhel 7 node 14 locally all tests pass. I cant reproduce that error. My only suspicion is that the STI_SCRIPTS_PATH variable is not defined, so i changed it to an absolute path 25bf629. Can you run only the tests for rhel7 on node 14?

@lholmquist
Copy link
Member

[test-openshift]

@zmiklank
Copy link
Member

Hi @lholmquist, I see a lot of work has been done on this PR. Is something blocking the merge currently? Could you please re-review, it seems that @pacostas has done some changes in order to fix the issues with tests. I can re-run the tests again:

[test-all]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants