Example #1
0
 def result_fact(self, host, result):
     facts = result.get('ansible_facts', {})
     arch = facts.get('ansible_architecture', '')
     mem = facts.get('ansible_memtotal_mb', 0)
     cpu = facts.get('ansible_processor_vcpus', 0)
     os = facts.get('ansible_os_family', '')
     ip = facts.get('ansible_default_ipv4', {}).get('address', '')
     name = facts.get('ansible_nodename', host)
     Host.create_or_replace(name, ip, arch, os, mem, cpu)
Example #2
0
 def handle(self, *args, **options):
     (options, args) = parser.parse_args() 
     file_path = os.path.abspath(options.iplist)
     ip_list = open(file_path)
     for ip in ip_list:
         #print ip,
         ip = ip.strip('\n')
         h = Host(name=options.name, ip_in=ip, ip_out=options.ip_out, model=options.model, image=options.image, cloudandservice_id=options.cloudandservice, service_id=options.service, type=options.type, status=options.status)
         #h = Host(name=options.name, ip_in=ip,  internetdatacenter_id=options.internetdatacenter, service_id=options.service, type=options.type, status=options.status)
         h.save()     
Example #3
0
 def handle(self, namespace):
     origin = getOrigin(namespace.origin)
     for host in namespace.host:
         host = host.lower()
         if self.checkHost(host):
             raise HostinfoException("Host %s already exists" % host)
         if host[0] in ('-',):
             raise HostinfoException(
                 "Host begins with a forbidden character ('%s') - not adding" % host[0])
         hobj = Host(hostname=host, origin=origin)
         hobj.save()
     return None, 0
 def handle(self, namespace):
     origin = getOrigin(namespace.origin)
     for host in namespace.host:
         host = host.lower()
         if self.checkHost(host):
             raise HostinfoException("Host %s already exists" % host)
         if host[0] in ('-', ):
             raise HostinfoException(
                 "Host begins with a forbidden character ('%s') - not adding"
                 % host[0])
         hobj = Host(hostname=host, origin=origin)
         hobj.save()
     return None, 0
Example #5
0
 def handle(self, *args, **options):
     (options, args) = parser.parse_args()
     file_path = os.path.abspath(options.iplist)
     ip_list = open(file_path)
     for ip in ip_list:
         #print ip,
         ip = ip.strip('\n')
         h = Host(name=options.name,
                  ip_in=ip,
                  ip_out=options.ip_out,
                  internetdatacenter_id=options.internetdatacenter,
                  service_id=options.service,
                  type=options.type,
                  status=options.status)
         #h = Host(name=options.name, ip_in=ip,  internetdatacenter_id=options.internetdatacenter, service_id=options.service, type=options.type, status=options.status)
         h.save()
         h.hostcomment_set.create(comment=options.comment)
Example #6
0
 def handle(self, *args, **options):
     (options, args) = parser.parse_args()
     file_path = os.path.abspath(options.iplist)
     ip_list = open(file_path)
     for ip in ip_list:
         #print ip,
         ip = ip.strip('\n')
         h = Host(name=options.name,
                  ip_in=ip,
                  ip_out=options.ip_out,
                  model=options.model,
                  image=options.image,
                  cloudandservice_id=options.cloudandservice,
                  service_id=options.service,
                  type=options.type,
                  status=options.status)
         #h = Host(name=options.name, ip_in=ip,  internetdatacenter_id=options.internetdatacenter, service_id=options.service, type=options.type, status=options.status)
         h.save()
Example #7
0
 def handle(self, *args, **options):
     (options, args) = parser.parse_args()
     file_path = os.path.abspath(options.iplist)
     ip_list = open(file_path)
     for ip in ip_list:
         # print ip,
         ip = ip.strip("\n")
         h = Host(
             name=options.name,
             ip_in=ip,
             ip_out=options.ip_out,
             internetdatacenter_id=options.internetdatacenter,
             service_id=options.service,
             type=options.type,
             status=options.status,
         )
         # h = Host(name=options.name, ip_in=ip,  internetdatacenter_id=options.internetdatacenter, service_id=options.service, type=options.type, status=options.status)
         h.save()
         h.hostcomment_set.create(comment=options.comment)
    def handleHost(self, hosttree):
        """
        We can't load dates (created/modified) of elements because these are
        set automatically by Django

          <host docpage="None"  origin="explorer2hostinfo.py by w86765"  modified="2008-03-20" created="2008-03-20" >
            <hostname>zone_161.117.101.190</hostname>
            <data>
              <confitem key="os" origin="w86765" modified="2008-08-19" created="2008-08-19">solaris</confitem>
              <confitem key="type" origin="..." modified="2008-07-02" created="2008-03-20">virtual</confitem>
              <confitem key="virtualmaster" origin="..." modified="2008-03-20" created="2008-03-20">idmsvrqv01d</confitem>
              <confitem key="zonename" origin="..." modified="2008-03-20" created="2008-03-20">app04</confitem>
            </data>
          </host>
        """
        hostname = hosttree.find('hostname').text
        self.verbose(hostname)
        try:
            host = Host.objects.get(hostname=hostname)
        except ObjectDoesNotExist:
            host = Host(hostname=hostname,
                        docpage=hosttree.attrib.get('docpage', None),
                        origin=hosttree.attrib.get('origin',
                                                   'unknown - import'))
            self.verbose("New host %s" % repr(host))
            if not self.namespace.kiddingFlag:
                host.save()

        for data in hosttree.find('data').findall('confitem'):
            key = data.attrib['key']
            if 'origin' in data.attrib:
                origin = data.attrib['origin']
            else:
                origin = 'unknown'
            value = data.text
            try:
                self.handleValue(host, key, origin, value)
            except RestrictedValueException:
                sys.stderr.write(
                    "Trying to change a restricted value: %s:%s=%s - ignoring\n"
                    % (hostname, key, value))
Example #9
0
class Command(BaseCommand):
    def handle(self, *args, **options):
        a = '11'

    arch = "x86_64"
    mem = "100"
    cpu = "24"
    os = "Centos"
    ip = "127.0.0.1"
    p_ip = "127.0.0.1"
    name = "test"
    Host.create_or_replace(name, ip, arch, os, mem, cpu, p_ip)
Example #10
0
    def handleHost(self, hosttree):
        """
        We can't load dates (created/modified) of elements because these are
        set automatically by Django

          <host docpage="None"  origin="explorer2hostinfo.py by w86765"  modified="2008-03-20" created="2008-03-20" >
            <hostname>zone_161.117.101.190</hostname>
            <data>
              <confitem key="os" origin="w86765" modified="2008-08-19" created="2008-08-19">solaris</confitem>
              <confitem key="type" origin="..." modified="2008-07-02" created="2008-03-20">virtual</confitem>
              <confitem key="virtualmaster" origin="..." modified="2008-03-20" created="2008-03-20">idmsvrqv01d</confitem>
              <confitem key="zonename" origin="..." modified="2008-03-20" created="2008-03-20">app04</confitem>
            </data>
          </host>
        """
        hostname = hosttree.find('hostname').text
        self.verbose(hostname)
        try:
            host = Host.objects.get(hostname=hostname)
        except ObjectDoesNotExist:
            host = Host(
                hostname=hostname,
                docpage=hosttree.attrib.get('docpage', None),
                origin=hosttree.attrib.get('origin', 'unknown - import')
                )
            self.verbose("New host %s" % repr(host))
            if not self.namespace.kiddingFlag:
                host.save()

        for data in hosttree.find('data').findall('confitem'):
            key = data.attrib['key']
            if 'origin' in data.attrib:
                origin = data.attrib['origin']
            else:
                origin = 'unknown'
            value = data.text
            try:
                self.handleValue(host, key, origin, value)
            except RestrictedValueException:
                sys.stderr.write("Trying to change a restricted value: %s:%s=%s - ignoring\n" % (hostname, key, value))
Example #11
0
    def handle(self, *args, **options):
        Options = namedtuple('Options', 
                 ['connection', 'module_path', 'forks', 'become', 'become_user', 'become_method', 'check', 'diff'])
        options = Options(connection='smart', 
                  module_path=[], forks=10, become=None, become_user=None,  become_method=None, check=False, diff=False)


        callback = ModelResultsCollector()
        loader = DataLoader()
        inventory = InventoryManager(loader=loader, sources=["etc/hosts/"])
        variable_manager = VariableManager(loader=loader, inventory=inventory)
        passwords = {}
        play_source = {
            'name' : 'fact',
            'hosts' : 'all',
            'gather_facts' : 'no',
            'tasks' : [
                {
                    'name' : 'fact',
                    'setup' : ''
                }
            ],
        }

    
        play = Play().load(play_source, variable_manager=variable_manager, loader=loader)    
        tqm = TaskQueueManager(
           inventory=inventory,
           variable_manager=variable_manager,
           loader=loader,
           options=options,
           passwords=passwords,
           stdout_callback=callback,
           )

        result = tqm.run(play)
        result_raw = {'success':{},'failed':{},'unreachable':{}}
        for host,result in callback.host_ok.items():
            result_raw['success'][host] = result._result
        for host,result in callback.host_failed.items():
            result_raw['failed'][host] = result._result
        for host,result in callback.host_unreachable.items():
            result_raw['unreachable'][host] = result._result
        

        #js = json.dumps(result_raw, sort_keys=False, indent=4)
        sucess = list(result_raw['success'].keys())
        for i in sucess:
            dic=result_raw['success'][i]["ansible_facts"]
            arch=dic.get('ansible_architecture','')
            mem=dic.get('ansible_memtotal_mb',0)
            cpu=dic.get('ansible_processor_vcpus',0)
            os=dic.get('ansible_os_family','')
            ip=dic.get('ansible_default_ipv4',{}).get('address', '')
            p_ip=i              #公网IP
            name=dic.get('ansible_nodename',i)
            os_v = dic.get('ansible_distribution_version','')
            Host.create_or_replace(name, ip, arch, os, mem, cpu,p_ip,os_v)
           

        fail = list(result_raw['failed'].keys())
        for i in fail:
            arch=''
            mem=0
            cpu=0
            os=''
            ip=i
            p_ip=i
            name=''
            os_v=''
            Host.create_or_replace(name, ip, arch, os, mem, cpu,p_ip,os_v) 


        unreachable = list(result_raw['unreachable'].keys())
        for i in unreachable:
            arch=''
            mem=0
            cpu=0
            os=''
            ip=i
            p_ip=i
            name=''
            os_v=''
            Host.create_or_replace(name, ip, arch, os, mem, cpu,p_ip,os_v)