Luigi Santivetti | 0fdd470 | 2020-06-22 19:00:32 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # common - module.sh |
| 4 | # |
| 5 | # Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com> |
| 6 | |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | # copy of this software and associated documentation files (the "Software"), |
| 9 | # to deal in the Software without restriction, including without limitation |
| 10 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | # and/or sell copies of the Software, and to permit persons to whom the |
| 12 | # Software is furnished to do so, subject to the following conditions: |
| 13 | |
| 14 | # The above copyright notice and this permission notice (including the next |
| 15 | # paragraph) shall be included in all copies or substantial portions of the |
| 16 | # Software. |
| 17 | |
| 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | # ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 22 | # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | |
| 25 | declare -r mod_module_d="$module_d/$module" |
| 26 | declare -r mod_instance_d="$instance_d" |
| 27 | declare -r mod_docker_d="$docker_d/$module" |
| 28 | declare -r mod_rootfs_d="$rootfs_d" |
| 29 | declare -r mod_mode_env="${module^^}_MODE" |
| 30 | declare -r mod_mode="$(eval "echo \${$mod_mode_env:-$instance_mode}")" |
| 31 | declare -r mod_staging_d="$mod_module_d/$staging" |
| 32 | |
| 33 | declare -ar mod_dirs=( $mod_instance_d $mod_docker_d $mod_rootfs_d ) |
| 34 | declare -ar mod_files=( ) |
| 35 | declare -ar mod_trefs=( ) |
| 36 | |
| 37 | if [ "${mod_mode}" != "${debug}" ] && [ "${mod_mode}" != "${release}" ]; then |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | # @1 : module name |
| 42 | # @2 : absolute path to module staging dir |
| 43 | function __fetch_module_common |
| 44 | { |
| 45 | local modname |
| 46 | local tmp_d |
| 47 | local tar |
| 48 | |
| 49 | [ -z "$1" ] && { |
| 50 | [ -n "$module" ] || { |
| 51 | lets -l -e "invalid module"; return $s_err |
| 52 | }; modname="$module" |
| 53 | } || modname=$1 |
| 54 | |
| 55 | [ -z "$2" ] && { |
| 56 | [ -n "$mod_staging_d" ] && [ -d "$mod_module_d" ] || { |
| 57 | lets -l -e "invalid module root"; return $s_err |
| 58 | }; tmp_d="$mod_staging_d" |
| 59 | } || tmp_d=$2 |
| 60 | |
| 61 | is_unix_path $tmp_d || { lets -l -e "invalid staging area"; return $s_err; } |
| 62 | |
| 63 | [ -d "$tmp_d" ] && { |
| 64 | lets --ask "${tmp_d##$module_d/} existing, wipe it?" && rm -rf $tmp_d || \ |
| 65 | lets -l -w "keeping staging" |
| 66 | } |
| 67 | |
| 68 | mkdir -p $tmp_d && process_manifest $modname $tmp_d || { |
| 69 | |
| 70 | # Wipe the whole staging_d for this module |
| 71 | [ "$tmp_d" = "$mod_staging_d" ] && rm -rf $tmp_d |
| 72 | return $s_err |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | function __dir_do_common_helper |
| 77 | { |
| 78 | local -a __a1=( ) |
| 79 | |
| 80 | if [ -z "${mod_more_dirs[*]}" ] || [ ${#mod_more_dirs[@]} -eq 0 ]; then |
| 81 | lets -l -w "$module: does not have more dirs" |
| 82 | __a1=( "${mod_dirs[@]}" ) |
| 83 | else |
| 84 | __a1=( $(__merge_array "${mod_dirs[@]}" "${mod_more_dirs[@]}") ) |
| 85 | fi |
| 86 | |
| 87 | create_directory "${__a1[@]}" || return $s_err |
| 88 | } |
| 89 | |
| 90 | function __file_do_common_helper |
| 91 | { |
| 92 | local -a __a1=( ); local -a __a2=( ) |
| 93 | |
| 94 | if [ -z "${mod_more_files[*]}" ] || [ ${#mod_more_files[@]} -eq 0 ]; then |
| 95 | lets -l -w "$module: does not have more files" |
| 96 | else |
| 97 | __a1=( $(__merge_array "${mod_files[@]}" "${mod_more_files[@]}") ) |
| 98 | fi |
| 99 | |
| 100 | if [ -z "${mod_more_trefs[*]}" ] || [ ${#mod_more_trefs[@]} -eq 0 ]; then |
| 101 | lets -l -w "$module: does not have more templates" |
| 102 | else |
| 103 | __a2=( $(__merge_array "${mod_trefs[@]}" "${mod_more_trefs[@]}") ) |
| 104 | fi |
| 105 | |
| 106 | __file_do "$1" "$uninstall_f" "${__a1[@]}" "${__a2[@]}" || return $s_err |
| 107 | } |
| 108 | |
| 109 | function __doins_module_common |
| 110 | { |
| 111 | __dir_do_common_helper && __file_do_common_helper --create |
| 112 | } |
| 113 | |
| 114 | function __watch_module_common |
| 115 | { |
| 116 | __file_do_common_helper --splash |
| 117 | } |
| 118 | |
| 119 | function __upins_module_common |
| 120 | { |
| 121 | __dir_do_common_helper && __file_do_common_helper --update |
| 122 | } |
| 123 | |
| 124 | function __clmod_module_common |
| 125 | { |
| 126 | rm -rf $mod_staging_d || sudo rm -rf $mod_staging_d |
| 127 | } |
| 128 | |
| 129 | function __clins_module_common |
| 130 | { |
| 131 | local -a __a1=( ); local -a __a2=( ) |
| 132 | |
| 133 | __file_do --update \ |
| 134 | "$uninstall_f" "${__a1[@]}" "${__a2[@]}" || return $s_err |
| 135 | } |