You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

939 lines
25 KiB

  1. #!/usr/bin/env bash
  2. beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
  3. function usage()
  4. {
  5. cat << HEREDOC
  6. Usage: install.sh [--version VERSION] [--collection COLLECTION] [--cleanup] [--noop]
  7. optional arguments:
  8. -h, --help show this help message and exit
  9. -v, --version VERSION install a specific puppet-agent version
  10. -c, --collection COLLECTION install a specific puppet-agent collection (e.g. puppet7)
  11. -n, --noop do a dry run, do not change any files
  12. --cleanup remove the puppetlabs repository after installation finishes
  13. HEREDOC
  14. }
  15. while [[ "$#" -gt 0 ]]; do
  16. case $1 in
  17. -v|--version) PT_version="$2"; shift ;
  18. if beginswith "6." "$PT_version"; then
  19. PT_collection="puppet6"
  20. elif beginswith "7." "$PT_version"; then
  21. PT_collection="puppet7"
  22. else
  23. PT_collection="puppet"
  24. fi ;;
  25. -c|--collection) PT_collection="$2"; shift ;;
  26. --cleanup) PT_cleanup=true; shift ;;
  27. -n|--noop) PT__noop=true; shift ;;
  28. -h|--help) usage; exit ;;
  29. *) echo "Unknown parameter passed: $1"; usage; exit 1 ;;
  30. esac
  31. shift
  32. done
  33. # shellcheck disable=SC1000-SC9999
  34. {
  35. #!/bin/bash
  36. # This script may be called outside of a task, e.g. by puppet_agent
  37. # so we have to just paste this code here. *grumbles*
  38. # Exit with an error message and error code, defaulting to 1
  39. fail() {
  40. # Print a message: entry if there were anything printed to stderr
  41. if [[ -s $_tmp ]]; then
  42. # Hack to try and output valid json by replacing newlines with spaces.
  43. error_data="{ \"msg\": \"$(tr '\n' ' ' <"$_tmp")\", \"kind\": \"bash-error\", \"details\": {} }"
  44. else
  45. error_data="{ \"msg\": \"Task error\", \"kind\": \"bash-error\", \"details\": {} }"
  46. fi
  47. echo "{ \"status\": \"failure\", \"_error\": $error_data }"
  48. exit "${2:-1}"
  49. }
  50. validation_error() {
  51. error_data="{ \"msg\": \""$1"\", \"kind\": \"bash-error\", \"details\": {} }"
  52. echo "{ \"status\": \"failure\", \"_error\": $error_data }"
  53. exit 255
  54. }
  55. success() {
  56. echo "$1"
  57. }
  58. determine_command_for_facter_4() {
  59. puppet_version="$(puppet --version)"
  60. if (( ${puppet_version%%.*} == 6 )); then
  61. # puppet 6 with facter 4
  62. facts_command=(facter --json --show-legacy)
  63. else
  64. # puppet 7 with facter 4
  65. facts_command=(puppet facts show --show-legacy --render-as json)
  66. fi
  67. }
  68. maybe_delegate_to_facter() {
  69. [[ $PATH =~ \/opt\/puppetlabs\/bin ]] || export PATH="${PATH}:/opt/puppetlabs/bin"
  70. # Only use facter if we're running as the "facts" task, not the "facts::bash"
  71. # task. This also skips calling facter if we're running as a script, which is
  72. # used by the puppet_agent task.
  73. if [[ $PT__task == facts ]] && type facter &>/dev/null; then
  74. facter_version="$(facter -v)"
  75. if (( ${facter_version%%.*} <= 2 )); then
  76. facts_command=(facter -p --json)
  77. elif (( ${facter_version%%.*} == 3 )); then
  78. facts_command=(facter -p --json --show-legacy)
  79. else
  80. # facter 4
  81. determine_command_for_facter_4
  82. fi
  83. exec -- "${facts_command[@]}"
  84. fi
  85. }
  86. # Get info from one of /etc/os-release or /usr/lib/os-release
  87. # This is the preferred method and is checked first
  88. _systemd() {
  89. # These files may have unquoted spaces in the "pretty" fields even if the spec says otherwise
  90. # source cannot use process subsitution in some versions of bash, so redirect to stdin instead
  91. source /dev/stdin <<<"$(sed 's/ /_/g' "$1")"
  92. # According to `man os-release`, the first entry in ID_LIKE
  93. # should be the one the platform most closely resembles
  94. if [[ $ID = 'rhel' ]]; then
  95. family='RedHat'
  96. elif [[ $ID = 'debian' ]]; then
  97. family='Debian'
  98. elif [[ $ID_LIKE ]]; then
  99. family="${ID_LIKE%% *}"
  100. else
  101. family="${ID}"
  102. fi
  103. }
  104. # Get info from lsb_release
  105. _lsb_release() {
  106. read -r ID < <(lsb_release -si)
  107. read -r VERSION_ID < <(lsb_release -sr)
  108. read -r VERSION_CODENAME < <(lsb_release -sc)
  109. }
  110. # Get info from rhel /etc/*-release files
  111. _rhel() {
  112. family='RedHat'
  113. # slurp the file
  114. ver_info=$(<"$1")
  115. # ID is the first word in the string
  116. ID="${ver_info%% *}"
  117. # Codename is hopefully the word(s) in parenthesis
  118. if echo "$ver_info" | grep -q '('; then
  119. VERSION_CODENAME="${ver_info##*\(}"
  120. VERSION_CODENAME=""${VERSION_CODENAME//[()]/}""
  121. fi
  122. # Get a string like 'release 1.2.3' and grab everything after the space
  123. release=$(echo "$ver_info" | grep -Eo 'release[[:space:]]*[0-9.]+')
  124. VERSION_ID="${release#* }"
  125. }
  126. # Last resort
  127. _uname() {
  128. [[ $ID ]] || ID="$(uname)"
  129. [[ $VERSION_ID ]] || VERSION_ID="$(uname -r)"
  130. }
  131. # Taken from https://github.com/puppetlabs/facter/blob/master/lib/inc/facter/facts/os.hpp
  132. # If not in this list, we just uppercase the first character and lowercase the rest
  133. munge_name() {
  134. case "$1" in
  135. redhat|rhel|red) echo "RedHat" ;;
  136. ol|oracle) echo "OracleLinux" ;;
  137. ubuntu) echo "Ubuntu" ;;
  138. debian) echo "Debian" ;;
  139. centos) echo "CentOS" ;;
  140. cloud) echo "CloudLinux" ;;
  141. virtuozzo) echo "VirtuozzoLinux" ;;
  142. psbm) echo "PSBM" ;;
  143. xenserver) echo "XenServer" ;;
  144. linuxmint) echo "LinuxMint" ;;
  145. sles) echo "SLES" ;;
  146. suse) echo "SuSE" ;;
  147. opensuse) echo "OpenSuSE" ;;
  148. sunos) echo "SunOS" ;;
  149. omni) echo "OmniOS" ;;
  150. openindiana) echo "OpenIndiana" ;;
  151. manjaro) echo "ManjaroLinux" ;;
  152. smart) echo "SmartOS" ;;
  153. openwrt) echo "OpenWrt" ;;
  154. meego) echo "MeeGo" ;;
  155. coreos) echo "CoreOS" ;;
  156. zen) echo "XCP" ;;
  157. kfreebsd) echo "GNU/kFreeBSD" ;;
  158. arista) echo "AristaEOS" ;;
  159. huawei) echo "HuaweiOS" ;;
  160. photon) echo "PhotonOS" ;;
  161. *) echo "$(tr '[:lower:]' '[:upper:]' <<<"${ID:0:1}")""$(tr '[:upper:]' '[:lower:'] <<<"${ID:1}")"
  162. esac
  163. }
  164. _tmp="$(mktemp)"
  165. exec 2>>"$_tmp"
  166. shopt -s nocasematch
  167. # Use indirection to munge PT_ environment variables
  168. # e.g. "$PT_version" becomes "$version"
  169. for v in ${!PT_*}; do
  170. declare "${v#*PT_}"="${!v}"
  171. done
  172. # Delegate to facter executable if it exists. This function will `exec` and not
  173. # return if facter exists. Otherwise, we'll continue on.
  174. maybe_delegate_to_facter "$@"
  175. if [[ -e /etc/os-release ]]; then
  176. _systemd /etc/os-release
  177. elif [[ -e /usr/lib/os-release ]]; then
  178. _systemd /usr/lib/os-release
  179. fi
  180. # If either systemd is not installed or we didn't get a minor version or codename from os-release
  181. if ! [[ $VERSION_ID ]] || (( ${VERSION_ID%%.*} == ${VERSION_ID#*.} )) || ! [[ $VERSION_CODENAME ]]; then
  182. if [[ -e /etc/fedora-release ]]; then
  183. _rhel /etc/fedora-release
  184. elif [[ -e /etc/centos-release ]]; then
  185. _rhel /etc/centos-release
  186. elif [[ -e /etc/oracle-release ]]; then
  187. _rhel /etc/oracle-release
  188. elif [[ -e /etc/redhat-release ]]; then
  189. _rhel /etc/redhat-release
  190. elif type lsb_release &>/dev/null; then
  191. _lsb_release
  192. else
  193. _uname
  194. fi
  195. fi
  196. full="${VERSION_ID}"
  197. major="${VERSION_ID%%.*}"
  198. # Minor is considered the second part of the version string
  199. IFS='.' read -ra minor <<<"$full"
  200. minor="${minor[1]}"
  201. ID="$(munge_name "$ID")"
  202. family="$(munge_name "$family")"
  203. #!/usr/bin/env bash
  204. # Install puppet-agent as a task
  205. #
  206. # From https://github.com/petems/puppet-install-shell/blob/master/install_puppet_5_agent.sh
  207. # Timestamp
  208. now () {
  209. date +'%H:%M:%S %z'
  210. }
  211. # Logging functions instead of echo
  212. log () {
  213. echo "`now` ${1}"
  214. }
  215. info () {
  216. if [[ $PT__noop != true ]]; then
  217. log "INFO: ${1}"
  218. fi
  219. }
  220. warn () {
  221. log "WARN: ${1}"
  222. }
  223. critical () {
  224. log "CRIT: ${1}"
  225. }
  226. # Check whether a command exists - returns 0 if it does, 1 if it does not
  227. exists() {
  228. if command -v $1 >/dev/null 2>&1
  229. then
  230. return 0
  231. else
  232. return 1
  233. fi
  234. }
  235. # Check whether the apt config file has been modified, warning and exiting early if it has
  236. assert_unmodified_apt_config() {
  237. puppet_list=/etc/apt/sources.list.d/puppet.list
  238. puppet6_list=/etc/apt/sources.list.d/puppet6.list
  239. puppet7_list=/etc/apt/sources.list.d/puppet7.list
  240. if [[ -f $puppet_list ]]; then
  241. list_file=puppet_list
  242. elif [[ -f $puppet6_list ]]; then
  243. list_file=puppet6_list
  244. elif [[ -f $puppet7_list ]]; then
  245. list_file=puppet7_list
  246. fi
  247. # If puppet.list exists, get its md5sum on disk and its md5sum from the puppet-release package
  248. if [[ -n $list_file ]]; then
  249. # For md5sum, the checksum is the first word
  250. file_md5=($(md5sum "$list_file"))
  251. # For dpkg-query with this output format, the sum is the second word
  252. package_md5=($(dpkg-query -W -f='${Conffiles}\n' 'puppet-release' | grep -F "$list_file"))
  253. # If the $package_md5 array is set, and the md5sum on disk doesn't match the md5sum from dpkg-query, it has been modified
  254. if [[ $package_md5 && ${file_md5[0]} != ${package_md5[1]} ]]; then
  255. warn "Configuration file $list_file has been modified from the default. Skipping agent installation."
  256. exit 1
  257. fi
  258. fi
  259. }
  260. # Check whether perl and LWP::Simple module are installed
  261. exists_perl() {
  262. if perl -e 'use LWP::Simple;' >/dev/null 2>&1
  263. then
  264. return 0
  265. else
  266. return 1
  267. fi
  268. }
  269. # Get command line arguments
  270. if [ -n "$PT_version" ]; then
  271. version=$PT_version
  272. fi
  273. if [ -n "$PT_collection" ]; then
  274. # Check whether collection is nightly
  275. if [[ "$PT_collection" == *"nightly"* ]]; then
  276. nightly=true
  277. else
  278. nightly=false
  279. fi
  280. collection=$PT_collection
  281. else
  282. collection='puppet'
  283. fi
  284. if [ -n "$PT_yum_source" ]; then
  285. yum_source=$PT_yum_source
  286. else
  287. if [ "$nightly" = true ]; then
  288. yum_source='http://nightlies.puppet.com/yum'
  289. else
  290. yum_source='http://yum.puppet.com'
  291. fi
  292. fi
  293. if [ -n "$PT_apt_source" ]; then
  294. apt_source=$PT_apt_source
  295. else
  296. if [ "$nightly" = true ]; then
  297. apt_source='http://nightlies.puppet.com/apt'
  298. else
  299. apt_source='http://apt.puppet.com'
  300. fi
  301. fi
  302. if [ -n "$PT_mac_source" ]; then
  303. mac_source=$PT_mac_source
  304. else
  305. if [ "$nightly" = true ]; then
  306. mac_source='http://nightlies.puppet.com/downloads'
  307. else
  308. mac_source='http://downloads.puppet.com'
  309. fi
  310. fi
  311. if [ -n "$PT_retry" ]; then
  312. retry=$PT_retry
  313. else
  314. retry=5
  315. fi
  316. # Track to handle puppet5 to puppet6
  317. if [ -f /opt/puppetlabs/puppet/VERSION ]; then
  318. installed_version=`cat /opt/puppetlabs/puppet/VERSION`
  319. else
  320. installed_version=uninstalled
  321. fi
  322. # Only install the agent in cases where no agent is present, or the version of the agent
  323. # has been explicitly defined and does not match the version of an installed agent.
  324. if [ -z "$version" ]; then
  325. if [ "$installed_version" == "uninstalled" ]; then
  326. info "Version parameter not defined and no agent detected. Assuming latest."
  327. version=latest
  328. else
  329. info "Version parameter not defined and agent detected. Nothing to do."
  330. exit 0
  331. fi
  332. else
  333. info "Version parameter defined: ${version}"
  334. if [ "$version" == "$installed_version" ]; then
  335. info "Version parameter defined: ${version}. Puppet Agent ${version} detected. Nothing to do."
  336. exit 0
  337. elif [ "$version" != "latest" ]; then
  338. puppet_agent_version="$version"
  339. fi
  340. fi
  341. # Error if non-root
  342. if [ `id -u` -ne 0 ]; then
  343. echo "puppet_agent::install task must be run as root"
  344. exit 1
  345. fi
  346. # Retrieve Platform and Platform Version
  347. # Utilize facts implementation when available
  348. if true; then
  349. # Use facts module bash.sh implementation
  350. platform=$ID
  351. platform_version=$full
  352. # Handle CentOS
  353. if test "x$platform" = "xCentOS"; then
  354. platform="el"
  355. # Handle Rocky
  356. elif test "x$platform" = "xRocky"; then
  357. platform="el"
  358. # Handle Oracle
  359. elif test "x$platform" = "xOracle Linux Server"; then
  360. platform="el"
  361. elif test "x$platform" = "xOracleLinux"; then
  362. platform="el"
  363. # Handle Scientific
  364. elif test "x$platform" = "xScientific Linux"; then
  365. platform="el"
  366. elif test "x$platform" = "xScientific"; then
  367. platform="el"
  368. # Handle RedHat
  369. elif test "x$platform" = "xRedHat"; then
  370. platform="el"
  371. # If facts task return "Linux" for platform, investigate.
  372. elif test "x$platform" = "xLinux"; then
  373. if test -f "/etc/SuSE-release"; then
  374. if grep -q 'Enterprise' /etc/SuSE-release; then
  375. platform="SLES"
  376. platform_version=`awk '/^VERSION/ {V = $3}; /^PATCHLEVEL/ {P = $3}; END {print V "." P}' /etc/SuSE-release`
  377. else
  378. echo "No builds for platform: SUSE"
  379. exit 1
  380. fi
  381. elif test -f "/etc/redhat-release"; then
  382. platform="el"
  383. platform_version=`sed 's/^.\+ release \([.0-9]\+\).*/\1/' /etc/redhat-release`
  384. fi
  385. # Handle OSX
  386. elif test "x$platform" = "xDarwin"; then
  387. platform="mac_os_x"
  388. # Matching the tab-space with sed is error-prone
  389. platform_version=`sw_vers | awk '/^ProductVersion:/ { print $2 }'`
  390. major_version=`echo $platform_version | cut -d. -f1,2`
  391. # Excepting MacOS 10.x, the major version is the first number only
  392. if ! echo "${major_version}" | grep -q '^10\.'; then
  393. major_version=$(echo "${major_version}" | cut -d '.' -f 1);
  394. fi
  395. case $major_version in
  396. "10.11") platform_version="10.11";;
  397. "10.12") platform_version="10.12";;
  398. "10.13") platform_version="10.13";;
  399. "10.14") platform_version="10.14";;
  400. "10.15") platform_version="10.15";;
  401. "11") platform_version="11";;
  402. *) echo "No builds for platform: $major_version"
  403. exit 1
  404. ;;
  405. esac
  406. fi
  407. else
  408. echo "This module depends on the puppetlabs-facts module"
  409. exit 1
  410. fi
  411. if test "x$platform" = "x"; then
  412. critical "Unable to determine platform version!"
  413. exit 1
  414. fi
  415. # Mangle $platform_version to pull the correct build
  416. # for various platforms
  417. major_version=`echo $platform_version | cut -d. -f1`
  418. case $platform in
  419. "el")
  420. platform_version=$major_version
  421. ;;
  422. "Fedora")
  423. case $major_version in
  424. "23") platform_version="22";;
  425. *) platform_version=$major_version;;
  426. esac
  427. ;;
  428. "Debian")
  429. case $major_version in
  430. "5") platform_version="6";;
  431. "6") platform_version="6";;
  432. "7") platform_version="6";;
  433. esac
  434. ;;
  435. "SLES")
  436. platform_version=$major_version
  437. ;;
  438. "Amzn"|"Amazon Linux")
  439. case $platform_version in
  440. "2") platform_version="7";;
  441. esac
  442. ;;
  443. esac
  444. # Find which version of puppet is currently installed if any
  445. if test "x$platform_version" = "x"; then
  446. critical "Unable to determine platform version!"
  447. exit 1
  448. fi
  449. unable_to_retrieve_package() {
  450. critical "Unable to retrieve a valid package!"
  451. exit 1
  452. }
  453. random_hexdump () {
  454. hexdump -n 2 -e '/2 "%u"' /dev/urandom
  455. }
  456. if test "x$TMPDIR" = "x"; then
  457. tmp="/tmp"
  458. else
  459. tmp=${TMPDIR}
  460. # TMPDIR has trailing file sep for OSX test box
  461. penultimate=$((${#tmp}-1))
  462. if test "${tmp:$penultimate:1}" = "/"; then
  463. tmp="${tmp:0:$penultimate}"
  464. fi
  465. fi
  466. # Random function since not all shells have $RANDOM
  467. if exists hexdump; then
  468. random_number=$(random_hexdump)
  469. else
  470. random_number="`date +%N`"
  471. fi
  472. tmp_dir="$tmp/install.sh.$$.$random_number"
  473. (umask 077 && mkdir $tmp_dir) || exit 1
  474. tmp_stderr="$tmp/stderr.$$.$random_number"
  475. capture_tmp_stderr() {
  476. # spool up tmp_stderr from all the commands we called
  477. if test -f $tmp_stderr; then
  478. output=`cat ${tmp_stderr}`
  479. stderr_results="${stderr_results}\nSTDERR from $1:\n\n$output\n"
  480. fi
  481. }
  482. trap "rm -f $tmp_stderr; rm -rf $tmp_dir; exit $1" 1 2 15
  483. # Run command and retry on failure
  484. # run_cmd CMD
  485. run_cmd() {
  486. eval $1
  487. rc=$?
  488. if test $rc -ne 0; then
  489. attempt_number=0
  490. while test $attempt_number -lt $retry; do
  491. info "Retrying... [$((attempt_number + 1))/$retry]"
  492. eval $1
  493. rc=$?
  494. if test $rc -eq 0; then
  495. break
  496. fi
  497. info "Return code: $rc"
  498. sleep 1s
  499. ((attempt_number=attempt_number+1))
  500. done
  501. fi
  502. return $rc
  503. }
  504. # do_wget URL FILENAME
  505. do_wget() {
  506. info "Trying wget..."
  507. run_cmd "wget -O '$2' '$1' 2>$tmp_stderr"
  508. rc=$?
  509. # check for 404
  510. grep "ERROR 404" $tmp_stderr 2>&1 >/dev/null
  511. if test $? -eq 0; then
  512. critical "ERROR 404"
  513. unable_to_retrieve_package
  514. fi
  515. # check for bad return status or empty output
  516. if test $rc -ne 0 || test ! -s "$2"; then
  517. capture_tmp_stderr "wget"
  518. return 1
  519. fi
  520. return 0
  521. }
  522. # do_curl URL FILENAME
  523. do_curl() {
  524. info "Trying curl..."
  525. run_cmd "curl -1 -sL -D $tmp_stderr '$1' > '$2'"
  526. rc=$?
  527. # check for 404
  528. grep "404 Not Found" $tmp_stderr 2>&1 >/dev/null
  529. if test $? -eq 0; then
  530. critical "ERROR 404"
  531. unable_to_retrieve_package
  532. fi
  533. # check for bad return status or empty output
  534. if test $rc -ne 0 || test ! -s "$2"; then
  535. capture_tmp_stderr "curl"
  536. return 1
  537. fi
  538. return 0
  539. }
  540. # do_fetch URL FILENAME
  541. do_fetch() {
  542. info "Trying fetch..."
  543. run_cmd "fetch -o '$2' '$1' 2>$tmp_stderr"
  544. rc=$?
  545. # check for 404
  546. grep "404 Not Found" $tmp_stderr 2>&1 >/dev/null
  547. if test $? -eq 0; then
  548. critical "ERROR 404"
  549. unable_to_retrieve_package
  550. fi
  551. # check for bad return status or empty output
  552. if test $rc -ne 0 || test ! -s "$2"; then
  553. capture_tmp_stderr "fetch"
  554. return 1
  555. fi
  556. return 0
  557. }
  558. # do_perl URL FILENAME
  559. do_perl() {
  560. info "Trying perl..."
  561. run_cmd "perl -e 'use LWP::Simple; getprint(\$ARGV[0]);' '$1' > '$2' 2>$tmp_stderr"
  562. rc=$?
  563. # check for 404
  564. grep "404 Not Found" $tmp_stderr 2>&1 >/dev/null
  565. if test $? -eq 0; then
  566. critical "ERROR 404"
  567. unable_to_retrieve_package
  568. fi
  569. # check for bad return status or empty output
  570. if test $rc -ne 0 || test ! -s "$2"; then
  571. capture_tmp_stderr "perl"
  572. return 1
  573. fi
  574. return 0
  575. }
  576. # do_download URL FILENAME
  577. do_download() {
  578. info "Downloading $1"
  579. info " to file $2"
  580. # we try all of these until we get success.
  581. # perl, in particular may be present but LWP::Simple may not be installed
  582. if exists wget; then
  583. do_wget $1 $2 && return 0
  584. fi
  585. if exists curl; then
  586. do_curl $1 $2 && return 0
  587. fi
  588. if exists fetch; then
  589. do_fetch $1 $2 && return 0
  590. fi
  591. if exists_perl; then
  592. do_perl $1 $2 && return 0
  593. fi
  594. critical "Cannot download package as none of wget/curl/fetch/perl-LWP-Simple is found"
  595. unable_to_retrieve_package
  596. }
  597. # install_file TYPE FILENAME
  598. # TYPE is "rpm", "deb", "solaris" or "dmg"
  599. install_file() {
  600. case "$1" in
  601. "rpm")
  602. info "installing puppetlabs yum repo with rpm..."
  603. if test -f "/etc/yum.repos.d/puppetlabs-pc1.repo"; then
  604. info "existing puppetlabs yum repo found, moving to old location"
  605. mv /etc/yum.repos.d/puppetlabs-pc1.repo /etc/yum.repos.d/puppetlabs-pc1.repo.old
  606. fi
  607. if test "x$installed_version" != "xuninstalled"; then
  608. info "Version ${installed_version} detected..."
  609. major=$(echo $installed_version | cut -d. -f1)
  610. pkg="puppet${major}-release"
  611. if echo $2 | grep $pkg; then
  612. info "No collection upgrade detected"
  613. else
  614. info "Collection upgrade detected, replacing puppet${major}-release"
  615. rpm -e "puppet${major}-release"
  616. fi
  617. fi
  618. rpm -Uvh --oldpackage --replacepkgs "$2"
  619. if test "$version" = 'latest'; then
  620. run_cmd "yum install -y puppet-agent && yum upgrade -y puppet-agent"
  621. else
  622. run_cmd "yum install -y 'puppet-agent-${puppet_agent_version}'"
  623. fi
  624. ;;
  625. "noarch.rpm")
  626. info "installing puppetlabs yum repo with zypper..."
  627. if test "x$installed_version" != "xuninstalled"; then
  628. info "Version ${installed_version} detected..."
  629. major=$(echo $installed_version | cut -d. -f1)
  630. pkg="puppet${major}-release"
  631. if echo $2 | grep $pkg; then
  632. info "No collection upgrade detected"
  633. else
  634. info "Collection upgrade detected, replacing puppet${major}-release"
  635. zypper remove --no-confirm "puppet${major}-release"
  636. fi
  637. fi
  638. run_cmd "zypper install --no-confirm '$2'"
  639. if test "$version" = "latest"; then
  640. run_cmd "zypper install --no-confirm 'puppet-agent'"
  641. else
  642. run_cmd "zypper install --no-confirm --oldpackage --no-recommends --no-confirm 'puppet-agent-${puppet_agent_version}'"
  643. fi
  644. ;;
  645. "deb")
  646. info "Installing puppetlabs apt repo with dpkg..."
  647. if test "x$installed_version" != "xuninstalled"; then
  648. info "Version ${installed_version} detected..."
  649. major=$(echo $installed_version | cut -d. -f1)
  650. pkg="puppet${major}-release"
  651. if echo $2 | grep $pkg; then
  652. info "No collection upgrade detected"
  653. else
  654. info "Collection upgrade detected, replacing puppet${major}-release"
  655. dpkg --purge "puppet${major}-release"
  656. fi
  657. fi
  658. assert_unmodified_apt_config
  659. dpkg -i --force-confmiss "$2"
  660. run_cmd 'apt-get update -y'
  661. if test "$version" = 'latest'; then
  662. run_cmd "apt-get install -y puppet-agent"
  663. else
  664. if test "x$deb_codename" != "x"; then
  665. run_cmd "apt-get install -y 'puppet-agent=${puppet_agent_version}-1${deb_codename}'"
  666. else
  667. run_cmd "apt-get install -y 'puppet-agent=${puppet_agent_version}'"
  668. fi
  669. fi
  670. ;;
  671. "dmg" )
  672. info "installing puppetlabs dmg with hdiutil and installer"
  673. mountpoint="$(mktemp -d -t $(random_hexdump))"
  674. /usr/bin/hdiutil attach "${download_filename?}" -nobrowse -readonly -mountpoint "${mountpoint?}"
  675. /usr/sbin/installer -pkg ${mountpoint?}/puppet-agent-*-installer.pkg -target /
  676. /usr/bin/hdiutil detach "${mountpoint?}"
  677. rm -f $download_filename
  678. ;;
  679. *)
  680. critical "Unknown filetype: $1"
  681. exit 1
  682. ;;
  683. esac
  684. if test $? -ne 0; then
  685. critical "Installation failed"
  686. exit 1
  687. fi
  688. }
  689. info "Downloading Puppet $version for ${platform}..."
  690. case $platform in
  691. "SLES")
  692. info "SLES platform! Lets get you an RPM..."
  693. if [[ $PT__noop != true ]]; then
  694. for key in "puppet" "puppet-20250406"; do
  695. gpg_key="${tmp_dir}/RPM-GPG-KEY-${key}"
  696. do_download "https://yum.puppet.com/RPM-GPG-KEY-${key}" "$gpg_key"
  697. rpm --import "$gpg_key"
  698. rm -f "$gpg_key"
  699. done
  700. fi
  701. filetype="noarch.rpm"
  702. filename="${collection}-release-sles-${platform_version}.noarch.rpm"
  703. download_url="${yum_source}/${filename}"
  704. ;;
  705. "el")
  706. info "Red hat like platform! Lets get you an RPM..."
  707. filetype="rpm"
  708. filename="${collection}-release-el-${platform_version}.noarch.rpm"
  709. download_url="${yum_source}/${filename}"
  710. ;;
  711. "Amzn"|"Amazon Linux")
  712. info "Amazon platform! Lets get you an RPM..."
  713. filetype="rpm"
  714. filename="${collection}-release-el-${platform_version}.noarch.rpm"
  715. download_url="${yum_source}/${filename}"
  716. ;;
  717. "Fedora")
  718. info "Fedora platform! Lets get the RPM..."
  719. filetype="rpm"
  720. filename="${collection}-release-fedora-${platform_version}.noarch.rpm"
  721. download_url="${yum_source}/${filename}"
  722. ;;
  723. "Debian")
  724. info "Debian platform! Lets get you a DEB..."
  725. case $major_version in
  726. "5") deb_codename="lenny";;
  727. "6") deb_codename="squeeze";;
  728. "7") deb_codename="wheezy";;
  729. "8") deb_codename="jessie";;
  730. "9") deb_codename="stretch";;
  731. "10") deb_codename="buster";;
  732. "11"|"testing") deb_codename="bullseye";; # FIXME: testing to be removed when Debian is released and major_version is changed to 11
  733. esac
  734. filetype="deb"
  735. filename="${collection}-release-${deb_codename}.deb"
  736. download_url="${apt_source}/${filename}"
  737. ;;
  738. "Linuxmint"|"LinuxMint")
  739. info "Mint platform! Lets get you a DEB..."
  740. case $major_version in
  741. "3") deb_codename="stretch";;
  742. "4") deb_codename="buster";;
  743. "20") deb_codename="focal";;
  744. "19") deb_codename="bionic";;
  745. "18") deb_codename="xenial";;
  746. "17") deb_codename="trusty";;
  747. esac
  748. filetype="deb"
  749. filename="${collection}-release-${deb_codename}.deb"
  750. download_url="${apt_source}/${filename}"
  751. ;;
  752. "Ubuntu")
  753. info "Ubuntu platform! Lets get you a DEB..."
  754. case $platform_version in
  755. "12.04") deb_codename="precise";;
  756. "12.10") deb_codename="quantal";;
  757. "13.04") deb_codename="raring";;
  758. "13.10") deb_codename="saucy";;
  759. "14.04") deb_codename="trusty";;
  760. "14.10") deb_codename="trusty";;
  761. "15.04") deb_codename="vivid";;
  762. "15.10") deb_codename="wily";;
  763. "16.04") deb_codename="xenial";;
  764. "16.10") deb_codename="yakkety";;
  765. "17.04") deb_codename="zesty";;
  766. "18.04") deb_codename="bionic";;
  767. "20.04") deb_codename="focal";;
  768. esac
  769. filetype="deb"
  770. filename="${collection}-release-${deb_codename}.deb"
  771. download_url="${apt_source}/${filename}"
  772. ;;
  773. "mac_os_x")
  774. info "OSX platform! Lets get you a DMG..."
  775. filetype="dmg"
  776. if test "$version" = "latest"; then
  777. filename="puppet-agent-latest.dmg"
  778. else
  779. filename="puppet-agent-${version}-1.osx${platform_version}.dmg"
  780. fi
  781. download_url="${mac_source}/mac/${collection}/${platform_version}/x86_64/${filename}"
  782. ;;
  783. *)
  784. critical "Sorry $platform is not supported yet!"
  785. exit 1
  786. ;;
  787. esac
  788. if [[ $PT__noop != true ]]; then
  789. download_filename="${tmp_dir}/${filename}"
  790. do_download "$download_url" "$download_filename"
  791. install_file $filetype "$download_filename"
  792. if [[ $PT_stop_service = true ]]; then
  793. /opt/puppetlabs/bin/puppet resource service puppet ensure=stopped enable=false
  794. fi
  795. #Cleanup
  796. if test "x$tmp_dir" != "x"; then
  797. rm -r "$tmp_dir"
  798. fi
  799. fi
  800. }
  801. if [[ $PT__noop != true ]]; then
  802. if [[ $PT_cleanup == true ]]; then
  803. info "Cleanup requested, removing ${collection}-release repository..."
  804. case $platform in
  805. SLES|el|Amzn|"Amazon Linux"|Fedora)
  806. rpm -e --allmatches ${collection}-release
  807. ;;
  808. Debian|LinuxMint|Linuxmint|Ubuntu)
  809. apt-get purge ${collection}-release -y
  810. ;;
  811. esac
  812. fi
  813. fi