Monty Taylor | 4a781a7 | 2017-07-25 07:28:04 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) 2016 NodeSource LLC |
| 4 | # |
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | # copy of this software and associated documentation files (the "Software"), |
| 7 | # to deal in the Software without restriction, including without limitation |
| 8 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | # and/or sell copies of the Software, and to permit persons to whom the |
| 10 | # Software is furnished to do so, subject to the following conditions: |
| 11 | # |
| 12 | # The above copyright notice and this permission notice shall be included in |
| 13 | # all copies or substantial portions of the Software. |
| 14 | # |
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | # THE SOFTWARE. |
| 22 | |
| 23 | # The above license is inferred from the |
| 24 | # https://github.com/nodesource/distributions source repository. |
| 25 | |
| 26 | # Discussion, issues and change requests at: |
| 27 | # https://github.com/nodesource/distributions |
| 28 | # |
| 29 | # Script to install the NodeSource Node.js 8.x repo onto an |
| 30 | # Enterprise Linux or Fedora Core based system. |
| 31 | # |
| 32 | # Run as root or insert `sudo -E` before `bash`: |
| 33 | # |
| 34 | # This was downloaded from https://rpm.nodesource.com/setup_8.x |
| 35 | # A few modifications have been made. |
| 36 | |
| 37 | print_status() { |
| 38 | local outp=$(echo "$1" | sed -r 's/\\n/\\n## /mg') |
| 39 | echo |
| 40 | echo -e "## ${outp}" |
| 41 | echo |
| 42 | } |
| 43 | |
| 44 | bail() { |
| 45 | echo 'Error executing command, exiting' |
| 46 | exit 1 |
| 47 | } |
| 48 | |
| 49 | exec_cmd_nobail() { |
| 50 | echo "+ $1" |
| 51 | bash -c "$1" |
| 52 | } |
| 53 | |
| 54 | exec_cmd() { |
| 55 | exec_cmd_nobail "$1" || bail |
| 56 | } |
| 57 | |
| 58 | print_status "Installing the NodeSource Node.js 8.x repo..." |
| 59 | |
| 60 | print_status "Inspecting system..." |
| 61 | |
| 62 | if [ ! -x /bin/rpm ]; then |
| 63 | print_status "\ |
| 64 | You don't appear to be running an Enterprise Linux based \ |
| 65 | system, please contact NodeSource at \ |
| 66 | https://github.com/nodesource/distributions/issues if you think this \ |
| 67 | is incorrect or would like your distribution to be considered for \ |
| 68 | support.\ |
| 69 | " |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | ## Annotated section for auto extraction in test.sh |
| 74 | #-check-distro-# |
| 75 | |
| 76 | ## Check distro and arch |
| 77 | echo "+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release" |
| 78 | DISTRO_PKG=$(rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release) |
| 79 | echo "+ uname -m" |
| 80 | UNAME_ARCH=$(uname -m) |
| 81 | |
| 82 | |
| 83 | if [ "X${UNAME_ARCH}" == "Xi686" ]; then |
| 84 | DIST_ARCH=i386 |
| 85 | elif [ "X${UNAME_ARCH}" == "Xx86_64" ]; then |
| 86 | DIST_ARCH=x86_64 |
| 87 | else |
| 88 | |
| 89 | print_status "\ |
| 90 | You don't appear to be running a supported machine architecture: ${UNAME_ARCH}. \ |
| 91 | Please contact NodeSource at \ |
| 92 | https://github.com/nodesource/distributions/issues if you think this is \ |
| 93 | incorrect or would like your architecture to be considered for support. \ |
| 94 | " |
| 95 | exit 1 |
| 96 | |
| 97 | fi |
| 98 | |
| 99 | if [[ $DISTRO_PKG =~ ^(redhat|centos|cloudlinux|sl)- ]]; then |
| 100 | DIST_TYPE=el |
| 101 | elif [[ $DISTRO_PKG =~ ^system-release- ]]; then # Amazon Linux |
| 102 | DIST_TYPE=el |
| 103 | elif [[ $DISTRO_PKG =~ ^(fedora|korora)- ]]; then |
| 104 | DIST_TYPE=fc |
| 105 | else |
| 106 | |
| 107 | print_status "\ |
| 108 | You don't appear to be running a supported version of Enterprise Linux. \ |
| 109 | Please contact NodeSource at \ |
| 110 | https://github.com/nodesource/distributions/issues if you think this is \ |
| 111 | incorrect or would like your architecture to be considered for support. \ |
| 112 | Include your 'distribution package' name: ${DISTRO_PKG}. \ |
| 113 | " |
| 114 | exit 1 |
| 115 | |
| 116 | fi |
| 117 | |
| 118 | if [[ $DISTRO_PKG =~ ^system-release-201[4-9]\. ]]; then #NOTE: not really future-proof |
| 119 | |
| 120 | # Amazon Linux, for 2014.* use el7, older versions are unknown, perhaps el6 |
| 121 | DIST_VERSION=7 |
| 122 | |
| 123 | else |
| 124 | |
| 125 | ## Using the redhat-release-server-X, centos-release-X, etc. pattern |
| 126 | ## extract the major version number of the distro |
| 127 | DIST_VERSION=$(echo $DISTRO_PKG | sed -r 's/^[[:alpha:]]+-release(-server|-workstation)?-([0-9]+).*$/\2/') |
| 128 | |
| 129 | if ! [[ $DIST_VERSION =~ ^[0-9][0-9]?$ ]]; then |
| 130 | |
| 131 | print_status "\ |
| 132 | Could not determine your distribution version, you may not be running a \ |
| 133 | supported version of Enterprise Linux. \ |
| 134 | Please contact NodeSource at \ |
| 135 | https://github.com/nodesource/distributions/issues if you think this is \ |
| 136 | incorrect. Include your 'distribution package' name: ${DISTRO_PKG}. \ |
| 137 | " |
| 138 | exit 1 |
| 139 | |
| 140 | fi |
| 141 | |
| 142 | fi |
| 143 | |
| 144 | |
| 145 | ## Given the distro, version and arch, construct the url for |
| 146 | ## the appropriate nodesource-release package (it's noarch but |
| 147 | ## we include the arch in the directory tree anyway) |
| 148 | RELEASE_URL_VERSION_STRING="${DIST_TYPE}${DIST_VERSION}" |
| 149 | RELEASE_URL="\ |
| 150 | https://rpm.nodesource.com/pub_8.x/\ |
| 151 | ${DIST_TYPE}/\ |
| 152 | ${DIST_VERSION}/\ |
| 153 | ${DIST_ARCH}/\ |
| 154 | nodesource-release-${RELEASE_URL_VERSION_STRING}-1.noarch.rpm" |
| 155 | |
| 156 | #-check-distro-# |
| 157 | |
| 158 | print_status "Confirming \"${DIST_TYPE}${DIST_VERSION}-${DIST_ARCH}\" is supported..." |
| 159 | |
| 160 | ## Simple fetch & fast-fail to see if the nodesource-release |
| 161 | ## file exists for this distro/version/arch |
| 162 | exec_cmd_nobail "curl -sLf -o /dev/null '${RELEASE_URL}'" |
| 163 | RC=$? |
| 164 | |
| 165 | if [[ $RC != 0 ]]; then |
| 166 | print_status "\ |
| 167 | Your distribution, identified as \"${DISTRO_PKG}\", \ |
| 168 | is not currently supported, please contact NodeSource at \ |
| 169 | https://github.com/nodesource/distributions/issues \ |
| 170 | if you think this is incorrect or would like your distribution to be considered for support" |
| 171 | exit 1 |
| 172 | fi |
| 173 | |
| 174 | ## EPEL is needed for EL5, we don't install it if it's missing but |
| 175 | ## we can give guidance |
| 176 | if [ "$DIST_TYPE" == "el" ] && [ "$DIST_VERSION" == "5" ]; then |
| 177 | |
| 178 | print_status "Checking if EPEL is enabled..." |
| 179 | |
| 180 | echo "+ yum repolist enabled 2> /dev/null | grep epel" |
| 181 | repolist=$(yum repolist enabled 2> /dev/null | grep epel) |
| 182 | |
| 183 | if [ "X${repolist}" == "X" ]; then |
| 184 | print_status "Finding current EPEL release RPM..." |
| 185 | |
| 186 | ## We can scrape the html to find the latest epel-release (likely 5.4) |
| 187 | epel_url="http://dl.fedoraproject.org/pub/epel/5/${DIST_ARCH}/" |
| 188 | epel_release_view="${epel_url}repoview/epel-release.html" |
| 189 | echo "+ curl -s $epel_release_view | grep -oE 'epel-release-[0-9\-]+\.noarch\.rpm'" |
| 190 | epel=$(curl -s $epel_release_view | grep -oE 'epel-release-[0-9\-]+\.noarch\.rpm') |
| 191 | if [ "X${epel}" = "X" ]; then |
| 192 | print_status "Error: Could not find current EPEL release RPM!" |
| 193 | exit 1 |
| 194 | fi |
| 195 | |
| 196 | print_status "\ |
| 197 | The EPEL (Extra Packages for Enterprise Linux) repository is a\n\ |
| 198 | prerequisite for installing Node.js on your operating system. Please\n\ |
| 199 | add it and re-run this setup script.\n\ |
| 200 | \n\ |
| 201 | The EPEL repository RPM is available at:\n\ |
| 202 | ${epel_url}${epel}\n\ |
| 203 | You can try installing with: \`rpm -ivh <url>\`\ |
| 204 | " |
| 205 | |
| 206 | exit 1 |
| 207 | fi |
| 208 | |
| 209 | fi |
| 210 | |
| 211 | print_status "Downloading release setup RPM..." |
| 212 | |
| 213 | ## Two-step process to install the nodesource-release RPM, |
| 214 | ## Download to a tmp file then install it directly with `rpm`. |
| 215 | ## We don't rely on RPM's ability to fetch from HTTPS directly |
| 216 | echo "+ mktemp" |
| 217 | RPM_TMP=$(mktemp || bail) |
| 218 | |
| 219 | exec_cmd "curl -sL -o '${RPM_TMP}' '${RELEASE_URL}'" |
| 220 | |
| 221 | print_status "Installing release setup RPM..." |
| 222 | |
| 223 | ## --nosignature because nodesource-release contains the signature! |
| 224 | exec_cmd "rpm -i --nosignature --force '${RPM_TMP}'" |
| 225 | |
| 226 | print_status "Cleaning up..." |
| 227 | |
| 228 | exec_cmd "rm -f '${RPM_TMP}'" |
| 229 | |
| 230 | print_status "Checking for existing installations..." |
| 231 | |
| 232 | ## Nasty consequences if you have an existing Node or npm package |
| 233 | ## installed, need to inform if they are there |
| 234 | echo "+ rpm -qa 'node|npm' | grep -v nodesource" |
| 235 | EXISTING_NODE=$(rpm -qa 'node|npm|iojs' | grep -v nodesource) |
| 236 | |
| 237 | if [ "X${EXISTING_NODE}" != "X" ]; then |
| 238 | |
| 239 | # NOTE(mordred) Removed -y from the yum command below. |
| 240 | print_status "\ |
| 241 | Your system appears to already have Node.js installed from an alternative source.\n\ |
| 242 | Run \`\033[1myum remove nodejs npm\033[22m\` (as root) to remove these first.\ |
| 243 | " |
| 244 | |
| 245 | fi |
| 246 | |
| 247 | # NOTE(mordred) Removed -y from the yum commands below. |
| 248 | print_status "\ |
| 249 | Run \`\033[1myum install nodejs\033[22m\` (as root) to install Node.js 8.x and npm.\n\ |
| 250 | You may also need development tools to build native addons:\n\ |
| 251 | \`yum install gcc-c++ make\`\ |
| 252 | " |
| 253 | |
| 254 | ## Alternative to install dev tools: `yum groupinstall 'Development Tools' |
| 255 | |
| 256 | exit 0 |