3 # dib-lint: disable=setu sete setpipefail dibdebugtrace
8 OK=/mnt/state/var/lib/rhsm/rhsm.ok
19 repos="repos --enable rhel-7-server-rpms"
20 satellite_repo=${REG_SAT_REPO}
28 if [ -n "${REG_AUTO_ATTACH:-}" ]; then
29 opts="$opts --auto-attach"
31 if [ -n "${REG_SERVICE_LEVEL:-}" ]; then
32 opts="$opts --servicelevel $REG_SERVICE_LEVEL"
35 if [ -n "${REG_RELEASE:-}" ]; then
36 opts="$opts --release=$REG_RELEASE"
39 if [ -n "${REG_SERVICE_LEVEL:-}" ]; then
40 echo "WARNING: REG_SERVICE_LEVEL set without REG_AUTO_ATTACH."
43 if [ -n "${REG_RELEASE:-}" ]; then
44 echo "WARNING: REG_RELEASE set without REG_AUTO_ATTACH."
47 if [ -n "${REG_POOL_ID:-}" ]; then
48 attach_opts="$attach_opts --pool=$REG_POOL_ID"
52 if [ -n "${REG_BASE_URL:-}" ]; then
53 opts="$opts --baseurl=$REG_BASE_URL"
56 if [ -n "${REG_ENVIRONMENT:-}" ]; then
57 opts="$opts --env=$REG_ENVIRONMENT"
60 if [ -n "${REG_FORCE:-}" ]; then
62 sat5_opts="$sat5_opts --force"
65 if [ -n "${REG_SERVER_URL:-}" ]; then
66 opts="$opts --serverurl=$REG_SERVER_URL"
69 if [ -n "${REG_ACTIVATION_KEY:-}" ]; then
70 opts="$opts --activationkey=$REG_ACTIVATION_KEY"
71 sat5_opts="$sat5_opts --activationkey=$REG_ACTIVATION_KEY"
73 if [ -z "${REG_ORG:-}" ]; then
74 echo "WARNING: REG_ACTIVATION_KEY set without REG_ORG."
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"
83 if [ -n "${REG_USER:-}" ]; then
84 opts="$opts --username $REG_USER"
88 if [ -n "${REG_MACHINE_NAME:-}" ]; then
89 opts="$opts --name $REG_MACHINE_NAME"
90 sat5_opts="$sat5_opts --profilename=$REG_MACHINE_NAME"
93 if [ -n "${REG_ORG:-}" ]; then
94 opts="$opts --org=$REG_ORG"
95 sat5_opts="$sat5_opts --systemorgid=$REG_ORG"
98 if [ -n "${REG_REPOS:-}" ]; then
99 for repo in $(echo $REG_REPOS | tr ',' '\n'); do
100 repos="$repos --enable $repo"
104 if [ -n "${REG_TYPE:-}" ]; then
105 opts="$opts --type=$REG_TYPE"
108 # Proxy settings (host and port)
109 if [ -n "${REG_HTTP_PROXY_HOST:-}" ]; then
110 proxy_host="${REG_HTTP_PROXY_HOST}"
113 if [ -n "${REG_HTTP_PROXY_PORT:-}" ]; then
114 proxy_port="${REG_HTTP_PROXY_PORT}"
117 # Proxy settings (user and password)
118 if [ -n "${REG_HTTP_PROXY_USERNAME:-}" ]; then
119 proxy_username="${REG_HTTP_PROXY_USERNAME}"
122 if [ -n "${REG_HTTP_PROXY_PASSWORD:-}" ]; then
123 proxy_password="${REG_HTTP_PROXY_PASSWORD}"
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}"
139 echo "Warning: REG_HTTP_PROXY_PASSWORD cannot be null with non-empty REG_HTTP_PROXY_USERNAME! Skipping..."
140 proxy_username= ; proxy_password=
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=
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=
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=
160 # Inhibit -e since we want to retry without exiting..
162 # Retry delay (seconds)
166 while [ $retry_count -lt ${retry_max_count} ]
168 echo "INFO: Sleeping ${retry_delay} ..."
170 echo "INFO: Executing '${mycli}' ..."
172 if [ $? -eq 0 ]; then
173 echo "INFO: Ran '${mycli}' successfully, not retrying..."
176 echo "WARN: Failed to connect when running '${mycli}', retrying (attempt #$retry_count )..."
177 retry_count=$(echo $retry_count + 1 | bc)
181 if [ $retry_count -ge ${retry_max_count} ]; then
182 echo "ERROR: Failed to connect after ${retry_max_count} attempts when running '${mycli}'"
185 # Re-enable -e when exiting retry()
189 function detect_satellite_version {
190 ping_api=$REG_SAT_URL/katello/api/ping
191 if curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $ping_api | grep "200 OK"; then
192 echo Satellite 6 detected at $REG_SAT_URL
194 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
195 echo Satellite 5 detected at $REG_SAT_URL
198 echo No Satellite detected at $REG_SAT_URL
203 if [ "x${proxy_url}" != "x" ];then
204 # Config subscription-manager for proxy
205 subscription-manager config ${config_opts}
207 # Config yum for proxy..
208 sed -i -e '/^proxy=/d' /etc/yum.conf
209 echo "proxy=${proxy_url}" >> /etc/yum.conf
211 # Handle optional username/password
212 if [ -n "${proxy_username}" ]; then
213 sed -i -e '/^proxy_username=/d' /etc/yum.conf
214 echo "proxy_username=${proxy_username}" >> /etc/yum.conf
217 if [ -n "${proxy_password}" ]; then
218 sed -i -e '/^proxy_password=/d' /etc/yum.conf
219 echo "proxy_password=${proxy_password}" >> /etc/yum.conf
224 case "${REG_METHOD:-}" in
226 retry subscription-manager register $opts
227 if [ -z "${REG_AUTO_ATTACH:-}" -a -z "${REG_ACTIVATION_KEY:-}" ]; then
228 retry subscription-manager attach $attach_opts
230 retry subscription-manager repos --disable='*'
231 retry subscription-manager $repos
234 detect_satellite_version
235 if [ "$satellite_version" = "6" ]; then
236 repos="$repos --enable ${satellite_repo}"
237 curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -O "$REG_SAT_URL/pub/katello-ca-consumer-latest.noarch.rpm"
238 rpm -Uvh katello-ca-consumer-latest.noarch.rpm || true
239 retry subscription-manager register $opts
240 retry subscription-manager $repos
241 retry yum install -y katello-agent || true # needed for errata reporting to satellite6
242 katello-package-upload
243 retry subscription-manager repos --disable ${satellite_repo}
245 pushd /usr/share/rhn/
246 curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -k -O $REG_SAT_URL/pub/RHN-ORG-TRUSTED-SSL-CERT
248 retry rhnreg_ks --serverUrl=$REG_SAT_URL/XMLRPC $sat5_opts
252 echo "Disabling RHEL registration"
255 echo "WARNING: only 'portal', 'satellite', and 'disable' are valid values for REG_METHOD."
259 mkdir -p $(dirname $OK)