什么是在 OpenShift v2 push hook 期间的最后一秒重置 PATH 变量?

What is resetting the PATH variable at the last second during an OpenShift v2 push hook?

TL;DR:正在运行的应用程序,已克隆它,克隆无法从 push hook 正确启动(但如果我 ssh 进入,则手动工作正常。)PATH 添加了正确的 Node 版本,但在某处就在最后一步,不正确的节点版本再次添加到 PATH 之前。

这里的路径是正确的:

remote:     PATH = /var/lib/openshift/.../app-root/data//node-v4.x.x-linux-x64/bin:/var/lib/openshift/.../app-root/runtime/repo/node_modules/.bin:/var/lib/openshift/...//.node_modules/.bin:/opt/rh/nodejs010/root/usr/bin:/bin:/usr/bin:/usr/sbin

然后在之后立即不正确,在此处:

remote: Starting NodeJS cartridge
remote: Tue Aug 22 2017 15:39:10 GMT-0400 (EDT): Starting application 'staging' ...

那么在最后两行中或之前代表了哪些脚本和挂钩? PATH 不只是向自身添加行...


我有一个可用的 OpenShift v2 应用程序 运行正在使用 NodeJS 版本

它似乎是根据 设置的,因为来自该 repo 的脚本(用于使用标记文件)存在于 .openshift.

我使用 rhc create --from-app 设置了第二个基于工作的,重置 repo,然后重新部署到它上面。第二个效果很好,除了开始 node:

的最后一步
remote: npm info ok 
remote: NOTE: The .openshift/action_hooks/build hook is not executable, to make it executable:
remote:       On Windows run:   git update-index --chmod=+x .openshift/action_hooks/build
remote:       On Linux/OSX run: chmod +x .openshift/action_hooks/build
remote: Preparing build for deployment
remote: Deployment id is cedf7f51
remote: Activating deployment
remote: NOTE: The .openshift/action_hooks/deploy hook is not executable, to make it executable:
remote:       On Windows run:   git update-index --chmod=+x .openshift/action_hooks/deploy
remote:       On Linux/OSX run: chmod +x .openshift/action_hooks/deploy
remote: 
remote:   - pre_start_nodejs: Adding Node.js version 4.x.x binaries to path
remote:   - PATH set to include custom node version (4.x.x) from
remote:        /var/lib/openshift/.../app-root/data//node-v4.x.x-linux-x64/bin 
remote:     PATH = /var/lib/openshift/.../app-root/data//node-v4.x.x-linux-x64/bin:/var/lib/openshift/.../app-root/runtime/repo/node_modules/.bin:/var/lib/openshift/...//.node_modules/.bin:/opt/rh/nodejs010/root/usr/bin:/bin:/usr/bin:/usr/sbin
remote: Starting NodeJS cartridge
remote: Tue Aug 22 2017 15:39:10 GMT-0400 (EDT): Starting application 'staging' ...
remote: Waiting for application port (8080) become available ...

(到目前为止,除了名称之外,一切都与正在运行的应用程序完全相同。)

remote: Application 'staging' failed to start (port 8080 not available)
remote: -------------------------
remote: Git Post-Receive Result: failure
remote: Activation status: failure
remote: Activation failed for the following gears:
remote: ... (Error activating gear: CLIENT_ERROR: Failed to execute: 'control start' for /var/lib/openshift/.../nodejs
remote: #<IO:0x00000001cd42d0>
remote: #<IO:0x00000001cd4258>
remote: )
remote: Deployment completed with status: failure
remote: postreceive failed

rhc envrhc app show 显示设置在所有方面都是相同的。

我从其他问题中得知上面的 "port 8080" 部分是转移注意力的问题。另外,如果我 rhc ssh 手动 node www.js,它使用那个端口就好了,我可以通过浏览器访问应用程序。

所以我使用 rhc tail 进行了调查。由于粗箭头符号,我可以看到它反复启动失败:

pg.on('error', (err) => {
                      ^
SyntaxError: Unexpected token >
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
...
DEBUG: Program node ./www.js exited with code 8
DEBUG: Starting child process with 'node ./www.js'

然而,如果我 rhc ssh 进入同一台服务器并 运行 node --version,我会得到较新的版本(与其他服务器的版本相同,都从我部署的 .openshift 目录中的标记文件。)

我猜 push hook 的最后一步是使用 Node 0.10,因为这就是这两个应用程序中的墨盒名称。但是,如果我检查添加到 PATH 的路径,实际上那里确实存在更新的 Node 版本。

然而,这就是事情变得有趣的地方。上面报告的 PATH(其中 4.x.x 是前置的) 不再是节点启动时的路径 。我把 www.js 改成只吐出 process.env.PATH 并比较了两者。后者在开头添加了这两条路径!

/opt/rh/nodejs010/root/usr/bin
/opt/rh/v8314/root/usr/bin

这是什么原因,我该如何阻止它?在这些输出行中还有什么机会?

remote:     PATH = /var/lib/openshift/.../app-root/data//node-v4.x.x-linux-x64/bin:/var/lib/openshift/.../app-root/runtime/repo/node_modules/.bin:/var/lib/openshift/...//.node_modules/.bin:/opt/rh/nodejs010/root/usr/bin:/bin:/usr/bin:/usr/sbin
remote: Starting NodeJS cartridge
remote: Tue Aug 22 2017 15:39:10 GMT-0400 (EDT): Starting application 'staging' ...

(为什么只在我的第二个应用程序上,当我使用 --from-app 创建它时,所有其他设置似乎在两者之间是相等的?)

Node.js (Latest) 墨盒

@hub.openshift : Node.js Latest

By default, the Node.js version is determined by querying semver.io/node/stable.

A different URL can be specified either via NODE_VERSION_URL environment variable or by setting .openshift/NODE_VERSION_URL marker in your application repository.

For instance, you'd get the latest 0.10.x by putting this in NODE_VERSION_URL variable or .openshift/NODE_VERSION_URL marker:

https://semver.io/node/resolve/0.10

If you're using a non-default Node.js version and you're planning to scale the application across multiple gears, you must use the environment variable (learn here why).


其他可能的方法

pre_build action hook 解决方法:

#!/bin/bash

#NODE_VERSION_URL
TARGET_NODE_VERSION=$(node -e "var p = require('$OPENSHIFT_REPO_DIR/package.json')||{engines:{}}; console.log(encodeURI(p.engines.node||''));")
echo "https://semver.io/node/resolve/$TARGET_NODE_VERSION" > ${HOME}.env/user_vars/NODE_VERSION_URL

#NPM_VERSION_URL
TARGET_VERSION=$(node -e "var p = require('$OPENSHIFT_REPO_DIR/package.json')||{engines:{}}; console.log(encodeURI(p.engines.npm||''));")
echo "https://semver.io/npm/resolve/$TARGET_VERSION" > ${HOME}.env/user_vars/NPM_VERSION_URL

package.json engines 解决方法:

添加the .openshift folder from this repo will let you run any version of nodejs that you need using the engines section of your package.json file (see the README for usage):

"engines": {
    "node": ">= 0.12.0"
 },

在应用程序最初用于自定义其 NodeJS 版本的 129 个回购分支之一中,a comment on an issue 解决了它:

.openshift\lib\utils中添加一行:

#  Add the node binary path to the PATH.
export OPENSHIFT_NODEJS_VERSION=0.6 ### this is the new line
export PATH="$node_bin_path:${PATH}"

"It's important, like you mentioned, that it's set to 0.6, EVEN IF YOU'RE NOT RUNNING 0.6."

将它设置为 0.6 也很重要,即使您恢复到的原始 Node 版本也不是 0.6。 (我试过 0.10,没有运气。0.6 是神奇的答案,在这里。)