Example #1
0
 def host_by_mac(self, fqdn, address):
     """return mac address, hostname and domainname"""
     if len(address) == 12:
         pass
     elif not re.match('([a-fA-F0-9]{2}[:|\-]?){6}', address):
         return
     return self.return_host(fqdn, seedlib.format_address(address))
Example #2
0
def main():
    """main application, this function won't called when used as a module"""
    base_dir = os.path.dirname(os.path.abspath(__file__))

    for path in sp_paths:
        if not sp_paths[path].startswith('/'):
            sp_paths[path] = os.path.join(base_dir, sp_paths[path])

    parser = optparse.OptionParser(prog='seedbank', version=__version__)

    parser.set_description('seedbank - Debian/Ubuntu netboot installations the way it\'s meant to be\
                            (c) 2009-2011 Jasper Poppe <*****@*****.**>')

    parser.set_usage('%prog [-e]|[-M macaddress] [-r recipe] [-o overlay] [-m manifest] [-s seed] [-c override] <fqdn> <release>|-l|-p')

    parser.add_option('-M', '--macaddress', dest='macaddress', help='use mac address as pxe filename')

    parser.add_option('-e', '--externalhost', dest='externalhost', help='get the node information from an external source', action='store_true')

    parser.add_option('-l', '--list', dest='list', help='list available releases, (disk) recipes and manifests', action='store_true')

    parser.add_option('-r', '--recipe', dest='recipes', help='choose (disk) recipe(s)', action='append')

    parser.add_option('-m', '--manifest', dest='manifests', help='choose Puppet manifest(s) to apply after the installation', action='append')

    parser.add_option('-o', '--overlay', dest='overlay', help='choose an overlay from which files/templates should be copied')

    parser.add_option('-u', '--udeb', dest='udebs', help='choose custom udebs to use in the installer', action='append')

    parser.add_option('-s', '--seed', dest='seed', help='override default seed file (default: distribution name)')

    parser.add_option('-c', '--custom', dest='custom', help='override setting(s) from settings.py (dict:key:\'new_value\')', action='append')

    parser.add_option('-p', '--listpxefiles', dest='pxefiles', help='list all pxe host configs', action='store_true')

    (opts, args) = parser.parse_args()

    if opts.custom:
        error = override_settings(opts.custom)
        if error:
            parser.error(error)

    if opts.list:
        list_releases()
        sys.exit(0)
    elif opts.pxefiles:
        list_pxe_files()
        sys.exit(0)
    elif not args:
        parser.print_help()
        sys.exit(0)
    elif not len(args) == 2:
        parser.error('you need to specify 2 arguments (fqdn|ip and a release), use -h for help')
        
    target, release = args
    process = ProcessInput()

    if process.validate_release(release):
        parser.error('%s is an unknown release (use -l to list available releases)' % release)

    if opts.macaddress:
        try:
            address, hostname, domainname = process.host_by_mac(target, opts.macaddress)
        except Exception:
            parser.error('"%s" is not a valid MAC address' % opts.macaddress)
    elif opts.externalhost:
        if external_nodes['provider']:
            hostname, domainname = seedlib.get_domain(args[0])
        else:
            parser.error('a provider needs to be specified in settings.py '
                            'in the external_nodes section')
    else:
        try:
            address, hostname, domainname = process.host_by_ip(target)
        except:
            try:
                address, hostname, domainname = process.host_by_name(target)
            except:
                parser.error('reverse DNS lookup for "%s" failed' % target)

    host_dict = {'hostname': hostname, 'domainname': domainname}

    if opts.externalhost:
        if external_nodes['provider']:
            external = ExternalNodes(external_nodes['provider'], host_dict)
            external_vars = external.gather()
            if not external_vars:
                parser.error('failed to gather information via "%s"' % external_nodes['provider'])

        address = external_vars[external_nodes['address']]
        address = seedlib.format_address(address)
    else:
        external_vars = ''

    pxe = ManagePxe()

    if not process.validate_seed(opts.seed):
        parser.error('seed file "%s/%s.seed" does not exist' % (sp_paths['seeds'], opts.seed))

    if opts.recipes and not process.validate_recipes(opts.recipes):
        parser.error('"%s" is/are unknown recipe(s) (use -l to list available recipes)' % ', '.join(opts.recipes))

    if opts.manifests and process.validate_manifests(opts.manifests):
        manifest = process.validate_manifests(opts.manifests)
        parser.error('"%s" is an unknown manifest (use -l to list available manifests)' % (manifest))

    if opts.udebs and process.validate_udebs(opts.udebs):
        udeb = process.validate_udebs(opts.udebs)
        parser.error('"%s" is an unknown udeb (use -l to list available udebs)' % (udeb))

    if opts.overlay and process.validate_overlay(opts.overlay):
        overlay = process.validate_overlay(opts.overlay)
        parser.error('"%s" is an unknown overlay (use -l to list available overlays)' % (overlay))

    contents = pxe.create(hostname, domainname, address, release, opts.seed,
                            opts.recipes, opts.manifests, opts.overlay, opts.udebs, external_vars)
    if not contents:
        parser.error('failed to generate pxe file')

    if not pxe.write(address, contents):
        parser.error('can not write pxe file (hint: do you have the right permissions?')

    hooks = Hooks(host_dict)
    hooks.run(hooks_enable)