Debian Codegraph @

src/make-dbeaver-configuration

raw
1 #!/usr/bin/env bash
2 # ==========================================================================
3 # _ _ _ ____ ____ _____
4 # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __
5 # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__|
6 # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ |
7 # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_|
8 #
9 # --- High-Performance Connectivity Tracer (HiPerConTracer) ---
10 # https://www.nntb.no/~dreibh/hipercontracer/
11 # ==========================================================================
12 #
13 # High-Performance Connectivity Tracer (HiPerConTracer)
14 # Copyright (C) 2015-2026 by Thomas Dreibholz
15 #
16 # This program is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #
29 # Contact: [email protected]
30
31 # Bash options:
32 set -eu
33
34
35 # ###### Main program #######################################################
36
37 # ====== Handle arguments ===================================================
38 if [ $# -lt 1 ] ; then
39 echo >&2 "Usage: $0 database_configuration_file ... [--join]"
40 exit 1
41 fi
42 DIRNAME="$(dirname "$0")"
43
44
45 # ====== Generate DBeaver configuration files ===============================
46 echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Generating DBeaver configuration ...\x1b[0m"
47 tempDirectory="$(mktemp -d -t "make-dbeaver-configuration-XXXXXX")"
48 trap 'rm -rf "${tempDirectory}"' EXIT
49
50 join=0
51 dbeaverConfig=""
52 dbeaverCredentials=""
53 while [ $# -gt 0 ] ; do
54 if [[ "$1" =~ ^-- ]] ; then
55 if [ "$1" == "--join" ] ; then
56 join=1
57 else
58 echo >&2 "ERROR: Invalid option $1!"
59 exit 1
60 fi
61 else
62 echo -e "\x1b[37mProcessing $(basename "$1") ...\x1b[0m"
63 "$(dirname "$0")"/dbshell "$1" --dry-run -q -B "${tempDirectory}/$(basename "${1//.conf/}")"
64 fi
65 shift
66 done
67
68
69 # ====== Read existing configuration ========================================
70 if [ ${join} -eq 1 ] ; then
71 echo -e "\x1b[37mLooking for existing DBeaver configuration ...\x1b[0m"
72 for dbeaverDirectory in \
73 "${HOME}/snap/dbeaver-ce/current/.local/share/DBeaverData/workspace6/General/.dbeaver" \
74 "${HOME}/.local/share/DBeaverData/workspace6/General/.dbeaver" ; do
75 if [ -e "${dbeaverDirectory}" ] ; then
76 if [ -e "${dbeaverDirectory}/data-sources.json" ] ; then
77 dbeaverConfig="${dbeaverDirectory}/data-sources.json"
78 echo -e "\x1b[37mJoining data sources in ${dbeaverConfig}\x1b[0m"
79 fi
80 if [ -e "${dbeaverDirectory}/credentials-config.json" ] ; then
81 dbeaverCredentials="${tempDirectory}/credentials-config.json"
82 echo -e "\x1b[37mJoining credentials in ${dbeaverDirectory}/credentials-config.json\x1b[0m"
83 "${DIRNAME}"/decrypt-dbeaver-configuration \
84 "${dbeaverDirectory}/credentials-config.json" \
85 "${dbeaverCredentials}"
86 fi
87 break
88 fi
89 done
90 if [ ! -e "${dbeaverDirectory}" ] ; then
91 echo >&2 "ERROR: Unable to find DBeaver configuration directory!"
92 exit 1
93 fi
94 fi
95
96
97 # ====== Join new DBeaver configuration files ===============================
98 echo -e "\x1b[37mJoining configurations ...\x1b[0m"
99 # shellcheck disable=SC2086
100 jq --sort-keys --slurp 'reduce .[] as $item ({}; . * $item)' ${dbeaverConfig} "${tempDirectory}"/*-data-source.json >"${tempDirectory}/data-sources.json"
101 # shellcheck disable=SC2086
102 jq --sort-keys --slurp 'reduce .[] as $item ({}; . * $item)' ${dbeaverCredentials} "${tempDirectory}"/*-credentials-config.json >"${tempDirectory}/credentials-config.json.plaintext"
103
104 # Encrypt the new credentials files:
105 "${DIRNAME}"/encrypt-dbeaver-configuration \
106 "${tempDirectory}/credentials-config.json.plaintext" \
107 "${tempDirectory}/credentials-config.json"
108
109
110 # ====== Join with existing configuration ===================================
111 if [ ${join} -eq 1 ] ; then
112 if [ "${dbeaverConfig}" != "" ] ; then
113 if ! diff "${tempDirectory}/data-sources.json" "${dbeaverDirectory}/data-sources.json" >/dev/null ; then
114 dbeaverConfigBackup="${dbeaverConfig}.backup-$(date +"%F-%H:%M:%S")"
115 echo -e "\x1b[37mMaking data sources backup in ${dbeaverConfigBackup} ...\x1b[0m"
116 cp "${dbeaverConfig}" "${dbeaverConfigBackup}"
117 fi
118 fi
119 echo -e "\x1b[37mUpdating data sources ${dbeaverDirectory}/data-sources.json ...\x1b[0m"
120 mv "${tempDirectory}/data-sources.json" "${dbeaverDirectory}/data-sources.json"
121
122 if [ "${dbeaverCredentials}" != "" ] ; then
123 if ! diff "${dbeaverCredentials}" "${tempDirectory}/credentials-config.json.plaintext" >/dev/null ; then
124 dbeaverCredentialsBackup="${dbeaverDirectory}/credentials-config.json-$(date +"%F-%H:%M:%S")"
125 echo -e "\x1b[37mKeeping backup in ${dbeaverCredentialsBackup} ...\x1b[0m"
126 cp "${dbeaverCredentials}" "${dbeaverCredentialsBackup}"
127 fi
128 fi
129 echo -e "\x1b[37mUpdating credentials ${dbeaverDirectory}/credentials-config.json ...\x1b[0m"
130 mv "${tempDirectory}/credentials-config.json" "${dbeaverDirectory}/credentials-config.json"
131
132 else
133
134 # Just generated the configuration files -> no joining was performed!
135 mv "${tempDirectory}/data-sources.json" "${tempDirectory}/credentials-config.json" .
136 echo "Generated configuration files:"
137 ls -l credentials-config.json data-sources.json
138
139 fi
140
141
142 # ====== Clean up ===========================================================
143 echo -e "\x1b[37mDone!\x1b[0m"
144 rm -rf "${tempDirectory}"