Improved installer script

pull/50/head
deajan 8 years ago
parent bcd79fc139
commit f1b76472e2

@ -10,6 +10,8 @@ SCRIPT_BUILD=2016052501
## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8 & 10 ## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8 & 10
## Please adapt this to fit your distro needs ## Please adapt this to fit your distro needs
#TODO: silent mode and no stats mode
CONF_DIR=/etc/$PROGRAM CONF_DIR=/etc/$PROGRAM
BIN_DIR=/usr/local/bin BIN_DIR=/usr/local/bin
SERVICE_DIR_INIT=/etc/init.d SERVICE_DIR_INIT=/etc/init.d
@ -28,184 +30,272 @@ PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM="pmocr-srv.service"
## Generic code ## Generic code
USER=root ## Default log file
if [ -w /var/log ]; then
local_os_var="$(uname -spio 2>&1)" LOG_FILE="/var/log/$PROGRAM-install.log"
if [ $? != 0 ]; then elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
local_os_var="$(uname -v 2>&1)" LOG_FILE="$HOME/$PROGRAM-install.log"
if [ $? != 0 ]; then else
local_os_var="$(uname)" LOG_FILE="./$PROGRAM-install.log"
fi
fi fi
case $local_os_var in # Generic quick logging function
*"BSD"*) function _QuickLogger {
GROUP=wheel local value="${1}"
;; local destination="${2}" # Destination: stdout, log, both
*"Darwin"*)
GROUP=admin
;;
*)
GROUP=root
;;
*"MINGW32"*|*"CYGWIN"*)
USER=""
GROUP=""
;;
esac
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ]); then
echo "Must be run as $USER."
exit 1
fi
if [ -f /sbin/init ]; then if ([ "$destination" == "log" ] || [ "$destination" == "both" ]); then
if file /sbin/init | grep systemd > /dev/null; then echo -e "$(date) - $value" >> "$LOG_FILE"
init="systemd" elif ([ "$destination" == "stdout" ] || [ "$destination" == "both" ]); then
else echo -e "$value"
init="initV" fi
fi }
else
echo "Can't detect initV or systemd. Service files won't be installed. You can still run $PROGRAM manually or via cron." function QuickLogger {
init=none local value="${1}"
fi
if [ ! -d "$CONF_DIR" ]; then if [ "$_SILENT" -eq 1 ]; then
mkdir "$CONF_DIR" _QuickLogger "$value" "log"
if [ $? == 0 ]; then
echo "Created directory [$CONF_DIR]."
else else
echo "Cannot create directory [$CONF_DIR]." _QuickLogger "$value" "stdout"
exit 1
fi fi
else }
echo "Config directory [$CONF_DIR] exists."
fi
if [ -f "./sync.conf.example" ]; then function urlencode() {
cp "./sync.conf.example" "/etc/$PROGRAM/sync.conf.example" # urlencode <string>
fi
if [ -f "./host_backup.conf.example" ]; then local LANG=C
cp "./host_backup.conf.example" "/etc/$PROGRAM/host_backup.conf.example" local length="${#1}"
fi for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
if [ -f "./exlude.list.example" ]; then case $c in
cp "./exclude.list.example" "/etc/$PROGRAM" [a-zA-Z0-9.~_-]) printf "$c" ;;
fi *) printf '%%%02X' "'$c" ;;
esac
done
}
if [ -f "./snapshot.conf.example" ]; then function SetOSSettings {
cp "./snapshot.conf.example" "/etc/$PROGRAM/snapshot.conf.example" USER=root
fi
cp "./$PROGRAM_BINARY" "$BIN_DIR" local local_os_var
if [ $? != 0 ]; then
echo "Cannot copy $PROGRAM_BINARY to [$BIN_DIR]."
else
chmod 755 "$BIN_DIR/$PROGRAM_BINARY"
echo "Copied $PROGRAM_BINARY to [$BIN_DIR]."
fi
if [ -f "./$PROGRAM_BATCH" ]; then local_os_var="$(uname -spio 2>&1)"
cp "./$PROGRAM_BATCH" "$BIN_DIR"
if [ $? != 0 ]; then if [ $? != 0 ]; then
echo "Cannot copy $PROGRAM_BATCH to [$BIN_DIR]." local_os_var="$(uname -v 2>&1)"
else if [ $? != 0 ]; then
chmod 755 "$BIN_DIR/$PROGRAM_BATCH" local_os_var="$(uname)"
echo "Copied $PROGRAM_BATCH to [$BIN_DIR]." fi
fi fi
fi
if [ -f "./ssh_filter.sh" ]; then case $local_os_var in
cp "./ssh_filter.sh" "$BIN_DIR" *"BSD"*)
if [ $? != 0 ]; then GROUP=wheel
echo "Cannot copy ssh_filter.sh to [$BIN_DIR]." ;;
else *"Darwin"*)
chmod 755 "$BIN_DIR/ssh_filter.sh" GROUP=admin
if ([ "$USER" != "" ] && [ "$GROUP" != "" ]); then ;;
chown $USER:$GROUP "$BIN_DIR/ssh_filter.sh" *)
fi GROUP=root
echo "Copied ssh_filter.sh to [$BIN_DIR]." ;;
*"MINGW32"*|*"CYGWIN"*)
USER=""
GROUP=""
;;
esac
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ]); then
QuickLogger "Must be run as $USER."
exit 1
fi fi
fi
OS=$(urlencode "$local_os_var")
}
# OSYNC SPECIFIC function GetInit {
if ([ "$init" == "systemd" ] && [ -f "./$OSYNC_SERVICE_FILE_SYSTEMD_SYSTEM" ]); then if [ -f /sbin/init ]; then
cp "./$OSYNC_SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_SYSTEM" && cp "./$OSYNC_SERVICE_FILE_SYSTEMD_USER" "$SERVICE_DIR_SYSTEMD_USER/$SERVICE_FILE_SYSTEMD_SYSTEM" if file /sbin/init | grep systemd > /dev/null; then
if [ $? != 0 ]; then init="systemd"
echo "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]." else
init="initV"
fi
else else
echo "Created osync-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]." QuickLogger "Can't detect initV or systemd. Service files won't be installed. You can still run $PROGRAM manually or via cron."
echo "Can be activated with [systemctl start osync-srv@instance.conf] where instance.conf is the name of the config file in /etc/osync." init="none"
echo "Can be enabled on boot with [systemctl enable osync-srv@instance.conf]."
echo "In userland, active with [systemctl --user start osync-srv@instance.conf]."
fi fi
elif ( "$init" == "initV" ] && [ -f "./$OSYNC_SERVICE_FILE_INIT" ]); then }
cp "./$OSYNC_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT"
if [ $? != 0 ]; then function CreateConfDir {
echo "Cannot copy osync-srv to [$SERVICE_DIR_INIT]." if [ ! -d "$CONF_DIR" ]; then
mkdir "$CONF_DIR"
if [ $? == 0 ]; then
QuickLogger "Created directory [$CONF_DIR]."
else
QuickLogger "Cannot create directory [$CONF_DIR]."
exit 1
fi
else else
chmod 755 "$SERVICE_DIR_INIT/$OSYNC_SERVICE_FILE_INIT" QuickLogger "Config directory [$CONF_DIR] exists."
echo "Created osync-srv service in [$SERVICE_DIR_INIT]."
echo "Can be activated with [service $OSYNC_SERVICE_FILE_INIT start]."
echo "Can be enabled on boot with [chkconfig $OSYNC_SERVICE_FILE_INIT on]."
fi fi
fi }
# PMOCR SPECIFIC function CopyExampleFiles {
if ([ "$init" == "systemd" ] && [ -f "./$PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM" ]); then if [ -f "./sync.conf.example" ]; then
cp "./$PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_SYSTEM" cp "./sync.conf.example" "/etc/$PROGRAM/sync.conf.example"
if [ $? != 0 ]; then
echo "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
else
echo "Created pmocr-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
echo "Can be activated with [systemctl start pmocr-srv] after configuring file options in [$BIN_DIR/$PROGRAM]."
echo "Can be enabled on boot with [systemctl enable pmocr-srv]."
fi fi
elif ([ "$init" == "initV" ] && [ -f "./$PMOCR_SERVICE_FILE_INIT" ]); then
cp "./$PMOCR_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT" if [ -f "./host_backup.conf.example" ]; then
cp "./host_backup.conf.example" "/etc/$PROGRAM/host_backup.conf.example"
fi
if [ -f "./exlude.list.example" ]; then
cp "./exclude.list.example" "/etc/$PROGRAM"
fi
if [ -f "./snapshot.conf.example" ]; then
cp "./snapshot.conf.example" "/etc/$PROGRAM/snapshot.conf.example"
fi
}
function CopyProgram {
cp "./$PROGRAM_BINARY" "$BIN_DIR"
if [ $? != 0 ]; then if [ $? != 0 ]; then
echo "Cannot copy pmoct-srv to [$SERVICE_DIR_INIT]." QuickLogger "Cannot copy $PROGRAM_BINARY to [$BIN_DIR]. Make sure to run install script in the directory containing all other files."
QuickLogger "Also make sure you have permissions to write to [$BIN_DIR]."
exit 1
else else
chmod 755 "$SERVICE_DIR_INIT/$PMOCR_SERVICE_FILE_INIT" chmod 755 "$BIN_DIR/$PROGRAM_BINARY"
echo "Created osync-srv service in [$SERVICE_DIR_INIT]." QuickLogger "Copied $PROGRAM_BINARY to [$BIN_DIR]."
echo "Can be activated with [service $PMOCR_SERVICE_FILE_INIT start]."
echo "Can be enabled on boot with [chkconfig $PMOCR_SERVICE_FILE_INIT on]."
fi fi
fi
function Statistics { if [ -f "./$PROGRAM_BATCH" ]; then
cp "./$PROGRAM_BATCH" "$BIN_DIR"
if [ $? != 0 ]; then
QuickLogger "Cannot copy $PROGRAM_BATCH to [$BIN_DIR]."
else
chmod 755 "$BIN_DIR/$PROGRAM_BATCH"
QuickLogger "Copied $PROGRAM_BATCH to [$BIN_DIR]."
fi
fi
if [ -f "./ssh_filter.sh" ]; then
cp "./ssh_filter.sh" "$BIN_DIR"
if [ $? != 0 ]; then
QuickLogger "Cannot copy ssh_filter.sh to [$BIN_DIR]."
else
chmod 755 "$BIN_DIR/ssh_filter.sh"
if ([ "$USER" != "" ] && [ "$GROUP" != "" ]); then
chown $USER:$GROUP "$BIN_DIR/ssh_filter.sh"
fi
QuickLogger "Copied ssh_filter.sh to [$BIN_DIR]."
fi
fi
}
function CopyServiceFiles {
# OSYNC SPECIFIC
if ([ "$init" == "systemd" ] && [ -f "./$OSYNC_SERVICE_FILE_SYSTEMD_SYSTEM" ]); then
cp "./$OSYNC_SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_SYSTEM" && cp "./$OSYNC_SERVICE_FILE_SYSTEMD_USER" "$SERVICE_DIR_SYSTEMD_USER/$SERVICE_FILE_SYSTEMD_SYSTEM"
if [ $? != 0 ]; then
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
else
QuickLogger "Created osync-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
QuickLogger "Can be activated with [systemctl start osync-srv@instance.conf] where instance.conf is the name of the config file in /etc/osync."
QuickLogger "Can be enabled on boot with [systemctl enable osync-srv@instance.conf]."
QuickLogger "In userland, active with [systemctl --user start osync-srv@instance.conf]."
fi
elif ([ "$init" == "initV" ] && [ -f "./$OSYNC_SERVICE_FILE_INIT" ]); then
cp "./$OSYNC_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT"
if [ $? != 0 ]; then
QuickLogger "Cannot copy osync-srv to [$SERVICE_DIR_INIT]."
else
chmod 755 "$SERVICE_DIR_INIT/$OSYNC_SERVICE_FILE_INIT"
QuickLogger "Created osync-srv service in [$SERVICE_DIR_INIT]."
QuickLogger "Can be activated with [service $OSYNC_SERVICE_FILE_INIT start]."
QuickLogger "Can be enabled on boot with [chkconfig $OSYNC_SERVICE_FILE_INIT on]."
fi
fi
local link="http://instcount.netpower.fr?program=$PROGRAM&version=$PROGRAM_VERSION&os=$local_os_var" # PMOCR SPECIFIC
if ([ "$init" == "systemd" ] && [ -f "./$PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM" ]); then
cp "./$PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_SYSTEM"
if [ $? != 0 ]; then
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
else
QuickLogger "Created pmocr-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
QuickLogger "Can be activated with [systemctl start pmocr-srv] after configuring file options in [$BIN_DIR/$PROGRAM]."
QuickLogger "Can be enabled on boot with [systemctl enable pmocr-srv]."
fi
elif ([ "$init" == "initV" ] && [ -f "./$PMOCR_SERVICE_FILE_INIT" ]); then
cp "./$PMOCR_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT"
if [ $? != 0 ]; then
QuickLogger "Cannot copy pmoct-srv to [$SERVICE_DIR_INIT]."
else
chmod 755 "$SERVICE_DIR_INIT/$PMOCR_SERVICE_FILE_INIT"
QuickLogger "Created osync-srv service in [$SERVICE_DIR_INIT]."
QuickLogger "Can be activated with [service $PMOCR_SERVICE_FILE_INIT start]."
QuickLogger "Can be enabled on boot with [chkconfig $PMOCR_SERVICE_FILE_INIT on]."
fi
fi
}
function Statistics {
if type wget > /dev/null; then if type wget > /dev/null; then
wget -qO- $link > /dev/null 2>&1 wget -qO- "$STATS_LINK" > /dev/null 2>&1
if [ $? == 0 ]; then if [ $? == 0 ]; then
return 0 return 0
fi fi
fi fi
if type curl > /dev/null; then if type curl > /dev/null; then
curl -o /dev/null $link > /dev/null 2>&1 curl "$STATS_LINK" -o /dev/null > /dev/null 2>&1
if [ $? == 0 ]; then if [ $? == 0 ]; then
return 0 return 0
fi fi
fi fi
echo "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please." QuickLogger "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please."
retun 1 return 1
} }
echo "$PROGRAM installed. Use with $BIN_DIR/$PROGRAM" _SILENT=0
echo "" _STATS=1
echo "In order to make install statistics, the script would like to connect to http://instcount.netpower.fr?program=$PROGRAM&version=$PROGRAM_VERSION" for i in "$@"
read -r -p "No data except those in the url will be send. Allow [Y/n]" response do
case $response in case $i in
[nN]) --silent)
exit _SILENT=1
;; ;;
*) --no-stats)
Statistics _STATS=0
exit $? ;;
;; esac
esac done
SetOSSettings
CreateConfDir
CopyExampleFiles
CopyProgram
GetInit
CopyServiceFiles
STATS_LINK="http://instcount.netpower.fr?program=$PROGRAM&version=$PROGRAM_VERSION&os=$OS"
QuickLogger "$PROGRAM installed. Use with $BIN_DIR/$PROGRAM"
if [ $_STATS -eq 1 ]; then
if [ $_SILENT -eq 1 ]; then
Statistics
else
QuickLogger "In order to make install statistics, the script would like to connect to $STATS_LINK"
read -r -p "No data except those in the url will be send. Allow [Y/n]" response
case $response in
[nN])
exit
;;
*)
Statistics
exit $?
;;
esac
fi
fi

Loading…
Cancel
Save