libs/setup_installskel.sh

Thu, 24 Jan 2008 15:05:45 +0800

author
Tim Post <tinkertim@gmail.com>
date
Thu, 24 Jan 2008 15:05:45 +0800
branch
tinkertim
changeset 17
fc888ff3ced7
parent 14
1115a3d0d415
permissions
-rw-r--r--

Fixed output in die()

die() was using too many newlines after re-formatting
functions to fit inside 80 columns. The output was
confusing, now fixed.

Signed-off-by: Tim Post (tinkertim@gmail.com)

     1 # Functions used by install.sh, also shared by setup.
     3 # Ensure that we exit properly, paying attention to --force
     4 # Syntax: safe_exit EXIT_SUCCESS or EXIT_FAILURE, --force will over-ride
     5 # safe_exit $EXIT_SUCCESS or $EXIT_FAILURE, exits regardless of --force
     6 safe_exit()
     7 {
     8 	local mode="$1"
    10 	# If called with EXIT_FAILURE and $FORCE is true, we don't exit.
    11 	# If called with $EXIT_FAILURE, we exit no matter the value of $FORCE
    12 	# Simple work-around so that we can die if we really need to, even if
    13 	# --force was passed. Note, EXIT_FAILURE and $EXIT_FAILURE are different
    15 	# TODO: Make me sain with a simple EXIT_FORCE macro, duh!
    17 	[ -z "$EXIT_FAILURE" ] || [ "$EXIT_FAILURE" = "" ] && EXIT_FAILURE="1"
    19 	[ "$FORCE" = "yes" ] && [ "$mode" != "$EXIT_FAILURE" ] && return 0
    21 	[ -z "$mode" ] && safe_exit EXIT_SUCCESS
    23 	case $mode in
    24 		0|EXIT_SUCCESS)
    25 			# if EXIT_SUCCESS undefined, just exit and rely on the shell
    26 			# to invoke the proper EXIT_ macro
    27 			[ -z "$EXIT_SUCCESS" ] || [ "$EXIT_SUCCESS" = "" ] && {
    28 				exit
    29 			}
    30 			exit $EXIT_SUCCESS
    31 			;;
    32 		*)
    33 			# Exit with whatever EXIT_FAILURE is defined as, or with
    34 			# whatever argument was passed to safe_exit()
    35 			[ $mode = "EXIT_FAILURE" ] && exit $EXIT_FAILURE
    36 			exit $mode
    37 			;;
    39 	esac
    40 }
    42 # 99 ways to die.
    43 # Syntax: die [reason] [why]
    44 # Reason can be one of the pre-defined causes, specifying "why", or just
    45 # die "reason"
    46 # Syntax: die prog "gcc" -> Complains that the program gcc can not be found
    47 die()
    48 {
    49 	local mode=$1
    50 	local why=$2
    52 	case $mode in
    53 		prog)
    54 			printf "\nThe program %s could not be found in your path\n" \
    55 				"$why"
    56 			printf "Please install '%s' (or ensure its in your path) " \
    57 				"$why"
    58 			printf "and try again.\n"
    59 			printf "If you are running this as an ordinary user, make sure "
    60 			printf "that /sbin and /usr/sbin\nare in your path.\n"
    61 			logg "FATAL: Could not find the program $why"
    62 			safe_exit EXIT_FAILURE
    63 		;;
    64 		lib)
    65 			printf "\nThe library %s could not be located.\n" "$why"
    66 			printf "Please install the library '%s' and try again.\n" "$why"
    67 			logg "FATAL: Could not find the library $why"
    68 			safe_exit EXIT_FAILURE
    69 		;;
    70 		evar)
    71 			printf "\nThe environmental variable %s is not set.\n" "$why"
    72 			printf "Please set %s properly and try again.\n" "$why"
    73 			logg "FATAL: env setting $why is null or undefined."
    74 			safe_exit EXIT_FAILURE
    75 		;;
    76 		err)
    77 			printf "\n%s\n" "$why"
    78 			logg "FATAL: $why"
    79 			safe_exit EXIT_FAILURE
    80 		;;
    81 		arch)
    82 			printf "\nYour architecture, %s, is not supported by %s.\n" \
    83 				"$try" "$PROGNAME"
    84 			printf "You can contact %s to see if this will change soon.\n" \
    85 				"$BUGMAIL"
    86 			logg "FATAL: Arch $why is not supported."
    87 			safe_exit EXIT_FAILURE
    88 		;;
    89 		kernel)
    90 			printf "\nYour kernel, version %s, is not supported by %s.\n" \
    91 				"$why" "$PROGNAME"
    92 			printf "You can contact %s to see if this will change soon.\n" \
    93 				"$BUGMAIL"
    94 			logg "FATAL: Kernel $why is not supported."
    95 			safe_exit EXIT_FAILURE
    96 		;;
    97 		internal)
    98 			printf "\nThe POSIX invocation of your shell lacks the %s "
    99 			printf "built-in.\n" "$why"
   100 			printf "Please link /bin/sh to something supporting %s\n" "$why"
   101 			logg "FATAL: Internal shell error, $why doesn't seem to work."
   102 			safe_exit EXIT_FAILURE
   103 		;;
   104 		package)
   105 			printf "\nI could not install the package %s with %s.\n" \
   106 				"$why" "$DIST_PACKAGER"
   107 			printf "Perhaps missing sources in your packager config?\n"
   108 			logg "FATAL: Could not install the mandatory package '$why'"
   109 			safe_exit EXIT_FAILURE
   110 		;;
   111 		*)
   112 			printf "\n%s\n" "$*"
   113 			logg "FATAL: $*"
   114 			safe_exit EXIT_FAILURE
   115 		;;
   116 	esac
   117 }
   119 # returns non-zero if uid !=0
   120 # Syntax: check_root || die "You must be root to run this program"
   121 check_root()
   122 {
   123 	[ $(id -u) = 0 ] || return 1
   124 	return 0
   125 }
   127 # Loggs $* (all arguments) to the defined logfile, if defined.
   128 # Syntax: logg Do re me fa so la ti do
   129 logg()
   130 {
   131 	local str="$*"
   133 	[ -z $INSTALL_LOG ] || [ "$INSTALL_LOG" = "" ] && return 0
   135 	local stamp=$(date +%s)
   137 	printf "%d %s\n" "$stamp" "$str" >> $INSTALL_LOG
   138 }
   140 # Writes a newline to the logfile
   141 logg_nl()
   142 {
   143 	[ -z $INSTALL_LOG ] || [ "$INSTALL_LOG" = "" ] && return 0
   145 	printf "\n" >> $INSTALL_LOG
   146 }
   148 # Much like check_prog, however sets PROGNAME=/path/to/progname
   149 # as a global (r/w) variable. (does not write to paths.in)
   150 # Syntax: check_prog_export "progname" , or more typical
   151 # check_prog_export "progname" || handle_failure_somehow
   152 check_prog_export()
   153 {
   154 	local target="$1"
   155 	local dir=""
   156 	local prog=""
   158 	printf "Checking for the program %s ... " "$target"
   159 	for dir in $(echo $PATH | $TR -d '\n' | $TR ':' ' '); do
   160 		[ -x ${dir}/${target} ] && {
   161 			printf "${dir}/${target} [ok]\n"
   162 			prog="${dir}/${target}"
   163 			target="$(echo $target | $TR 'a-z' 'A-Z' | \
   164 				$TR '\-' '_' | $TR '\.' '_')"
   165 			export $target="$prog"
   166 			return 0
   167 		}
   168 	done
   169 	printf "[no]\n"
   170 	return 1
   171 }
   173 # "Smart" init updater, calls the appropriate tool
   174 # Syntax: update_init initscript on/off
   175 update_init()
   176 {
   177 	local prog="$1"
   178 	local mode="$2"
   179 	local retval=0
   181 	case $mode in
   182 		on|start)
   183 			printf "Setting %s to start on boot ... " "$prog"
   184 			case $DIST_INITCTL in
   185 				chkconfig)
   186 					$CHKCONFIG $prog on >/dev/null 2>&1
   187 					retval=$?
   188 				;;
   189 				update-rc.d)
   190 					$UPDATE_RC_D $prog defaults >/dev/null 2>&1
   191 					retval=$?
   192 				;;
   193 				*)
   194 					retval=1
   195 				;;
   196 			esac
   197 		;;
   198 		off|stop)
   199 			printf "Setting %s to NOT start on boot ... " "$prog"
   200 			case $DIST_INITCTL in
   201 				chkconfig)
   202 					$CHKCONFIG $prog off >/dev/null 2>&1
   203 					retval=$?
   204 				;;
   205 				update-rc.d)
   206 					$UPDATE_RC_D -f $prog remove >/dev/null 2>&1
   207 					retval=$?
   208 				;;
   209 				*)
   210 					retval=1
   211 				;;
   212 			esac
   213 		;;
   214 		*)
   215 			retval=1
   216 		;;
   217 	esac
   218 	[ $retval = 0 ] && {
   219 		printf "[ok]\n"
   220 		logg "Modified init, $prog set to $mode"
   221 		return 0
   222 	}
   223 	printf "[failed]\n"
   224 	logg "Could not modify init settings for $prog - attempted $mode"
   225 	return $retval
   226 }
   228 # Generates a PEM encoded self signed certificate
   229 # Syntax: generate_pem /path/to/cert.pem
   230 # returns non-zero on failure.
   231 generate_pem()
   232 {
   233 	# Country Name (2 letter code) [AU]:
   234 	local A="."
   235 	# State or Province Name (full name) [Some-State]:
   236 	local B="."
   237 	# Locality Name (eg, city) []:
   238 	local C="."
   239 	# Organization Name (eg, company) [Internet Widgits Pty Ltd]:
   240 	local D="$(hostname)"
   241 	# Organizational Unit Name (eg, section) []:
   242 	local E="User Services"
   243 	# Common Name (eg, YOUR name) []:
   244 	local F="GNUPanel"
   245 	# Email Address []:
   246 	local G="ssl@$(hostname)"
   248 	local target=$1
   250 	[ -z "$OPENSSL" ] && {
   251 		check_prog_export "openssl" || {
   252 			printf "\nOpenSSL could not be found, can't generate %s\n" \
   253 				"$target"
   254 			return 1
   255 		}
   256 	}
   258 	printf "Generating PEM encoded self signed certificate $target ... "
   260 	printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n" "$A" "$B" "$C" "$D" "$E" \
   261 		"$F" "$G" 2>&1 | \
   262 		$OPENSSL req -days 1000 -new -x509 \
   263 			-nodes -out "$target" -keyout $target >/dev/null 2>&1
   264 	[ $? != 0 ] && {
   265 		printf "[failed]\n"
   266 		return 1
   267 	}
   269 	$CHMOD 0600 $target
   270 	openssl verify "$target" >/dev/null 2>&1 || {
   271 		printf "\n %s failed verification and has been deleted.\n" \
   272 			"$target"
   273 		$RM -f $target
   274 		return 1
   275 	}
   277 	printf "[success]\n"
   278 	return 0
   279 }

mercurial