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 if [[ $retryCount < 3 ]]; then
162 if ! [[ $? == 0 ]]; then
163 retryCount=$(echo $retryCount + 1 | bc)
164 echo "WARN: Failed to connect when running '$@', retrying..."
170 echo "ERROR: Failed to connect after 3 attempts when running '$@'"
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
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
184 echo No Satellite detected at $REG_SAT_URL
189 if [ "x${proxy_url}" != "x" ];then
190 # Config subscription-manager for proxy
191 subscription-manager config ${config_opts}
193 # Config yum for proxy..
194 sed -i -e '/^proxy=/d' /etc/yum.conf
195 echo "proxy=${proxy_url}" >> /etc/yum.conf
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
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
210 case "${REG_METHOD:-}" in
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
216 retry subscription-manager repos --disable '*'
217 retry subscription-manager $repos
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}
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
234 retry rhnreg_ks --serverUrl=$REG_SAT_URL/XMLRPC $sat5_opts
238 echo "Disabling RHEL registration"
241 echo "WARNING: only 'portal', 'satellite', and 'disable' are valid values for REG_METHOD."
245 mkdir -p $(dirname $OK)