freedombone-utils-nodejs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.8'
  37. NPM_VERSION='5.8.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/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js" ]; then
  63. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js"
  64. fi
  65. if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js" ]; then
  66. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js"
  67. fi
  68. if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
  69. 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"
  70. fi
  71. if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/node-gyp/lib/build.js" ]; then
  72. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/node-gyp/lib/build.js"
  73. fi
  74. if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js" ]; then
  75. 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/examples/pi/index.js"
  76. fi
  77. # installing worker farm fixes the cpu detection bug
  78. #$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g worker-farm@1.6.0 --save
  79. }
  80. function mesh_install_nodejs {
  81. mesh_install_nodejs_prefix=
  82. if [ "$rootdir" ]; then
  83. mesh_install_nodejs_prefix="chroot $rootdir"
  84. fi
  85. $mesh_install_nodejs_prefix apt-get -yq install wget curl g++ m4 libtool automake
  86. $mesh_install_nodejs_prefix apt-get -yq install libxext-dev libxtst-dev libxkbfile-dev
  87. $mesh_install_nodejs_prefix apt-get -yq install apt-transport-https
  88. $mesh_install_nodejs_prefix wget https://deb.nodesource.com/gpgkey/nodesource.gpg.key -O /root/node.gpg.key
  89. if [ ! -f "$rootdir/root/node.gpg.key" ]; then
  90. echo $'Unable to obtain gpg key for nodejs repo'
  91. NODE_UPGRADE=
  92. exit 6389252
  93. fi
  94. $mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
  95. echo "deb https://deb.nodesource.com/node_8.x stretch main" > "$rootdir/etc/apt/sources.list.d/nodesource.list"
  96. echo "deb-src https://deb.nodesource.com/node_8.x stretch main" >> "$rootdir/etc/apt/sources.list.d/nodesource.list"
  97. $mesh_install_nodejs_prefix apt-mark -q unhold nodejs
  98. $mesh_install_nodejs_prefix apt-get update
  99. $mesh_install_nodejs_prefix apt-get -yq remove --purge nodejs
  100. if [ ! $NODE_UPGRADE ]; then
  101. if [ -d "$rootdir/usr/local/lib/node_modules" ]; then
  102. rm -rf "$rootdir/usr/local/lib/node_modules"
  103. fi
  104. fi
  105. if [ -f "$rootdir/usr/local/bin/node" ]; then
  106. rm "$rootdir/usr/local/bin/node"
  107. fi
  108. if [ -f "$rootdir/usr/bin/node" ]; then
  109. rm "$rootdir/usr/bin/node"
  110. fi
  111. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  112. rm "$rootdir/usr/bin/nodejs"
  113. fi
  114. $mesh_install_nodejs_prefix apt-get -yq install nodejs
  115. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  116. cp "$rootdir/usr/bin/nodejs" "$rootdir/usr/bin/node"
  117. fi
  118. if [ -f "$rootdir/usr/bin/node" ]; then
  119. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  120. fi
  121. $mesh_install_nodejs_prefix apt-mark -q hold nodejs
  122. if [ ! -f "${rootdir}/usr/bin/node" ]; then
  123. if [ ! -f "${rootdir}/usr/local/bin/node" ]; then
  124. if [ ! -f "${rootdir}/usr/bin/nodejs" ]; then
  125. echo $'nodejs was not installed'
  126. NODE_UPGRADE=
  127. exit 63962
  128. fi
  129. fi
  130. fi
  131. if [ ! -f "$rootdir/usr/bin/node" ]; then
  132. echo $'/usr/bin/node not found'
  133. NODE_UPGRADE=
  134. exit 7235728
  135. fi
  136. get_npm_arch
  137. $mesh_install_nodejs_prefix npm config set unsafe-perm true
  138. nodejs_setup_global_modules
  139. nodejs_fix_cpu_detection
  140. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  141. if [ -f "$rootdir/.npm-global/bin/npm" ]; then
  142. cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/local/bin/npm"
  143. cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/bin/npm"
  144. fi
  145. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  146. cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
  147. fi
  148. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  149. # update from the old debian nodejs version
  150. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
  151. $mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
  152. nodejs_fix_cpu_detection
  153. cp "$rootdir/root/npm" "$rootdir/usr/bin/npm"
  154. cp "$rootdir/root/npm" "$rootdir/usr/local/bin/npm"
  155. # deliberate second install of npm
  156. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  157. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  158. cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
  159. fi
  160. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  161. if [ -f "$rootdir/usr/bin/node" ]; then
  162. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  163. fi
  164. # check the version numbers
  165. cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
  166. #!/bin/bash
  167. node_version=\$(node -v)
  168. if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
  169. echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
  170. exit 1
  171. fi
  172. npm_version=\$(npm -v)
  173. if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
  174. echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
  175. exit 2
  176. fi
  177. EOF
  178. chmod +x "$rootdir/usr/bin/test_nodejs_install"
  179. if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
  180. echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
  181. NODE_UPGRADE=
  182. exit 76835282
  183. fi
  184. rm "$rootdir/usr/bin/test_nodejs_install"
  185. NODE_UPGRADE=
  186. }
  187. function nodejs_upgrade {
  188. if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
  189. return
  190. fi
  191. nodejs_setup_global_modules
  192. if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
  193. if [ -f /usr/local/bin/node ]; then
  194. CURR_NODE_VERSION=$(node --version)
  195. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  196. return
  197. fi
  198. fi
  199. fi
  200. if [ -f /usr/local/bin/node ]; then
  201. CURR_NODE_VERSION=$(node --version)
  202. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  203. return
  204. fi
  205. fi
  206. read_config_param ARCHITECTURE
  207. get_npm_arch
  208. NODE_UPGRADE=1
  209. rootdir=
  210. mesh_install_nodejs
  211. npm update -g
  212. }
  213. function nodejs_setup_global_modules {
  214. if [ ! -f /usr/local/bin/node ]; then
  215. return
  216. fi
  217. if [ ! -d "$rootdir/root/.npm-global" ]; then
  218. mkdir "$rootdir/root/.npm-global"
  219. fi
  220. $mesh_install_nodejs_prefix npm config set prefix '/root/.npm-global'
  221. if ! grep -q "PATH=/root/.npm-global/bin" "$rootdir/root/.bashrc"; then
  222. echo "PATH=/root/.npm-global/bin:\$PATH" >> "$rootdir/root/.bashrc"
  223. fi
  224. if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
  225. echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
  226. fi
  227. # shellcheck disable=SC2086
  228. $mesh_install_nodejs_prefix export PATH=/root/.npm-global/bin:$PATH
  229. $mesh_install_nodejs_prefix export NPM_CONFIG_PREFIX=/root/.npm-global
  230. }
  231. function remove_nodejs {
  232. if [ ! "$1" ]; then
  233. return
  234. fi
  235. if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  236. #remove_app nodejs
  237. return
  238. fi
  239. sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
  240. if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
  241. apt-get -yq remove --purge nodejs
  242. if [ -f /usr/bin/nodejs ]; then
  243. rm /usr/bin/nodejs
  244. fi
  245. if [ -f /usr/local/bin/node ]; then
  246. rm /usr/local/bin/node
  247. fi
  248. if [ -f /usr/bin/node ]; then
  249. rm /usr/bin/node
  250. fi
  251. if [ -d /usr/lib/node_modules ]; then
  252. rm -rf /usr/lib/node_modules
  253. fi
  254. if [ -f /usr/bin/n ]; then
  255. rm /usr/bin/n
  256. fi
  257. remove_app nodejs
  258. rm "$NODEJS_INSTALLED_APPS_FILE"
  259. apt-get -yq autoremove
  260. fi
  261. }
  262. function upgrade_nodejs {
  263. CURR_NODE_VERSION=$(node --version)
  264. CURR_NPM_VERSION=$(npm --version)
  265. CURR_N_VERSION=$(n --version)
  266. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  267. npm upgrade -g npm@${NPM_VERSION} --save
  268. fi
  269. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  270. npm upgrade -g n@${NODEJS_N_VERSION} --save
  271. fi
  272. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  273. n ${NODEJS_VERSION}
  274. fi
  275. cp /usr/local/bin/n /usr/bin/n
  276. if [ -f /usr/local/bin/npm ]; then
  277. cp /usr/local/bin/npm /usr/bin/npm
  278. fi
  279. if [ -f /usr/local/bin/node ]; then
  280. cp /usr/local/bin/node /usr/bin/nodejs
  281. cp /usr/local/bin/node /usr/bin/node
  282. fi
  283. }
  284. function install_nodejs {
  285. if [ "$INSTALLING_MESH" ]; then
  286. mesh_install_nodejs
  287. return
  288. fi
  289. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  290. upgrade_nodejs
  291. return
  292. fi
  293. if [ ! "$ARCHITECTURE" ]; then
  294. ARCHITECTURE=$(uname -m)
  295. fi
  296. rootdir=
  297. mesh_install_nodejs
  298. # verify nodejs versions are what we expect
  299. CURR_NODE_VERSION=$(node --version)
  300. CURR_NPM_VERSION=$(npm --version)
  301. CURR_N_VERSION=$(n --version)
  302. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  303. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  304. exit 6728252
  305. fi
  306. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  307. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  308. exit 5267452
  309. fi
  310. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  311. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  312. exit 5274527
  313. fi
  314. if [ "$1" ]; then
  315. if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
  316. echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
  317. fi
  318. fi
  319. mark_completed "${FUNCNAME[0]}"
  320. }
  321. # NOTE: deliberately there is no "exit 0"