c7d0b231c9671f3f1e6cd2391c6e44f84c436b90
[apex-tripleo-heat-templates.git] / extraconfig / pre_deploy / rhel-registration / scripts / rhel-registration
1 #!/bin/bash
2
3 # dib-lint: disable=setu sete setpipefail dibdebugtrace
4
5 set -eu
6 set -o pipefail
7
8 OK=/mnt/state/var/lib/rhsm/rhsm.ok
9
10 if [ -e $OK ] ; then
11     exit 0
12 fi
13
14 retry_max_count=10
15 opts=
16 config_opts=
17 attach_opts=
18 sat5_opts=
19 repos="repos --enable rhel-7-server-rpms"
20 satellite_repo=${REG_SAT_REPO}
21 proxy_host=
22 proxy_port=
23 proxy_url=
24 proxy_username=
25 proxy_password=
26
27 # process variables..
28 if [ -n "${REG_AUTO_ATTACH:-}" ]; then
29     opts="$opts --auto-attach"
30
31     if [ -n "${REG_SERVICE_LEVEL:-}" ]; then
32         opts="$opts --servicelevel $REG_SERVICE_LEVEL"
33     fi
34
35     if [ -n "${REG_RELEASE:-}" ]; then
36         opts="$opts --release=$REG_RELEASE"
37     fi
38 else
39     if [ -n "${REG_SERVICE_LEVEL:-}" ]; then
40         echo "WARNING: REG_SERVICE_LEVEL set without REG_AUTO_ATTACH."
41     fi
42
43     if [ -n "${REG_RELEASE:-}" ]; then
44         echo "WARNING: REG_RELEASE set without REG_AUTO_ATTACH."
45     fi
46
47     if [ -n "${REG_POOL_ID:-}" ]; then
48         attach_opts="$attach_opts --pool=$REG_POOL_ID"
49     fi
50 fi
51
52 if [ -n "${REG_BASE_URL:-}" ]; then
53     opts="$opts --baseurl=$REG_BASE_URL"
54 fi
55
56 if [ -n "${REG_ENVIRONMENT:-}" ]; then
57     opts="$opts --env=$REG_ENVIRONMENT"
58 fi
59
60 if [ -n "${REG_FORCE:-}" ]; then
61     opts="$opts --force"
62     sat5_opts="$sat5_opts --force"
63 fi
64
65 if [ -n "${REG_SERVER_URL:-}" ]; then
66     opts="$opts --serverurl=$REG_SERVER_URL"
67 fi
68
69 if [ -n "${REG_ACTIVATION_KEY:-}" ]; then
70     opts="$opts --activationkey=$REG_ACTIVATION_KEY"
71     sat5_opts="$sat5_opts --activationkey=$REG_ACTIVATION_KEY"
72
73     if [ -z "${REG_ORG:-}" ]; then
74         echo "WARNING: REG_ACTIVATION_KEY set without REG_ORG."
75     fi
76 else
77     echo "WARNING: Support for registering with a username and password is deprecated."
78     echo "Please use activation keys instead.  See the README for more information."
79     if [ -n "${REG_PASSWORD:-}" ]; then
80         opts="$opts --password $REG_PASSWORD"
81     fi
82
83     if [ -n "${REG_USER:-}" ]; then
84         opts="$opts --username $REG_USER"
85     fi
86 fi
87
88 if [ -n "${REG_MACHINE_NAME:-}" ]; then
89     opts="$opts --name $REG_MACHINE_NAME"
90     sat5_opts="$sat5_opts --profilename=$REG_MACHINE_NAME"
91 fi
92
93 if [ -n "${REG_ORG:-}" ]; then
94     opts="$opts --org=$REG_ORG"
95     sat5_opts="$sat5_opts --systemorgid=$REG_ORG"
96 fi
97
98 if [ -n "${REG_REPOS:-}" ]; then
99     for repo in $(echo $REG_REPOS | tr ',' '\n'); do
100         repos="$repos --enable $repo"
101     done
102 fi
103
104 if [ -n "${REG_TYPE:-}" ]; then
105     opts="$opts --type=$REG_TYPE"
106 fi
107
108 # Proxy settings (host and port)
109 if [ -n "${REG_HTTP_PROXY_HOST:-}" ]; then
110     proxy_host="${REG_HTTP_PROXY_HOST}"
111 fi
112
113 if [ -n "${REG_HTTP_PROXY_PORT:-}" ]; then
114     proxy_port="${REG_HTTP_PROXY_PORT}"
115 fi
116
117 # Proxy settings (user and password)
118 if [ -n "${REG_HTTP_PROXY_USERNAME:-}" ]; then
119     proxy_username="${REG_HTTP_PROXY_USERNAME}"
120 fi
121
122 if [ -n "${REG_HTTP_PROXY_PASSWORD:-}" ]; then
123     proxy_password="${REG_HTTP_PROXY_PASSWORD}"
124 fi
125
126 # Sanity Checks for proxy host/port/user/password
127 if [ -n "${REG_HTTP_PROXY_HOST:-}" ]; then
128     if [ -n "${REG_HTTP_PROXY_PORT:-}" ]; then
129         # Good both values are not empty
130         proxy_url="http://${proxy_host}:${proxy_port}"
131         config_opts="--server.proxy_hostname=${proxy_host} --server.proxy_port=${proxy_port}"
132         sat5_opts="${sat5_opts} --proxy_hostname=${proxy_url}"
133         echo "RHSM Proxy set to: ${proxy_url}"
134         if [ -n "${REG_HTTP_PROXY_USERNAME:-}" ]; then
135             if [ -n "${REG_HTTP_PROXY_PASSWORD:-}" ]; then
136                 config_opts="${config_opts} --server.proxy_user=${proxy_username} --server.proxy_password=${proxy_password}"
137                 sat5_opts="${sat5_opts} --proxyUser=${proxy_username} --proxyPassword=${proxy_password}"
138             else
139                 echo "Warning: REG_HTTP_PROXY_PASSWORD cannot be null with non-empty REG_HTTP_PROXY_USERNAME! Skipping..."
140                 proxy_username= ; proxy_password=
141             fi
142         else
143             if [ -n "${REG_HTTP_PROXY_PASSWORD:-}" ]; then
144                 echo "Warning: REG_HTTP_PROXY_USERNAME cannot be null with non-empty REG_HTTP_PROXY_PASSWORD! Skipping..."
145                 proxy_username= ; proxy_password=
146             fi
147         fi
148     else
149         echo "Warning: REG_HTTP_PROXY_PORT cannot be null with non-empty REG_HTTP_PROXY_HOST! Skipping..."
150         proxy_host= ; proxy_port= ; proxy_url= ; proxy_username= ; proxy_password=
151     fi
152 else
153     if [ -n "${REG_HTTP_PROXY_PORT:-}" ]; then
154         echo "Warning: REG_HTTP_PROXY_HOST cannot be null with non-empty REG_HTTP_PROXY_PORT! Skipping..."
155         proxy_host= ; proxy_port= ; proxy_url= ; proxy_username= ; proxy_password=
156     fi
157 fi
158
159 function retry() {
160     # Inhibit -e since we want to retry without exiting..
161     set +e
162     # Retry delay (seconds)
163     retry_delay=2.0
164     retry_count=0
165     mycli="$@"
166     while [ $retry_count -lt ${retry_max_count} ]
167     do
168         echo "INFO: Sleeping ${retry_delay} ..."
169         sleep ${retry_delay}
170         echo "INFO: Executing '${mycli}' ..."
171         ${mycli}
172         if [ $? -eq 0 ]; then
173             echo "INFO: Ran '${mycli}' successfully, not retrying..."
174             break
175         else
176             echo "WARN: Failed to connect when running '${mycli}', retrying (attempt #$retry_count )..."
177             retry_count=$(echo $retry_count + 1 | bc)
178         fi
179     done
180
181     if [ $retry_count -ge ${retry_max_count} ]; then
182         echo "ERROR: Failed to connect after ${retry_max_count} attempts when running '${mycli}'"
183         exit 1
184     fi
185     # Re-enable -e when exiting retry()
186     set -e
187 }
188
189 function detect_satellite_server {
190     if curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $REG_SAT_URL/pub/katello-ca-consumer-latest.noarch.rpm | grep "200 OK"; then
191         echo Satellite 6 or beyond with Katello API detected at $REG_SAT_URL
192         katello_api_enabled=1
193     elif curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $REG_SAT_URL/rhn/Login.do | grep "200 OK"; then
194         echo Satellite 5 with RHN detected at $REG_SAT_URL
195         katello_api_enabled=0
196     else
197         echo No Satellite detected at $REG_SAT_URL
198         exit 1
199     fi
200 }
201
202 if [ "x${proxy_url}" != "x" ];then
203     # Config subscription-manager for proxy
204     subscription-manager config ${config_opts}
205
206     # Config yum for proxy..
207     sed -i -e '/^proxy=/d' /etc/yum.conf
208     echo "proxy=${proxy_url}" >> /etc/yum.conf
209
210     # Handle optional username/password
211     if [ -n "${proxy_username}" ]; then
212         sed -i -e '/^proxy_username=/d' /etc/yum.conf
213         echo "proxy_username=${proxy_username}" >> /etc/yum.conf
214     fi
215
216     if [ -n "${proxy_password}" ]; then
217         sed -i -e '/^proxy_password=/d' /etc/yum.conf
218         echo "proxy_password=${proxy_password}" >> /etc/yum.conf
219     fi
220
221 fi
222
223 case "${REG_METHOD:-}" in
224     portal)
225         retry subscription-manager register $opts
226         if [ -z "${REG_AUTO_ATTACH:-}" -a -z "${REG_ACTIVATION_KEY:-}" ]; then
227             retry subscription-manager attach $attach_opts
228         fi
229         retry subscription-manager repos --disable='*'
230         retry subscription-manager $repos
231         ;;
232     satellite)
233         detect_satellite_server
234         if [ "$katello_api_enabled" = "1" ]; then
235             repos="$repos --enable ${satellite_repo}"
236             curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -O "$REG_SAT_URL/pub/katello-ca-consumer-latest.noarch.rpm"
237
238             # https://bugs.launchpad.net/tripleo/+bug/1711435
239             # Delete the /etc/rhsm/facts directory entirely so that the
240             # %post script from katello-ca-consumer does not override the
241             # hostname with $(hostname -f) if there is no fqdn set
242             fqdn=$(hostname -f)
243             if [ "$fqdn" = "localhost" -o "$fqdn" = "localhost.localdomain" ]; then
244                 rm -rf /etc/rhsm/facts
245             fi
246
247             rpm -Uvh katello-ca-consumer-latest.noarch.rpm || true
248             retry subscription-manager register $opts
249             retry subscription-manager $repos
250             yum install -y katello-agent || true # needed for errata reporting to satellite6
251             katello-package-upload
252
253             # https://bugs.launchpad.net/tripleo/+bug/1711435
254             # recreate the facts dir just in case we rm'd it earlier
255             mkdir -p /etc/rhsm/facts
256         else
257             pushd /usr/share/rhn/
258             curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -k -O $REG_SAT_URL/pub/RHN-ORG-TRUSTED-SSL-CERT
259             popd
260             retry rhnreg_ks --serverUrl=$REG_SAT_URL/XMLRPC $sat5_opts
261         fi
262         ;;
263     disable)
264         echo "Disabling RHEL registration"
265         ;;
266     *)
267         echo "WARNING: only 'portal', 'satellite', and 'disable' are valid values for REG_METHOD."
268         exit 0
269 esac
270
271 mkdir -p $(dirname $OK)
272 touch $OK