From 845d2ae46873444576b9eba0e5fc44db065361ac Mon Sep 17 00:00:00 2001 From: sayalilunkad Date: Sat, 7 Mar 2015 02:26:21 +0530 Subject: [PATCH] labs: Adds colorizer for osbash script Adds script with all the colors and decorators like bold, underline. Uses this script in main osbash.sh script to bring color to the script run. Also adds -c option to enable colors while buiding osbash script. Usage: ./osbash.sh -b cluster (colors enabled by default) ./osbash.sh -b --no-color cluster (colors disabled) Implements: blueprint osbash-color-support Change-Id: I5b17221f67f76552deb3498b8a020f1b0d3a64a8 --- labs/lib/osbash/lib.color | 54 +++++++++++++++++++++++++++++++++++ labs/osbash.sh | 60 ++++++++++++++++++++++++--------------- 2 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 labs/lib/osbash/lib.color diff --git a/labs/lib/osbash/lib.color b/labs/lib/osbash/lib.color new file mode 100644 index 00000000..d69a61c8 --- /dev/null +++ b/labs/lib/osbash/lib.color @@ -0,0 +1,54 @@ +#!/bin/bash +# Colors and decorations + +function colorizer { + + # Text Reset + local Reset_Color='\x1b[0m' + + # Regular + local Black='\x1b[0;30m' + local Red='\x1b[0;31m' + local Green='\x1b[0;32m' + local Yellow='\x1b[0;33m' + local Blue='\x1b[0;34m' + local Purple='\x1b[0;35m' + local Cyan='\x1b[0;36m' + local White='\x1b[0;37m' + + # Bold + local Bold_Black='\x1b[1;30m' + local Bold_Red='\x1b[1;31m' + local Bold_Green='\x1b[1;32m' + local Bold_Yellow='\x1b[1;33m' + local Bold_Blue='\x1b[1;34m' + local Bold_Purple='\x1b[1;35m' + local Bold_Cyan='\x1b[1;36m' + local Bold_White='\x1b[1;37m' + + # Underline + local Underline_Black='\x1b[4;30m' + local Underline_Red='\x1b[4;31m' + local Underline_Green='\x1b[4;32m' + local Underline_Yellow='\x1b[4;33m' + local Underline_Blue='\x1b[4;34m' + local Underline_Purple='\x1b[1;35m' + local Underline_Cyan='\x1b[4;36m' + local Underline_White='\x1b[4;37m' + + # Background + local On_Black='\x1b[40m' + local On_Blue='\x1b[44m' + local On_White='\x1b[47m' + + # Color codes + CError=$Red + CStatus=$Bold_Purple + CInfo=$Bold_Yellow + CProcess=$Bold_Blue + CData=$Bold_Cyan + CMissing=$Bold_Red + CReset=$Reset_Color +} +colorizer +unset colorizer diff --git a/labs/osbash.sh b/labs/osbash.sh index fd0f0e93..c44715ef 100755 --- a/labs/osbash.sh +++ b/labs/osbash.sh @@ -19,9 +19,10 @@ source "$OSBASH_LIB_DIR/lib.$DISTRO" source "$OSBASH_LIB_DIR/functions.host" source "$OSBASH_LIB_DIR/virtualbox.functions" source "$OSBASH_LIB_DIR/virtualbox.install_base" +source "$LIB_DIR/osbash/lib.color" function usage { - echo "Usage: $0 {-b|-w} [-g GUI] [-n] {basedisk|cluster}" + echo "Usage: $0 {-b|-w} [-g GUI] [--no-color] [-n] {basedisk|NODE [NODE..]}" # Don't advertise export until it is working properly #echo " $0 [-e EXPORT] [-n] NODE [NODE..]" echo "" @@ -31,6 +32,7 @@ function usage { echo "-w Create Windows batch files" echo "-g GUI GUI type during build" #echo "-e EXPORT Export node VMs" + echo "--no-color Disables colors during build" echo "" echo "basedisk Build configured basedisk" echo "cluster Build OpenStack cluster [all nodes]" \ @@ -44,10 +46,10 @@ function usage { function print_config { if [ "$CMD" = "basedisk" ]; then - echo "Target is base disk: $BASE_DISK" + echo -e "${CInfo:-}Target is base disk:${CData:-} $BASE_DISK${CReset:-}" else - echo "Base disk: $BASE_DISK" - echo "Distribution name: $(get_distro_name "$DISTRO")" + echo -e "${CInfo:-}Base disk:${CData:-} $BASE_DISK${CReset:-}" + echo -e "${CInfo:-}Distribution name: ${CData:-} $(get_distro_name "$DISTRO")${CReset:-}" fi if [ -n "${EXPORT_OVA:-}" ]; then @@ -55,23 +57,23 @@ function print_config { elif [ -n "${EXPORT_VM_DIR:-}" ]; then echo "Exporting to directory: ${EXPORT_VM_DIR}" else - echo -n "Creating Windows batch scripts: " - ${WBATCH:-:} echo "yes" - ${WBATCH:+:} echo "no" + echo -e -n "${CInfo:-}Creating Windows batch scripts:${CReset:-} " + ${WBATCH:-:} echo -e "${CData:-}yes${CReset:-}" + ${WBATCH:+:} echo -e "${CData:-}no${CReset:-}" - echo -n "Creating $CMD on this machine: " - ${OSBASH:-:} echo "yes" - ${OSBASH:+:} echo "no" + echo -e -n "${CInfo:-}Creating $CMD on this machine:${CReset:-} " + ${OSBASH:-:} echo -e "${CData:-}yes${CReset:-}" + ${OSBASH:+:} echo -e "${CData:-}no${CReset:-}" - echo "VM access method: $VM_ACCESS" + echo -e "${CInfo:-}VM access method:${CData:-} $VM_ACCESS${CReset:-}" # GUI is the VirtualBox default - echo "GUI type: ${VM_UI:-gui}" + echo -e "${CInfo:-}GUI type:${CData:-} ${VM_UI:-gui}${CReset:-}" fi } -while getopts :be:g:hnw opt; do +while getopts :be:g:-:hnw opt; do case $opt in e) if [ "$OPTARG" = ova ]; then @@ -79,7 +81,7 @@ while getopts :be:g:hnw opt; do elif [ "$OPTARG" = dir ]; then EXPORT_VM_DIR=$IMG_DIR/oslabs-$DISTRO else - echo "Error: -e argument must be ova or dir" + echo -e "${CError:-}Error: -e argument must be ova or dir${CReset:-}" exit fi OSBASH=exec_cmd @@ -91,10 +93,22 @@ while getopts :be:g:hnw opt; do if [[ "$OPTARG" =~ (headless|gui|sdl) ]]; then VM_UI=$OPTARG else - echo "Error: -g argument must be gui, sdl, or headless" + echo -e "${CError:-}Error: -g argument must be gui, sdl, or headless${CReset:-}" exit fi ;; + -) + case $OPTARG in + no-color) + unset CError CStatus CInfo CProcess CData CMissing CReset + ;; + *) + echo -e "${CError:-}Error: invalid option -$OPTARG${CReset:-}" + echo + usage + ;; + esac + ;; h) usage ;; @@ -105,10 +119,10 @@ while getopts :be:g:hnw opt; do source "$LIB_DIR/wbatch/batch_for_windows" ;; :) - echo "Error: -$OPTARG needs argument" + echo -e "${CError:-}Error: -$OPTARG needs argument${CReset:-}" ;; ?) - echo "Error: invalid option -$OPTARG" + echo -e "${CError:-}Error: invalid option -$OPTARG${CReset:-}" echo usage ;; @@ -152,12 +166,12 @@ fi if [ -z "${OSBASH:-}" -a -z "${WBATCH:-}" ]; then echo - echo "No -b, -w, or -e option given. Exiting." + echo -e "${CMissing:-}No -b, -w, or -e option given. Exiting.${CReset:-}" exit fi STARTTIME=$(date +%s) -echo >&2 "$(date) osbash starting" +echo -e >&2 "${CStatus:-} $(date) osbash starting ${CReset:-}" clean_dir "$LOG_DIR" @@ -175,10 +189,10 @@ function cleanup_base_disk { echo >&2 "Unregistering old base disk." disk_unregister "$BASE_DISK" fi - echo >&2 "Removing old base disk." + echo -e >&2 "${CStatus:-}Removing old base disk.${CReset:-}" rm -f "$BASE_DISK" else - echo >&2 "Nothing to do. Exiting." + echo -e >&2 "${CMissing:-}Nothing to do. Exiting.${CReset:-}" exit fi fi @@ -209,5 +223,5 @@ source "$OSBASH_LIB_DIR/virtualbox.install_nodes" vm_build_nodes "$CMD" #------------------------------------------------------------------------------- ENDTIME=$(date +%s) -echo >&2 "$(date) osbash finished successfully" -echo "osbash completed in $(($ENDTIME - $STARTTIME)) seconds." +echo -e >&2 "${CStatus:-}$(date) osbash finished successfully${CReset:-}" +echo "${CStatus:-}osbash completed in $(($ENDTIME - $STARTTIME)) seconds.${CReset:-}"