This shows you the differences between two versions of the page.
Next revision Both sides next revision | |||
userspace:opsi-info [2013/09/03 16:39] islam created |
userspace:opsi-info [2013/09/03 16:41] islam |
||
---|---|---|---|
Line 7: | Line 7: | ||
Download Link here: http:// | Download Link here: http:// | ||
+ | <code Bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | ############### | ||
+ | # opsi-info | ||
+ | # read / edit opsi-package info | ||
+ | # By Islam Adel | ||
+ | |||
+ | ############### | ||
+ | |||
+ | #usage: opsi-info -h (help file) | ||
+ | |||
+ | ############### | ||
+ | #change log: | ||
+ | # 2013-09-02 view, | ||
+ | # 2013-09-03 view, | ||
+ | |||
+ | #target: - read/edit opsi package information | ||
+ | # - list all package-ids | ||
+ | # - error verification / reporting | ||
+ | # - read / edit package version | ||
+ | # - backup all relevant files | ||
+ | # - fix conflict if product version = package version | ||
+ | |||
+ | #to-do: auto detect depot files | ||
+ | |||
+ | #input: arguments 1. option, 2. product_id, 3. value | ||
+ | |||
+ | # option: -ver (product version) | ||
+ | |||
+ | |||
+ | ###################### | ||
+ | # global variables | ||
+ | ver=" | ||
+ | #set loop delimiter as " | ||
+ | IFS=$' | ||
+ | |||
+ | # | ||
+ | NOW=" | ||
+ | |||
+ | ###################### | ||
+ | # user variables | ||
+ | # opsi=" | ||
+ | # opsi=" | ||
+ | opsi=" | ||
+ | |||
+ | ###################### | ||
+ | #functions | ||
+ | |||
+ | #error message | ||
+ | error() { | ||
+ | echo " | ||
+ | echo | ||
+ | # echo "use -h argument for help" | ||
+ | # echo | ||
+ | #exit | ||
+ | } | ||
+ | |||
+ | #display hel | ||
+ | f_printUsage() { | ||
+ | echo " | ||
+ | echo | ||
+ | echo " | ||
+ | echo | ||
+ | echo " | ||
+ | echo | ||
+ | echo " | ||
+ | echo | ||
+ | echo " | ||
+ | echo "-prod [product-id] : | ||
+ | #echo "-ver | --version : | ||
+ | echo "-ver [decimal] : | ||
+ | #echo "-pver | --pkgversion : | ||
+ | echo "-pver [integer] : | ||
+ | echo | ||
+ | echo | ||
+ | echo " | ||
+ | echo " | ||
+ | echo | ||
+ | exit | ||
+ | } | ||
+ | |||
+ | # read all passed arguments | ||
+ | f_GetArgs() { | ||
+ | |||
+ | #echo "all args=[${@}]" | ||
+ | #echo " | ||
+ | #echo " | ||
+ | #echo " | ||
+ | #echo " | ||
+ | #echo " | ||
+ | |||
+ | i=0 | ||
+ | argv=() | ||
+ | for arg in " | ||
+ | argv[$i]=" | ||
+ | i=$((i + 1)) | ||
+ | #debug# echo $i=$arg | ||
+ | done | ||
+ | |||
+ | # Handle OPTIONS and build directory array | ||
+ | |||
+ | # | ||
+ | # | ||
+ | #run_jobs=0 | ||
+ | # | ||
+ | # | ||
+ | |||
+ | i=0 | ||
+ | dir_index=0 | ||
+ | while test $i -lt $# ; do | ||
+ | |||
+ | arg=" | ||
+ | |||
+ | case " | ||
+ | |||
+ | # | ||
+ | |||
+ | -h|--help) | ||
+ | |||
+ | #-j|--jobs) i=$((i + 1)); n_threads=${argv[$i]};; | ||
+ | |||
+ | # | ||
+ | |||
+ | # | ||
+ | #verify decimal value before setting | ||
+ | --version|-ver) i=$((i + 1)); if [ " | ||
+ | #verify integer value before setting | ||
+ | --pkgversion|-pver) i=$((i + 1)); if [ " | ||
+ | | ||
+ | --product|-prod) i=$((i + 1)); product=" | ||
+ | |||
+ | *) if ! test -d " | ||
+ | error " | ||
+ | else | ||
+ | source_dirs[$dir_index]=" | ||
+ | dir_index=$((dir_index + 1)) | ||
+ | fi;; | ||
+ | |||
+ | esac | ||
+ | |||
+ | i=$((i + 1)) | ||
+ | |||
+ | done | ||
+ | |||
+ | } | ||
+ | |||
+ | # input file | ||
+ | f_Depot() { | ||
+ | #autodetect depot file | ||
+ | if [ " | ||
+ | #local_test | ||
+ | if [ " | ||
+ | |||
+ | dep=0 | ||
+ | for d in $(ls " | ||
+ | # add depot number | ||
+ | dep=$((dep + 1)) | ||
+ | dep_[$i]=" | ||
+ | #echo " | ||
+ | #prompt to choose depot number if multiple found | ||
+ | depot_name=" | ||
+ | depot=" | ||
+ | #echo $depot_name | ||
+ | #echo $depot | ||
+ | if [ " | ||
+ | echo "[ $dep ] depots have been found" | ||
+ | echo " | ||
+ | #add this feature later, to choose depot number | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | if [ ! " | ||
+ | |||
+ | depot_bak_dir=" | ||
+ | depot_bak_file=" | ||
+ | #create backup folder if not exist | ||
+ | if [ ! -d " | ||
+ | |||
+ | #tmp files | ||
+ | depot_tmp1=" | ||
+ | # remove existing tmp file | ||
+ | if [ -f $depot_tmp1 ]; then rm -f $depot_tmp1; | ||
+ | depot_tmp2=" | ||
+ | if [ -f $depot_tmp2 ]; then rm -f $depot_tmp2; | ||
+ | } | ||
+ | |||
+ | #version info in install script | ||
+ | f_Install() { | ||
+ | if [ " | ||
+ | # | ||
+ | if [ " | ||
+ | setup_file=" | ||
+ | |||
+ | setup_bak_dir=" | ||
+ | setup_bak_file=" | ||
+ | #create backup folder if not exist | ||
+ | |||
+ | if [ ! -d " | ||
+ | |||
+ | #Set $ProductVersion$ | ||
+ | if [ -f " | ||
+ | #echo " | ||
+ | #change version info in setup-script | ||
+ | #echo " | ||
+ | #cat " | ||
+ | #echo " | ||
+ | #backup | ||
+ | cp -f " | ||
+ | cat " | ||
+ | mv -f " | ||
+ | |||
+ | #verify edit | ||
+ | if grep -q "Set \$ProductVersion\$ | ||
+ | then | ||
+ | # code if found | ||
+ | echo "OK: ProductVersion replaced in $setup_file" | ||
+ | else | ||
+ | # code if not found | ||
+ | error " | ||
+ | fi | ||
+ | |||
+ | |||
+ | fi | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | # products info | ||
+ | f_Product() { | ||
+ | |||
+ | if [ " | ||
+ | #local test | ||
+ | if [ " | ||
+ | |||
+ | #echo " | ||
+ | #echo " | ||
+ | #read pkg_ver | ||
+ | #set old packageversion if new is not defined | ||
+ | if [ ! " | ||
+ | #set old product version if new is not defined | ||
+ | if [ ! " | ||
+ | |||
+ | # | ||
+ | #identify type .localboot or .netboot #later feature | ||
+ | product_file_old=" | ||
+ | product_file_new=" | ||
+ | # echo | ||
+ | # echo old_version=" | ||
+ | # echo new_version=" | ||
+ | # echo old_pkg_version=" | ||
+ | # echo new_pkg_version=" | ||
+ | |||
+ | product_bak_dir=" | ||
+ | product_bak_file=" | ||
+ | #create backup folder if not exist | ||
+ | if [ ! -d $product_bak_dir ]; then mkdir -p $product_bak_dir; | ||
+ | |||
+ | |||
+ | #check if product file exists | ||
+ | if [ -f " | ||
+ | #### rename product file | ||
+ | #backup file? #later | ||
+ | cp -f " | ||
+ | |||
+ | mv -f " | ||
+ | #check if renamed file exists? # | ||
+ | |||
+ | #### change file version in content | ||
+ | case " | ||
+ | 1) # change product version | ||
+ | cat " | ||
+ | mv -f " | ||
+ | # verify change, #later | ||
+ | # cat " | ||
+ | #### change file version in setup script | ||
+ | f_Install | ||
+ | ;; | ||
+ | 3) # change package version / sed ====> first match for version: only / first occurence | ||
+ | cat " | ||
+ | # all occurences | ||
+ | #cat " | ||
+ | mv -f " | ||
+ | ;; | ||
+ | 4) #change prod. version first, then pkg version | ||
+ | cat " | ||
+ | cat " | ||
+ | #mv -f " | ||
+ | #### change file version in setup script | ||
+ | f_Install | ||
+ | ;; | ||
+ | esac | ||
+ | |||
+ | if [ -f " | ||
+ | |||
+ | else | ||
+ | error " | ||
+ | #restore backed up file | ||
+ | mv -f $depot_bak_file $depot | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | |||
+ | ###################### | ||
+ | #script start | ||
+ | echo | ||
+ | echo "#################################" | ||
+ | echo "# | ||
+ | echo "# opsi-info version: $ver #" | ||
+ | echo "# | ||
+ | echo "# use -h for help #" | ||
+ | echo "# | ||
+ | echo "#################################" | ||
+ | echo | ||
+ | |||
+ | # read arguments | ||
+ | f_GetArgs $@ | ||
+ | |||
+ | f_Depot | ||
+ | #display values | ||
+ | #echo "using depot file :" | ||
+ | #echo " | ||
+ | #echo ver = $new_version | ||
+ | #echo product = $product | ||
+ | |||
+ | #read depot file | ||
+ | |||
+ | # sed '/ | ||
+ | # replace line starting with " | ||
+ | # replace new lines by ";" | ||
+ | # replace " | ||
+ | # replace ";;" | ||
+ | # replace ";" | ||
+ | #cat $depot | sed '/ | ||
+ | |||
+ | #create tmp file and change structure for search | ||
+ | cat $depot | sed '/ | ||
+ | |||
+ | # find line with product name and replace string (version value) till ";" | ||
+ | found=0 | ||
+ | change=0 | ||
+ | for v in $(cat $depot_tmp1 | grep " | ||
+ | found=$((found + 1)) | ||
+ | if [ " | ||
+ | # must find 1 instance only - replace only one product | ||
+ | # read current version | ||
+ | #last working: old_version=$(echo $v | sed ' | ||
+ | old_version=$(echo " | ||
+ | old_pkg_version=$(echo " | ||
+ | |||
+ | # echo old_version=" | ||
+ | # echo new_version=" | ||
+ | # echo old_pkg_version=" | ||
+ | # echo new_pkg_version=" | ||
+ | # echo v=" | ||
+ | |||
+ | #edit only if new_version is defined | ||
+ | if [ " | ||
+ | change=$((change + 1)) | ||
+ | # replace old version with new, retain whole string | ||
+ | n=$(echo " | ||
+ | #echo n=" | ||
+ | #exit | ||
+ | echo " | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | |||
+ | #edit only if new_pkg_version is defined | ||
+ | if [ " | ||
+ | change=$((change + 3)) | ||
+ | # replace old version with new, retain whole string | ||
+ | p=$(echo " | ||
+ | echo " | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | else | ||
+ | error "[ $found ] matches found for: $product " | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | #case for changes | ||
+ | case " | ||
+ | # 0) echo "# nothing to change";; | ||
+ | 1) #echo "# change version only" | ||
+ | # backup original depot file | ||
+ | cp -f $depot $depot_bak_file | ||
+ | # replace version info and retain original file structure | ||
+ | sed ' | ||
+ | f_Product | ||
+ | ;; | ||
+ | 3) #echo "# change pkg version only" | ||
+ | # backup original depot file | ||
+ | cp -f $depot $depot_bak_file | ||
+ | sed ' | ||
+ | # change further files | ||
+ | f_Product | ||
+ | ;; | ||
+ | 4) #echo "# change pver + ver" | ||
+ | p=$(echo " | ||
+ | # backup original depot file | ||
+ | cp -f $depot $depot_bak_file | ||
+ | # replace version info and retain original file structure | ||
+ | sed ' | ||
+ | # change further files | ||
+ | f_Product | ||
+ | ;; | ||
+ | esac | ||
+ | |||
+ | # errors only if product / arguments specified | ||
+ | #echo $i | ||
+ | if [ " | ||
+ | if [ " | ||
+ | else | ||
+ | error "no arguments specified" | ||
+ | echo "use -h argument for help" | ||
+ | fi | ||
+ | |||
+ | # file end | ||
+ | echo | ||
+ | echo "###############################" | ||
+ | echo | ||
+ | echo " | ||
+ | # remove existing tmp file | ||
+ | if [ -f $depot_tmp1 ]; then rm -f $depot_tmp1; | ||
+ | if [ -f $depot_tmp2 ]; then rm -f $depot_tmp2; | ||
+ | echo | ||
+ | #read -p "" | ||
+ | #exit | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </ |