Add delete option

This commit is contained in:
Filippo Squillace 2014-10-22 00:41:25 +01:00
parent 21165f3615
commit 53d8a8e72e
4 changed files with 52 additions and 3 deletions

View file

@ -70,7 +70,7 @@ of GNU/Linux distributions. The dependencies needed to be in the host OS are:
- bash
- wget or curl
- mkdir
- linux kernel 2.6.32
- linux kernel 2.6.32+
Troubleshooting
---------------

View file

@ -35,6 +35,7 @@ usage() {
echo -e "-f, --fakeroot Run JuJu with fakeroot privileges"
echo -e "-r, --root Run JuJu with root privileges"
echo -e "-b, --build-image Build a JuJu image (must run in ArchLinux only)"
echo -e "-d, --delete Delete JuJu from ${JUJU_HOME}"
echo -e "-h, --help Show this help message"
echo -e "-v, --version Show the $NAME version"
}
@ -50,7 +51,7 @@ version() {
### MAIN PROGRAM ###
###################################
TEMP=`getopt -o rfbihv --long root,fakeroot,build-image,setup-from-file,help,version -n 'juju' -- "$@"`
TEMP=`getopt -o drfbihv --long delete,root,fakeroot,build-image,setup-from-file,help,version -n 'juju' -- "$@"`
if [ $? != 0 ] ; then error "Error on parsing the command line. Try juju -h." ; exit ; fi
@ -61,6 +62,7 @@ OPT_SETUP_FROM_FILE=false
OPT_FAKEROOT=false
OPT_ROOT=false
OPT_BUILD_IMAGE=false
OPT_DELETE=false
OPT_HELP=false
OPT_VERSION=false
while true ; do
@ -69,6 +71,7 @@ while true ; do
-f|--fakeroot) OPT_FAKEROOT=true ; shift ;;
-r|--root) OPT_ROOT=true ; shift ;;
-b|--build-image) OPT_BUILD_IMAGE=true ; shift ;;
-d|--delete) OPT_DELETE=true ; shift ;;
-h|--help) OPT_HELP=true ; shift ;;
-v|--version) OPT_VERSION=true ; shift ;;
--) shift ; break ;;
@ -93,12 +96,14 @@ elif $OPT_BUILD_IMAGE; then
exit
fi
[ ! "$(ls -A $JUJU_HOME)" ] && setup_juju
[ ! "$(ls -A $JUJU_HOME)" ] && ! $OPT_DELETE && setup_juju
if $OPT_FAKEROOT; then
run_juju_as_fakeroot
elif $OPT_ROOT; then
run_juju_as_root
elif $OPT_DELETE; then
delete_juju
else
run_juju_as_user
fi

View file

@ -133,6 +133,20 @@ function run_juju_as_user(){
_run_juju_with_proot "-R"
}
function delete_juju(){
! ask "Are you sure to delete JuJu located in ${JUJU_HOME}" "N" && return
if mountpoint -q ${JUJU_HOME}
then
info "There are mounted directories inside ${JUJU_HOME}"
if ! umount --force ${JUJU_HOME}
then
error "Cannot umount directories in ${JUJU_HOME}"
error "Try to delete juju using root permissions"
exit 1
fi
fi
rm -rf ${JUJU_HOME}/*
}
function build_image_juju(){
# The function must runs on ArchLinux

View file

@ -35,3 +35,33 @@ function info(){
# $@: msg (mandatory) - str: Message to print
echo -e "\033[1;37m$@\033[0m"
}
function ask(){
# $1: question string
# $2: default value - can be either Y, y, N, n (by default Y)
local default="Y"
[ -z $2 ] || default=$(echo "$2" | tr '[:lower:]' '[:upper:]')
local other="n"
[ "$default" == "N" ] && other="y"
local prompt="$1 (${default}/${other})> "
local res="none"
while [ "$res" != "Y" ] && [ "$res" != "N" ] && [ "$res" != "" ];
do
read -p "$prompt" res
res=$(echo "$res" | tr '[:lower:]' '[:upper:]')
done
[ "$res" == "" ] && res="$default"
if [ "$res" == "Y" ]
then
return 0
else
return 1
fi
}