Skip to content

Instantly share code, notes, and snippets.

@DennisLfromGA
Created March 20, 2014 00:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DennisLfromGA/9654604 to your computer and use it in GitHub Desktop.
Save DennisLfromGA/9654604 to your computer and use it in GitHub Desktop.
A shell script to display the 'crouton' version in the host and optionally update it. If a local version of 'crouton' cannot be found it downloads & installs one. Also, optionally displays the croutonversion and architecture ([-s option]) and other info. ([-v option]) about all [default] or given chroots.
#!/bin/sh
###
### Set Variables
APPLICATION="${0##*/}"
CHECK_IT=''
CHROOT_LOC=/usr/local/chroots
[ -d /var/crouton/chroots ] && CHROOT_LOC='/var/crouton/chroots'
[ ! -d "$CHROOT_LOC" ] && CHROOT_LOC=''
CHROOT_NAME=''
CROUTON_LOC="`which crouton 2>/dev/null`"
if [ ! -s "$CROUTON_LOC" ]; then CROUTON_LOC=''; fi
CROUTON_NEW=''
CROUTON_PAR="`dirname $CROUTON_LOC 2>/dev/null`"
if [ ! -d "$CROUTON_PAR" ]; then CROUTON_PAR=''; fi
DISPLAY_ALL=''
DOWNLOAD_LOC="$HOME/Downloads"
DOWNLOAD_NEW=''
EACH=''
HOST=''
LATEST=''
LIST_ONLY=''
UPDATE=''
# VERSION format: date '+%Y%m%d%H%M%S'
VERSION='20140319195918'
VERHIST="\
$APPLICATION-20140319195918 : Copied latest 'private' version to 'public' area
$APPLICATION-20140319195918 + and added version tracking and version history option"
###
### Define Colors
C0="\033[0m" # Reset Text
Bold="\033[1m" # Bold Text
Underline="\033[4m" # Underline Text
Black='\033[30m'
Red='\033[31m'
Green='\033[32m'
Brown='\033[33m'
Blue='\033[34m'
Purple='\033[35m'
Cyan='\033[36m'
Light_Grey='\033[37m'
Dark_Grey='\033[1;30m'
Light_Red='\033[1;31m'
Light_Green='\033[1;32m'
Yellow='\033[1;33m'
Light_Blue='\033[1;34m'
Light_Purple='\033[1;35m'
Light_Cyan='\033[1;36m'
White='\033[1;37m'
###
### Define USAGE
USAGE="${Bold}USAGE: $ APPLICATION [options] [chroot_name]
${Yellow}
A shell script to display the 'crouton' version in the host and optionally update it.
If a local version of 'crouton' cannot be found it downloads & installs one.
Also, optionally displays the croutonversion and architecture ([-s option])
and other info. ([-v option]) about all [default] or given chroots.
${C0}${Light_Cyan}
Options:
-s Displays only the 'croutonversion' & 'architecture' of all [default] or given chroots
-v Displays the complete output of 'croutonversion' of all [default] or given chroots
-c Checks the local host 'crouton' version against the latest version
-C Checks the local host 'crouton' version against the latest version & updates if needed
-l Lists the chroot(s) in $CHROOT_LOC [default] & exits
-t CHROOT Lists the chroot's DEs/targets & exits - chroot_name required
# -u CHROOT Displays the chroot's version & updates 'crouton' if needed - chroot_name required # BROKEN - inoperative #
-f DOWNLOADS Changes the destination of the downloaded crouton - Default: '$DOWNLOAD_LOC'
-p CHROOTS Changes the location of your chroot(s) folder - Default: '$CHROOT_LOC'
-h Displays this usage message and exits ${C0}
-V Version & History - print version number plus version history & exit
"
###
### Exits the script with exit code $1, spitting out message $@ to stderr
error() {
local ecode="$1"
shift
echo "$*" 1>&2
exit "$ecode"
}
###
### Display a help message if requested
if [ "$1" = "--help" ]
then error 0 "$USAGE"; fi
###
### Get command line parameters
while getopts svcClt:u:f:p:hV OPT; do
case $OPT in
s) DISPLAY_ALL='n';;
v) DISPLAY_ALL='y';;
c) CHECK_IT='y';;
C) CHECK_IT='y'; UPDATE='y';;
l) LIST_ONLY='y';;
t) LIST_ONLY='y'; CHROOT_NAME="$OPTARG";;
u) UPDATE='-du'; CHROOT_NAME="$OPTARG";;
f) DOWNLOAD_NEW='y'; DOWNLOAD_LOC="`readlink -f "$OPTARG"`";;
p) CHROOT_LOC="`readlink -f "$OPTARG"`";;
h) error 0 "$USAGE";;
V) error 0 "${APPLICATION}-${VERSION}\n${VERHIST}" ;;
\?) error 2 "$USAGE";;
esac
done
shift "$((OPTIND-1))"
###
### List chroot's targets if requested
if [ -n "$LIST_ONLY" -a -n "$CHROOT_NAME" ]
then
targetfile="$CHROOT_LOC/$CHROOT_NAME/etc/crouton/targets"
if [ -r "$targetfile" ]; then
echo -n "DEs/targets in '$CHROOT_NAME': " 1>&2
error 0 "`sed 's/^,//' "$targetfile" 2>/dev/null`"
else
error 1 "${Light_Red}Sorry, '$targetfile' not found.$C0"
fi
fi
###
### List chroots if requested
if [ -n "$LIST_ONLY" ]
then
echo -n "chroot(s) in '$CHROOT_LOC': " 1>&2
error 0 "`ls -Cm "$CHROOT_LOC" 2>/dev/null`"
fi
###
### Check to see: if root, assign a new Downloads folder
##+ since /root/Downloads is not present or writeable by default.
if [ "$UID" = 0 -o "$USER" = 'root' ]; then
if [ -z "$DOWNLOAD_NEW" ]; then
DOWNLOAD_LOC='/home/chronos/user/Downloads'
fi
echo "${Yellow}'$APPLICATION' will be run as 'root' but 'crouton' will$C0" 1>&2
echo "${Yellow}be saved to '$DOWNLOAD_LOC' if needed.$C0" 1>&2
fi
###
### Make sure the Downloads folder is a valid directory
if [ ! -d "$DOWNLOAD_LOC" ]; then
error 1 "${Light_Red}Sorry, no folder named: $DOWNLOAD_LOC${C0}"
elif [ -n "$DOWNLOAD_NEW" ]; then
echo "New Downloads folder: '$DOWNLOAD_LOC'" 1>&2
fi
###
### Check local host for 'crouton' and get one if needed
if [ -z "$CROUTON_LOC" ]
then
if [ -s "$DOWNLOAD_LOC/crouton" ]
then
CROUTON_LOC="$DOWNLOAD_LOC/crouton"
CROUTON_PAR="`dirname $CROUTON_LOC`"
else
echo "${Light_Red}Can't find 'crouton' on the host, so...${C0}" 1>&2
sudo rm /tmp/crouton 2>/dev/null
echo "Retreiving latest version of crouton." 1>&2
wget -qO /tmp/crouton http://goo.gl/fd3zc
if [ -s /tmp/crouton ]; then
echo "${Light_Blue}Saving the latest version to '$DOWNLOAD_LOC/crouton'.${C0}" 1>&2
sudo cp -f /tmp/crouton $DOWNLOAD_LOC
sudo chmod +x $DOWNLOAD_LOC/crouton
CROUTON_LOC="$DOWNLOAD_LOC/crouton"
CROUTON_PAR="`dirname $CROUTON_LOC`"
CROUTON_NEW='y'
else
error 2 "${Yellow}Oh no - couldn't grab the latest 'crouton'!!!$C0"
fi
if [ -n "$CHECK_IT" ]
then
CHECK_IT='n'
echo "${White}Checking &/or Updating no longer required...${C0}" 1>&2
echo "${Green}latest: $DOWNLOAD_LOC/`sh $DOWNLOAD_LOC/crouton -V 2>&1`${C0}" 1>&2
fi
fi
fi
###
### Check for the latest version of 'crouton' and update if requested
if [ "$CHECK_IT" = 'y' ]
then
sudo rm /tmp/crouton 2>/dev/null
echo "Retreiving latest version of crouton." 1>&2
wget -qO /tmp/crouton http://goo.gl/fd3zc
if [ -s /tmp/crouton ]; then
HOST="`sh $CROUTON_LOC -V 2>&1`"
LATEST="`sh /tmp/crouton -V 2>&1`"
echo "${Green}latest: /tmp/$LATEST${C0}" 1>&2
echo -n "${Blue}host: $CROUTON_PAR/$HOST${C0}" 1>&2
if [ ! "$HOST" = "$LATEST" ]
then echo "${Bold}${Light_Red} *** THE HOST NEEDS AN UPDATE ***${C0}" 1>&2
if [ -n "$UPDATE" ]
then
echo "${Light_Blue}Saving the latest version to '$DOWNLOAD_LOC/crouton'.${C0}" 1>&2
sudo cp -f /tmp/crouton $DOWNLOAD_LOC
sudo chmod +x $DOWNLOAD_LOC/crouton
CROUTON_NEW='y'
if [ -n "$CROUTON_LOC" ]
then
echo "${Light_Blue}Also saving the latest version to '$CROUTON_LOC'.${C0}" 1>&2
sudo cp -f /tmp/crouton $CROUTON_LOC
sudo chmod +x $CROUTON_LOC
sudo chown chronos:chronos $CROUTON_LOC
fi
else
echo "${Yellow}...but, no 'crouton' update requested...${C0}" 1>&2
fi
else
echo "${Light_Green} *** Your up-to-date ***${C0}" 1>&2
fi
else
error 2 "${Yellow}Oh no - couldn't grab the latest 'crouton'!!!$C0"
fi
else
echo "${Blue}host: $CROUTON_PAR/`sh $CROUTON_LOC -V 2>&1`${C0}" 1>&2
fi
###
### Get a valid chroot_name or names
if [ -z "$CHROOT_NAME" ]
then
if [ "$#" -gt 0 ]
then CHROOT_NAME="$*"
else CHROOT_NAME="`ls "$CHROOT_LOC" 2>/dev/null`"
fi
fi
###
### Display 'croutonversion', 'architecture' and optionally other info.
if [ "$DISPLAY_ALL" = 'y' ]
then
for EACH in $CHROOT_NAME
do
if [ ! -d "$CHROOT_LOC/$EACH" ]
then
error 1 "${Light_Red}Sorry, no chroot named: $EACH${C0}"
continue
fi
(
echo '----------------------------------------------------------------------' 1>&2
echo "chroot name: $EACH" 1>&2
sudo enter-chroot -c $CHROOT_LOC -n $EACH -x /usr/local/bin/croutonversion $UPDATE 2>&1 | grep -v -e ^Unmount -e ^Entering
)
done
elif [ "$DISPLAY_ALL" = 'n' ]
then
for EACH in $CHROOT_NAME
do
if [ ! -d "$CHROOT_LOC/$EACH" ]
then
error 1 "${Light_Red}Sorry, no chroot named: $EACH${C0}"
continue
fi
echo '----------------------------------------------------------------------' 1>&2
vers="`grep ^VERSION $CHROOT_LOC/$EACH/usr/local/bin/croutonversion|cut -d= -f2 2>&1`"
arch="`grep ^ARCH $CHROOT_LOC/$EACH/usr/local/bin/croutonversion|cut -d= -f2 2>&1`"
if [ -z "$arch" ]; then echo "chroot name: $EACH - version: $vers" 1>&2; fi
if [ -n "$arch" ]; then echo "chroot name: $EACH - version: $vers - architecture: $arch" 1>&2; fi
done
fi
echo '----------------------------------------------------------------------' 1>&2
@DennisLfromGA
Copy link
Author

NOTE: Copied latest 'private' version to 'public' area and added version tracking and version history option.
Also, added detection of and compliance with CROUTON partition in @drinkcat's 'separate_partition branch'.

Regretfully, 'crv -u (chroot-name)' is still broken :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment