freedombone-utils-nodejs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # nodejs functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # For reasons unknown we initially have to upgrade to an intermediate version
  31. # before getting to the version we want
  32. VARIANTS='mesh'
  33. # change these versions at your peril. Things will often crash if you don't
  34. # have specifically the correct versions
  35. NODEJS_VERSION='8.11.1'
  36. NODEJS_N_VERSION='2.1.7'
  37. NPM_VERSION='5.6.0'
  38. # This file keeps track of the apps needing nodejs
  39. # so that it can be removed if tere are no apps which need it
  40. NODEJS_INSTALLED_APPS_FILE=$HOME/.nodejs-apps
  41. function get_npm_arch {
  42. N_ARCH='x86'
  43. NPM_ARCH='ia32'
  44. if [[ $ARCHITECTURE == 'arm'* ]]; then
  45. NPM_ARCH='armv7l'
  46. N_ARCH='armv7l'
  47. fi
  48. if [[ $ARCHITECTURE == *"aarch"* ]]; then
  49. NPM_ARCH='arm64'
  50. N_ARCH='arm64'
  51. fi
  52. if [[ $ARCHITECTURE == 'x86_64' || $ARCHITECTURE == 'amd64' ]]; then
  53. NPM_ARCH='x64'
  54. N_ARCH='x64'
  55. fi
  56. }
  57. function nodejs_fix_cpu_detection {
  58. # fix for failing cpu detection during image build with qemu, see https://github.com/npm/npm/issues/19265
  59. if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
  60. sed -i "s|require('os').cpus().length|(require('os').cpus() || { length: 1 }).length|g" "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js"
  61. fi
  62. if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
  63. sed -i "s|require('os').cpus().length|(require('os').cpus() || { length: 1 }).length|g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js"
  64. fi
  65. # installing worker farm fixes the cpu detection bug
  66. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g worker-farm@1.6.0 --save
  67. }
  68. function mesh_install_nodejs {
  69. mesh_install_nodejs_prefix=
  70. if [ "$rootdir" ]; then
  71. mesh_install_nodejs_prefix="chroot $rootdir"
  72. fi
  73. $mesh_install_nodejs_prefix apt-get -yq install wget curl g++ m4 libtool automake
  74. $mesh_install_nodejs_prefix apt-get -yq install libxext-dev libxtst-dev libxkbfile-dev
  75. $mesh_install_nodejs_prefix apt-get -yq install apt-transport-https
  76. $mesh_install_nodejs_prefix wget https://deb.nodesource.com/gpgkey/nodesource.gpg.key -O /root/node.gpg.key
  77. if [ ! -f "$rootdir/root/node.gpg.key" ]; then
  78. echo $'Unable to obtain gpg key for nodejs repo'
  79. NODE_UPGRADE=
  80. exit 6389252
  81. fi
  82. $mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
  83. echo "deb https://deb.nodesource.com/node_8.x stretch main" > "$rootdir/etc/apt/sources.list.d/nodesource.list"
  84. echo "deb-src https://deb.nodesource.com/node_8.x stretch main" >> "$rootdir/etc/apt/sources.list.d/nodesource.list"
  85. $mesh_install_nodejs_prefix apt-mark -q unhold nodejs
  86. $mesh_install_nodejs_prefix apt-get update
  87. $mesh_install_nodejs_prefix apt-get -yq remove --purge nodejs
  88. if [ ! $NODE_UPGRADE ]; then
  89. if [ -d "$rootdir/usr/local/lib/node_modules" ]; then
  90. rm -rf "$rootdir/usr/local/lib/node_modules"
  91. fi
  92. fi
  93. if [ -f "$rootdir/usr/local/bin/node" ]; then
  94. rm "$rootdir/usr/local/bin/node"
  95. fi
  96. if [ -f "$rootdir/usr/bin/node" ]; then
  97. rm "$rootdir/usr/bin/node"
  98. fi
  99. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  100. rm "$rootdir/usr/bin/nodejs"
  101. fi
  102. $mesh_install_nodejs_prefix apt-get -yq install nodejs
  103. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  104. cp "$rootdir/usr/bin/nodejs" "$rootdir/usr/bin/node"
  105. fi
  106. if [ -f "$rootdir/usr/bin/node" ]; then
  107. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  108. fi
  109. $mesh_install_nodejs_prefix apt-mark -q hold nodejs
  110. if [ ! -f "${rootdir}/usr/bin/node" ]; then
  111. if [ ! -f "${rootdir}/usr/local/bin/node" ]; then
  112. if [ ! -f "${rootdir}/usr/bin/nodejs" ]; then
  113. echo $'nodejs was not installed'
  114. NODE_UPGRADE=
  115. exit 63962
  116. fi
  117. fi
  118. fi
  119. if [ ! -f "$rootdir/usr/bin/node" ]; then
  120. echo $'/usr/bin/node not found'
  121. NODE_UPGRADE=
  122. exit 7235728
  123. fi
  124. get_npm_arch
  125. $mesh_install_nodejs_prefix npm config set unsafe-perm true
  126. nodejs_setup_global_modules
  127. nodejs_fix_cpu_detection
  128. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  129. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  130. cp "$rootdir/usr/local/bin/npm" /usr/bin/npm
  131. fi
  132. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  133. # deliberately called again
  134. nodejs_fix_cpu_detection
  135. # update from the old debian nodejs version
  136. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
  137. $mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
  138. cp "$rootdir/root/npm" "$rootdir/usr/bin/npm"
  139. # TEST
  140. grep -r "require('os').cpus().length" "$rootdir/.npm-global/lib/node_modules/npm"/*
  141. # deliberate second install of npm
  142. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  143. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  144. cp "$rootdir/usr/local/bin/npm" /usr/bin/npm
  145. fi
  146. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  147. if [ -f "$rootdir/usr/bin/node" ]; then
  148. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  149. fi
  150. # check the version numbers
  151. cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
  152. #!/bin/bash
  153. node_version=\$(node -v)
  154. if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
  155. echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
  156. exit 1
  157. fi
  158. npm_version=\$(npm -v)
  159. if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
  160. echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
  161. exit 2
  162. fi
  163. EOF
  164. chmod +x "$rootdir/usr/bin/test_nodejs_install"
  165. if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
  166. echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
  167. NODE_UPGRADE=
  168. exit 76835282
  169. fi
  170. rm "$rootdir/usr/bin/test_nodejs_install"
  171. NODE_UPGRADE=
  172. }
  173. function nodejs_upgrade {
  174. if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
  175. return
  176. fi
  177. nodejs_setup_global_modules
  178. if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
  179. if [ -f /usr/local/bin/node ]; then
  180. CURR_NODE_VERSION=$(node --version)
  181. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  182. return
  183. fi
  184. fi
  185. fi
  186. if [ -f /usr/local/bin/node ]; then
  187. CURR_NODE_VERSION=$(node --version)
  188. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  189. return
  190. fi
  191. fi
  192. read_config_param ARCHITECTURE
  193. get_npm_arch
  194. NODE_UPGRADE=1
  195. rootdir=
  196. mesh_install_nodejs
  197. npm update -g
  198. }
  199. function nodejs_setup_global_modules {
  200. if [ ! -f /usr/local/bin/node ]; then
  201. return
  202. fi
  203. if [ ! -d "$rootdir/root/.npm-global" ]; then
  204. mkdir "$rootdir/root/.npm-global"
  205. fi
  206. $mesh_install_nodejs_prefix npm config set prefix '/root/.npm-global'
  207. if ! grep -q "PATH=/root/.npm-global/bin" "$rootdir/root/.bashrc"; then
  208. echo "PATH=/root/.npm-global/bin:\$PATH" >> "$rootdir/root/.bashrc"
  209. fi
  210. if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
  211. echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
  212. fi
  213. # shellcheck disable=SC2086
  214. $mesh_install_nodejs_prefix export PATH=/root/.npm-global/bin:$PATH
  215. $mesh_install_nodejs_prefix export NPM_CONFIG_PREFIX=/root/.npm-global
  216. }
  217. function remove_nodejs {
  218. if [ ! "$1" ]; then
  219. return
  220. fi
  221. if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  222. #remove_app nodejs
  223. return
  224. fi
  225. sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
  226. if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
  227. apt-get -yq remove --purge nodejs
  228. if [ -f /usr/bin/nodejs ]; then
  229. rm /usr/bin/nodejs
  230. fi
  231. if [ -f /usr/local/bin/node ]; then
  232. rm /usr/local/bin/node
  233. fi
  234. if [ -f /usr/bin/node ]; then
  235. rm /usr/bin/node
  236. fi
  237. if [ -d /usr/lib/node_modules ]; then
  238. rm -rf /usr/lib/node_modules
  239. fi
  240. if [ -f /usr/bin/n ]; then
  241. rm /usr/bin/n
  242. fi
  243. remove_app nodejs
  244. rm "$NODEJS_INSTALLED_APPS_FILE"
  245. apt-get -yq autoremove
  246. fi
  247. }
  248. function upgrade_nodejs {
  249. CURR_NODE_VERSION=$(node --version)
  250. CURR_NPM_VERSION=$(npm --version)
  251. CURR_N_VERSION=$(n --version)
  252. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  253. npm upgrade -g npm@${NPM_VERSION} --save
  254. fi
  255. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  256. npm upgrade -g n@${NODEJS_N_VERSION} --save
  257. fi
  258. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  259. n ${NODEJS_VERSION}
  260. fi
  261. cp /usr/local/bin/n /usr/bin/n
  262. if [ -f /usr/local/bin/npm ]; then
  263. cp /usr/local/bin/npm /usr/bin/npm
  264. fi
  265. if [ -f /usr/local/bin/node ]; then
  266. cp /usr/local/bin/node /usr/bin/nodejs
  267. cp /usr/local/bin/node /usr/bin/node
  268. fi
  269. }
  270. function install_nodejs {
  271. if [ "$INSTALLING_MESH" ]; then
  272. mesh_install_nodejs
  273. return
  274. fi
  275. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  276. upgrade_nodejs
  277. return
  278. fi
  279. if [ ! "$ARCHITECTURE" ]; then
  280. ARCHITECTURE=$(uname -m)
  281. fi
  282. rootdir=
  283. mesh_install_nodejs
  284. # verify nodejs versions are what we expect
  285. CURR_NODE_VERSION=$(node --version)
  286. CURR_NPM_VERSION=$(npm --version)
  287. CURR_N_VERSION=$(n --version)
  288. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  289. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  290. exit 6728252
  291. fi
  292. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  293. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  294. exit 5267452
  295. fi
  296. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  297. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  298. exit 5274527
  299. fi
  300. if [ "$1" ]; then
  301. if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
  302. echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
  303. fi
  304. fi
  305. mark_completed "${FUNCNAME[0]}"
  306. }
  307. # NOTE: deliberately there is no "exit 0"