コード例 #1
0
def main():
    dh = DHCPHelper()
    dhcp_scopes = []
    dhcp_scopes = dh.get_scopes_to_generate()
    print dhcp_scopes
    output_dir = DHCP_CONFIG_OUTPUT_DIRECTORY
    for scope in dhcp_scopes:
        dhcp_scope = scope.task
        try:
            dir = dhcp_scope.split("-")[0]
            output_file = '-'.join(dhcp_scope.split("-")[1:])
            final_destination_file = "%s/%s/%s_generated_hosts.conf" % (output_dir,dir, output_file)
            systems = dh.systems_by_scope(dhcp_scope)
            adapters = []
            for host in systems:
                hostname = host['hostname']
                adapters.append(dh.adapters_by_system_and_scope(hostname, dhcp_scope))
            output_text = DHCPInterface([], adapters).get_hosts()
            try:
                f = open(final_destination_file,"w")
                f.write(output_text)

                sregs = StaticReg.objects.filter(
                    hwadapter_set__keyvalue_set__key='dhcp_scope',
                    hwadapter_set__keyvalue_set__value=dhcp_scope
                )
                # Django doesn't allow DISTINCT ON so we must simulate this in
                # python. There is probably a better way.
                sreg_pks = set()
                distinct_sregs = []
                for sreg in sregs:
                    if sreg.pk in sreg_pks:
                        continue
                    sreg_pks.add(sreg.pk)
                    distinct_sregs.append(sreg)

                print render_sregs(distinct_sregs)

                f.write('\n\n')
                f.write(render_sregs(distinct_sregs))


                f.close()
                print "Wrote config to {0}".format(final_destination_file)
            except IOError:
                pass
            try:
                DHCPFile.objects.filter(dhcp_scope=dhcp_scope).delete()
            except:
                pass
            d = DHCPFile(dhcp_scope=dhcp_scope,file_text=output_text)
            d.save()
            scope.delete()
        except IndexError:
            scope.delete()
    if len(dhcp_scopes) > 0 or always_push_svn:
        os.chdir(output_dir)
        os.system('/usr/bin/svn update')
        os.system('/usr/bin/svn add * --force')
        os.system('/usr/bin/svn commit -m "Autogenerated addition from inventory"')
コード例 #2
0
ファイル: combine_utils.py プロジェクト: rrsc/inventory-1
def get_all_dhcp_for_system(system, wrap_in_html=True):
    kv_output = get_old_dhcp_statements(system)
    sreg_output = render_sregs(system.staticreg_set.all())
    if not (kv_output.strip(" \n") or sreg_output.strip(" \n")):
        return "<span class='no-dhcp-output'>No old DHCP output</span>"

    if not wrap_in_html:
        return kv_output + "\n\n" + sreg_output

    return htmlify_dhcp_output(kv_output, preserve_tabs=True) + htmlify_dhcp_output(sreg_output)
def get_all_dhcp_for_system(system, wrap_in_html=True):
    kv_output = get_old_dhcp_statements(system)
    sreg_output = render_sregs(system.staticreg_set.all())
    if not (kv_output.strip(' \n') or sreg_output.strip(' \n')):
        return "<span class='no-dhcp-output'>No old DHCP output</span>"

    if not wrap_in_html:
        return kv_output + '\n\n' + sreg_output

    return (htmlify_dhcp_output(kv_output, preserve_tabs=True) +
            htmlify_dhcp_output(sreg_output))
def main():
    dh = DHCPHelper()
    dhcp_scopes = []
    dhcp_scopes = dh.get_scopes_to_generate()
    print dhcp_scopes
    output_dir = DHCP_CONFIG_OUTPUT_DIRECTORY
    for scope in dhcp_scopes:
        dhcp_scope = scope.task
        try:
            dir = dhcp_scope.split("-")[0]
            output_file = '-'.join(dhcp_scope.split("-")[1:])
            final_destination_file = "%s/%s/%s_generated_hosts.conf" % (
                output_dir, dir, output_file)
            systems = dh.systems_by_scope(dhcp_scope)
            adapters = []
            for host in systems:
                hostname = host['hostname']
                adapters.append(
                    dh.adapters_by_system_and_scope(hostname, dhcp_scope))
            output_text = DHCPInterface([], adapters).get_hosts()
            try:
                f = open(final_destination_file, "w")
                f.write(output_text)

                sregs = StaticReg.objects.filter(
                    hwadapter_set__keyvalue_set__key='dhcp_scope',
                    hwadapter_set__keyvalue_set__value=dhcp_scope)
                # Django doesn't allow DISTINCT ON so we must simulate this in
                # python. There is probably a better way.
                sreg_pks = set()
                distinct_sregs = []
                for sreg in sregs:
                    if sreg.pk in sreg_pks:
                        continue
                    sreg_pks.add(sreg.pk)
                    distinct_sregs.append(sreg)

                print render_sregs(distinct_sregs)

                f.write('\n\n')
                f.write(render_sregs(distinct_sregs))

                f.close()
                print "Wrote config to {0}".format(final_destination_file)
            except IOError:
                pass
            try:
                DHCPFile.objects.filter(dhcp_scope=dhcp_scope).delete()
            except:
                pass
            d = DHCPFile(dhcp_scope=dhcp_scope, file_text=output_text)
            d.save()
            scope.delete()
        except IndexError:
            scope.delete()
    if len(dhcp_scopes) > 0 or always_push_svn:
        os.chdir(output_dir)
        os.system('/usr/bin/svn update')
        os.system('/usr/bin/svn add * --force')
        os.system(
            '/usr/bin/svn commit -m "Autogenerated addition from inventory"')
コード例 #5
0
ファイル: views.py プロジェクト: Marlburo/inventory
    #scope = get_object_or_404(models.DHCP, pk=id)
    #truth = Truth.objects.get(name='phx-vlan73')
    #scope = TruthKeyValue(truth=truth)
    try:
        d = models.DHCPFile.objects.get(dhcp_scope=dhcp_scope)
        content = d.file_text
    except Exception, e:
        content = """This file has not been stored in inventory yet. 
        To get it stored. Make an innocous change to a hosts key/value entry. 
        An example would be to change the nic name from nic0 to nic1 then back to nic0 again and click save. 
        Once the file gets regenerated, it will be stored here"""
    sregs = StaticReg.objects.filter(
        hwadapter_set__keyvalue_set__key='dhcp_scope',
        hwadapter_set__keyvalue_set__value=dhcp_scope
    )
    content += '\n\n' + render_sregs(sregs)
    output = content.replace("\n","<br />")

    return render_to_response('dhcp/showfile.html', {

        "output": output 
        },
        RequestContext(request))
def create(request):
    if request.method == 'POST':
        form = forms.AddDHCPScopeForm(request.POST)
        if form.is_valid():
            pass
            #s = form.save()
            #return redirect('/dhcp/show/')
    else:
def showfile(request, dhcp_scope):
    #scope = get_object_or_404(models.DHCP, pk=id)
    #truth = Truth.objects.get(name='phx-vlan73')
    #scope = TruthKeyValue(truth=truth)
    try:
        d = models.DHCPFile.objects.get(dhcp_scope=dhcp_scope)
        content = d.file_text
    except Exception, e:
        content = """This file has not been stored in inventory yet. 
        To get it stored. Make an innocous change to a hosts key/value entry. 
        An example would be to change the nic name from nic0 to nic1 then back to nic0 again and click save. 
        Once the file gets regenerated, it will be stored here"""
    sregs = StaticReg.objects.filter(
        hwadapter_set__keyvalue_set__key='dhcp_scope',
        hwadapter_set__keyvalue_set__value=dhcp_scope)
    content += '\n\n' + render_sregs(sregs)
    output = content.replace("\n", "<br />")

    return render_to_response('dhcp/showfile.html', {"output": output},
                              RequestContext(request))


def create(request):
    if request.method == 'POST':
        form = forms.AddDHCPScopeForm(request.POST)
        if form.is_valid():
            pass
            #s = form.save()
            #return redirect('/dhcp/show/')
    else:
        form = forms.AddDHCPScopeForm()