Removing deprecated DMAAP library
[policy/drools-pdp.git] / packages / docker / src / main / docker / pdpd-entrypoint.sh
1 #!/usr/bin/env sh
2
3 # ########################################################################
4 # Copyright 2019-2021 AT&T Intellectual Property. All rights reserved
5 # Modifications Copyright (C) 2024 Nordix Foundation.
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #         http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ########################################################################
19
20
21 function maven {
22     if [ "${DEBUG}" = "y" ]; then
23         echo "-- maven --"
24         set -x
25     fi
26
27     if [ -f "${POLICY_INSTALL_INIT}"/settings.xml ]; then
28         if ! cmp -s "${POLICY_INSTALL_INIT}"/settings.xml "${POLICY_HOME}"/etc/m2/settings.xml; then
29             echo "overriding settings.xml"
30             cp -f "${POLICY_INSTALL_INIT}"/settings.xml "${POLICY_HOME}"/etc/m2
31         fi
32     fi
33
34     if [ -f "${POLICY_INSTALL_INIT}"/standalone-settings.xml ]; then
35         if ! cmp -s "${POLICY_INSTALL_INIT}"/standalone-settings.xml "${POLICY_HOME}"/etc/m2/standalone-settings.xml; then
36             echo "overriding standalone-settings.xml"
37             cp -f "${POLICY_INSTALL_INIT}"/standalone-settings.xml "${POLICY_HOME}"/etc/m2
38         fi
39     fi
40 }
41
42 function systemConfs {
43     if [ "${DEBUG}" = "y" ]; then
44         echo "-- systemConfs --"
45         set -x
46     fi
47
48     local confName
49
50     if ! ls "${POLICY_INSTALL_INIT}"/*.conf > /dev/null 2>&1; then
51         return 0
52     fi
53
54     for c in $(ls "${POLICY_INSTALL_INIT}"/*.conf 2> /dev/null); do
55         echo "adding system conf file: ${c}"
56         cp -f "${c}" "${POLICY_HOME}"/etc/profile.d/
57         confName="$(basename "${c}")"
58         sed -i -e "s/ *= */=/" -e "s/=\([^\"\']*$\)/='\1'/" "${POLICY_HOME}/etc/profile.d/${confName}"
59     done
60
61     source "${POLICY_HOME}"/etc/profile.d/env.sh
62 }
63
64 function features {
65     if [ "${DEBUG}" = "y" ]; then
66         echo "-- features --"
67         set -x
68     fi
69
70     if ! ls "${POLICY_INSTALL_INIT}"/features*.zip > /dev/null 2>&1; then
71         return 0
72     fi
73
74     source "${POLICY_HOME}"/etc/profile.d/env.sh
75
76     for f in $(ls "${POLICY_INSTALL_INIT}"/features*.zip 2> /dev/null); do
77         echo "installing feature: ${f}"
78         "${POLICY_HOME}"/bin/features install "${f}"
79     done
80 }
81
82 function scripts {
83     if [ "${DEBUG}" = "y" ]; then
84         echo "-- scripts --"
85         set -x
86     fi
87
88     local scriptExtSuffix=${1:-"sh"}
89
90     if ! ls "${POLICY_INSTALL_INIT}"/*."${scriptExtSuffix}" > /dev/null 2>&1; then
91         return 0
92     fi
93
94     source "${POLICY_HOME}"/etc/profile.d/env.sh
95
96     for s in $(ls "${POLICY_INSTALL_INIT}"/*."${scriptExtSuffix}" 2> /dev/null); do
97         echo "executing script: ${s}"
98         source "${s}"
99     done
100 }
101
102 function security {
103     if [ "${DEBUG}" = "y" ]; then
104         echo "-- security --"
105         set -x
106     fi
107
108     if [ -f "${POLICY_INSTALL_INIT}"/policy-keystore ]; then
109         if ! cmp -s "${POLICY_INSTALL_INIT}"/policy-keystore "${POLICY_HOME}"/etc/ssl/policy-keystore; then
110             echo "overriding policy-keystore"
111             cp -f "${POLICY_INSTALL_INIT}"/policy-keystore "${POLICY_HOME}"/etc/ssl
112         fi
113     fi
114
115     if [ -f "${POLICY_INSTALL_INIT}"/policy-truststore ]; then
116         if ! cmp -s "${POLICY_INSTALL_INIT}"/policy-truststore "${POLICY_HOME}"/etc/ssl/policy-truststore; then
117             echo "overriding policy-truststore"
118             cp -f "${POLICY_INSTALL_INIT}"/policy-truststore "${POLICY_HOME}"/etc/ssl
119         fi
120     fi
121 }
122
123 function serverConfig {
124     if [ "${DEBUG}" = "y" ]; then
125         echo "-- serverConfig --"
126         set -x
127     fi
128
129     local configExtSuffix=${1:-"properties"}
130
131     if ! ls "${POLICY_INSTALL_INIT}"/*."${configExtSuffix}" > /dev/null 2>&1; then
132         return 0
133     fi
134
135     for p in $(ls "${POLICY_INSTALL_INIT}"/*."${configExtSuffix}" 2> /dev/null); do
136         echo "configuration ${configExtSuffix}: ${p}"
137         cp -f "${p}" "${POLICY_HOME}"/config
138     done
139 }
140
141 function db {
142     if [ "${DEBUG}" = "y" ]; then
143         echo "-- db --"
144         set -x
145     fi
146
147     if [ -z "${SQL_HOST}" ]; then
148         return 0
149     fi
150
151     if [ -z "${SQL_PORT}" ]; then
152         export SQL_PORT=3306
153     fi
154
155     echo "Waiting for ${SQL_HOST}:${SQL_PORT} ..."
156     timeout 120 sh -c 'until nc -vz -w 20 "${SQL_HOST}" "${SQL_PORT}"; do echo -n "."; sleep 1; done'
157
158     "${POLICY_HOME}"/bin/db-migrator -s ALL -o upgrade
159 }
160
161 function inspect {
162     if [ "${DEBUG}" = "y" ]; then
163         echo "-- inspect --"
164         set -x
165     fi
166
167     echo "ENV: "
168     env
169     echo
170     echo
171
172     source "${POLICY_HOME}"/etc/profile.d/env.sh
173     policy status
174
175     echo
176     echo
177 }
178
179 function reload {
180     if [ "${DEBUG}" = "y" ]; then
181         echo "-- reload --"
182         set -x
183     fi
184
185     systemConfs
186     maven
187     features
188     security
189     serverConfig "properties"
190     serverConfig "xml"
191     serverConfig "json"
192     scripts "pre.sh"
193 }
194
195 function start {
196     if [ "${DEBUG}" = "y" ]; then
197         echo "-- start --"
198         set -x
199     fi
200
201     source "${POLICY_HOME}"/etc/profile.d/env.sh
202     policy start
203 }
204
205 function configure {
206     if [ "${DEBUG}" = "y" ]; then
207         echo "-- configure --"
208         set -x
209     fi
210
211     reload
212     db
213 }
214
215 function vmBoot {
216     if [ "${DEBUG}" = "y" ]; then
217         echo "-- vmBoot --"
218         set -x
219     fi
220
221     reload
222     db
223     start
224     scripts "post.sh"
225 }
226
227 function dockerBoot {
228     if [ "${DEBUG}" = "y" ]; then
229         echo "-- dockerBoot --"
230         set -x
231     fi
232
233     set -e
234
235     configure
236
237     source "${POLICY_HOME}"/etc/profile.d/env.sh
238     policy exec
239 }
240
241 if [ "${DEBUG}" = "y" ]; then
242     echo "-- $0 $* --"
243     set -x
244 fi
245
246 operation="${1}"
247 case "${operation}" in
248     inspect)    inspect
249                 ;;
250     boot)       dockerBoot
251                 ;;
252     vmboot)     vmBoot
253                 ;;
254     configure)  configure
255                 ;;
256     *)          exec "$@"
257                 ;;
258 esac