Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # vim: ts=4 sw=4 sts=4 et tw=72 : |
| 4 | |
| 5 | # force any errors to cause the script and job to end in failure |
| 6 | set -xeu -o pipefail |
| 7 | |
| 8 | ensure_kernel_install() { |
| 9 | # Workaround for mkinitrd failing on occassion. |
| 10 | # On CentOS 7 it seems like the kernel install can fail it's mkinitrd |
| 11 | # run quietly, so we may not notice the failure. This script retries for a |
| 12 | # few times before giving up. |
| 13 | initramfs_ver=$(rpm -q kernel | tail -1 | sed "s/kernel-/initramfs-/") |
| 14 | grub_conf="/boot/grub/grub.conf" |
| 15 | # Public cloud does not use /boot/grub/grub.conf and uses grub2 instead. |
| 16 | if [ ! -e "$grub_conf" ]; then |
| 17 | echo "$grub_conf not found. Using Grub 2 conf instead." |
| 18 | grub_conf="/boot/grub2/grub.cfg" |
| 19 | fi |
| 20 | |
| 21 | for i in $(seq 3); do |
| 22 | if grep "$initramfs_ver" "$grub_conf"; then |
| 23 | break |
| 24 | fi |
| 25 | echo "Kernel initrd missing. Retrying to install kernel..." |
| 26 | yum reinstall -y kernel |
| 27 | done |
| 28 | if ! grep "$initramfs_ver" "$grub_conf"; then |
| 29 | cat /boot/grub/grub.conf |
| 30 | echo "ERROR: Failed to install kernel." |
| 31 | exit 1 |
| 32 | fi |
| 33 | } |
| 34 | |
| 35 | ensure_ubuntu_install() { |
| 36 | # Workaround for mirrors occassionally failing to install a package. |
| 37 | # On Ubuntu sometimes the mirrors fail to install a package. This wrapper |
| 38 | # checks that a package is successfully installed before moving on. |
| 39 | |
| 40 | packages=($@) |
| 41 | |
| 42 | for pkg in "${packages[@]}" |
| 43 | do |
| 44 | # Retry installing package 5 times if necessary |
| 45 | for i in {0..5} |
| 46 | do |
| 47 | if [ "$(dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then |
| 48 | apt-cache policy "$pkg" |
| 49 | apt-get install "$pkg" |
| 50 | continue |
| 51 | else |
| 52 | echo "$pkg already installed." |
| 53 | break |
| 54 | fi |
| 55 | done |
| 56 | done |
| 57 | } |
| 58 | |
| 59 | rh_systems() { |
| 60 | # Handle the occurance where SELINUX is actually disabled |
| 61 | SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config) |
| 62 | MODE=$(echo "$SELINUX" | cut -f 2 -d '=') |
| 63 | case "$MODE" in |
| 64 | permissive) |
| 65 | echo "************************************" |
| 66 | echo "** SYSTEM ENTERING ENFORCING MODE **" |
| 67 | echo "************************************" |
| 68 | # make sure that the filesystem is properly labelled. |
| 69 | # it could be not fully labeled correctly if it was just switched |
| 70 | # from disabled, the autorelabel misses some things |
| 71 | # skip relabelling on /dev as it will generally throw errors |
| 72 | restorecon -R -e /dev / |
| 73 | |
| 74 | # enable enforcing mode from the very start |
| 75 | setenforce enforcing |
| 76 | |
| 77 | # configure system for enforcing mode on next boot |
| 78 | sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config |
| 79 | ;; |
| 80 | disabled) |
| 81 | sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config |
| 82 | touch /.autorelabel |
| 83 | |
| 84 | echo "*******************************************" |
| 85 | echo "** SYSTEM REQUIRES A RESTART FOR SELINUX **" |
| 86 | echo "*******************************************" |
| 87 | ;; |
| 88 | enforcing) |
| 89 | echo "*********************************" |
| 90 | echo "** SYSTEM IS IN ENFORCING MODE **" |
| 91 | echo "*********************************" |
| 92 | ;; |
| 93 | esac |
| 94 | |
| 95 | # Allow jenkins access to alternatives command to switch java version |
| 96 | cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults |
| 97 | Defaults:jenkins !requiretty |
| 98 | jenkins ALL = NOPASSWD: /usr/sbin/alternatives |
| 99 | EOF |
| 100 | |
| 101 | echo "---> Updating operating system" |
| 102 | yum clean all |
| 103 | yum install -y deltarpm |
| 104 | yum update -y |
| 105 | |
| 106 | ensure_kernel_install |
| 107 | |
| 108 | # add in components we need or want on systems |
| 109 | echo "---> Installing base packages" |
| 110 | yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm |
| 111 | # separate group installs from package installs since a non-existing |
| 112 | # group with dnf based systems (F21+) will fail the install if such |
| 113 | # a group does not exist |
| 114 | yum install -y unzip xz puppet git git-review perl-XML-XPath |
Zack Williams | 05f0608 | 2020-01-22 14:19:34 -0700 | [diff] [blame] | 115 | yum install -y python-{devel,virtualenv} |
| 116 | yum install -y python3-{devel,setuptools,pip} |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 117 | |
| 118 | # All of our systems require Java (because of Jenkins) |
| 119 | # Install all versions of the OpenJDK devel but force 1.7.0 to be the |
| 120 | # default |
| 121 | |
| 122 | echo "---> Configuring OpenJDK" |
| 123 | yum install -y 'java-*-openjdk-devel' |
| 124 | |
| 125 | FACTER_OS=$(/usr/bin/facter operatingsystem) |
| 126 | FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease) |
| 127 | case "$FACTER_OS" in |
| 128 | Fedora) |
| 129 | if [ "$FACTER_OSVER" -ge "21" ] |
| 130 | then |
| 131 | echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist" |
| 132 | else |
| 133 | alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java |
| 134 | alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64 |
| 135 | fi |
| 136 | ;; |
| 137 | RedHat|CentOS) |
| 138 | if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ] |
| 139 | then |
| 140 | echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist" |
| 141 | else |
| 142 | alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java |
| 143 | alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64 |
| 144 | fi |
| 145 | ;; |
| 146 | *) |
| 147 | alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java |
| 148 | alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64 |
| 149 | ;; |
| 150 | esac |
| 151 | |
| 152 | ######################## |
| 153 | # --- START LFTOOLS DEPS |
| 154 | |
| 155 | # Used by various scripts to push patches to Gerrit |
| 156 | yum install -y git-review |
| 157 | |
| 158 | # Needed to parse OpenStack commands used by opendaylight-infra stack commands |
| 159 | # to initialize Heat template based systems. |
| 160 | yum install -y jq |
| 161 | |
| 162 | # Used by lftools scripts to parse XML |
| 163 | yum install -y xmlstarlet |
| 164 | |
| 165 | # Haskel Packages |
| 166 | # Cabal update fails on a 1G system so workaround that with a swap file |
| 167 | dd if=/dev/zero of=/tmp/swap bs=1M count=1024 |
| 168 | mkswap /tmp/swap |
| 169 | swapon /tmp/swap |
| 170 | |
Zack Williams | cd7b3fc | 2020-01-22 14:19:34 -0700 | [diff] [blame] | 171 | # Install Shellcheck from archive |
| 172 | SHELLCHECK_VERSION="v0.6.0" |
HungWei Chiu | 1df10dc | 2022-05-27 13:25:37 -0700 | [diff] [blame] | 173 | SHELLCHECK_SHA256SUM="eabb8c324fa849cdbff3305255d03a0f58a7d5fc66c2e709d83cf22a51fc95fa" |
Zack Williams | cd7b3fc | 2020-01-22 14:19:34 -0700 | [diff] [blame] | 174 | curl -L -o /tmp/shellcheck.tar.xz https://storage.googleapis.com/shellcheck/shellcheck-${SHELLCHECK_VERSION}.linux.x86_64.tar.xz |
| 175 | echo "$SHELLCHECK_SHA256SUM /tmp/shellcheck.tar.xz" | sha256sum -c - |
| 176 | pushd /tmp |
| 177 | tar -xJvf shellcheck.tar.xz |
| 178 | cp shellcheck-${SHELLCHECK_VERSION}/shellcheck /usr/local/bin/shellcheck |
| 179 | chmod a+x /usr/local/bin/shellcheck |
| 180 | popd |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 181 | |
| 182 | # --- END LFTOOLS DEPS |
| 183 | ###################### |
| 184 | |
| 185 | # install haveged to avoid low entropy rejecting ssh connections |
| 186 | yum install -y haveged |
| 187 | systemctl enable haveged.service |
| 188 | } |
| 189 | |
| 190 | ubuntu_systems() { |
| 191 | # Ignore SELinux since slamming that onto Ubuntu leads to |
| 192 | # frustration |
| 193 | |
| 194 | # Allow jenkins access to update-alternatives command to switch java version |
| 195 | cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults |
Zack Williams | a4cc07c | 2019-08-29 22:17:29 -0700 | [diff] [blame] | 196 | Cmnd_Alias CMDS = /usr/sbin/update-alternatives, /usr/sbin/update-java-alternatives |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 197 | Defaults:jenkins !requiretty |
Zack Williams | a4cc07c | 2019-08-29 22:17:29 -0700 | [diff] [blame] | 198 | jenkins ALL = NOPASSWD: CMDS |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 199 | EOF |
| 200 | |
| 201 | export DEBIAN_FRONTEND=noninteractive |
| 202 | cat <<EOF >> /etc/apt/apt.conf |
| 203 | APT { |
| 204 | Get { |
| 205 | Assume-Yes "true"; |
| 206 | allow-change-held-packages "true"; |
| 207 | allow-downgrades "true"; |
| 208 | allow-remove-essential "true"; |
| 209 | }; |
| 210 | }; |
| 211 | |
| 212 | Dpkg::Options { |
| 213 | "--force-confdef"; |
| 214 | "--force-confold"; |
| 215 | }; |
| 216 | |
| 217 | EOF |
| 218 | |
| 219 | # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo |
| 220 | sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts |
| 221 | |
| 222 | echo "---> Updating operating system" |
| 223 | |
Jeremy Ronquillo | 51d0bbe | 2022-07-19 14:07:59 -0700 | [diff] [blame] | 224 | apt-get clean |
Kailash Khalasi | e0964b1 | 2018-07-10 09:22:00 -0700 | [diff] [blame] | 225 | apt-get update -m |
Jeremy Ronquillo | 51d0bbe | 2022-07-19 14:07:59 -0700 | [diff] [blame] | 226 | apt-get -f install || true # attempt to fix broken packages |
Jeremy Ronquillo | c288e6d | 2022-07-20 09:54:03 -0700 | [diff] [blame^] | 227 | apt-get install -y iso-codes python3-software-properties isoquery software-properties-common apt-transport-https |
Kailash Khalasi | e0964b1 | 2018-07-10 09:22:00 -0700 | [diff] [blame] | 228 | |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 229 | # add additional repositories |
Kailash Khalasi | e0964b1 | 2018-07-10 09:22:00 -0700 | [diff] [blame] | 230 | add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse" |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 231 | |
| 232 | echo "---> Installing base packages" |
| 233 | apt-get clean |
| 234 | apt-get update -m |
| 235 | apt-get upgrade -m |
| 236 | apt-get dist-upgrade -m |
| 237 | |
| 238 | ensure_ubuntu_install unzip xz-utils puppet git libxml-xpath-perl |
| 239 | |
Zack Williams | fef4927 | 2019-08-28 13:47:09 -0700 | [diff] [blame] | 240 | echo "---> Configuring Corretto JDK Distribution" |
| 241 | # instructions: https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/generic-linux-install.html |
| 242 | # install prereqs |
| 243 | apt-get install java-common |
| 244 | |
| 245 | # install Java8 |
| 246 | CORRETTO_JAVA8_VERSION="8.222.10-1" |
| 247 | CORRETTO_JAVA8_SHA256SUM="e5fd6c6f2d1a1fc5e6926f7a543e67ad0f0e0389ddc5d2deb5890bdeb21ea445" |
| 248 | curl -L -o /tmp/corretto_java8.deb "https://d3pxv6yz143wms.cloudfront.net/$(echo $CORRETTO_JAVA8_VERSION | tr - .)/java-1.8.0-amazon-corretto-jdk_${CORRETTO_JAVA8_VERSION}_amd64.deb" |
| 249 | echo "$CORRETTO_JAVA8_SHA256SUM /tmp/corretto_java8.deb" | sha256sum -c - |
| 250 | dpkg -i /tmp/corretto_java8.deb |
| 251 | |
| 252 | # install Java11 |
| 253 | CORRETTO_JAVA11_VERSION="11.0.4.11-1" |
| 254 | CORRETTO_JAVA11_SHA256SUM="f47c77f8f9ee5a80804765236c11dc749d351d3b8f57186c6e6b58a6c4019d3e" |
| 255 | curl -L -o /tmp/corretto_java11.deb "https://d3pxv6yz143wms.cloudfront.net/$(echo $CORRETTO_JAVA11_VERSION | tr - .)/java-11-amazon-corretto-jdk_${CORRETTO_JAVA11_VERSION}_amd64.deb" |
| 256 | echo "$CORRETTO_JAVA11_SHA256SUM /tmp/corretto_java11.deb" | sha256sum -c - |
| 257 | dpkg -i /tmp/corretto_java11.deb |
| 258 | |
Zack Williams | 2c6c25e | 2019-08-29 16:31:20 -0700 | [diff] [blame] | 259 | # Fix corretto 11 lack of jinfo that prevents update-java-alternatives from working |
| 260 | # Upstream fix not integrated yet: https://github.com/corretto/corretto-11/pull/27 |
| 261 | cat <<EOF >/usr/lib/jvm/.java-11-amazon-corretto.jinfo |
| 262 | name=java-11-amazon-corretto |
| 263 | alias=java-11-amazon-corretto |
| 264 | priority=11100002 |
| 265 | section=main |
| 266 | |
| 267 | jdk java /usr/lib/jvm/java-11-amazon-corretto/bin/java |
| 268 | jdk keytool /usr/lib/jvm/java-11-amazon-corretto/bin/keytool |
| 269 | jdk rmid /usr/lib/jvm/java-11-amazon-corretto/bin/rmid |
| 270 | jdk rmiregistry /usr/lib/jvm/java-11-amazon-corretto/bin/rmiregistry |
| 271 | jdk jjs /usr/lib/jvm/java-11-amazon-corretto/bin/jjs |
| 272 | jdk pack200 /usr/lib/jvm/java-11-amazon-corretto/bin/pack200 |
| 273 | jdk unpack200 /usr/lib/jvm/java-11-amazon-corretto/bin/unpack200 |
| 274 | jdk javac /usr/lib/jvm/java-11-amazon-corretto/bin/javac |
| 275 | jdk jaotc /usr/lib/jvm/java-11-amazon-corretto/bin/jaotc |
| 276 | jdk jlink /usr/lib/jvm/java-11-amazon-corretto/bin/jlink |
| 277 | jdk jmod /usr/lib/jvm/java-11-amazon-corretto/bin/jmod |
| 278 | jdk jhsdb /usr/lib/jvm/java-11-amazon-corretto/bin/jhsdb |
| 279 | jdk jar /usr/lib/jvm/java-11-amazon-corretto/bin/jar |
| 280 | jdk jarsigner /usr/lib/jvm/java-11-amazon-corretto/bin/jarsigner |
| 281 | jdk javadoc /usr/lib/jvm/java-11-amazon-corretto/bin/javadoc |
| 282 | jdk javap /usr/lib/jvm/java-11-amazon-corretto/bin/javap |
| 283 | jdk jcmd /usr/lib/jvm/java-11-amazon-corretto/bin/jcmd |
| 284 | jdk jconsole /usr/lib/jvm/java-11-amazon-corretto/bin/jconsole |
| 285 | jdk jdb /usr/lib/jvm/java-11-amazon-corretto/bin/jdb |
| 286 | jdk jdeps /usr/lib/jvm/java-11-amazon-corretto/bin/jdeps |
| 287 | jdk jdeprscan /usr/lib/jvm/java-11-amazon-corretto/bin/jdeprscan |
| 288 | jdk jimage /usr/lib/jvm/java-11-amazon-corretto/bin/jimage |
| 289 | jdk jinfo /usr/lib/jvm/java-11-amazon-corretto/bin/jinfo |
| 290 | jdk jmap /usr/lib/jvm/java-11-amazon-corretto/bin/jmap |
| 291 | jdk jps /usr/lib/jvm/java-11-amazon-corretto/bin/jps |
| 292 | jdk jrunscript /usr/lib/jvm/java-11-amazon-corretto/bin/jrunscript |
| 293 | jdk jshell /usr/lib/jvm/java-11-amazon-corretto/bin/jshell |
| 294 | jdk jstack /usr/lib/jvm/java-11-amazon-corretto/bin/jstack |
| 295 | jdk jstat /usr/lib/jvm/java-11-amazon-corretto/bin/jstat |
| 296 | jdk jstatd /usr/lib/jvm/java-11-amazon-corretto/bin/jstatd |
| 297 | jdk rmic /usr/lib/jvm/java-11-amazon-corretto/bin/rmic |
| 298 | jdk serialver /usr/lib/jvm/java-11-amazon-corretto/bin/serialver |
| 299 | |
| 300 | EOF |
| 301 | |
Zack Williams | fef4927 | 2019-08-28 13:47:09 -0700 | [diff] [blame] | 302 | # Set default version to be Java8 |
Zack Williams | 2c6c25e | 2019-08-29 16:31:20 -0700 | [diff] [blame] | 303 | update-java-alternatives --set java-1.8.0-amazon-corretto |
Zack Williams | fef4927 | 2019-08-28 13:47:09 -0700 | [diff] [blame] | 304 | |
| 305 | # Set default version to be Java11 |
Zack Williams | 2c6c25e | 2019-08-29 16:31:20 -0700 | [diff] [blame] | 306 | # update-java-alternatives -s java-11-amazon-corretto |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 307 | |
| 308 | ######################## |
| 309 | # --- START LFTOOLS DEPS |
| 310 | |
| 311 | # Used by various scripts to push patches to Gerrit |
| 312 | ensure_ubuntu_install git-review |
| 313 | |
| 314 | # Needed to parse OpenStack commands used by opendaylight-infra stack commands |
| 315 | # to initialize Heat template based systems. |
| 316 | ensure_ubuntu_install jq |
| 317 | |
| 318 | # Used by lftools scripts to parse XML |
| 319 | ensure_ubuntu_install xmlstarlet |
| 320 | |
Zack Williams | fef4927 | 2019-08-28 13:47:09 -0700 | [diff] [blame] | 321 | # Install Shellcheck from archive |
| 322 | SHELLCHECK_VERSION="v0.6.0" |
HungWei Chiu | c8011b2 | 2022-05-27 14:39:39 -0700 | [diff] [blame] | 323 | SHELLCHECK_SHA256SUM="eabb8c324fa849cdbff3305255d03a0f58a7d5fc66c2e709d83cf22a51fc95fa" |
Zack Williams | fef4927 | 2019-08-28 13:47:09 -0700 | [diff] [blame] | 324 | curl -L -o /tmp/shellcheck.tar.xz https://storage.googleapis.com/shellcheck/shellcheck-${SHELLCHECK_VERSION}.linux.x86_64.tar.xz |
| 325 | echo "$SHELLCHECK_SHA256SUM /tmp/shellcheck.tar.xz" | sha256sum -c - |
| 326 | pushd /tmp |
| 327 | tar -xJvf shellcheck.tar.xz |
| 328 | cp shellcheck-${SHELLCHECK_VERSION}/shellcheck /usr/local/bin/shellcheck |
| 329 | chmod a+x /usr/local/bin/shellcheck |
| 330 | popd |
Andrew Grimberg | 78bc6d5 | 2017-10-13 12:55:05 -0700 | [diff] [blame] | 331 | |
| 332 | # --- END LFTOOLS DEPS |
| 333 | ###################### |
| 334 | |
| 335 | # install haveged to avoid low entropy rejecting ssh connections |
| 336 | apt-get install haveged |
| 337 | update-rc.d haveged defaults |
| 338 | |
| 339 | # disable unattended upgrades & daily updates |
| 340 | echo '---> Disabling automatic daily upgrades' |
| 341 | sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic |
| 342 | echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic |
| 343 | } |
| 344 | |
| 345 | all_systems() { |
| 346 | # Do any Distro specific installations here |
| 347 | echo "Checking distribution" |
| 348 | FACTER_OS=$(/usr/bin/facter operatingsystem) |
| 349 | case "$FACTER_OS" in |
| 350 | *) |
| 351 | echo "---> $FACTER_OS found" |
| 352 | echo "No extra steps for $FACTER_OS" |
| 353 | ;; |
| 354 | esac |
| 355 | } |
| 356 | |
| 357 | echo "---> Attempting to detect OS" |
| 358 | # upstream cloud images use the distro name as the initial user |
| 359 | ORIGIN=$(if [ -e /etc/redhat-release ] |
| 360 | then |
| 361 | echo redhat |
| 362 | else |
| 363 | echo ubuntu |
| 364 | fi) |
| 365 | #ORIGIN=$(logname) |
| 366 | |
| 367 | case "${ORIGIN}" in |
| 368 | fedora|centos|redhat) |
| 369 | echo "---> RH type system detected" |
| 370 | rh_systems |
| 371 | ;; |
| 372 | ubuntu) |
| 373 | echo "---> Ubuntu system detected" |
| 374 | ubuntu_systems |
| 375 | ;; |
| 376 | *) |
| 377 | # Kill the build for unhandled distributions |
| 378 | echo "---> Unknown operating system" 1>&2 |
| 379 | exit 1 |
| 380 | ;; |
| 381 | esac |
| 382 | |
| 383 | # execute steps for all systems |
| 384 | all_systems |