diff --git a/scripts/_common.sh b/scripts/_common.sh index 345d1c5..f62eb39 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -112,81 +112,88 @@ sudo_path () { } # INFOS -# nvm utilise la variable PATH pour stocker le path de la version de node à utiliser. +# n (Node version management) utilise la variable PATH pour stocker le path de la version de node à utiliser. # C'est ainsi qu'il change de version -# En attendant une généralisation de root, il est possible d'utiliser sudo aevc le helper temporaire sudo_path +# En attendant une généralisation de root, il est possible d'utiliser sudo avec le helper temporaire sudo_path # Il permet d'utiliser sudo en gardant le $PATH modifié -# ynh_install_nodejs installe la version de nodejs demandée en argument, avec nvm +# ynh_install_nodejs installe la version de nodejs demandée en argument, avec n # ynh_use_nodejs active une version de nodejs dans le script courant # 3 variables sont mises à disposition, et 2 sont stockées dans la config de l'app # - nodejs_path: Le chemin absolu de cette version de node -# Utilisé pour des appels directs à npm ou node. +# Utilisé pour des appels directs à node. # - nodejs_version: Simplement le numéro de version de nodejs pour cette application # - nodejs_use_version: Un alias pour charger une version de node dans le shell courant. # Utilisé pour démarrer un service ou un script qui utilise node ou npm # Dans ce cas, c'est $PATH qui contient le chemin de la version de node. Il doit être propagé sur les autres shell si nécessaire. -nvm_install_dir="/opt/nvm" +n_install_dir="/opt/node_n" ynh_use_nodejs () { - nodejs_path=$(ynh_app_setting_get $app nodejs_path) nodejs_version=$(ynh_app_setting_get $app nodejs_version) - # And store the command to use a specific version of node. Equal to `nvm use version` - nodejs_use_version="source $nvm_install_dir/nvm.sh; nvm use \"$nodejs_version\"" + load_n_path="[[ :$PATH: == *\":$n_install_dir/bin:\"* ]] || PATH+=\":$n_install_dir/bin\"" - # Desactive set -u for this script. - set +u - eval $nodejs_use_version - set -u + nodejs_use_version="n $nodejs_version" + + # "Load" a version of node + eval $load_n_path; $nodejs_use_version + eval $load_n_path; sudo env "PATH=$PATH" $nodejs_use_version + + # Get the absolute path of this version of node + nodejs_path="$(n bin $nodejs_version)" + + # Make an alias for node use + ynh_node_exec="eval $load_n_path; n use $nodejs_version" + sudo_ynh_node_exec="eval $load_n_path; sudo env \"PATH=$PATH\" n use $nodejs_version" } ynh_install_nodejs () { + # Use n, https://github.com/tj/n to manage the nodejs versions local nodejs_version="$1" - local nvm_install_script="https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh" + local n_install_script="https://git.io/n-install" - local nvm_exec="source $nvm_install_dir/nvm.sh; nvm" + # Create $n_install_dir + sudo mkdir -p "$n_install_dir" - sudo mkdir -p "$nvm_install_dir" + # Load n path in PATH + PATH+=":$n_install_dir/bin" - # If nvm is not previously setup, install it - "$nvm_exec --version" > /dev/null 2>&1 || \ - ( cd "$nvm_install_dir" - echo "Installation of NVM" - sudo wget --no-verbose "$nvm_install_script" -O- | sudo NVM_DIR="$nvm_install_dir" bash > /dev/null) + # If n is not previously setup, install it + n --version > /dev/null 2>&1 || \ + ( echo "Installation of N - Node.js version management" >&2; \ + curl -sL $n_install_script | sudo N_PREFIX="$n_install_dir" bash -s -- -y $nodejs_version ) - # Install the requested version of nodejs - sudo su -c "$nvm_exec install \"$nodejs_version\" > /dev/null" + # Install the requested version of nodejs (except for the first installation of n, which installed the requested version of node.) + sudo env "PATH=$PATH" n $nodejs_version + + # Use the real installed version. Sometimes slightly different + nodejs_version=$(node --version | cut -c2-) # Store the ID of this app and the version of node requested for it - echo "$YNH_APP_ID:$nodejs_version" | sudo tee --append "$nvm_install_dir/ynh_app_version" + echo "$YNH_APP_ID:$nodejs_version" | sudo tee --append "$n_install_dir/ynh_app_version" - # Get the absolute path of this version of node - nodejs_path="$(dirname "$(sudo su -c "$nvm_exec which \"$nodejs_version\"")")" - - # Store nodejs_path and nodejs_version into the config of this app - ynh_app_setting_set $app nodejs_path $nodejs_path + # Store nodejs_version into the config of this app ynh_app_setting_set $app nodejs_version $nodejs_version ynh_use_nodejs } ynh_remove_nodejs () { - nodejs_version=$(ynh_app_setting_get $app nodejs_version) + ynh_use_nodejs # Remove the line for this app - sudo sed --in-place "/$YNH_APP_ID:$nodejs_version/d" "$nvm_install_dir/ynh_app_version" + sudo sed --in-place "/$YNH_APP_ID:$nodejs_version/d" "$n_install_dir/ynh_app_version" # If none another app uses this version of nodejs, remove it. - if ! grep --quiet "$nodejs_version" "$nvm_install_dir/ynh_app_version" + if ! grep --quiet "$nodejs_version" "$n_install_dir/ynh_app_version" then - sudo su -c "source $nvm_install_dir/nvm.sh; nvm deactivate; nvm uninstall \"$nodejs_version\" > /dev/null" + n rm $nodejs_version fi - # If none another app uses nvm, remove nvm and clean the root's bashrc file - if [ ! -s "$nvm_install_dir/ynh_app_version" ] + # If none another app uses n, remove n + if [ ! -s "$n_install_dir/ynh_app_version" ] then - ynh_secure_remove "$nvm_install_dir" - sudo sed --in-place "/NVM_DIR/d" /root/.bashrc + ynh_secure_remove "$n_install_dir" + sudo sed --in-place "/N_PREFIX/d" /root/.bashrc fi } diff --git a/scripts/install b/scripts/install index 54574ad..469d02b 100755 --- a/scripts/install +++ b/scripts/install @@ -56,6 +56,7 @@ ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" # install nodejs ynh_install_nodejs 6.10.3 +ynh_package_install g++ # extract monica into $final_path extract_monica $final_path @@ -94,10 +95,10 @@ sudo sed -i "s/yunodomain/$domain/g" $final_path/.env sudo sed -i "s,yunopath,${path_url},g" $final_path/.env # Install nodejs packages -cd $final_path && sudo_path "$nodejs_path/npm" install -g npm@4 pnpm -cd $final_path && sudo_path "$nodejs_path/pnpm" install -cd $final_path && sudo_path "$nodejs_path/pnpm" install -g bower gulp -cd $final_path && sudo_path "$nodejs_path/bower" install -f --allow-root +cd $final_path && sudo_path npm install npm@4 pnpm +cd $final_path && sudo_path pnpm install +cd $final_path && sudo_path pnpm install bower gulp +cd $final_path && sudo_path bower install -f --allow-root # setup application config cd $final_path && sudo /usr/bin/php7.0 artisan -q migrate --force