summaryrefslogtreecommitdiffstats
path: root/share/postgresql-setup/library.sh.in
blob: 9835029596ed2987ecfec5c12a580be3ee89cedd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
die()     { echo >&2 $"FATAL: $@" ; exit 1 ; }
error()   { echo >&2 $"ERROR: $@" ; }
error_q() { echo >&2 $"       $@" ; }
warn()    { echo >&2 $"WARNING: $@" ; }
warn_q()  { echo >&2 $"         $@" ; }
info()    { echo >&2 $" * $@" ; }
info_q()  { echo >&2 $"   $@" ; }
debug()   { test "$option_debug" = "1" && echo >&2 $"DEBUG: $@";  }


set_var()
{
    eval "$1=\"$2\""
}


root_prereq()
{
    test "$(id -u)" -eq 0 || die "$0 requires root access for this action"
}


read_config_file()
{
    local key="" val=""

    test -r "$1" || die "can't read file '$1'"

    for i in $2; do
        eval "unset __pg_conf_$i"
    done

    # No easy (and secure) way to read configuration files from bash script,
    # sorry.
    while read key val; do
        [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]] || return 1

        case " $2 " in
        *" $key "*)
            ;;
        *)
            warn "config file '$1':  unknown key '$key'"
            continue
            ;;
        esac

        # Strip double quotes
        case "$val" in
        \"*\")
            val=${val##\"}
            val=${val%%\"}
            ;;
        esac

        # Make it reasonably safe.  Keep dolar-sign escaped.
        eval "__pg_conf_$key=\$val"

    done < <(grep -v -e "^$" -e "^#" < "$1")
}


parse_upgrade_setup()
{
    local action="$1"
    local expected_id="$2"
    local id temp_major temp_engine temp_data_default temp_description

    local upgrade_confdir="@sysconfdir@/@NAME_BINARYBASE@-setup/upgrade"

    debug "using 'upgrade' confdir $upgrade_confdir"
    test -d "$upgrade_confdir" || die "can't read confdir $upgrade_confdir"

    local my_vars="id comment data_default engine description major scls \
        redhat_sockets_hack pghost_override package"
    while read conffile
    do
        read_config_file "$conffile" "$my_vars"

        if test help = "$action"; then
            echo "$__pg_conf_id - $__pg_conf_description"
        elif test list = "$action"; then
            echo "$__pg_conf_id $__pg_conf_major"
        elif test config = "$action"; then
            test "$__pg_conf_id" = "$expected_id" || continue
            debug "reading config $conffile"
            for i in $my_vars; do
                set_var "upgradefrom_$i" "\$__pg_conf_$i"

                local cm="config file '$conffile'"
                # 'scls' and 'redhat_sockets_hack' are used to adjust
                # environment and could be bash-injected.
                case "$i" in
                scls)
                    test -z "$upgrade_from_scls" \
                        || [[ $upgrade_from_scls =~ ^[-a-zA-Z0-9_\ ]+$ ]] \
                        || die "$cm: bad '$i' value '$upgrade_from_scls'"
                    ;;
                redhat_sockets_hack)
                    case "$upgradefrom_redhat_sockets_hack" in
                    yes|no|'')
                        ;;
                    *)
                        die "$cm: bad '$i' value"
                        ;;
                    esac
                    ;;
                esac
            done
            return 0
        fi
    done < <(find "$upgrade_confdir" -maxdepth 1 -type f -name '*.conf')

    case "$action" in
        help|list)
            return 0
            ;;
    esac
    return 1
}


# run_cmd_as_dbadmin COMMAND STDOUT STDERR
run_cmd_as_dbadmin()
{
    # Convenient tool-aliases
    local SU_POSTGRES="@SU_POSTGRES@"

    local cmd="@SCL_SOURCE@$1"
    local stdout="$2" stderr="$3"

    # don't print additional spaces
    set -- $cmd
    debug "running command under postgres user: $@"
    (
        test -n "$stdout" && exec >>"$stdout"
        test -n "$stderr" && exec 2>>"$stderr"
        $SU_POSTGRES -c "$cmd" < /dev/null
    )
}