diff --git a/install.rb b/install.rb new file mode 100644 index 0000000..92e6a3c --- /dev/null +++ b/install.rb @@ -0,0 +1,74 @@ +# This script generates an install.sh file which can be used to install +# puppet-agent on supported FOSS POSIX platforms. +# +# The script leverages tasks from the puppet_agent and facts modules, and +# attempts to change as few things as possible. + +# we just need the variables from here +facts_script = `sed '/munge_name "$family"/q' < modules/facts/tasks/bash.sh` +install_script = File.read('modules/puppet_agent/tasks/install_shell.sh') + .sub('[ -f "$PT__installdir/facts/tasks/bash.sh" ]', 'true') + .sub('$(bash $PT__installdir/facts/tasks/bash.sh "platform")', '$ID') + .sub('$(bash $PT__installdir/facts/tasks/bash.sh "release")', '$full') + +File.write('install.sh', <<-SH) +#!/usr/bin/env bash + +beginswith() { case $2 in "$1"*) true;; *) false;; esac; } + +function usage() +{ + cat << HEREDOC + + Usage: install.sh [--version VERSION] [--collection COLLECTION] [--cleanup] [--noop] + + optional arguments: + -h, --help show this help message and exit + -v, --version VERSION install a specific puppet-agent version + -c, --collection COLLECTION install a specific puppet-agent collection (e.g. puppet7) + -n, --noop do a dry run, do not change any files + --cleanup remove the puppetlabs repository after installation finishes + +HEREDOC +} + +while [[ "$#" -gt 0 ]]; do + case $1 in + -v|--version) PT_version="$2"; shift ; + if beginswith "6." "$PT_version"; then + PT_collection="puppet6" + elif beginswith "7." "$PT_version"; then + PT_collection="puppet7" + else + PT_collection="puppet" + fi ;; + -c|--collection) PT_collection="$2"; shift ;; + --cleanup) PT_cleanup=true; shift ;; + -n|--noop) PT__noop=true; shift ;; + -h|--help) usage; exit ;; + *) echo "Unknown parameter passed: $1"; usage; exit 1 ;; + esac + shift +done + +# shellcheck disable=SC1000-SC9999 +{ +#{facts_script} +#{install_script} +} + +if [[ $PT__noop != true ]]; then + if [[ $PT_cleanup == true ]]; then + info "Cleanup requested, removing ${collection}-release repository..." + case $platform in + SLES|el|Amzn|"Amazon Linux"|Fedora) + rpm -e --allmatches ${collection}-release + ;; + Debian|LinuxMint|Linuxmint|Ubuntu) + dpkg -r ${collection}-release + ;; + esac + fi +fi +SH + diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..a3e7c0d --- /dev/null +++ b/install.sh @@ -0,0 +1,938 @@ +#!/usr/bin/env bash + +beginswith() { case $2 in "$1"*) true;; *) false;; esac; } + +function usage() +{ + cat << HEREDOC + + Usage: install.sh [--version VERSION] [--collection COLLECTION] [--cleanup] [--noop] + + optional arguments: + -h, --help show this help message and exit + -v, --version VERSION install a specific puppet-agent version + -c, --collection COLLECTION install a specific puppet-agent collection (e.g. puppet7) + -n, --noop do a dry run, do not change any files + --cleanup remove the puppetlabs repository after installation finishes + +HEREDOC +} + +while [[ "$#" -gt 0 ]]; do + case $1 in + -v|--version) PT_version="$2"; shift ; + if beginswith "6." "$PT_version"; then + PT_collection="puppet6" + elif beginswith "7." "$PT_version"; then + PT_collection="puppet7" + else + PT_collection="puppet" + fi ;; + -c|--collection) PT_collection="$2"; shift ;; + --cleanup) PT_cleanup=true; shift ;; + -n|--noop) PT__noop=true; shift ;; + -h|--help) usage; exit ;; + *) echo "Unknown parameter passed: $1"; usage; exit 1 ;; + esac + shift +done + +# shellcheck disable=SC1000-SC9999 +{ +#!/bin/bash + +# This script may be called outside of a task, e.g. by puppet_agent +# so we have to just paste this code here. *grumbles* +# Exit with an error message and error code, defaulting to 1 +fail() { + # Print a message: entry if there were anything printed to stderr + if [[ -s $_tmp ]]; then + # Hack to try and output valid json by replacing newlines with spaces. + error_data="{ \"msg\": \"$(tr '\n' ' ' <"$_tmp")\", \"kind\": \"bash-error\", \"details\": {} }" + else + error_data="{ \"msg\": \"Task error\", \"kind\": \"bash-error\", \"details\": {} }" + fi + echo "{ \"status\": \"failure\", \"_error\": $error_data }" + exit "${2:-1}" +} + +validation_error() { + error_data="{ \"msg\": \""$1"\", \"kind\": \"bash-error\", \"details\": {} }" + echo "{ \"status\": \"failure\", \"_error\": $error_data }" + exit 255 +} + +success() { + echo "$1" +} + +determine_command_for_facter_4() { + puppet_version="$(puppet --version)" + + if (( ${puppet_version%%.*} == 6 )); then + # puppet 6 with facter 4 + facts_command=(facter --json --show-legacy) + else + # puppet 7 with facter 4 + facts_command=(puppet facts show --show-legacy --render-as json) + fi +} + +maybe_delegate_to_facter() { + [[ $PATH =~ \/opt\/puppetlabs\/bin ]] || export PATH="${PATH}:/opt/puppetlabs/bin" + + # Only use facter if we're running as the "facts" task, not the "facts::bash" + # task. This also skips calling facter if we're running as a script, which is + # used by the puppet_agent task. + if [[ $PT__task == facts ]] && type facter &>/dev/null; then + facter_version="$(facter -v)" + + if (( ${facter_version%%.*} <= 2 )); then + facts_command=(facter -p --json) + elif (( ${facter_version%%.*} == 3 )); then + facts_command=(facter -p --json --show-legacy) + else + # facter 4 + determine_command_for_facter_4 + fi + + exec -- "${facts_command[@]}" + fi +} + +# Get info from one of /etc/os-release or /usr/lib/os-release +# This is the preferred method and is checked first +_systemd() { + # These files may have unquoted spaces in the "pretty" fields even if the spec says otherwise + # source cannot use process subsitution in some versions of bash, so redirect to stdin instead + source /dev/stdin <<<"$(sed 's/ /_/g' "$1")" + + # According to `man os-release`, the first entry in ID_LIKE + # should be the one the platform most closely resembles + if [[ $ID = 'rhel' ]]; then + family='RedHat' + elif [[ $ID = 'debian' ]]; then + family='Debian' + elif [[ $ID_LIKE ]]; then + family="${ID_LIKE%% *}" + else + family="${ID}" + fi +} + +# Get info from lsb_release +_lsb_release() { + read -r ID < <(lsb_release -si) + read -r VERSION_ID < <(lsb_release -sr) + read -r VERSION_CODENAME < <(lsb_release -sc) +} + +# Get info from rhel /etc/*-release files +_rhel() { + family='RedHat' + # slurp the file + ver_info=$(<"$1") + # ID is the first word in the string + ID="${ver_info%% *}" + # Codename is hopefully the word(s) in parenthesis + if echo "$ver_info" | grep -q '('; then + VERSION_CODENAME="${ver_info##*\(}" + VERSION_CODENAME=""${VERSION_CODENAME//[()]/}"" + fi + + # Get a string like 'release 1.2.3' and grab everything after the space + release=$(echo "$ver_info" | grep -Eo 'release[[:space:]]*[0-9.]+') + VERSION_ID="${release#* }" +} + +# Last resort +_uname() { + [[ $ID ]] || ID="$(uname)" + [[ $VERSION_ID ]] || VERSION_ID="$(uname -r)" +} + +# Taken from https://github.com/puppetlabs/facter/blob/master/lib/inc/facter/facts/os.hpp +# If not in this list, we just uppercase the first character and lowercase the rest +munge_name() { + case "$1" in + redhat|rhel|red) echo "RedHat" ;; + ol|oracle) echo "OracleLinux" ;; + ubuntu) echo "Ubuntu" ;; + debian) echo "Debian" ;; + centos) echo "CentOS" ;; + cloud) echo "CloudLinux" ;; + virtuozzo) echo "VirtuozzoLinux" ;; + psbm) echo "PSBM" ;; + xenserver) echo "XenServer" ;; + linuxmint) echo "LinuxMint" ;; + sles) echo "SLES" ;; + suse) echo "SuSE" ;; + opensuse) echo "OpenSuSE" ;; + sunos) echo "SunOS" ;; + omni) echo "OmniOS" ;; + openindiana) echo "OpenIndiana" ;; + manjaro) echo "ManjaroLinux" ;; + smart) echo "SmartOS" ;; + openwrt) echo "OpenWrt" ;; + meego) echo "MeeGo" ;; + coreos) echo "CoreOS" ;; + zen) echo "XCP" ;; + kfreebsd) echo "GNU/kFreeBSD" ;; + arista) echo "AristaEOS" ;; + huawei) echo "HuaweiOS" ;; + photon) echo "PhotonOS" ;; + *) echo "$(tr '[:lower:]' '[:upper:]' <<<"${ID:0:1}")""$(tr '[:upper:]' '[:lower:'] <<<"${ID:1}")" + esac +} + +_tmp="$(mktemp)" +exec 2>>"$_tmp" + +shopt -s nocasematch + +# Use indirection to munge PT_ environment variables +# e.g. "$PT_version" becomes "$version" +for v in ${!PT_*}; do + declare "${v#*PT_}"="${!v}" +done + +# Delegate to facter executable if it exists. This function will `exec` and not +# return if facter exists. Otherwise, we'll continue on. +maybe_delegate_to_facter "$@" + +if [[ -e /etc/os-release ]]; then + _systemd /etc/os-release +elif [[ -e /usr/lib/os-release ]]; then + _systemd /usr/lib/os-release +fi + +# If either systemd is not installed or we didn't get a minor version or codename from os-release +if ! [[ $VERSION_ID ]] || (( ${VERSION_ID%%.*} == ${VERSION_ID#*.} )) || ! [[ $VERSION_CODENAME ]]; then + if [[ -e /etc/fedora-release ]]; then + _rhel /etc/fedora-release + elif [[ -e /etc/centos-release ]]; then + _rhel /etc/centos-release + elif [[ -e /etc/oracle-release ]]; then + _rhel /etc/oracle-release + elif [[ -e /etc/redhat-release ]]; then + _rhel /etc/redhat-release + elif type lsb_release &>/dev/null; then + _lsb_release + else + _uname + fi +fi + +full="${VERSION_ID}" +major="${VERSION_ID%%.*}" +# Minor is considered the second part of the version string +IFS='.' read -ra minor <<<"$full" +minor="${minor[1]}" + +ID="$(munge_name "$ID")" +family="$(munge_name "$family")" + +#!/usr/bin/env bash +# Install puppet-agent as a task +# +# From https://github.com/petems/puppet-install-shell/blob/master/install_puppet_5_agent.sh + +# Timestamp +now () { + date +'%H:%M:%S %z' +} + +# Logging functions instead of echo +log () { + echo "`now` ${1}" +} + +info () { + if [[ $PT__noop != true ]]; then + log "INFO: ${1}" + fi +} + +warn () { + log "WARN: ${1}" +} + +critical () { + log "CRIT: ${1}" +} + +# Check whether a command exists - returns 0 if it does, 1 if it does not +exists() { + if command -v $1 >/dev/null 2>&1 + then + return 0 + else + return 1 + fi +} + +# Check whether the apt config file has been modified, warning and exiting early if it has +assert_unmodified_apt_config() { + puppet_list=/etc/apt/sources.list.d/puppet.list + puppet6_list=/etc/apt/sources.list.d/puppet6.list + puppet7_list=/etc/apt/sources.list.d/puppet7.list + + if [[ -f $puppet_list ]]; then + list_file=puppet_list + elif [[ -f $puppet6_list ]]; then + list_file=puppet6_list + elif [[ -f $puppet7_list ]]; then + list_file=puppet7_list + fi + + # If puppet.list exists, get its md5sum on disk and its md5sum from the puppet-release package + if [[ -n $list_file ]]; then + # For md5sum, the checksum is the first word + file_md5=($(md5sum "$list_file")) + # For dpkg-query with this output format, the sum is the second word + package_md5=($(dpkg-query -W -f='${Conffiles}\n' 'puppet-release' | grep -F "$list_file")) + + # If the $package_md5 array is set, and the md5sum on disk doesn't match the md5sum from dpkg-query, it has been modified + if [[ $package_md5 && ${file_md5[0]} != ${package_md5[1]} ]]; then + warn "Configuration file $list_file has been modified from the default. Skipping agent installation." + exit 1 + fi + fi +} + +# Check whether perl and LWP::Simple module are installed +exists_perl() { + if perl -e 'use LWP::Simple;' >/dev/null 2>&1 + then + return 0 + else + return 1 + fi +} + +# Get command line arguments +if [ -n "$PT_version" ]; then + version=$PT_version +fi + +if [ -n "$PT_collection" ]; then + # Check whether collection is nightly + if [[ "$PT_collection" == *"nightly"* ]]; then + nightly=true + else + nightly=false + fi + + collection=$PT_collection +else + collection='puppet' +fi + +if [ -n "$PT_yum_source" ]; then + yum_source=$PT_yum_source +else + if [ "$nightly" = true ]; then + yum_source='http://nightlies.puppet.com/yum' + else + yum_source='http://yum.puppet.com' + fi +fi + +if [ -n "$PT_apt_source" ]; then + apt_source=$PT_apt_source +else + if [ "$nightly" = true ]; then + apt_source='http://nightlies.puppet.com/apt' + else + apt_source='http://apt.puppet.com' + fi +fi + +if [ -n "$PT_mac_source" ]; then + mac_source=$PT_mac_source +else + if [ "$nightly" = true ]; then + mac_source='http://nightlies.puppet.com/downloads' + else + mac_source='http://downloads.puppet.com' + fi +fi + +if [ -n "$PT_retry" ]; then + retry=$PT_retry +else + retry=5 +fi + +# Track to handle puppet5 to puppet6 +if [ -f /opt/puppetlabs/puppet/VERSION ]; then + installed_version=`cat /opt/puppetlabs/puppet/VERSION` +else + installed_version=uninstalled +fi + +# Only install the agent in cases where no agent is present, or the version of the agent +# has been explicitly defined and does not match the version of an installed agent. +if [ -z "$version" ]; then + if [ "$installed_version" == "uninstalled" ]; then + info "Version parameter not defined and no agent detected. Assuming latest." + version=latest + else + info "Version parameter not defined and agent detected. Nothing to do." + exit 0 + fi +else + info "Version parameter defined: ${version}" + if [ "$version" == "$installed_version" ]; then + info "Version parameter defined: ${version}. Puppet Agent ${version} detected. Nothing to do." + exit 0 + elif [ "$version" != "latest" ]; then + puppet_agent_version="$version" + fi +fi + +# Error if non-root +if [ `id -u` -ne 0 ]; then + echo "puppet_agent::install task must be run as root" + exit 1 +fi + +# Retrieve Platform and Platform Version +# Utilize facts implementation when available +if true; then + # Use facts module bash.sh implementation + platform=$ID + platform_version=$full + + # Handle CentOS + if test "x$platform" = "xCentOS"; then + platform="el" + + # Handle Rocky + elif test "x$platform" = "xRocky"; then + platform="el" + + # Handle Oracle + elif test "x$platform" = "xOracle Linux Server"; then + platform="el" + elif test "x$platform" = "xOracleLinux"; then + platform="el" + + # Handle Scientific + elif test "x$platform" = "xScientific Linux"; then + platform="el" + elif test "x$platform" = "xScientific"; then + platform="el" + + # Handle RedHat + elif test "x$platform" = "xRedHat"; then + platform="el" + + # If facts task return "Linux" for platform, investigate. + elif test "x$platform" = "xLinux"; then + if test -f "/etc/SuSE-release"; then + if grep -q 'Enterprise' /etc/SuSE-release; then + platform="SLES" + platform_version=`awk '/^VERSION/ {V = $3}; /^PATCHLEVEL/ {P = $3}; END {print V "." P}' /etc/SuSE-release` + else + echo "No builds for platform: SUSE" + exit 1 + fi + elif test -f "/etc/redhat-release"; then + platform="el" + platform_version=`sed 's/^.\+ release \([.0-9]\+\).*/\1/' /etc/redhat-release` + fi + + # Handle OSX + elif test "x$platform" = "xDarwin"; then + platform="mac_os_x" + # Matching the tab-space with sed is error-prone + platform_version=`sw_vers | awk '/^ProductVersion:/ { print $2 }'` + + major_version=`echo $platform_version | cut -d. -f1,2` + + # Excepting MacOS 10.x, the major version is the first number only + if ! echo "${major_version}" | grep -q '^10\.'; then + major_version=$(echo "${major_version}" | cut -d '.' -f 1); + fi + + case $major_version in + "10.11") platform_version="10.11";; + "10.12") platform_version="10.12";; + "10.13") platform_version="10.13";; + "10.14") platform_version="10.14";; + "10.15") platform_version="10.15";; + "11") platform_version="11";; + *) echo "No builds for platform: $major_version" + exit 1 + ;; + esac + fi +else + echo "This module depends on the puppetlabs-facts module" + exit 1 +fi + +if test "x$platform" = "x"; then + critical "Unable to determine platform version!" + exit 1 +fi + +# Mangle $platform_version to pull the correct build +# for various platforms +major_version=`echo $platform_version | cut -d. -f1` +case $platform in + "el") + platform_version=$major_version + ;; + "Fedora") + case $major_version in + "23") platform_version="22";; + *) platform_version=$major_version;; + esac + ;; + "Debian") + case $major_version in + "5") platform_version="6";; + "6") platform_version="6";; + "7") platform_version="6";; + esac + ;; + "SLES") + platform_version=$major_version + ;; + "Amzn"|"Amazon Linux") + case $platform_version in + "2") platform_version="7";; + esac + ;; +esac + +# Find which version of puppet is currently installed if any + +if test "x$platform_version" = "x"; then + critical "Unable to determine platform version!" + exit 1 +fi + +unable_to_retrieve_package() { + critical "Unable to retrieve a valid package!" + exit 1 +} + +random_hexdump () { + hexdump -n 2 -e '/2 "%u"' /dev/urandom +} + +if test "x$TMPDIR" = "x"; then + tmp="/tmp" +else + tmp=${TMPDIR} + # TMPDIR has trailing file sep for OSX test box + penultimate=$((${#tmp}-1)) + if test "${tmp:$penultimate:1}" = "/"; then + tmp="${tmp:0:$penultimate}" + fi +fi + +# Random function since not all shells have $RANDOM +if exists hexdump; then + random_number=$(random_hexdump) +else + random_number="`date +%N`" +fi + +tmp_dir="$tmp/install.sh.$$.$random_number" +(umask 077 && mkdir $tmp_dir) || exit 1 + +tmp_stderr="$tmp/stderr.$$.$random_number" + +capture_tmp_stderr() { + # spool up tmp_stderr from all the commands we called + if test -f $tmp_stderr; then + output=`cat ${tmp_stderr}` + stderr_results="${stderr_results}\nSTDERR from $1:\n\n$output\n" + fi +} + +trap "rm -f $tmp_stderr; rm -rf $tmp_dir; exit $1" 1 2 15 + +# Run command and retry on failure +# run_cmd CMD +run_cmd() { + eval $1 + rc=$? + + if test $rc -ne 0; then + attempt_number=0 + while test $attempt_number -lt $retry; do + info "Retrying... [$((attempt_number + 1))/$retry]" + eval $1 + rc=$? + + if test $rc -eq 0; then + break + fi + + info "Return code: $rc" + sleep 1s + ((attempt_number=attempt_number+1)) + done + fi + + return $rc +} + +# do_wget URL FILENAME +do_wget() { + info "Trying wget..." + run_cmd "wget -O '$2' '$1' 2>$tmp_stderr" + rc=$? + + # check for 404 + grep "ERROR 404" $tmp_stderr 2>&1 >/dev/null + if test $? -eq 0; then + critical "ERROR 404" + unable_to_retrieve_package + fi + + # check for bad return status or empty output + if test $rc -ne 0 || test ! -s "$2"; then + capture_tmp_stderr "wget" + return 1 + fi + + return 0 +} + +# do_curl URL FILENAME +do_curl() { + info "Trying curl..." + run_cmd "curl -1 -sL -D $tmp_stderr '$1' > '$2'" + rc=$? + + # check for 404 + grep "404 Not Found" $tmp_stderr 2>&1 >/dev/null + if test $? -eq 0; then + critical "ERROR 404" + unable_to_retrieve_package + fi + + # check for bad return status or empty output + if test $rc -ne 0 || test ! -s "$2"; then + capture_tmp_stderr "curl" + return 1 + fi + + return 0 +} + +# do_fetch URL FILENAME +do_fetch() { + info "Trying fetch..." + run_cmd "fetch -o '$2' '$1' 2>$tmp_stderr" + rc=$? + + # check for 404 + grep "404 Not Found" $tmp_stderr 2>&1 >/dev/null + if test $? -eq 0; then + critical "ERROR 404" + unable_to_retrieve_package + fi + + # check for bad return status or empty output + if test $rc -ne 0 || test ! -s "$2"; then + capture_tmp_stderr "fetch" + return 1 + fi + + return 0 +} + +# do_perl URL FILENAME +do_perl() { + info "Trying perl..." + run_cmd "perl -e 'use LWP::Simple; getprint(\$ARGV[0]);' '$1' > '$2' 2>$tmp_stderr" + rc=$? + + # check for 404 + grep "404 Not Found" $tmp_stderr 2>&1 >/dev/null + if test $? -eq 0; then + critical "ERROR 404" + unable_to_retrieve_package + fi + + # check for bad return status or empty output + if test $rc -ne 0 || test ! -s "$2"; then + capture_tmp_stderr "perl" + return 1 + fi + + return 0 +} + +# do_download URL FILENAME +do_download() { + info "Downloading $1" + info " to file $2" + + # we try all of these until we get success. + # perl, in particular may be present but LWP::Simple may not be installed + + if exists wget; then + do_wget $1 $2 && return 0 + fi + + if exists curl; then + do_curl $1 $2 && return 0 + fi + + if exists fetch; then + do_fetch $1 $2 && return 0 + fi + + if exists_perl; then + do_perl $1 $2 && return 0 + fi + + critical "Cannot download package as none of wget/curl/fetch/perl-LWP-Simple is found" + unable_to_retrieve_package +} + +# install_file TYPE FILENAME +# TYPE is "rpm", "deb", "solaris" or "dmg" +install_file() { + case "$1" in + "rpm") + info "installing puppetlabs yum repo with rpm..." + if test -f "/etc/yum.repos.d/puppetlabs-pc1.repo"; then + info "existing puppetlabs yum repo found, moving to old location" + mv /etc/yum.repos.d/puppetlabs-pc1.repo /etc/yum.repos.d/puppetlabs-pc1.repo.old + fi + + if test "x$installed_version" != "xuninstalled"; then + info "Version ${installed_version} detected..." + major=$(echo $installed_version | cut -d. -f1) + pkg="puppet${major}-release" + + if echo $2 | grep $pkg; then + info "No collection upgrade detected" + else + info "Collection upgrade detected, replacing puppet${major}-release" + rpm -e "puppet${major}-release" + fi + fi + + rpm -Uvh --oldpackage --replacepkgs "$2" + if test "$version" = 'latest'; then + run_cmd "yum install -y puppet-agent && yum upgrade -y puppet-agent" + else + run_cmd "yum install -y 'puppet-agent-${puppet_agent_version}'" + fi + ;; + "noarch.rpm") + info "installing puppetlabs yum repo with zypper..." + + if test "x$installed_version" != "xuninstalled"; then + info "Version ${installed_version} detected..." + major=$(echo $installed_version | cut -d. -f1) + pkg="puppet${major}-release" + + if echo $2 | grep $pkg; then + info "No collection upgrade detected" + else + info "Collection upgrade detected, replacing puppet${major}-release" + zypper remove --no-confirm "puppet${major}-release" + fi + fi + + run_cmd "zypper install --no-confirm '$2'" + if test "$version" = "latest"; then + run_cmd "zypper install --no-confirm 'puppet-agent'" + else + run_cmd "zypper install --no-confirm --oldpackage --no-recommends --no-confirm 'puppet-agent-${puppet_agent_version}'" + fi + ;; + "deb") + info "Installing puppetlabs apt repo with dpkg..." + + if test "x$installed_version" != "xuninstalled"; then + info "Version ${installed_version} detected..." + major=$(echo $installed_version | cut -d. -f1) + pkg="puppet${major}-release" + + if echo $2 | grep $pkg; then + info "No collection upgrade detected" + else + info "Collection upgrade detected, replacing puppet${major}-release" + dpkg --purge "puppet${major}-release" + fi + fi + + assert_unmodified_apt_config + + dpkg -i --force-confmiss "$2" + run_cmd 'apt-get update -y' + + if test "$version" = 'latest'; then + run_cmd "apt-get install -y puppet-agent" + else + if test "x$deb_codename" != "x"; then + run_cmd "apt-get install -y 'puppet-agent=${puppet_agent_version}-1${deb_codename}'" + else + run_cmd "apt-get install -y 'puppet-agent=${puppet_agent_version}'" + fi + fi + ;; + "dmg" ) + info "installing puppetlabs dmg with hdiutil and installer" + mountpoint="$(mktemp -d -t $(random_hexdump))" + /usr/bin/hdiutil attach "${download_filename?}" -nobrowse -readonly -mountpoint "${mountpoint?}" + /usr/sbin/installer -pkg ${mountpoint?}/puppet-agent-*-installer.pkg -target / + /usr/bin/hdiutil detach "${mountpoint?}" + rm -f $download_filename + ;; + *) + critical "Unknown filetype: $1" + exit 1 + ;; + esac + if test $? -ne 0; then + critical "Installation failed" + exit 1 + fi +} + +info "Downloading Puppet $version for ${platform}..." +case $platform in + "SLES") + info "SLES platform! Lets get you an RPM..." + + if [[ $PT__noop != true ]]; then + for key in "puppet" "puppet-20250406"; do + gpg_key="${tmp_dir}/RPM-GPG-KEY-${key}" + do_download "https://yum.puppet.com/RPM-GPG-KEY-${key}" "$gpg_key" + rpm --import "$gpg_key" + rm -f "$gpg_key" + done + fi + + filetype="noarch.rpm" + filename="${collection}-release-sles-${platform_version}.noarch.rpm" + download_url="${yum_source}/${filename}" + ;; + "el") + info "Red hat like platform! Lets get you an RPM..." + filetype="rpm" + filename="${collection}-release-el-${platform_version}.noarch.rpm" + download_url="${yum_source}/${filename}" + ;; + "Amzn"|"Amazon Linux") + info "Amazon platform! Lets get you an RPM..." + filetype="rpm" + filename="${collection}-release-el-${platform_version}.noarch.rpm" + download_url="${yum_source}/${filename}" + ;; + "Fedora") + info "Fedora platform! Lets get the RPM..." + filetype="rpm" + filename="${collection}-release-fedora-${platform_version}.noarch.rpm" + download_url="${yum_source}/${filename}" + ;; + "Debian") + info "Debian platform! Lets get you a DEB..." + case $major_version in + "5") deb_codename="lenny";; + "6") deb_codename="squeeze";; + "7") deb_codename="wheezy";; + "8") deb_codename="jessie";; + "9") deb_codename="stretch";; + "10") deb_codename="buster";; + esac + filetype="deb" + filename="${collection}-release-${deb_codename}.deb" + download_url="${apt_source}/${filename}" + ;; + "Linuxmint"|"LinuxMint") + info "Mint platform! Lets get you a DEB..." + case $major_version in + "3") deb_codename="stretch";; + "4") deb_codename="buster";; + "20") deb_codename="focal";; + "19") deb_codename="bionic";; + "18") deb_codename="xenial";; + "17") deb_codename="trusty";; + esac + filetype="deb" + filename="${collection}-release-${deb_codename}.deb" + download_url="${apt_source}/${filename}" + ;; + "Ubuntu") + info "Ubuntu platform! Lets get you a DEB..." + case $platform_version in + "12.04") deb_codename="precise";; + "12.10") deb_codename="quantal";; + "13.04") deb_codename="raring";; + "13.10") deb_codename="saucy";; + "14.04") deb_codename="trusty";; + "14.10") deb_codename="trusty";; + "15.04") deb_codename="vivid";; + "15.10") deb_codename="wily";; + "16.04") deb_codename="xenial";; + "16.10") deb_codename="yakkety";; + "17.04") deb_codename="zesty";; + "18.04") deb_codename="bionic";; + "20.04") deb_codename="focal";; + esac + filetype="deb" + filename="${collection}-release-${deb_codename}.deb" + download_url="${apt_source}/${filename}" + ;; + "mac_os_x") + info "OSX platform! Lets get you a DMG..." + filetype="dmg" + if test "$version" = "latest"; then + filename="puppet-agent-latest.dmg" + else + filename="puppet-agent-${version}-1.osx${platform_version}.dmg" + fi + download_url="${mac_source}/mac/${collection}/${platform_version}/x86_64/${filename}" + ;; + *) + critical "Sorry $platform is not supported yet!" + exit 1 + ;; +esac + +if [[ $PT__noop != true ]]; then + download_filename="${tmp_dir}/${filename}" + + do_download "$download_url" "$download_filename" + + install_file $filetype "$download_filename" + + if [[ $PT_stop_service = true ]]; then + /opt/puppetlabs/bin/puppet resource service puppet ensure=stopped enable=false + fi + + #Cleanup + if test "x$tmp_dir" != "x"; then + rm -r "$tmp_dir" + fi +fi + +} + +if [[ $PT__noop != true ]]; then + if [[ $PT_cleanup == true ]]; then + info "Cleanup requested, removing ${collection}-release repository..." + case $platform in + SLES|el|Amzn|"Amazon Linux"|Fedora) + rpm -e --allmatches ${collection}-release + ;; + Debian|LinuxMint|Linuxmint|Ubuntu) + dpkg -r ${collection}-release + ;; + esac + fi +fi