summaryrefslogtreecommitdiffstats
path: root/puppet-host-package
blob: 195a1df46bd29c38de34c2adc908291b8797eab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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)