freedombone-utils-nodejs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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='4'
  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. # deliberate second install of npm
  140. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  141. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  142. cp "$rootdir/usr/local/bin/npm" /usr/bin/npm
  143. fi
  144. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  145. if [ -f "$rootdir/usr/bin/node" ]; then
  146. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  147. fi
  148. # check the version numbers
  149. cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
  150. #!/bin/bash
  151. node_version=\$(node -v)
  152. if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
  153. echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
  154. exit 1
  155. fi
  156. npm_version=\$(npm -v)
  157. if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
  158. echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
  159. exit 2
  160. fi
  161. EOF
  162. chmod +x "$rootdir/usr/bin/test_nodejs_install"
  163. if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
  164. echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
  165. NODE_UPGRADE=
  166. exit 76835282
  167. fi
  168. rm "$rootdir/usr/bin/test_nodejs_install"
  169. NODE_UPGRADE=
  170. }
  171. function nodejs_upgrade {
  172. if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
  173. return
  174. fi
  175. nodejs_setup_global_modules
  176. if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
  177. if [ -f /usr/local/bin/node ]; then
  178. CURR_NODE_VERSION=$(node --version)
  179. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  180. return
  181. fi
  182. fi
  183. fi
  184. if [ -f /usr/local/bin/node ]; then
  185. CURR_NODE_VERSION=$(node --version)
  186. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  187. return
  188. fi
  189. fi
  190. read_config_param ARCHITECTURE
  191. get_npm_arch
  192. NODE_UPGRADE=1
  193. rootdir=
  194. mesh_install_nodejs
  195. npm update -g
  196. }
  197. function nodejs_setup_global_modules {
  198. if [ ! -f /usr/local/bin/node ]; then
  199. return
  200. fi
  201. if [ ! -d "$rootdir/root/.npm-global" ]; then
  202. mkdir "$rootdir/root/.npm-global"
  203. fi
  204. $mesh_install_nodejs_prefix npm config set prefix '/root/.npm-global'
  205. if ! grep -q "PATH=/root/.npm-global/bin" "$rootdir/root/.bashrc"; then
  206. echo "PATH=/root/.npm-global/bin:\$PATH" >> "$rootdir/root/.bashrc"
  207. fi
  208. if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
  209. echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
  210. fi
  211. # shellcheck disable=SC2086
  212. $mesh_install_nodejs_prefix export PATH=/root/.npm-global/bin:$PATH
  213. $mesh_install_nodejs_prefix export NPM_CONFIG_PREFIX=/root/.npm-global
  214. }
  215. function remove_nodejs {
  216. if [ ! "$1" ]; then
  217. return
  218. fi
  219. if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  220. #remove_app nodejs
  221. return
  222. fi
  223. sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
  224. if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
  225. apt-get -yq remove --purge nodejs
  226. if [ -f /usr/bin/nodejs ]; then
  227. rm /usr/bin/nodejs
  228. fi
  229. if [ -f /usr/local/bin/node ]; then
  230. rm /usr/local/bin/node
  231. fi
  232. if [ -f /usr/bin/node ]; then
  233. rm /usr/bin/node
  234. fi
  235. if [ -d /usr/lib/node_modules ]; then
  236. rm -rf /usr/lib/node_modules
  237. fi
  238. if [ -f /usr/bin/n ]; then
  239. rm /usr/bin/n
  240. fi
  241. remove_app nodejs
  242. rm "$NODEJS_INSTALLED_APPS_FILE"
  243. apt-get -yq autoremove
  244. fi
  245. }
  246. function upgrade_nodejs {
  247. CURR_NODE_VERSION=$(node --version)
  248. CURR_NPM_VERSION=$(npm --version)
  249. CURR_N_VERSION=$(n --version)
  250. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  251. npm upgrade -g npm@${NPM_VERSION} --save
  252. fi
  253. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  254. npm upgrade -g n@${NODEJS_N_VERSION} --save
  255. fi
  256. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  257. n ${NODEJS_VERSION}
  258. fi
  259. cp /usr/local/bin/n /usr/bin/n
  260. if [ -f /usr/local/bin/npm ]; then
  261. cp /usr/local/bin/npm /usr/bin/npm
  262. fi
  263. if [ -f /usr/local/bin/node ]; then
  264. cp /usr/local/bin/node /usr/bin/nodejs
  265. cp /usr/local/bin/node /usr/bin/node
  266. fi
  267. }
  268. function install_nodejs {
  269. if [ "$INSTALLING_MESH" ]; then
  270. mesh_install_nodejs
  271. return
  272. fi
  273. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  274. upgrade_nodejs
  275. return
  276. fi
  277. if [ ! "$ARCHITECTURE" ]; then
  278. ARCHITECTURE=$(uname -m)
  279. fi
  280. rootdir=
  281. mesh_install_nodejs
  282. # verify nodejs versions are what we expect
  283. CURR_NODE_VERSION=$(node --version)
  284. CURR_NPM_VERSION=$(npm --version)
  285. CURR_N_VERSION=$(n --version)
  286. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  287. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  288. exit 6728252
  289. fi
  290. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  291. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  292. exit 5267452
  293. fi
  294. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  295. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  296. exit 5274527
  297. fi
  298. if [ "$1" ]; then
  299. if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
  300. echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
  301. fi
  302. fi
  303. mark_completed "${FUNCNAME[0]}"
  304. }
  305. # NOTE: deliberately there is no "exit 0"