PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/spacewalk-certs-tools-1.7.3/rhn_bootstrap_strings.py

#
Python | 475 lines | 454 code | 3 blank | 18 comment | 19 complexity | 4a59d06ba05bfe64499873a7c7a305de MD5 | raw file
Possible License(s): GPL-2.0
  1. #
  2. # Copyright (c) 2008--2011 Red Hat, Inc.
  3. #
  4. # This software is licensed to you under the GNU General Public License,
  5. # version 2 (GPLv2). There is NO WARRANTY for this software, express or
  6. # implied, including the implied warranties of MERCHANTABILITY or FITNESS
  7. # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
  8. # along with this software; if not, see
  9. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  10. #
  11. # Red Hat trademarks are not licensed under GPLv2. No permission is
  12. # granted to use or replicate Red Hat trademarks that are incorporated
  13. # in this software or its documentation.
  14. #
  15. #
  16. # shell script function library for rhn-bootstrap
  17. #
  18. # $Id$
  19. import os.path
  20. _header = """\
  21. #!/bin/bash
  22. echo "%s Client bootstrap script v4.0"
  23. # This file was autogenerated. Minor manual editing of this script (and
  24. # possibly the client-config-overrides.txt file) may be necessary to complete
  25. # the bootstrap setup. Once customized, the bootstrap script can be triggered
  26. # in one of two ways (the first is preferred):
  27. #
  28. # (1) centrally, from the %s via ssh (i.e., from the
  29. # %s):
  30. # cd /var/www/html/pub/bootstrap/
  31. # cat bootstrap-<edited_name>.sh | ssh root@<client-hostname> /bin/bash
  32. #
  33. # ...or...
  34. #
  35. # (2) in a decentralized manner, executed on each client, via wget or curl:
  36. # wget -qO- https://<hostname>/pub/bootstrap/bootstrap-<edited_name>.sh | /bin/bash
  37. # ...or...
  38. # curl -Sks https://<hostname>/pub/bootstrap/bootstrap-<edited_name>.sh | /bin/bash
  39. # SECURITY NOTE:
  40. # Use of these scripts via the two methods discussed is the most expedient
  41. # way to register machines to your %s. Since "wget" is used
  42. # throughout the script to download various files, a "Man-in-the-middle"
  43. # attack is theoretically possible.
  44. #
  45. # The actual registration process is performed securely via SSL, so the risk
  46. # is minimized in a sense. This message merely serves as a warning.
  47. # Administrators need to appropriately weigh their concern against the
  48. # relative security of their internal network.
  49. # PROVISIONING/KICKSTART NOTE:
  50. # If provisioning a client, ensure the proper CA SSL public certificate is
  51. # configured properly in the post section of your kickstart profiles (the
  52. # RHN Satellite or hosted web user interface).
  53. # UP2DATE/RHN_REGISTER VERSIONING NOTE:
  54. # This script will not work with very old versions of up2date and
  55. # rhn_register.
  56. echo
  57. echo
  58. echo "MINOR MANUAL EDITING OF THIS FILE MAY BE REQUIRED!"
  59. echo
  60. echo "If this bootstrap script was created during the initial installation"
  61. echo "of an RHN Satellite, the ACTIVATION_KEYS, and ORG_GPG_KEY values will"
  62. echo "probably *not* be set (see below). If this is the case, please do the"
  63. echo "following:"
  64. echo " - copy this file to a name specific to its use."
  65. echo " (e.g., to bootstrap-SOME_NAME.sh - like bootstrap-web-servers.sh.)"
  66. echo " - on the website create an activation key or keys for the system(s) to"
  67. echo " be registered."
  68. echo " - edit the values of the VARIABLES below (in this script) as"
  69. echo " appropriate:"
  70. echo " - ACTIVATION_KEYS needs to reflect the activation key(s) value(s)"
  71. echo " from the website. XKEY or XKEY,YKEY"
  72. echo " - ORG_GPG_KEY needs to be set to the name(s) of the corporate public"
  73. echo " GPG key filename(s) (residing in /var/www/html/pub) if appropriate. XKEY or XKEY,YKEY"
  74. echo
  75. echo "Verify that the script variable settings are correct:"
  76. echo " - CLIENT_OVERRIDES should be only set differently if a customized"
  77. echo " client-config-overrides-VER.txt file was created with a different"
  78. echo " name."
  79. echo " - ensure the value of HOSTNAME is correct."
  80. echo " - ensure the value of ORG_CA_CERT is correct."
  81. echo
  82. echo "Enable this script: comment (with #'s) this block (or, at least just"
  83. echo "the exit below)"
  84. echo
  85. %s
  86. # can be edited, but probably correct (unless created during initial install):
  87. # NOTE: ACTIVATION_KEYS *must* be used to bootstrap a client machine.
  88. ACTIVATION_KEYS=%s
  89. ORG_GPG_KEY=%s
  90. # can be edited, but probably correct:
  91. CLIENT_OVERRIDES=%s
  92. HOSTNAME=%s
  93. ORG_CA_CERT=%s
  94. ORG_CA_CERT_IS_RPM_YN=%s
  95. USING_SSL=%s
  96. USING_GPG=%s
  97. REGISTER_THIS_BOX=1
  98. ALLOW_CONFIG_ACTIONS=%s
  99. ALLOW_REMOTE_COMMANDS=%s
  100. FULLY_UPDATE_THIS_BOX=%s
  101. # Set if you want to specify profilename for client systems.
  102. # NOTE: Make sure it's set correctly if any external command is used.
  103. #
  104. # ex. PROFILENAME="foo.example.com" # For specific clinet system
  105. # PROFILENAME=`hostname -s` # Short hostname
  106. # PROFILENAME=`hostname -f` # FQDN
  107. PROFILENAME="" # Empty by default to let it be set automatically.
  108. #
  109. # -----------------------------------------------------------------------------
  110. # DO NOT EDIT BEYOND THIS POINT -----------------------------------------------
  111. # -----------------------------------------------------------------------------
  112. #
  113. # an idea from Erich Morisse (of Red Hat).
  114. # use either wget *or* curl
  115. # Also check to see if the version on the
  116. # machine supports the insecure mode and format
  117. # command accordingly.
  118. if [ -x /usr/bin/wget ] ; then
  119. output=`LANG=en_US /usr/bin/wget --no-check-certificate 2>&1`
  120. error=`echo $output | grep "unrecognized option"`
  121. if [ -z "$error" ] ; then
  122. FETCH="/usr/bin/wget -q -r -nd --no-check-certificate"
  123. else
  124. FETCH="/usr/bin/wget -q -r -nd"
  125. fi
  126. else
  127. if [ -x /usr/bin/curl ] ; then
  128. output=`LANG=en_US /usr/bin/curl -k 2>&1`
  129. error=`echo $output | grep "is unknown"`
  130. if [ -z "$error" ] ; then
  131. FETCH="/usr/bin/curl -SksO"
  132. else
  133. FETCH="/usr/bin/curl -SsO"
  134. fi
  135. fi
  136. fi
  137. HTTP_PUB_DIRECTORY=http://${HOSTNAME}/%s
  138. HTTPS_PUB_DIRECTORY=https://${HOSTNAME}/%s
  139. if [ $USING_SSL -eq 0 ] ; then
  140. HTTPS_PUB_DIRECTORY=${HTTP_PUB_DIRECTORY}
  141. fi
  142. INSTALLER=up2date
  143. if [ -x /usr/bin/zypper ] ; then
  144. INSTALLER=zypper
  145. elif [ -x /usr/bin/yum ] ; then
  146. INSTALLER=yum
  147. fi
  148. """
  149. def getHeader(productName, activation_keys, org_gpg_key,
  150. overrides, hostname, orgCACert, isRpmYN,
  151. using_ssl, using_gpg,
  152. allow_config_actions, allow_remote_commands, up2dateYN, pubname):
  153. #2/14/06 wregglej 181407 If the org_gpg_key option has the path to the file
  154. #in it, remove it. It will cause the $FETCH to fail.
  155. path_list = os.path.split(org_gpg_key)
  156. if path_list[0] and path_list[0] != '':
  157. org_gpg_key = path_list[1]
  158. if not activation_keys:
  159. exit_call = "exit 1"
  160. else:
  161. exit_call = " "
  162. return _header % (productName, productName, productName, productName,
  163. exit_call, activation_keys, org_gpg_key,
  164. overrides, hostname, orgCACert, isRpmYN,
  165. using_ssl, using_gpg,
  166. allow_config_actions, allow_remote_commands, up2dateYN,
  167. pubname, pubname)
  168. def getConfigFilesSh():
  169. return """\
  170. echo
  171. echo "UPDATING RHN_REGISTER/UP2DATE CONFIGURATION FILES"
  172. echo "-------------------------------------------------"
  173. echo "* downloading necessary files"
  174. echo " client_config_update.py..."
  175. rm -f client_config_update.py
  176. $FETCH ${HTTPS_PUB_DIRECTORY}/bootstrap/client_config_update.py
  177. echo " ${CLIENT_OVERRIDES}..."
  178. rm -f ${CLIENT_OVERRIDES}
  179. $FETCH ${HTTPS_PUB_DIRECTORY}/bootstrap/${CLIENT_OVERRIDES}
  180. if [ ! -f "client_config_update.py" ] ; then
  181. echo "ERROR: client_config_update.py was not downloaded"
  182. exit 1
  183. fi
  184. if [ ! -f "${CLIENT_OVERRIDES}" ] ; then
  185. echo "ERROR: ${CLIENT_OVERRIDES} was not downloaded"
  186. exit 1
  187. fi
  188. """
  189. def getUp2dateScriptsSh():
  190. return """\
  191. echo "* running the update scripts"
  192. if [ -f "/etc/sysconfig/rhn/rhn_register" ] ; then
  193. echo " . rhn_register config file"
  194. /usr/bin/python -u client_config_update.py /etc/sysconfig/rhn/rhn_register ${CLIENT_OVERRIDES}
  195. fi
  196. echo " . up2date config file"
  197. /usr/bin/python -u client_config_update.py /etc/sysconfig/rhn/up2date ${CLIENT_OVERRIDES}
  198. """
  199. def getGPGKeyImportSh():
  200. return """\
  201. if [ ! -z "$ORG_GPG_KEY" ] ; then
  202. echo
  203. echo "* importing organizational GPG key"
  204. for GPG_KEY in $(echo "$ORG_GPG_KEY" | tr "," " "); do
  205. rm -f ${GPG_KEY}
  206. $FETCH ${HTTPS_PUB_DIRECTORY}/${GPG_KEY}
  207. # get the major version of up2date
  208. # this will also work for RHEL 5 and systems where no up2date is installed
  209. res=$(LC_ALL=C rpm -q --queryformat '%{version}' up2date | sed -e 's/\..*//g')
  210. if [ "x$res" == "x2" ] ; then
  211. gpg $(up2date --gpg-flags) --import $GPG_KEY
  212. else
  213. rpm --import $GPG_KEY
  214. fi
  215. done
  216. fi
  217. """
  218. def getCorpCACertSh():
  219. return """\
  220. echo
  221. echo "* attempting to install corporate public CA cert"
  222. if [ $ORG_CA_CERT_IS_RPM_YN -eq 1 ] ; then
  223. rpm -Uvh --force --replacefiles --replacepkgs ${HTTPS_PUB_DIRECTORY}/${ORG_CA_CERT}
  224. else
  225. rm -f ${ORG_CA_CERT}
  226. $FETCH ${HTTPS_PUB_DIRECTORY}/${ORG_CA_CERT}
  227. mv ${ORG_CA_CERT} /usr/share/rhn/
  228. fi
  229. if [ "$INSTALLER" == zypper ] ; then
  230. if [ $ORG_CA_CERT_IS_RPM_YN -eq 1 ] ; then
  231. # get name from config
  232. ORG_CA_CERT=$(basename $(sed -n 's/^sslCACert *= *//p' /etc/sysconfig/rhn/up2date))
  233. fi
  234. test -e "/etc/ssl/certs/${ORG_CA_CERT}.pem" || {
  235. test -d "/etc/ssl/certs" || mkdir -p "/etc/ssl/certs"
  236. ln -s "/usr/share/rhn/${ORG_CA_CERT}" "/etc/ssl/certs/${ORG_CA_CERT}.pem"
  237. }
  238. test -x /usr/bin/c_rehash && /usr/bin/c_rehash /etc/ssl/certs/ | grep "${ORG_CA_CERT}"
  239. fi
  240. """
  241. #5/16/05 wregglej 159437 - changed script to use rhn-actions-control
  242. def getAllowConfigManagement():
  243. return """\
  244. if [ $ALLOW_CONFIG_ACTIONS -eq 1 ] ; then
  245. echo
  246. echo "* setting permissions to allow configuration management"
  247. echo " NOTE: use an activation key to subscribe to the tools"
  248. if [ "$INSTALLER" == zypper ] ; then
  249. echo " channel and zypper install/update rhncfg-actions"
  250. elif [ "$INSTALLER" == yum ] ; then
  251. echo " channel and yum upgrade rhncfg-actions"
  252. else
  253. echo " channel and up2date rhncfg-actions"
  254. fi
  255. if [ -x "/usr/bin/rhn-actions-control" ] ; then
  256. rhn-actions-control --enable-all
  257. rhn-actions-control --disable-run
  258. else
  259. echo "Error setting permissions for configuration management."
  260. echo " Please ensure that the activation key subscribes the"
  261. if [ "$INSTALLER" == zypper ] ; then
  262. echo " system to the tools channel and zypper install/update rhncfg-actions."
  263. elif [ "$INSTALLER" == yum ] ; then
  264. echo " system to the tools channel and yum updates rhncfg-actions."
  265. else
  266. echo " system to the tools channel and up2dates rhncfg-actions."
  267. fi
  268. exit
  269. fi
  270. fi
  271. """
  272. #5/16/05 wregglej 158437 - changed script to use rhn-actions-control
  273. def getAllowRemoteCommands():
  274. return """\
  275. if [ $ALLOW_REMOTE_COMMANDS -eq 1 ] ; then
  276. echo
  277. echo "* setting permissions to allow remote commands"
  278. echo " NOTE: use an activation key to subscribe to the tools"
  279. if [ "$INSTALLER" == zypper ] ; then
  280. echo " channel and zypper update rhncfg-actions"
  281. elif [ "$INSTALLER" == yum ] ; then
  282. echo " channel and yum upgrade rhncfg-actions"
  283. else
  284. echo " channel and up2date rhncfg-actions"
  285. fi
  286. if [ -x "/usr/bin/rhn-actions-control" ] ; then
  287. rhn-actions-control --enable-run
  288. else
  289. echo "Error setting permissions for remote commands."
  290. echo " Please ensure that the activation key subscribes the"
  291. if [ "$INSTALLER" == zypper ] ; then
  292. echo " system to the tools channel and zypper updates rhncfg-actions."
  293. elif [ "$INSTALLER" == yum ] ; then
  294. echo " system to the tools channel and yum updates rhncfg-actions."
  295. else
  296. echo " system to the tools channel and up2dates rhncfg-actions."
  297. fi
  298. exit
  299. fi
  300. fi
  301. """
  302. def getRegistrationSh(productName):
  303. return """\
  304. echo
  305. echo "REGISTRATION"
  306. echo "------------"
  307. # Should have created an activation key or keys on the %s's
  308. # website and edited the value of ACTIVATION_KEYS above.
  309. #
  310. # If you require use of several different activation keys, copy this file and
  311. # change the string as needed.
  312. #
  313. if [ -z "$ACTIVATION_KEYS" ] ; then
  314. echo "*** ERROR: in order to bootstrap RHN clients, an activation key or keys"
  315. echo " must be created in the RHN web user interface, and the"
  316. echo " corresponding key or keys string (XKEY,YKEY,...) must be mapped to"
  317. echo " the ACTIVATION_KEYS variable of this script."
  318. exit 1
  319. fi
  320. if [ $REGISTER_THIS_BOX -eq 1 ] ; then
  321. echo "* registering"
  322. files=""
  323. directories=""
  324. if [ $ALLOW_CONFIG_ACTIONS -eq 1 ] ; then
  325. for i in "/etc/sysconfig/rhn/allowed-actions /etc/sysconfig/rhn/allowed-actions/configfiles"; do
  326. [ -d "$i" ] || (mkdir -p $i && directories="$directories $i")
  327. done
  328. [ -f /etc/sysconfig/rhn/allowed-actions/configfiles/all ] || files="$files /etc/sysconfig/rhn/allowed-actions/configfiles/all"
  329. [ -n "$files" ] && touch $files
  330. fi
  331. if [ -z "$PROFILENAME" ] ; then
  332. profilename_opt=""
  333. else
  334. profilename_opt="--profilename=$PROFILENAME"
  335. fi
  336. /usr/sbin/rhnreg_ks --force --activationkey "$ACTIVATION_KEYS" $profilename_opt
  337. RET="$?"
  338. [ -n "$files" ] && rm -f $files
  339. [ -n "$directories" ] && rmdir $directories
  340. if [ $RET -eq 0 ]; then
  341. echo
  342. echo "*** this system should now be registered, please verify ***"
  343. echo
  344. else
  345. echo
  346. echo "*** Error: Registering the system failed."
  347. echo
  348. exit 1
  349. fi
  350. else
  351. echo "* explicitely not registering"
  352. fi
  353. """ % (productName)
  354. def getUp2dateTheBoxSh(no_up2date=0):
  355. return """\
  356. echo
  357. echo "OTHER ACTIONS"
  358. echo "------------------------------------------------------"
  359. if [ $FULLY_UPDATE_THIS_BOX -eq 1 ] ; then
  360. if [ "$INSTALLER" == zypper ] ; then
  361. echo "zypper --non-interactive up zypper zypp-plugin-spacewalk; rhn-profile-sync; zypper --non-interactive up (conditional)"
  362. elif [ "$INSTALLER" == yum ] ; then
  363. echo "yum -y upgrade yum yum-rhn-plugin; rhn-profile-sync; yum upgrade (conditional)"
  364. else
  365. echo "up2date up2date; up2date -p; up2date -uf (conditional)"
  366. fi
  367. else
  368. if [ "$INSTALLER" == zypper ] ; then
  369. echo "zypper --non-interactive up zypper zypp-plugin-spacewalk; rhn-profile-sync"
  370. elif [ "$INSTALLER" == yum ] ; then
  371. echo "yum -y upgrade yum yum-rhn-plugin; rhn-profile-sync"
  372. else
  373. echo "up2date up2date; up2date -p"
  374. fi
  375. fi
  376. echo "but any post configuration action can be added here. "
  377. echo "------------------------------------------------------"
  378. if [ $FULLY_UPDATE_THIS_BOX -eq 1 ] ; then
  379. echo "* completely updating the box"
  380. else
  381. echo "* ensuring $INSTALLER itself is updated"
  382. fi
  383. if [ "$INSTALLER" == zypper ] ; then
  384. zypper ref -s
  385. zypper --non-interactive up zypper zypp-plugin-spacewalk
  386. if [ -x /usr/sbin/rhn-profile-sync ] ; then
  387. /usr/sbin/rhn-profile-sync
  388. else
  389. echo "Error updating system info in RHN Satellite."
  390. echo " Please ensure that rhn-profile-sync in installed and rerun it."
  391. fi
  392. if [ $FULLY_UPDATE_THIS_BOX -eq 1 ] ; then
  393. zypper --non-interactive up
  394. fi
  395. elif [ "$INSTALLER" == yum ] ; then
  396. /usr/bin/yum -y upgrade yum yum-rhn-plugin
  397. if [ -x /usr/sbin/rhn-profile-sync ] ; then
  398. /usr/sbin/rhn-profile-sync
  399. else
  400. echo "Error updating system info in RHN Satellite."
  401. echo " Please ensure that rhn-profile-sync in installed and rerun it."
  402. fi
  403. if [ $FULLY_UPDATE_THIS_BOX -eq 1 ] ; then
  404. /usr/bin/yum -y upgrade
  405. fi
  406. else
  407. /usr/sbin/up2date up2date
  408. /usr/sbin/up2date -p
  409. if [ $FULLY_UPDATE_THIS_BOX -eq 1 ] ; then
  410. /usr/sbin/up2date -uf
  411. fi
  412. fi
  413. echo "-bootstrap complete-"
  414. """