summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTodd Zullinger <tmz@pobox.com>2008-11-27 23:09:39 -0500
committerTodd Zullinger <tmz@pobox.com>2008-11-28 00:05:31 -0500
commitdeeb9f695dde6f5232524cc78b8e1be1657ff0a0 (patch)
tree577e92127765caeb673acd8620b12fb24df7ac86
parentc821aa3779f1fe068a5091c35985c545b46afead (diff)
downloadpuppet-host-package-deeb9f695dde6f5232524cc78b8e1be1657ff0a0.zip
puppet-host-package-deeb9f695dde6f5232524cc78b8e1be1657ff0a0.tar.gz
puppet-host-package-deeb9f695dde6f5232524cc78b8e1be1657ff0a0.tar.xz
Split puppethost.py into script and module
This will make it easier to create a tarball distribution using distutils.
-rwxr-xr-xpuppet-host-package108
-rw-r--r--[-rwxr-xr-x]puppethost.py89
2 files changed, 108 insertions, 89 deletions
diff --git a/puppet-host-package b/puppet-host-package
new file mode 100755
index 0000000..195a1df
--- /dev/null
+++ b/puppet-host-package
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2008 Todd Zullinger <tmz@pobox.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+"""Create a host-specific package for bootstrapping a puppet."""
+
+import os
+import glob
+import optparse
+import puppethost
+
+def _main():
+ usage = '%prog [options] hostname [hostname ...]'
+ parser = optparse.OptionParser(usage=usage)
+ parser.defaults = puppethost.defaults
+ parser.add_option('-a', '--all', dest='allcerts', action='store_true',
+ help='Create packages for all signed certificates')
+ parser.add_option('-d', '--domain', dest='domain',
+ help='Domain append to non fqdn hostnames [%default]')
+ parser.add_option('-f', '--force', dest='force', action='store_true',
+ help='Overwrite existing certs, tarballs, and packages')
+ parser.add_option('--force-cert', dest='force_cert', action='store_true',
+ help='Overwrite existing certs')
+ parser.add_option('--force-tarball', dest='force_tarball',
+ action='store_true', help='Overwrite existing tarballs')
+ parser.add_option('--force-package', dest='force_package',
+ action='store_true', help='Overwrite existing packages')
+ parser.add_option('-r', '--rpmdir', dest='rpmdir', metavar='dir',
+ help='Directory where packages are stored [%default]')
+ parser.add_option('--release', dest='release', metavar='num',
+ help='Package release number [%default]')
+ parser.add_option('-s', '--ssldir', dest='ssldir', metavar='dir',
+ help='Directory where ssl certs are stored [%default]')
+ # FIXME improve the help string
+ parser.add_option('-S', '--dest-ssldir', dest='destssldir', metavar='dir',
+ help='Directory where ssl certs are packaged')
+ parser.add_option('-t', '--template', dest='template', metavar='file',
+ help='RPM spec file template [%default]')
+ parser.add_option('-v', '--verbose', dest='verbose', action='count',
+ help='Be verbose (may be used more than once)')
+ opts, args = parser.parse_args()
+
+ if opts.force:
+ opts.force_cert = True
+ opts.force_tarball = True
+ opts.force_package = True
+
+ if opts.allcerts:
+ args = []
+ for cert in glob.glob('%s/ca/signed/*.pem' % opts.ssldir):
+ basename = os.path.basename(cert)
+ # we need the private key as well as the cert
+ if os.path.exists('%s/private_keys/%s' % (opts.ssldir, basename)):
+ args.append(os.path.splitext(basename)[0])
+ args.sort()
+
+ if not args:
+ raise SystemExit(parser.print_usage())
+
+ opts.rpmdir = os.path.expanduser(os.path.abspath(opts.rpmdir))
+ opts.ssldir = os.path.expanduser(os.path.abspath(opts.ssldir))
+ for d in [opts.rpmdir, opts.ssldir]:
+ if not os.path.isdir(d):
+ raise SystemExit('%s does not exist (or is not a directory)' % d)
+
+ if not opts.destssldir:
+ opts.destssldir = opts.ssldir
+ else:
+ opts.destssldir = os.path.expanduser(os.path.abspath(opts.destssldir))
+
+ opts.template = os.path.expanduser(opts.template % opts.__dict__)
+ if not os.path.isfile(opts.template):
+ raise SystemExit('Template file (%s) does not exist' % opts.template)
+
+ for hostname in args:
+ if opts.verbose:
+ print '\nCreating a host package for %s' % hostname
+
+ host = puppethost.PuppetHost(hostname, opts.__dict__)
+
+ if not os.path.exists(host.files['cert']) or opts.force_cert:
+ try: host.gencert()
+ except puppethost.PuppetHostError, error:
+ print error
+ continue
+
+ try: host.package()
+ except Exception, error:
+ print error
+ continue
+
+if __name__ == '__main__':
+ try: _main()
+ except KeyboardInterrupt:
+ raise SystemExit(1)
diff --git a/puppethost.py b/puppethost.py
index ddd1d4a..a54b9cb 100755..100644
--- a/puppethost.py
+++ b/puppethost.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python
-
# Copyright (C) 2008 Todd Zullinger <tmz@pobox.com>
#
# This program is free software: you can redistribute it and/or modify
@@ -19,14 +17,12 @@
import os
import sys
-import glob
import time
import shutil
import socket
import OpenSSL
import tarfile
import commands
-import optparse
import tempfile
defaults = {
@@ -259,88 +255,3 @@ class PuppetHost(object):
else:
self._version = time.strftime(format)
return self._version
-
-def _main():
- usage = '%prog [options] hostname [hostname ...]'
- parser = optparse.OptionParser(usage=usage)
- parser.defaults = defaults
- parser.add_option('-a', '--all', dest='allcerts', action='store_true',
- help='Create packages for all signed certificates')
- parser.add_option('-d', '--domain', dest='domain',
- help='Domain append to non fqdn hostnames [%default]')
- parser.add_option('-f', '--force', dest='force', action='store_true',
- help='Overwrite existing certs, tarballs, and packages')
- parser.add_option('--force-cert', dest='force_cert', action='store_true',
- help='Overwrite existing certs')
- parser.add_option('--force-tarball', dest='force_tarball',
- action='store_true', help='Overwrite existing tarballs')
- parser.add_option('--force-package', dest='force_package',
- action='store_true', help='Overwrite existing packages')
- parser.add_option('-r', '--rpmdir', dest='rpmdir', metavar='dir',
- help='Directory where packages are stored [%default]')
- parser.add_option('--release', dest='release', metavar='num',
- help='Package release number [%default]')
- parser.add_option('-s', '--ssldir', dest='ssldir', metavar='dir',
- help='Directory where ssl certs are stored [%default]')
- # FIXME improve the help string
- parser.add_option('-S', '--dest-ssldir', dest='destssldir', metavar='dir',
- help='Directory where ssl certs are packaged')
- parser.add_option('-t', '--template', dest='template', metavar='file',
- help='RPM spec file template [%default]')
- parser.add_option('-v', '--verbose', dest='verbose', action='count',
- help='Be verbose (may be used more than once)')
- opts, args = parser.parse_args()
-
- if opts.force:
- opts.force_cert = True
- opts.force_tarball = True
- opts.force_package = True
-
- if opts.allcerts:
- args = []
- for cert in glob.glob('%s/ca/signed/*.pem' % opts.ssldir):
- basename = os.path.basename(cert)
- # we need the private key as well as the cert
- if os.path.exists('%s/private_keys/%s' % (opts.ssldir, basename)):
- args.append(os.path.splitext(basename)[0])
- args.sort()
-
- if not args:
- raise SystemExit(parser.print_usage())
-
- opts.rpmdir = os.path.expanduser(os.path.abspath(opts.rpmdir))
- opts.ssldir = os.path.expanduser(os.path.abspath(opts.ssldir))
- for d in [opts.rpmdir, opts.ssldir]:
- if not os.path.isdir(d):
- raise SystemExit('%s does not exist (or is not a directory)' % d)
-
- if not opts.destssldir:
- opts.destssldir = opts.ssldir
- else:
- opts.destssldir = os.path.expanduser(os.path.abspath(opts.destssldir))
-
- opts.template = os.path.expanduser(opts.template % opts.__dict__)
- if not os.path.isfile(opts.template):
- raise SystemExit('Template file (%s) does not exist' % opts.template)
-
- for hostname in args:
- if opts.verbose:
- print '\nCreating a host package for %s' % hostname
-
- host = PuppetHost(hostname, opts.__dict__)
-
- if not os.path.exists(host.files['cert']) or opts.force_cert:
- try: host.gencert()
- except PuppetHostError, error:
- print error
- continue
-
- try: host.package()
- except Exception, error:
- print error
- continue
-
-if __name__ == '__main__':
- try: _main()
- except KeyboardInterrupt:
- raise SystemExit(1)