Skip to content

Commit

Permalink
new install script from get.pigsty.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
Vonng committed May 16, 2024
1 parent 2b363bf commit 00e7f0c
Showing 1 changed file with 279 additions and 0 deletions.
279 changes: 279 additions & 0 deletions bin/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
#!/bin/bash
# set -o pipefail
#==============================================================#
# File : install
# Desc : download & install pigsty from get.pigsty.cc
# Ctime : 2022-10-30
# Mtime : 2024-05-16
# Path : https://get.pigsty.cc/{i,install,latest,beta}
# Usage : bash -c "$(curl -fsSL https://get.pigsty.cc/i)"
# Deps : curl
# Author : Ruohang Feng (rh@vonng.com)
# License : AGPLv3
#==============================================================#
DEFAULT_VERSION=v2.7.0

# To install the latest version of pigsty (v2.7.0):
# bash -c "$(curl -fsSL https://get.pigsty.cc/i)"
# bash -c "$(curl -fsSL https://get.pigsty.cc/install)"
# bash -c "$(curl -fsSL https://get.pigsty.cc/latest)"
#
# To install a specific version of pigsty (e.g. v2.6.0)
# bash -c "$(curl -fsSL https://get.pigsty.cc/i)" -- v2.6.0
# curl -fsSL https://get.pigsty.cc/i | bash -s v2.6.0


#--------------------------------------------------------------#
# Log Util
#--------------------------------------------------------------#
# if output is a terminal, setup color alias, else use empty str
if [[ -t 1 ]]; then
__CN='\033[0m';__CB='\033[0;30m';__CR='\033[0;31m';__CG='\033[0;32m';
__CY='\033[0;33m';__CB='\033[0;34m';__CM='\033[0;35m';__CC='\033[0;36m';__CW='\033[0;37m';
else
__CN='';__CB='';__CR='';__CG='';__CY='';__CB='';__CM='';__CC='';__CW='';
fi
function log_info() { printf "[${__CG} OK ${__CN}] ${__CG}$*${__CN}\n"; }
function log_warn() { printf "[${__CY}WARN${__CN}] ${__CY}$*${__CN}\n"; }
function log_error() { printf "[${__CR}FAIL${__CN}] ${__CR}$*${__CN}\n"; }
function log_red() { printf "[${__CR}WARN${__CN}] ${__CR}$*${__CN}\n"; }
function log_debug() { printf "[${__CB}HINT${__CN}] ${__CB}$*${__CN}\n"; }
function log_title() { printf "[${__CG}$1${__CN}] ${__CG}$2${__CN}\n"; }
function log_hint() { printf "${__CB}$*${__CN}\n"; }
function log_line() { printf "${__CM}[$*] ===========================================${__CN}\n"; }


#--------------------------------------------------------------#
# Version
#--------------------------------------------------------------#
VALID_VERSIONS="\
v1.0.0 v1.1.1 v1.2.0 v1.3.0 v1.4.0 v1.4.1 v1.5.0 v1.5.1 v2.0.0 \
v2.0.1 v2.0.2 v2.1.0 v2.2.0 v2.3.0 v2.3.1 v2.4.0 v2.4.1 v2.5.0 v2.5.1 v2.6.0 v2.7.0"
VERSION=${DEFAULT_VERSION}
VERSION_FROM="default"
KERNEL=$(uname)
ARCH=$(uname -m)

# arg1 > env > default
if [[ -n "$1" ]]; then
VERSION=$1
VERSION_FROM="arg"
fi

function check_version(){
local found=false

# validate version format
if [[ ! ${VERSION} =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+[0-9]+)?$ ]]; then
log_error "invalid version string from ${VERSION_FROM}: ${VERSION}"
exit 1
fi

# check version in table
for ver in ${VALID_VERSIONS}; do
if [ "${ver}" = "${VERSION}" ]; then
found="true"
break
fi
done

# print version string if valid
if [[ "${found}" != "true" ]]; then
log_error "invalid version from ${VERSION_FROM}: ${VERSION}"
log_hint "valid pigsty versions:"
for ver in ${VALID_VERSIONS}; do
log_hint " - ${ver}"
done
exit 1
fi
}

check_version


#--------------------------------------------------------------#
# Reference
#--------------------------------------------------------------#
log_line "${VERSION}"
log_hint "$ curl -fsSL https://pigsty.cc/install | bash"
log_title "Site" "https://pigsty.io"
log_title "Demo" "https://demo.pigsty.cc"
log_title "Repo" "https://github.com/Vonng/pigsty"
log_title "Docs" "https://pigsty.io/docs/setup/install"

log_line "Download"

log_info "version = ${VERSION} (from ${VERSION_FROM})"


#--------------------------------------------------------------#
# Download
#--------------------------------------------------------------#
PIGSTY_BASEURL="https://get.pigsty.cc"
GITHUB_BASEURL="https://github.com/Vonng/pigsty/releases/download"

SRC_FILENAME="pigsty-${VERSION}.tgz"
PIGSTY_SRC_URL="${PIGSTY_BASEURL}/${VERSION}/${SRC_FILENAME}"
GITHUB_SRC_URL="${GITHUB_BASEURL}/${VERSION}/${SRC_FILENAME}"
DOWNLOAD_TO="/tmp/${SRC_FILENAME}"

# download file from url, if file already exists with same size, skip download
function download_file(){
local data_url=$1
local data_file=$2

log_hint "curl -fSL ${data_url} -o ${data_file}"
# if file exists and have the exact same size, just use it and skip downloading
if [[ -f ${data_file} ]]; then
if [[ "$(uname)" == "Darwin" ]]; then
size=$(stat -f %z "${data_file}")
else
size=$(stat -c %s "${data_file}")
fi
curl_size=$(curl -fsLI ${data_url} | grep -i 'Content-Length' | awk '{print $2}' | tr -d '\r')
if [[ ${size} -eq ${curl_size} ]]; then
log_warn "tarball = ${data_file} exists, size = ${size}, use it"
#log_hint "rm -rf ${data_file}; # remove it to redownload source tarball"
return 0
fi
fi
curl -# -fSL ${data_url} -o ${data_file}
return $?
}

download_file "${PIGSTY_SRC_URL}" "${DOWNLOAD_TO}"
if [[ $? -ne 0 ]]; then
log_error "fail to download pigsty source from ${PIGSTY_SRC_URL}"
log_hint "check: https://pigsty.io/docs/setup/install"
log_hint "alternative url: ${GITHUB_SRC_URL}"
exit 2
fi
log_info "md5sums = $(md5sum ${DOWNLOAD_TO})"


#--------------------------------------------------------------#
# Install
#--------------------------------------------------------------#
INSTALL_TO="${HOME}/pigsty"
INSTALL_DIR=$(dirname ${INSTALL_TO})

log_line "Install"
if [[ $(whoami) == "root" ]]; then
log_warn "os user = root , it's recommended to install as a sudo-able admin"
fi

# extract to home dir if ~/pigsty not exists
if [[ ! -d ${INSTALL_TO} ]]; then
log_info "install = ${INSTALL_TO}, from ${DOWNLOAD_TO}"
tar -xf "${DOWNLOAD_TO}" -C "${INSTALL_DIR}";
else
log_warn "pigsty already installed on '${INSTALL_TO}', if you wish to overwrite:"
log_hint "sudo rm -rf /tmp/pigsty_bk; cp -r ${INSTALL_TO} /tmp/pigsty_bk; # backup old"
log_hint "sudo rm -rf /tmp/pigsty; tar -xf ${DOWNLOAD_TO} -C /tmp/; # extract new"
log_hint "rsync -av --exclude='/pigsty.yml' --exclude='/files/pki/***' /tmp/pigsty/ ${INSTALL_TO}/; # rsync src"
fi



#--------------------------------------------------------------#
# OS Info
#--------------------------------------------------------------#
# Print optional offline package info if application to current kernel, arch, os, distro
OS_VENDOR=""
OS_VERSION=""
OS_PACKAGE=""
OS_MANAGER=""
OS_CODENAME=""
PIGSTY_VERSION=${VERSION}

function check_os(){
if [[ "$(uname)" == "Darwin" ]]; then
OS_VENDOR="MacOS"
return 0
fi

if [[ -f /etc/os-release ]]; then
. /etc/os-release
OS_VENDOR="$ID"
OS_VERSION="$VERSION_ID"
OS_CODENAME=${VERSION_CODENAME-''}
if [[ $VERSION_ID == *.* ]]; then
OS_VERSION=$(echo "$VERSION_ID" | cut -d. -f1)
else
OS_VERSION="${VERSION_ID}"
fi
else
OS_VENDOR="unknown"
OS_VERSION="unknown"
fi

# get package / manager: rpm|deb and dnf|yum|apt|apt-get|zypper
if command -v dpkg >/dev/null 2>&1; then
OS_PACKAGE="deb"
if command -v apt >/dev/null 2>&1; then
OS_MANAGER="apt"
elif command -v apt-get >/dev/null 2>&1; then
OS_MANAGER="apt-get"
else
OS_MANAGER="unknown"
fi
elif command -v rpm >/dev/null 2>&1; then
OS_PACKAGE="rpm"
if command -v dnf >/dev/null 2>&1; then
OS_MANAGER="dnf"
elif command -v yum >/dev/null 2>&1; then
OS_MANAGER="yum"
elif command -v zypper >/dev/null 2>&1; then
OS_MANAGER="zypper"
else
OS_MANAGER="unknown"
fi
else
OS_PACKAGE="unknown"
OS_MANAGER="unknown"
fi
}

check_os

PKG_FILENAME=""


function check_offline_pkg_available(){
if [[ ${KERNEL} != "Linux" || ${ARCH} != "x86_64" ]]; then
return 0
fi

# EL 8, Debian 12, Ubuntu 22 has official offline packages!
if [[ ${OS_PACKAGE} == "rpm" && ${OS_VERSION} == "8" ]]; then
PKG_FILENAME="pigsty-pkg-${PIGSTY_VERSION}.el${OS_VERSION}.x86_64.tgz"
elif [[ ${OS_PACKAGE} == "deb" && ${OS_CODENAME} == "jammy" ]]; then
PKG_FILENAME="pigsty-pkg-${PIGSTY_VERSION}.${OS_VENDOR}${OS_VERSION}.x86_64.tgz"
elif [[ ${OS_PACKAGE} == "deb" && ${OS_CODENAME} == "bookworm" ]]; then
PKG_FILENAME="pigsty-pkg-${PIGSTY_VERSION}.${OS_VENDOR}${OS_VERSION}.x86_64.tgz"
fi
}

# if current OS is el8, debian 12, ubuntu 22, print offline package hint
check_offline_pkg_available
if [[ ${PKG_FILENAME} != "" ]]; then
log_line "Resource"
log_title "HINT" "${OS_VENDOR} ${OS_VERSION} ${OS_CODENAME} have [OPTIONAL] offline package available: https://pigsty.io/docs/setup/offline"

log_hint "curl -fSL ${GITHUB_BASEURL}/${PKG_FILENAME} -o /tmp/pkg.tgz"
log_hint "curl -fSL ${PIGSTY_BASEURL}/${PKG_FILENAME} -o /tmp/pkg.tgz # or use alternative CDN"
#log_warn 'RPM/DEB pkg conflict MAY occur if OS minor version mismatch! use with caution!'
fi


#--------------------------------------------------------------#
# Next Hint
#--------------------------------------------------------------#
log_line "TodoList"
log_hint "cd ${INSTALL_TO}"
log_hint './bootstrap # [OPTIONAL] install ansible & use offline package'
log_hint './configure # [OPTIONAL] preflight-check and config generation'
log_hint './install.yml # install pigsty modules according to your config.'

log_line "Complete"
# log_info "pigsty installed, ready for deploying with above command"

0 comments on commit 00e7f0c

Please sign in to comment.