Merge "Add retry to RHEL registration"
[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 attach_opts=
17 sat5_opts=
18 repos="repos --enable rhel-7-server-rpms"
19 satellite_repo=${REG_SAT_REPO}
20 if [ -n "${REG_AUTO_ATTACH:-}" ]; then
21     opts="$opts --auto-attach"
22
23     if [ -n "${REG_SERVICE_LEVEL:-}" ]; then
24         opts="$opts --servicelevel $REG_SERVICE_LEVEL"
25     fi
26
27     if [ -n "${REG_RELEASE:-}" ]; then
28         opts="$opts --release=$REG_RELEASE"
29     fi
30 else
31     if [ -n "${REG_SERVICE_LEVEL:-}" ]; then
32         echo "WARNING: REG_SERVICE_LEVEL set without REG_AUTO_ATTACH."
33     fi
34
35     if [ -n "${REG_RELEASE:-}" ]; then
36         echo "WARNING: REG_RELEASE set without REG_AUTO_ATTACH."
37     fi
38
39     if [ -n "${REG_POOL_ID:-}" ]; then
40         attach_opts="$attach_opts --pool=$REG_POOL_ID"
41     fi
42 fi
43
44 if [ -n "${REG_BASE_URL:-}" ]; then
45     opts="$opts --baseurl=$REG_BASE_URL"
46 fi
47
48 if [ -n "${REG_ENVIRONMENT:-}" ]; then
49     opts="$opts --env=$REG_ENVIRONMENT"
50 fi
51
52 if [ -n "${REG_FORCE:-}" ]; then
53     opts="$opts --force"
54     sat5_opts="$sat5_opts --force"
55 fi
56
57 if [ -n "${REG_SERVER_URL:-}" ]; then
58     opts="$opts --serverurl=$REG_SERVER_URL"
59 fi
60
61 if [ -n "${REG_ACTIVATION_KEY:-}" ]; then
62     opts="$opts --activationkey=$REG_ACTIVATION_KEY"
63     sat5_opts="$sat5_opts --activationkey=$REG_ACTIVATION_KEY"
64
65     if [ -z "${REG_ORG:-}" ]; then
66         echo "WARNING: REG_ACTIVATION_KEY set without REG_ORG."
67     fi
68 else
69     echo "WARNING: Support for registering with a username and password is deprecated."
70     echo "Please use activation keys instead.  See the README for more information."
71     if [ -n "${REG_PASSWORD:-}" ]; then
72         opts="$opts --password $REG_PASSWORD"
73     fi
74
75     if [ -n "${REG_USER:-}" ]; then
76         opts="$opts --username $REG_USER"
77     fi
78 fi
79
80 if [ -n "${REG_MACHINE_NAME:-}" ]; then
81     opts="$opts --name $REG_MACHINE_NAME"
82     sat5_opts="$sat5_opts --profilename=$REG_MACHINE_NAME"
83 fi
84
85 if [ -n "${REG_ORG:-}" ]; then
86     opts="$opts --org=$REG_ORG"
87     sat5_opts="$sat5_opts --systemorgid=$REG_ORG"
88 fi
89
90 if [ -n "${REG_REPOS:-}" ]; then
91     for repo in $(echo $REG_REPOS | tr ',' '\n'); do
92         repos="$repos --enable $repo"
93     done
94 fi
95
96 if [ -n "${REG_TYPE:-}" ]; then
97     opts="$opts --type=$REG_TYPE"
98 fi
99
100 function retry() {
101   if [[ $retryCount < 3 ]]; then
102     $@
103     if ! [[ $? == 0 ]]; then
104       retryCount=$(echo $retryCount + 1 | bc)
105       echo "WARN: Failed to connect when running '$@', retrying..."
106       retry $@
107     else
108       retryCount=0
109     fi
110   else
111     echo "ERROR: Failed to connect after 3 attempts when running '$@'"
112     exit 1
113   fi
114 }
115
116 function detect_satellite_version {
117     ping_api=$REG_SAT_URL/katello/api/ping
118     if curl --retry 3 --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $ping_api | grep "200 OK"; then
119         echo Satellite 6 detected at $REG_SAT_URL
120         satellite_version=6
121     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
122         echo Satellite 5 detected at $REG_SAT_URL
123         satellite_version=5
124     else
125         echo No Satellite detected at $REG_SAT_URL
126         exit 1
127     fi
128 }
129
130 case "${REG_METHOD:-}" in
131     portal)
132         retry subscription-manager register $opts
133         if [ -z "${REG_AUTO_ATTACH:-}" -a -z "${REG_ACTIVATION_KEY:-}" ]; then
134             retry subscription-manager attach $attach_opts
135         fi
136         retry subscription-manager repos --disable '*'
137         retry subscription-manager $repos
138         ;;
139     satellite)
140         detect_satellite_version
141         if [ "$satellite_version" = "6" ]; then
142             repos="$repos --enable ${satellite_repo}"
143             curl --retry 3 --retry-delay 10 --max-time 30 -L -k -O "$REG_SAT_URL/pub/katello-ca-consumer-latest.noarch.rpm"
144             rpm -Uvh katello-ca-consumer-latest.noarch.rpm || true
145             retry subscription-manager register $opts
146             retry subscription-manager $repos
147             retry yum install -y katello-agent || true # needed for errata reporting to satellite6
148             katello-package-upload
149             retry subscription-manager repos --disable ${satellite_repo}
150         else
151             pushd /usr/share/rhn/
152             curl --retry 3 --retry-delay 10 --max-time 30 -k -O $REG_SAT_URL/pub/RHN-ORG-TRUSTED-SSL-CERT
153             popd
154             retry rhnreg_ks --serverUrl=$REG_SAT_URL/XMLRPC $sat5_opts
155         fi
156         ;;
157     disable)
158         echo "Disabling RHEL registration"
159         ;;
160     *)
161         echo "WARNING: only 'portal', 'satellite', and 'disable' are valid values for REG_METHOD."
162         exit 0
163 esac
164
165 mkdir -p $(dirname $OK)
166 touch $OK