From 9876ecc2e0316316975c3e557b7420d33f884bba Mon Sep 17 00:00:00 2001 From: Chris McCormick Date: Mon, 5 Aug 2019 16:08:48 +0800 Subject: [PATCH] Fix to upgrade node version correctly. (#73) This patch changes piku to check the installed node version and leave it if correct. It also separates the nodeenv install and the npm install steps. It will also warn the user if they try to change the node version in a running app, and tells them to stop and then deploy the app again in order to update the node version. Fixes #70. --- docs/ENV.md | 6 ++++-- piku.py | 38 ++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/docs/ENV.md b/docs/ENV.md index 4e5b29e..42b4ebd 100644 --- a/docs/ENV.md +++ b/docs/ENV.md @@ -1,4 +1,4 @@ -## Configuring Piku via ENV +# Configuring Piku via ENV You can configure deployment settings by placing special variables in an `ENV` file deployed with your app. @@ -10,7 +10,9 @@ You can configure deployment settings by placing special variables in an `ENV` f ### Node -* `NODE_VERSION`: installs a particular version of node for your app if `nodeenv` is found on the path +* `NODE_VERSION`: installs a particular version of node for your app if `nodeenv` is found on the path. + +**Note**: you will need to stop and re-deploy the app to change the node version in a running app. ## Network Settings diff --git a/piku.py b/piku.py index 4939dd3..56f2731 100755 --- a/piku.py +++ b/piku.py @@ -371,6 +371,7 @@ def do_deploy(app, deltas={}, newrev=None): echo("-----> Releasing", fg='green') retval = call(workers["release"], cwd=app_path, env=settings, shell=True) if retval: + echo("-----> Exiting due to release command error value: {}".format(retval)) exit(retval) workers.pop("release", None) else: @@ -426,19 +427,32 @@ def deploy_node(app, deltas={}): makedirs(node_path) first_time = True - if exists(deps): - if first_time or getmtime(deps) > getmtime(node_path): - env = { - 'VIRTUAL_ENV': virtualenv_path, - 'NODE_PATH': node_path, - 'NPM_CONFIG_PREFIX': abspath(join(node_path, "..")), - "PATH": ':'.join([join(virtualenv_path, "bin"), join(node_path, ".bin"),environ['PATH']]) - } - if exists(env_file): - env.update(parse_settings(env_file, env)) - if env.get("NODE_VERSION") and check_requirements(['nodeenv']): + env = { + 'VIRTUAL_ENV': virtualenv_path, + 'NODE_PATH': node_path, + 'NPM_CONFIG_PREFIX': abspath(join(node_path, "..")), + "PATH": ':'.join([join(virtualenv_path, "bin"), join(node_path, ".bin"),environ['PATH']]) + } + if exists(env_file): + env.update(parse_settings(env_file, env)) + + version = env.get("NODE_VERSION") + node_binary = join(virtualenv_path, "bin", "node") + installed = check_output("{} -v".format(node_binary), cwd=join(APP_ROOT, app), env=env, shell=True).decode("utf8").rstrip("\n") if exists(node_binary) else "" + + if version and check_requirements(['nodeenv']): + if not installed.endswith(version): + started = glob(join(UWSGI_ENABLED, '{}*.ini'.format(app))) + if installed and len(started): + echo("Warning: Can't update node with app running. Stop the app & retry.", fg='yellow') + else: echo("-----> Installing node version '{NODE_VERSION:s}' using nodeenv".format(**env), fg='green') - call("nodeenv --prebuilt --node={NODE_VERSION:s} --force {VIRTUAL_ENV:s}".format(**env), cwd=virtualenv_path, env=env, shell=True) + call("nodeenv --prebuilt --node={NODE_VERSION:s} --clean-src --force {VIRTUAL_ENV:s}".format(**env), cwd=virtualenv_path, env=env, shell=True) + else: + echo("-----> Node is installed at {}.".format(version)) + + if exists(deps) and check_requirements(['npm']): + if first_time or getmtime(deps) > getmtime(node_path): echo("-----> Running npm for '{}'".format(app), fg='green') symlink(node_path, node_path_tmp) call('npm install', cwd=join(APP_ROOT, app), env=env, shell=True)