summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 7a5490c098c767b44d85a51c67828cfdad7e3453 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Maintainer: dakataca <🐬danieldakataca@gmail.com>

pkgname=pgadmin4-py
_pkgname=${pkgname%[0-9]*}
__pkgname=${pkgname%-*}
pkgver=7.8
pkgrel=1
pkgdesc='Most popular and feature rich Open Source administration and development platform for PostgreSQL'
url='https://www.pgadmin.org/'
arch=(x86_64)
license=(GPL3)
makedepends=('python-pip' 'python-build' 'python-installer' 'python-wheel' 'python-authlib')
depends=('postgresql' 'translate-shell' 'openbsd-netcat' 'e2fsprogs')

# Alistar ficheros en el directorio de instalación.
package() {
    # pgadmin configs.
    install -o $USER -g $USER -dvm 755 "$pkgdir/var/lib/$_pkgname/"
    install -o $USER -g $USER -dvm 755 "$pkgdir/var/log/$_pkgname/"
    install -dvm 755 "$pkgdir/opt/venvs/"

    # Existe /var/lib/postgres/?
    if [ ! -d "/var/lib/postgres/" ]; then
        install -o postgres -g postgres -dvm 755 "$pkgdir/var/lib/postgres/"
    fi
    cd $pkgdir/opt/venvs/
    python3 -m venv $__pkgname 
    source $__pkgname/bin/activate
    pip install $__pkgname

    # Script de ejecución pgadmin4.
    install -Dvm755 <(cat <<'EOF'
#!/usr/bin/env bash

translate_str() {
   local -r lang=${LANG%_[A-Z][A-Z].UTF-8}
   local str=""
   if [[ $lang == en ]]; then
       str=$1
   elif [[ $lang == es ]]; then
       str=$2
   else
       str=`trans -b :$lang "$str"`
   fi
   echo "$str"
}

open_server() {
    sleep 1
    while ! nc -z 127.0.0.1 5050; do
        sleep 1
    done
    [ $? -eq 0 ] && xdg-open http://127.0.0.1:5050
}

validate_postgres_password() {
    declare -r command="$1" level="$2"
    
    while true; do
        # Solicitar la contraseña
        translate_str "Enter the postgres user's password at the $level" \
	"Ingrese la contraseña del usuario postgres a nivel de $level:"
        bash -c "$command"

        # Validar el éxito del comando anterior
        if (( $? == 0 )); then
            translate_str "Valid password. Continuing..."  "Contraseña válida. Continuando..."
            break
        else
            translate_str "Incorrect password. Please try again." "Contraseña incorrecta. Inténtelo de nuevo."
        fi
    done
}
 
# Comprobar si el usuario es root.
if [ "$EUID" -ne 0 ]; then
    declare -r data=/var/lib/postgres/data \
        pg_hba=/var/lib/postgres/data/pg_hba.conf \
        postgres_config=/var/lib/postgres/data/postgresql.conf

    # Existe fichero de configuración base de datos postgres?
    if ! sudo test -f "$pg_hba"; then

        sudo -iu postgres initdb -D $data --data-checksums

        # Sistema de ficheros es BTRFS?
        #[ `stat -f -c %T $data` == btrfs ] && sudo chattr +C $data

        # Sistema de ficheros es BTRFS?
        if [ `stat -f -c %T $data` == btrfs ]; then
            echo "Sistema de ficheros es BTRFS"

            # Copia en escritura (copy on write) activo en BTRFS?
            if [[ `sudo lsattr -dl $data` =~ No_COW ]]; then
                echo "No_COW activo en BTRFS $data"
            else
                echo "No_COW activando en BTRFS $data"
                sudo chattr +C $data
                sudo lsattr -dl $data
            fi
        fi

        # Restricts access rights to the database superuser by default.
        sudo grep -E 'local +all +all +trust' $pg_hba || sudo sed -Ei.back \
            "s/(host +all +)all +([0-9]{,3}.[0-9]{,3}.[0-9]{,3}.[0-9]{,3}\/[0-9]{,2} +)trust$/\1postgres\t\2peer/ ; \
            s/(local +all +all +)trust$/\1scram-sha-256/" $pg_hba

        sudo grep -E "^listen_addresses = 'localhost'*" $postgres_config || sudo sed -i.back \
            "/^#password_encryption =*/  s/^#// ; \
            /listen_addresses = 'localhost'/ s/^#//" $postgres_config

        [[ `systemctl is-active --quiet postgresql.service` ]] && sudo systemctl restart postgresql.service || sudo systemctl start postgresql.service

        # Contraseña de usuario postgres a nivel de Base de Datos postgresql.
        validate_postgres_password "sudo -u postgres psql -c '\password'" "postgresql"
    
        # Contraseña de usuario postgres a nivel de Base de Datos postgresql.
        translate_str "User password \"postgres\" at the level of ArchLinux Operating System:" "Contraseña de usuario \"postgres\" a nivel de Sistema Operativo ArchLinux:"

        # Contraseña de usuario postgres a nivel de Sistema Operativo Linux.
        validate_postgres_password "sudo passwd postgres" "S.O Archlinux"
    fi

    # Servicio postgresql.service está activo?
    [[ `systemctl is-active --quiet postgresql.service` ]] && sudo systemctl restart postgresql.service || sudo systemctl start postgresql.service

    # Activar entorno virtual pgadmin4.
    source /opt/venvs/pgadmin4/bin/activate
    open_server &
    #[ $? -eq 0 ] && xdg-open http://127.0.0.1:5050
    pgadmin4
else
    translate_str "Error: You can't perform this operation as a superuser."\
    "Error: No puede realizar esta operación siendo superusuario."
fi
EOF
) $pkgdir/usr/bin/$__pkgname
cd -
}

## References:
# Support: pgadmin-support@lists.postgresql.org
# Website: https://www.pgadmin.org/
# Tracker: https://github.com/pgadmin-org/pgadmin4/issues https://www.pgadmin.org/

## Test:
# updpkgsums
# makepkg --printsrcinfo > .SRCINFO

## Clean:
# sudo pacman -Rnsc pgadmin4-py --noconfirm
# sudo rm -rf /var/lib/p{ostgres,gadmin}/
# git clean -dfx

# 👤 Autor: https://t.me/dakataca 💻 🐬 #