arch-create-system: handle config-file and config-name

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-01-29 17:14:25 +01:00
parent 737768405e
commit 0970d5254e

View File

@@ -25,6 +25,7 @@ version="0.1.0"
# global variables # global variables
color=1 color=1
config_json="$progname.json" config_json="$progname.json"
config_name="null"
dryrun=0 dryrun=0
quiet=0 quiet=0
verbose=0 verbose=0
@@ -419,14 +420,15 @@ get_config_id () {
exit 1 exit 1
fi fi
#cmd="jq --monochrome-output --ascii-output ' .ConfigEnvironment[] \
# | {Id: .ConfigId, Name: .ConfigName} ' \
# ${config_json}"
cmd="jq --monochrome-output --ascii-output ' .ConfigEnvironment[] \ cmd="jq --monochrome-output --ascii-output ' .ConfigEnvironment[] \
| .ConfigId as \config_id \ | .ConfigName ' \
| select(. == \"${ConfigId}\") \ ${config_json}"
| \$ConfigName'
${config _json}"
config_name=$(eval $cmd) config_name=$(eval $cmd)
config_name=$(echo $config_names | sed -e 's/\n//g') #config_name=$(echo $config_name | sed -e 's/\n//g')
if [ $verbose -ge 2 ]; then if [ $verbose -ge 2 ]; then
printf "${MAGENTA}Config environment id: ${GREEN}'%s'${NO_COLOR}\n" \ printf "${MAGENTA}Config environment id: ${GREEN}'%s'${NO_COLOR}\n" \
@@ -436,7 +438,7 @@ get_config_id () {
get_config_name () { get_config_name () {
local config_json=${configfile:-$config_json} local config_json=${configfile:-$config_json}
local config_name=${1} local name=${1}
local cmd local cmd
if [ $verbose -ge 1 ]; then if [ $verbose -ge 1 ]; then
@@ -448,17 +450,25 @@ get_config_name () {
printf "${RED}Error:${MAGENTA} config file ${GREEN}'%s'${MAGENTA} can't be opend!${NO_COLOR}\n" \ printf "${RED}Error:${MAGENTA} config file ${GREEN}'%s'${MAGENTA} can't be opend!${NO_COLOR}\n" \
"config_json" "config_json"
fi fi
exit 1 return 1
fi fi
cmd="jq --monochrome-output --ascii-output ' .ConfigEnvironment[] \ cmd="jq -r --monochrome-output --ascii-output ' .ConfigEnvironment[] \
| select(.ConfigName == \"${name}\") \
| .ConfigName ' \ | .ConfigName ' \
${config_json}" ${config_json}"
config_name=$(eval $cmd) config_name=$(eval $cmd)
config_name=$(echo $config_names | sed -e 's/\n//g') config_name=$(echo $config_name | sed -e 's/\n//g')
if [ ${#config_name} = 0 ]; then
if [ $verbose -ge 2 ]; then if [ $verbose -ge 2 ]; then
printf "${MAGENTA}Config environment name: ${GREEN}'%s'${NO_COLOR}\n" \ printf "${MAGENTA}Config environment name: ${GREEN}'%s'${MAGENTA} not found!${NO_COLOR}\n" \
"$name"
fi
return 1
fi
if [ $verbose -ge 2 ]; then
printf "${MAGENTA}Config environment name: ${GREEN}'%s'${MAGENTA} exists.${NO_COLOR}\n" \
"$config_name" "$config_name"
fi fi
} }
@@ -677,9 +687,9 @@ parse_config_file () {
return return
fi fi
if [ ${#config_name} -eq 0 ]; then #if [ ${#config_name} -eq 0 ]; then
get_config_names # get_config_names
fi #fi
} }
parse_params () { parse_params () {
@@ -698,16 +708,23 @@ parse_params () {
color=1 color=1
shift 1 shift 1
;; ;;
-c | --config) --config-file)
config_json="$2" config_json="$2"
shift 2 shift 2
;; ;;
--get-config-name) -c | --config)
shift 1 shift 1
config_params=${*} config_params=${*}
config_params="${config_params%% -?[a-z-]*}" config_params="${config_params%% -?[a-z]*}"
params=$* params=$*
set -- $config_params set -- $config_params
count=$#
( test $count -lt 1 || test ${#config_params} -eq 0 ) \
&& printf "missing argument: config_name\n" \
&& exit 1
test $count -ge 1 && config_name="$1"
set -- $params
shift $count
cmd=get-config-name cmd=get-config-name
;; ;;
--get-config-names) --get-config-names)
@@ -784,10 +801,14 @@ parse_params () {
esac esac
shift shift
;; ;;
--c=* | --config=*) --config-file=*)
config_json=${1#*=} config_json=${1#*=}
shift shift
;; ;;
-c | --config=*)
config_name=${1#*=}
shift
;;
--disk_label_data=*|--DISK_LABEL_DATA=*) --disk_label_data=*|--DISK_LABEL_DATA=*)
disk_label_data=${1#*=} disk_label_data=${1#*=}
shift shift
@@ -1034,6 +1055,7 @@ show_config () {
printf "${BLUE}$progname (runtime arguments) ...${NO_COLOR}\n" printf "${BLUE}$progname (runtime arguments) ...${NO_COLOR}\n"
i=0 i=0
printf " Config File: '%s'\n" "$config_json" printf " Config File: '%s'\n" "$config_json"
printf " Config: '%s'\n" "$config_name"
printf " Mount Point: '%s'\n" "$mount_point" printf " Mount Point: '%s'\n" "$mount_point"
printf " Label Prefix: '%s'\n" "$label_prefix" printf " Label Prefix: '%s'\n" "$label_prefix"
printf " Target OS: '%s'\n" "$disk_dev_root" printf " Target OS: '%s'\n" "$disk_dev_root"
@@ -1061,9 +1083,11 @@ usage () {
"$progname $version" \ "$progname $version" \
"Usage: $progname [options]" \ "Usage: $progname [options]" \
"Options:" \ "Options:" \
" -c, --config Read config parameters from given file" \ " -c, --config-file Use given given file" \
" --config use pre-defined values from given config name" \
" --color Enable colored output messages" \ " --color Enable colored output messages" \
" --dry-run perform a trial run (no changes are written)" \ " --dry-run perform a trial run (no changes are written)" \
" --get-config-names show pre-defined configs from config-file" \
" --mountpoint Specify MountPoint preparing targets OS-Disk" \ " --mountpoint Specify MountPoint preparing targets OS-Disk" \
" --label_prefix <prefix> Specify the partion label prefix" \ " --label_prefix <prefix> Specify the partion label prefix" \
" -t, --disk_dev_root <target> Specify the blockdevice for the target OS (e.g /dev/sda)" \ " -t, --disk_dev_root <target> Specify the blockdevice for the target OS (e.g /dev/sda)" \
@@ -1088,24 +1112,21 @@ parse_params $@
# commandline options handled with priority # commandline options handled with priority
parse_config_file parse_config_file
show_config
printf "${MAGENTA}cmd: ${GREEN}'%s'${NO_COLOR}\n" "$cmd"
case $cmd in case $cmd in
get-config-name) get-config-name)
get_config_name #get_config_name 'HyperV-SingleDisk'
get_config_name $config_name
if test $? -gt 0; then if test $? -gt 0; then
exit 1 exit 1
else #else
if [ $quiet -eq 0 ]; then # if [ $quiet -eq 0 ]; then
printf "${MAGENTA}Config Environment: ${GREEN}'%s'${NO_COLOR}\n" \ # printf "${MAGENTA}Config Environment: ${GREEN}'%s'${NO_COLOR}\n" \
"${config_name}" # "${config_name}"
fi # fi
fi fi
;; ;;
get-config-names) get-config-names)
#echo $config_names
get_config_names get_config_names
if [ $? -gt 0 ]; then if [ $? -gt 0 ]; then
exit 1 exit 1
@@ -1114,25 +1135,9 @@ case $cmd in
printf "${MAGENTA}Config environment names: ${GREEN}'%s'${NO_COLOR}\n" \ printf "${MAGENTA}Config environment names: ${GREEN}'%s'${NO_COLOR}\n" \
"${config_names}" "${config_names}"
fi fi
exit 0
fi fi
;; ;;
get-config-disk-names)
valid_member=0
get_config_disk_names "${config_name}" "${disk_name}"
if [ $? -gt 0 ]; then
exit 1
else
if [ $quiet = 0 ]; then
printf "Config environment ${GREEN}'%s'${NO_COLOR} with disks: ${GREEN}'%s'${NO_COLOR}\n" \
"${config_name}" "${disk_names}"
fi
valid_member=${#disk_names}
fi
;;
get-config-partition_names)
echo get_config_partition_names
;;
install-target) install-target)
prepare_target_disks prepare_target_disks
prepare_target_filesystems prepare_target_filesystems
@@ -1150,3 +1155,7 @@ case $cmd in
fi fi
;; ;;
esac esac
if [ $quiet -eq 0 ]; then
show_config
fi