Merge "Make neutron dhcp agents per network conditional"
[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 retryCount=0
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   if [[ $retryCount < 3 ]]; then
161     $@
162     if ! [[ $? == 0 ]]; then
163       retryCount=$(echo $retryCount + 1 | bc)
164       echo "WARN: Failed to connect when running '$@', retrying..."
165       retry $@
166     else
167       retryCount=0
168     fi
169   else
170     echo "ERROR: Failed to connect after 3 attempts when running '$@'"
171     exit 1
172   fi
173 }
174
175 function detect_satellite_version {
176     ping_api=$REG_SAT_URL/katello/api/ping
177     if curl --retry 3 --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $ping_api | grep "200 OK"; then
178         echo Satellite 6 detected at $REG_SAT_URL
179         satellite_version=6
180     elif curl --retry 3 --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $REG_SAT_URL/rhn/Login.do | grep "200 OK"; then
181         echo Satellite 5 detected at $REG_SAT_URL
182         satellite_version=5
183     else
184         echo No Satellite detected at $REG_SAT_URL
185         exit 1
186     fi
187 }
188
189 if [ "x${proxy_url}" != "x" ];then
190     # Config subscription-manager for proxy
191     subscription-manager config ${config_opts}
192
193     # Config yum for proxy..
194     sed -i -e '/^proxy=/d' /etc/yum.conf
195     echo "proxy=${proxy_url}" >> /etc/yum.conf
196
197     # Handle optional username/password
198     if [ -n "${proxy_username}" ]; then
199         sed -i -e '/^proxy_username=/d' /etc/yum.conf
200         echo "proxy_username=${proxy_username}" >> /etc/yum.conf
201     fi
202
203     if [ -n "${proxy_password}" ]; then
204         sed -i -e '/^proxy_password=/d' /etc/yum.conf
205         echo "proxy_password=${proxy_password}" >> /etc/yum.conf
206     fi
207
208 fi
209
210 case "${REG_METHOD:-}" in
211     portal)
212         retry subscription-manager register $opts
213         if [ -z "${REG_AUTO_ATTACH:-}" -a -z "${REG_ACTIVATION_KEY:-}" ]; then
214             retry subscription-manager attach $attach_opts
215         fi
216         retry subscription-manager repos --disable='*'
217         retry subscription-manager $repos
218         ;;
219     satellite)
220         detect_satellite_version
221         if [ "$satellite_version" = "6" ]; then
222             repos="$repos --enable ${satellite_repo}"
223             curl --retry 3 --retry-delay 10 --max-time 30 -L -k -O "$REG_SAT_URL/pub/katello-ca-consumer-latest.noarch.rpm"
224             rpm -Uvh katello-ca-consumer-latest.noarch.rpm || true
225             retry subscription-manager register $opts
226             retry subscription-manager $repos
227             retry yum install -y katello-agent || true # needed for errata reporting to satellite6
228             katello-package-upload
229             retry subscription-manager repos --disable ${satellite_repo}
230         else
231             pushd /usr/share/rhn/
232             curl --retry 3 --retry-delay 10 --max-time 30 -k -O $REG_SAT_URL/pub/RHN-ORG-TRUSTED-SSL-CERT
233             popd
234             retry rhnreg_ks --serverUrl=$REG_SAT_URL/XMLRPC $sat5_opts
235         fi
236         ;;
237     disable)
238         echo "Disabling RHEL registration"
239         ;;
240     *)
241         echo "WARNING: only 'portal', 'satellite', and 'disable' are valid values for REG_METHOD."
242         exit 0
243 esac
244
245 mkdir -p $(dirname $OK)
246 touch $OK