@@ -37,11 +37,36 @@ if [ -f /etc/os-release ] ; then
3737 . /etc/os-release
3838fi
3939
40+ # Choose package manager
41+ # apt-get for Debian & Ubuntu
42+ # dnf for Fedora (version > = 22) & RHEL family (version > 7)
43+ # yum for Fedora (version < 22 ) & RHEL family (version < 8 )
44+ if [ x$ID = xfedora ]; then
45+ if [ "${VERSION_ID%.*}" -gt 21 ]; then
46+ PKG_MANAGER='dnf '
47+ else
48+ PKG_MANAGER ='yum '
49+ fi
50+ elif [ -f /etc/redhat-release ] ; then
51+ if [ "${VERSION_ID%.*}" -gt 7 ]; then
52+ PKG_MANAGER='dnf '
53+ else
54+ PKG_MANAGER ='yum '
55+ fi
56+ elif [ -f /etc/debian_version ]; then
57+ PKG_MANAGER='apt-get '
58+ fi
59+
4060SSL_CA_CERT =$(mktemp)
4161cat < < EOF > $SSL_CA_CERT
4262<%= foreman_server_ca_cert %>
4363EOF
4464
65+ cleanup_and_exit() {
66+ rm -f $SSL_CA_CERT
67+ exit $1
68+ }
69+
4570<% unless @repo . blank? -%>
4671echo '#'
4772echo '# Adding repository'
@@ -58,8 +83,8 @@ gpgkey=<%= shell_escape @repo_gpg_key_url %>
5883EOF
5984
6085 echo "Building yum metadata cache, this may take a few minutes"
61- yum makecache
62- elif [ x$ID = xdebian ] || [ x$ID = xubuntu ]; then
86+ $PKG_MANAGER makecache
87+ elif [ -f /etc/debian_version ]; then
6388 cat < < EOF > /etc/apt/sources.list.d/foreman_registration.list
6489<%= shell_escape @repo %>
6590EOF
7196
7297else
7398 echo "Unsupported operating system, can't add repository."
74- exit 1
99+ cleanup_and_exit 1
75100fi
76101<% end -%>
77102
@@ -102,7 +127,7 @@ echo "#"
102127if [ -f /etc/redhat-release ]; then
103128 register_katello_host(){
104129 UUID=$(subscription-manager identity | head -1 | awk '{print $3}')
105- curl --silent --show-error --cacert $SSL_CA_CERT --request POST "<%= @registration_url %> " \
130+ curl --silent --show-error --cacert $KATELLO_SERVER_CA_CERT --request POST "<%= @registration_url %> " \
106131 --data "uuid=$UUID" \
107132 <%= headers . join ( ' ' ) %> \
108133<%= " --data 'host[organization_id]=#{ @organization . id } ' \\ \n " if @organization -%>
@@ -115,42 +140,77 @@ if [ -f /etc/redhat-release ]; then
115140<%= " --data packages=#{ shell_escape ( @packages ) } \\ \n " if @packages . present? -%>
116141<%= " --data 'update_packages=#{ @update_packages } ' \\ \n " unless @update_packages . nil? -%>
117142
118- }
143+ }
144+
145+ KATELLO_SERVER_CA_CERT=/etc/rhsm/ca/katello-server-ca.pem
146+ RHSM_CFG=/etc/rhsm/rhsm.conf
119147
148+ # Backup rhsm.conf
149+ if [ -f $RHSM_CFG ] ; then
150+ test -f $RHSM_CFG.bak || cp $RHSM_CFG $RHSM_CFG.bak
151+ fi
152+
153+ # rhn-client-tools conflicts with subscription-manager package
154+ # since rhn tools replaces subscription-manager, we need to explicitly
155+ # install subscription-manager after the rhn tools cleanup
156+ if [ x$ID = xol ]; then
157+ $PKG_MANAGER remove -y rhn-client-tools
158+ $PKG_MANAGER install -y --setopt=obsoletes=0 subscription-manager
159+ fi
160+
161+ # Prepare SSL certificate
162+ mkdir -p /etc/rhsm/ca
163+ cp -f $SSL_CA_CERT $KATELLO_SERVER_CA_CERT
164+ chmod 644 $KATELLO_SERVER_CA_CERT
165+
166+ # Prepare subscription-manager
120167 <% if @force -%>
121168 if [ -x "$(command -v subscription-manager)" ] ; then
122169 subscription-manager unregister || true
123170 subscription-manager clean
124171 fi
125172
126- yum remove -y katello-ca-consumer\*
173+ $PKG_MANAGER remove -y katello-ca-consumer\*
127174 <% end -%>
128175
129- # rhn-client-tools conflicts with subscription-manager package
130- # since rhn tools replaces subscription-manager, we need to explicitly
131- # install subscription-manager after the rhn tools cleanup
132- if [ x$ID = xol ]; then
133- yum remove -y rhn-client-tools
134- yum install -y --setopt=obsoletes=0 subscription-manager
176+ if ! [ -x "$(command -v subscription-manager)" ] ; then
177+ $PKG_MANAGER install -y subscription-manager
178+ else
179+ $PKG_MANAGER upgrade -y subscription-manager
135180 fi
136181
137- CONSUMER_RPM=$(mktemp --suffix .rpm)
138- curl --silent --show-error --output $CONSUMER_RPM <%= subscription_manager_configuration_url ( hostname : @url_host ) %>
182+ if ! [ -f $RHSM_CFG ] ; then
183+ echo "'$RHSM_CFG' not found, cannot configure subscription-manager"
184+ cleanup_and_exit 1
185+ fi
139186
140- # Workaround for systems with enabled FIPS,
141- # where installation of RPM generated on RHEL7 cause 'no digest' error
142- # See https://projects.theforeman.org/issues/32068
143- if [ "$(cat /proc/sys/crypto/fips_enabled)" = "1" ]; then
144- rpm -ivh --nodigest --nofiledigest $CONSUMER_RPM
187+ # Configure subscription-manager
188+ test -f $RHSM_CFG.bak || cp $RHSM_CFG $RHSM_CFG.bak
189+ subscription-manager config \
190+ --server.hostname="<%= @rhsm_url . host %> " \
191+ --server.port="<%= @rhsm_url . port %> " \
192+ --server.prefix="<%= @rhsm_url . path %> " \
193+ --rhsm.repo_ca_cert="$KATELLO_SERVER_CA_CERT" \
194+ --rhsm.baseurl="<%= @pulp_content_url %> "
195+
196+ # Older versions of subscription manager may not recognize
197+ # report_package_profile and package_profile_on_trans options.
198+ # So set them separately and redirect out & error to /dev/null
199+ # to fail silently.
200+ subscription-manager config --rhsm.package_profile_on_trans=1 > /dev/null 2> &1 || true
201+ subscription-manager config --rhsm.report_package_profile=1 > /dev/null 2> &1 || true
202+
203+ # Configuration for EL6
204+ if grep --quiet full_refresh_on_yum $RHSM_CFG; then
205+ sed -i "s/full_refresh_on_yum\s*=.*$/full_refresh_on_yum = 1/g" $RHSM_CFG
145206 else
146- yum localinstall $CONSUMER_RPM -y
207+ full_refresh_config="#config for on-premise management\nfull_refresh_on_yum = 1"
208+ sed -i "/baseurl/a $full_refresh_config" $RHSM_CFG
147209 fi
148210
149- rm -f $CONSUMER_RPM
150-
151211 subscription-manager register <%= '--force' if @force %> \
152- --org='<%= @organization . label %> ' \
153- --activationkey=<%= shell_escape ( activation_keys ) %> || <%= @ignore_subman_errors ? 'true' : 'exit 1' %>
212+ --org='<%= @organization . label %> ' \
213+ --activationkey=' <%= activation_keys %> ' || <%= @ignore_subman_errors ? 'true' : 'cleanup_and_exit 1' %>
154214
155215 register_katello_host | bash
156216else
159219<% else -%>
160220register_host | bash
161221<% end -%>
222+
223+ cleanup_and_exit
0 commit comments