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