예제 #1
0
    def luna_inventory(self):
        osimage_suffix = ".osimages.luna"
        group_suffix = ".groups.luna"
        inventory = {}
        inventory['_meta'] = { 'hostvars': {}}
        osimages = {'hosts':[],'vars': {'ansible_connection': 'lchroot' }}
        for osimage in luna.list('osimage'):
            #osimages['hosts'].append(luna.OsImage(osimage).get('path'))
            osimages['hosts'].append(osimage + osimage_suffix)
            osimage_path = luna.OsImage(osimage).get('path')
            inventory['_meta']['hostvars'][osimage + osimage_suffix]= {
                'ansible_host': osimage,
            }

        inventory['osimages.luna'] = osimages

        nodes = {}
        inventory['groups.luna'] = {'hosts': []}

        for g in luna.list('group'):
            group = luna.Group(g)
            hosts = []
            nodes = group.list_nodes()

            for node_name in nodes:
                node = luna.Node(node_name)
                hosts.append(node_name)
                inventory['_meta']['hostvars'][node.show()['name']]={
                    "bmc_ip":node.get_ip('BMC',version=4)}

            inventory[g + group_suffix] = {'hosts': hosts}
            inventory['groups.luna']['hosts'].extend(hosts)

        return inventory
예제 #2
0
    def luna_inventory(self):
        inventory = {}
        osimages = {'hosts': [], 'vars': {'ansible_connection': 'lchroot'}}
        for osimage in luna.list('osimage'):
            #osimages['hosts'].append(luna.OsImage(osimage).get('path'))
            osimages['hosts'].append(osimage)

        inventory['osimages'] = osimages
        nodes = {}
        for n in luna.list('node'):
            node = luna.Node(n).show()
            group = node['group'].replace("[", "").replace("]", "")
            if group in inventory:
                inventory[group]['hosts'].append(node['name'])
            else:
                inventory[group] = {'hosts': [node['name']], 'vars': {}}
        return inventory
예제 #3
0
    def get(self):
        step = self.get_argument('step')

        if step == 'boot':
            nodes = luna.list('node')
            self.render("templ_ipxe.cfg", server_ip = self.server_ip, server_port = self.server_port, nodes = nodes)

        if step == 'discovery':
            try:
                hwdata = self.get_argument('hwdata')
            except:
                hwdata = None
            if not bool(hwdata):
                self.send_error(400) #  Bad Request
                return
            try:
                req_nodename = self.get_argument('node')
            except:
                req_nodename = None
            macs = set(hwdata.split('|'))
            # enter node name manualy from ipxe
            if req_nodename:
                self.app_logger.info("Node '{}' was chosen in iPXE".format(req_nodename))
                try:
                    node = luna.Node(name = req_nodename, mongo_db = self.mongo)
                except:
                    self.app_logger.error("No such node configured in DB. '{}'".format(req_nodename))
                    self.send_error(400)
                    return
                mac = None
                for mac in macs:
                    if bool(mac):
                        mac = str(mac.lower())
                        self.app_logger.info("Node '{}' trying to set '{}' as mac".format(req_nodename, mac))
                        if node.set_mac(mac):
                            break
                        self.app_logger.error("MAC: '{}' looks wrong.".format(mac))
            # need to find node fo given macs.
            # first step - trying to find in know macs
            found_node_dbref = None
            for mac in macs:
                if not bool(mac):
                    continue
                mac = mac.lower()
                try:
                    found_node_dbref = self.mongo['mac'].find_one({'mac': mac}, {'_id': 0, 'node': 1})['node']
                except:
                    #self.app_logger.error("Mac record exists, but no node configured for given mac '{}'".format(mac))
                    #self.send_error(404)
                    #return
                    continue
                if bool(found_node_dbref):
                    break

            # second step. now try to find in learned switch macs if we have switch/port configured
            if not bool(found_node_dbref):
                mac_from_cache = None
                for mac in macs:
                    #mac_cursor = self.mongo['switch_mac'].find({'mac': mac}).sort([('updated', -1)]).limit(1)
                    mac_cursor = self.mongo['switch_mac'].find({'mac': mac})
                    # first search mac in switch_mac using portnames like 'Gi2/0/26'
                    for elem in mac_cursor:
                        switch_id = elem['switch_id']
                        portname = elem['portname']
                        try:
                            node_id = self.mongo['node'].find_one({'switch': DBRef('switch', switch_id), 'port': portname}, {})['_id']
                            mac_from_cache = mac
                        except:
                            node_id = None
                            mac_from_cache = None
                        if mac_from_cache:
                            break
                    if mac_from_cache:
                        break
                    mac_cursor = self.mongo['switch_mac'].find({'mac': mac})
                    # now search mac in switch_mac using portnumbers
                    for elem in mac_cursor:
                        switch_id = elem['switch_id']
                        port = elem['port']
                        try:
                            node_id = self.mongo['node'].find_one({'switch': DBRef('switch', switch_id), 'port': port}, {})['_id']
                            mac_from_cache = mac
                        except:
                            node_id = None
                            mac_from_cache = None
                        if mac_from_cache:
                            break
                    if mac_from_cache:
                        break
                if not bool(mac_from_cache):
                    self.app_logger.info("Cannot find '{}' in learned macs.".format("', '".join([mac for mac in macs])))
                    # did not find in learned macs
                    self.send_error(404)
                    return
                # here we should have node_id and mac_from_cache
                try:
                    node = luna.Node(id = node_id, mongo_db = self.mongo)
                    set_mac_node(mac_from_cache, node.DBRef)
                    found_node_dbref = node.DBRef
                except:
                    # should not be here
                    self.app_logger.info("Cannot create node object for '{}' and '{}'".format(found_name_from_learned, self.mongo))
                    self.send_error(404)
                    return
            # here we should have found_node_dbref
            try:
                node = luna.Node(id = found_node_dbref.id, mongo_db = self.mongo)
            except:
                # should not be here
                self.app_logger.info("Cannot create node object for '{}' and '{}'".format(found_node_dbref, self.mongo))
                self.send_error(404)
                return
            # found node finally
            #http_path = "http://" + self.server_ip + ":" + str(self.server_port) + "/boot/"
            boot_params = node.boot_params
            if not boot_params['boot_if']:
                boot_params['ifcfg'] = 'dhcp'
            else:
                boot_params['ifcfg'] = boot_params['boot_if'] + ":" + boot_params['ip'] + "/" + str(boot_params['net_prefix'])
            boot_params['delay'] = 10
            boot_params['server_ip'] = self.server_ip
            boot_params['server_port'] = self.server_port
            self.render("templ_nodeboot.cfg", p = boot_params)
        if step == 'install':
            try:
                node_name = self.get_argument('node')
            except:
                self.app_logger.error("No nodename for install step specified.")
                #return self.send_error(400)
                self.send_error(400)
                return
            try:
                node = luna.Node(name = node_name, mongo_db = self.mongo)
            except:
                self.app_logger.error("No such node for install step found '{}'.".format(node_name))
                #return self.send_error(400)
                self.send_error(400)
                return
                #self.finish()
            install_params = node.install_params
            if not bool(install_params['torrent']):
                #return self.send_error(404)
                self.send_error(404)
                return
                #self.finish()
            self.render("templ_install.cfg", p = install_params, server_ip = self.server_ip, server_port = self.server_port,)
예제 #4
0
    def get(self):
        step = self.get_argument('step')

        if step == 'boot':
            nodes = luna.list('node')
            p = {
                'protocol': self.protocol,
                'server_ip': self.server_ip,
                'server_port': self.server_port,
                'nodes': nodes
            }

            self.render("templ_ipxe.cfg", p=p)

        elif step == 'discovery':
            hwdata = self.get_argument('hwdata', default=None)
            if not hwdata:
                self.send_error(400)  # Bad Request
                return

            macs = set(hwdata.split('|'))

            # Node name selected manualy in ipxe
            req_nodename = self.get_argument('node', default=None)
            if req_nodename:
                self.log.info("Node '{}' was chosen in iPXE"
                              .format(req_nodename))
                req_nodename = str(req_nodename)
                try:
                    node = luna.Node(name=req_nodename, mongo_db=self.mongo)
                except:
                    self.log.error("No such node '{}' exists"
                                   .format(req_nodename))
                    self.send_error(400)
                    return

                mac = None
                for mac in macs:
                    if mac:
                        mac = str(mac.lower())
                        self.log.info("Node '{}' trying to set '{}' as mac"
                                      .format(req_nodename, mac))

                        if node.set_mac(mac):
                            node.update_status('boot.mac_assigned')
                            break

                        self.log.error("MAC: '{}' looks wrong.".format(mac))

            # need to find node for given macs.
            # first step - trying to find in known macs
            found_node = None
            for mac in macs:
                if not mac:
                    continue

                mac = mac.lower()
                try:
                    found_node = self.mongo['mac'].find_one({'mac': mac}, {'_id': 0, 'node': 1})['node']
                except:
                    continue

                if found_node:
                    break

            # second step. now try to find in learned switch macs
            # if we have switch/port configured
            if not found_node:
                mac_from_cache = None

                for mac in macs:
                    mac_cursor = self.mongo['switch_mac'].find({'mac': mac})

                    # first search mac in switch_mac using
                    # portnames like 'Gi2/0/26'
                    for elem in mac_cursor:
                        switch_id = elem['switch_id']
                        portname = elem['portname']

                        try:
                            node_id = self.mongo['node'].find_one({'switch': DBRef('switch', switch_id), 'port': portname}, {})['_id']
                            mac_from_cache = mac
                        except:
                            continue

                        if mac_from_cache:
                            break

                    if mac_from_cache:
                        break

                    # now search mac in switch_mac using portnumbers
                    for elem in mac_cursor:
                        switch_id = elem['switch_id']
                        port = elem['port']

                        try:
                            node_id = self.mongo['node'].find_one({'switch': DBRef('switch', switch_id), 'port': port}, {})['_id']
                            mac_from_cache = mac
                        except:
                            continue

                        if mac_from_cache:
                            break

                    if mac_from_cache:
                        break

                if not mac_from_cache:
                    self.log.info("Cannot find '{}' in learned macs."
                                  .format("', '".join([mac for mac in macs])))
                    # did not find in learned macs
                    self.send_error(404)
                    return

                # here we should have node_id and mac_from_cache
                try:
                    node = luna.Node(id=node_id, mongo_db=self.mongo)
                    utils.helpers.set_mac_node(mac_from_cache, node.DBRef)
                    found_node = node.DBRef
                except:
                    # should not be here
                    self.log.info("Cannot create node object for '{}' and '{}'"
                                  .format(found_name_from_learned, self.mongo))
                    self.send_error(404)
                    return

            # here we should have found_node
            try:
                node = luna.Node(id=found_node.id, mongo_db=self.mongo)
            except:
                # should not be here
                self.log.info("Cannot create node object for '{}' and '{}'"
                              .format(found_node, self.mongo))
                self.send_error(404)
                return

            # found node finally
            boot_params = node.boot_params

            boot_params['server_ip'] = self.server_ip
            boot_params['server_port'] = self.server_port
            boot_params['protocol'] = self.protocol

            boot_type = self.get_argument('type', default='ipxe')
            if boot_type == 'ipxe':
                node.update_status('boot.request')
                self.render("templ_nodeboot.cfg", p=boot_params)

            elif boot_type == 'syslinux':
                node.update_status('boot.request')
                self.render("templ_nodeboot_syslinux.cfg", p=boot_params)

            else:
                self.send_error(404)

        elif step == 'install':
            node_name = self.get_argument('node', default=None)
            if not node_name:
                self.log.error("Node name must be provided")
                self.send_error(400)
                return

            node_name = str(node_name)

            try:
                node = luna.Node(name=node_name, mongo_db=self.mongo)
            except:
                self.log.error("No such node '{}' exists".format(node_name))
                self.send_error(400)
                return

            status = self.get_argument('status', default='')

            if status:
                node.update_status(status)
                self.finish()
                return

            install_params = node.install_params
            if not install_params['torrent']:
                self.send_error(404)
                return

            install_params['protocol'] = self.protocol
            install_params['server_ip'] = self.server_ip
            install_params['server_port'] = self.server_port

            node.update_status('install.request')

            self.render("templ_install.cfg", p=install_params)
예제 #5
0
    def _get_nodes(self, group=None, nodelist=None):
        def transform_node_dict(nodes, node):
            node_dict = nodes[node]
            ret_dict = {'node': node, 'BOOTIF': '', 'BMC': ''}
            if 'interfaces' in node_dict:
                if 'BOOTIF' in node_dict['interfaces']:
                    ret_dict['BOOTIF'] = node_dict['interfaces']['BOOTIF'][4]
                if 'BMC' in node_dict['interfaces']:
                    ret_dict['BMC'] = node_dict['interfaces']['BMC'][4]

            return ret_dict

        if not luna_present:
            log.error("Luna 1.2 is not installed")
            return []
        nodes = []
        av_groups = luna.list('group')
        if group is None:
            groups = av_groups
        else:
            if group not in av_groups:
                self.log.error("No such group '{}'".format(group))
                return []
            groups = [group]

        for group_name in groups:
            group = luna.Group(group_name)
            domain = group.boot_params['domain']
            group_nodes = group.list_nodes()
            sorted_keys = group_nodes.keys()
            sorted_keys.sort()
            ipmi_username = ''
            ipmi_password = ''

            if 'bmcsetup' in group.install_params:
                bmcsetup = group.install_params['bmcsetup']
                if 'user' in bmcsetup:
                    ipmi_username = bmcsetup['user']
                if 'password' in bmcsetup:
                    ipmi_password = bmcsetup['password']

            for node_name in sorted_keys:
                node_dict = transform_node_dict(group_nodes, node_name)
                node_dict['ipmi_username'] = ipmi_username
                node_dict['ipmi_password'] = ipmi_password
                node_dict['hostname'] = node_name
                if domain:
                    node_dict['hostname'] += "." + domain
                nodes.append(node_dict)

        if not nodelist:
            return nodes

        nodelist = ",".join(nodelist)

        if not hostlist_present:
            self.log.info(
                "hostlist is not installed. List of nodes will not be expanded"
            )
            nodelist = nodelist.split(",")
        else:
            nodelist = hostlist.expand_hostlist(nodelist)

        return filter(lambda x: x['node'] in nodelist, nodes)
예제 #6
0
파일: test33.py 프로젝트: rispoli/luna
import luna
print luna.list('node')
print luna.list('group')
print luna.list('ifcfg')