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.

287 lines
6.8 KiB

# !/bin/sh
# (c) Hukadan hukadan [at] protonmail [dot] com
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# just to be sure
set -e
set -u
usage(){
cat << EOF
Usage : artificier command [options]
Commands:
version -- Show the version of artificier
help -- Show usage
install -- Install packages
upgrade -- Upgrade packages
build -- Build repository
status-install -- Show what would be done on install
status-build -- Show what would be done on build
EOF
exit 1
}
####################################
# --- VARIABLES ---- #
####################################
ARTIFICIER_VERSION="0.0.1"
# SYSTEM PROPERTIES
ARCH=$(uname -p)
FREEBSD_BRANCH=$(uname -r | cut -d - -f 2)
FREEBSD_VERSION=$(uname -r | cut -d - -f 1)
if [ ${FREEBSD_BRANCH} == "STABLE" -o ${FREEBSD_BRANCH} == "CURRENT" ] ; then
FREEBSD_VERSION=$(echo ${FREEBSD_VERSION}| cut -d . -f 1)
fi
LOCAL_ETC=/usr/local/etc
# POUDRIERE RELATED SETTINGS
POUDRIERE_FOLDER=/usr/local/poudriere
POUDRIERE_CONF=${LOCAL_ETC}
MAKE_FILE=${POUDRIERE_CONF}/poudriere.d/make.conf
DIST_FILES=/usr/ports/distfiles
#PORTSTREE_NAME=artificier
PORTSTREE_NAME=local
# jail name cannot contain '.'
SHORT_ARCH=$(echo $ARCH | sed "s/[a-z]//g")
JAIL_NAME=$(echo ${FREEBSD_VERSION}${FREEBSD_BRANCH}${SHORT_ARCH} \
| sed 's/\.//g' | tr "[:upper:]" "[:lower:]")
PORTSTREE_DIR=/usr/ports
# PKG RELATED SETTINGS
PKGCONF_DIR=${LOCAL_ETC}/pkg/repos
REPO_NAME="poudriere"
PKG_NONAUTO=$(pkg query -e '%a = 0' %o)
# MISC
EDITOR=ee
DRY_OPTION=""
# LOADING CONFIG FILE (OVERWRITE DEFAULT VALUES)
if [ -f ${LOCAL_ETC}/artificier.conf ] ; then
. ${LOCAL_ETC}/artificier.conf
else
echo "Config file not found, using default values"
fi
show_artificier_version(){
echo "Wrapper around pkg(8) and poudriere(8)"
echo "Version ${ARTIFICIER_VERSION}"
echo "Copyright (C) 2019 Hukadan hukadan@protonmail.com"
exit 0
}
####################################
# --- COMMON FUNCTIONS --- #
####################################
# Error handling
check_errs(){
if [ "${1}" -ne 0 ] ; then
echo "ERROR ${1} : ${2}"
return ${1}
fi
return 0
}
exit_on_fail(){
if [ "${1}" -ne 0 ] ; then
exit ${1}
fi
}
# BOOLEAN FUNCTIONS
are_installed(){
for pkg_to_check in $@
do
[ $(echo "$PKG_NONAUTO" | egrep -c "${pkg_to_check}") -gt 0 ] \
|| return 1
done
return 0
}
portstree_present(){
poudriere -e $POUDRIERE_CONF ports -ln |
grep -w "$PORTSTREE_NAME" >/dev/null \
&& return 0
return 1
}
jail_present(){
poudriere -e $POUDRIERE_CONF jail -ln |
grep -w "$JAIL_NAME" >/dev/null \
&& return 0
return 1
}
needed_present(){
jail_present && portstree_present && return 0
return 1
}
# BASIC FUNCTIONS
install_package(){
CMD="pkg install -r ${REPO_NAME} $@"
$CMD
check_errs $? "Problem while installing"
return $?
}
upgrade_packages(){
CMD="pkg upgrade -r ${REPO_NAME}"
$CMD
check_errs $? "Prolem while upgrading"
return $?
}
bulk_ports(){
# bulk build packages
if ! needed_present ; then
echo "Jail and/or ports tree missing. Create them first"
return 1
else
echo "Building packages..."
CMD="poudriere -e $POUDRIERE_CONF \
bulk ${DRY_OPTION} \
-j ${JAIL_NAME} \
-p ${PORTSTREE_NAME} $@"
$CMD
check_errs $? "Some ports failed to build"
return $?
fi
}
# ACTIONS
action_install(){
[ $# -lt 1 ] && usage
if are_installed $@ ; then
{ echo "Package(s) already installed" ; exit 1 ; }
fi
bulk_ports $@
check_errs $? "Something went wrong while building ports"
exit_on_fail $?
install_package $@
check_errs $? "Something went wrong while installing packages"
exit_on_fail $?
exit 0
}
action_status-install(){
[ $# -lt 1 ] && usage
if are_installed $@ ; then
{ echo "Package(s) already installed" ; exit 1 ; }
fi
echo "Calculating ports that would be built. This may take a while..."
tobuild=$(bulk_ports $@ 2>&1 | grep -E 'Ports to' \
| cut -d ] -f3 | cut -d : -f2)
for port in $tobuild
do
echo $port
done
exit 0
}
action_build(){
bulk_ports ${PKG_NONAUTO}
check_errs $? "Something went wrong while building ports"
exit_on_fail $?
exit 0
}
action_status-build(){
echo "Calculating ports that would be built. This may take a while..."
tobuild=$(bulk_ports ${PKG_NONAUTO} 2>&1 | grep -E 'Ports to' \
| cut -d ] -f3 | cut -d : -f2)
for port in $tobuild
do
echo $port
done
exit 0
}
action_upgrade(){
bulk_ports ${PKG_NONAUTO}
check_errs $? "Something went wrong while building ports"
exit_on_fail $?
upgrade_packages
check_errs $? "Something went wrong while upgrading package"
exit_on_fail $?
exit 0
}
####################################
# --- DO THE JOB --- #
####################################
CMD=$1
shift
case "${CMD}" in
install)
action_install $@
;;
build)
action_build
;;
upgrade)
action_upgrade
;;
status-install)
DRY_OPTION="-n"
action_status-install $@
;;
status-build)
DRY_OPTION="-n"
action_status-build
;;
version)
show_artificier_version
;;
help)
usage
;;
*)
echo "Unknown command '${CMD}'"
usage
;;
esac