Ejemplo n.º 1
0
    def handle(self, *args, **options):
        '''
        Handle command
        '''

        if args is None or len(args) != 1:
            raise CommandError(_('You must supply a file name'))
        csvname = args[0]

        count = 0
        try:
            with open(csvname, 'rb') as f:
                reader = csv.reader(f)
                for row in reader:
                    (aa, eq, sn, dns, passwd, rack, unit, pdua, pdub, mac) = row[:10]

                    if eq.startswith('VMC'):
                        eq = 'VMC'
                    elif re.match('^SC.-DS.$', eq):
                        eq = 'DS'
                    elif eq.startswith('SC'):
                        eq = 'SC'
                    elif eq.startswith('HN'):
                        eq = 'HN'
                    else:
                        continue

                    try:
                        rack = Rack.objects.get(name=rack)
                    except Rack.DoesNotExist:
                        raise RuntimeError(_('The Rack %(rack)s you specified does not exist. You should create it first' % {'rack': rack}))

                    unit = unit.split('-')[0][1:]

                    e = Equipment()
                    if eq == 'VMC':
                        e.model = EquipmentModel.objects.get(name='DL385 G7')
                        e.purpose = eq
                    elif eq == 'SC':
                        e.model = EquipmentModel.objects.get(name='DL380 G7')
                        e.purpose = eq
                    elif eq == 'DS':
                        e.model = EquipmentModel.objects.get(name='DS2600')
                        e.purpose = eq
                    elif eq == 'HN':
                        e.model = EquipmentModel.objects.get(name='PRIMERGY RX200 S5')
                        e.purpose = eq

                    e.serial = sn
                    e.rack = rack
                    e.unit = unit
                    e.save()

                    if eq == 'VMC' or eq == 'SC' or eq == 'HN':
                        s = ServerManagement()
                        s.equipment = e
                        if eq == 'HN':
                            s.method = 'irmc'
                            s.username = '******'
                        elif eq == 'VMC' or eq == 'SC':
                            s.method = 'ilo3'
                            s.username = '******'
                        s.hostname = dns
                        s.password = passwd
                        s.mac = mac
                        s.save()

                    print _('OK: %(eq)s %(sn)s') % {'eq': eq, 'sn': sn}
                    count += 1
                print _('Total ') + str(count)
        except IOError:
            raise CommandError(_('No such file or directory'))
    def create_hwdoc_equipments(self, equipment_number, domain_name='example.com'):
        equipments = []
        hosts = list(Host.objects.all())  # Fetch them all as an optimization
        racks = list(Rack.objects.all())  # Fetch them all as an optimization
        storages = list(Storage.objects.all())
        projects = list(Project.objects.all())  # Fetch them all as an optimization
        print('Creating %s equipments' % len(hosts))

        # This is really the NP-hard problem, discrete knapsack. Well, a
        # variation but solving it well is out of the scope, so using a dumb
        # probabilistic algorith to assign a box a u
        def assign_u(e):
            rack = None
            units = None
            for i in range(len(racks)):
                tmp = random.choice(racks)
                if tmp.model.inrow_ac:
                    continue
                # See if a rack possibly fits us
                units = tmp.get_empty_units()
                if len(units) >= e.model.u:
                    rack = tmp
                    units = list(units)
                    break
            # No rack had even remotely space for us
            if not rack:
                return None, None
            # Pick a free u and check if the next u units are free
            # If not. just ignore the equipment
            start = random.choice(units)
            for i in range(e.model.u):
                if start + i not in units:
                    return None, None
            return rack, start

        for host in hosts:
            vendor = Vendor.objects.get(name=host.get_fact_value('manufacturer'))

            model = EquipmentModel.objects.get(vendor=vendor,
                                               name=host.get_fact_value('productname'))

            e = Equipment(
                model=model,
                serial=host.get_fact_value('serialnumber'))
            r, _ = assign_u(e)
            if r:
                e.rack, e.unit = assign_u(e)
            elif random.randint(1, 10) >= 9:
                # 90% in some storage
                e.storage = random.choice(storages)
            # 50% allocated to a project
            if random.randint(1, 10) >= 5:
                e.allocation = random.choice(projects)
            # 10% having a test comment
            if random.randint(1, 10) == 10:
                e.comments = 'Test comment'
            equipments.append(e)
        Equipment.objects.bulk_create(equipments)
        # And now add ServerManagement to 70% of them
        eqs = list(Equipment.objects.all())
        servermanagements = []
        will_get_sm = random.sample(eqs, len(eqs) * 7 / 10)  # Too bored to do 0.7 and cast to int
        for i in will_get_sm:
            servermanagements.append(
                ServerManagement(
                    method='dummy',
                    hostname='%s.%s' % (Command.id_generator(random.randint(6, 20)), domain_name),
                    equipment=i,
                ))
        ServerManagement.objects.bulk_create(servermanagements)
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        '''
        Handle command
        '''

        if args is None or len(args) != 1:
            raise CommandError(_('You must supply a file name'))
        csvname = args[0]

        count = 0
        try:
            with io.open(csvname, 'r') as f:
                reader = csv.reader(f)
                for row in reader:
                    (aa, eq, sn, dns, passwd, rack, unit, pdua, pdub,
                     mac) = row[:10]

                    if eq.startswith('VMC'):
                        eq = 'VMC'
                    elif re.match('^SC.-DS.$', eq):
                        eq = 'DS'
                    elif eq.startswith('SC'):
                        eq = 'SC'
                    elif eq.startswith('HN'):
                        eq = 'HN'
                    else:
                        continue

                    try:
                        rack = Rack.objects.get(name=rack)
                    except Rack.DoesNotExist:
                        raise RuntimeError(
                            _('The Rack %(rack)s you specified does not exist. You should create it first'
                              % {'rack': rack}))

                    unit = unit.split('-')[0][1:]

                    e = Equipment()
                    if eq == 'VMC':
                        e.model = EquipmentModel.objects.get(name='DL385 G7')
                        e.purpose = eq
                    elif eq == 'SC':
                        e.model = EquipmentModel.objects.get(name='DL380 G7')
                        e.purpose = eq
                    elif eq == 'DS':
                        e.model = EquipmentModel.objects.get(name='DS2600')
                        e.purpose = eq
                    elif eq == 'HN':
                        e.model = EquipmentModel.objects.get(
                            name='PRIMERGY RX200 S5')
                        e.purpose = eq

                    e.serial = sn
                    e.rack = rack
                    e.unit = unit
                    e.save()

                    if eq == 'VMC' or eq == 'SC' or eq == 'HN':
                        s = ServerManagement()
                        s.equipment = e
                        if eq == 'HN':
                            s.method = 'irmc'
                            s.username = '******'
                        elif eq == 'VMC' or eq == 'SC':
                            s.method = 'ilo3'
                            s.username = '******'
                        s.hostname = dns
                        s.password = passwd
                        s.mac = mac
                        s.save()

                    print(_('OK: %(eq)s %(sn)s') % {'eq': eq, 'sn': sn})
                    count += 1
                print(_('Total ') + str(count))
        except IOError:
            raise CommandError(_('No such file or directory'))
Ejemplo n.º 4
0
    def handle(self, *args, **options):
        """
        Handle command
        """

        if args is None or len(args) != 1:
            raise CommandError(_("You must supply a file name"))
        csvname = args[0]

        count = 0
        try:
            with open(csvname, "rb") as f:
                reader = csv.reader(f)
                for row in reader:
                    (aa, eq, sn, dns, passwd, rack, unit, pdua, pdub, mac) = row[:10]

                    if eq.startswith("VMC"):
                        eq = "VMC"
                    elif re.match("^SC.-DS.$", eq):
                        eq = "DS"
                    elif eq.startswith("SC"):
                        eq = "SC"
                    elif eq.startswith("HN"):
                        eq = "HN"
                    else:
                        continue

                    try:
                        rack = Rack.objects.get(name=rack)
                    except Rack.DoesNotExist:
                        raise RuntimeError(
                            _(
                                "The Rack %(rack)s you specified does not exist. You should create it first"
                                % {"rack": rack}
                            )
                        )

                    unit = unit.split("-")[0][1:]

                    e = Equipment()
                    if eq == "VMC":
                        e.model = EquipmentModel.objects.get(name="DL385 G7")
                        e.purpose = eq
                    elif eq == "SC":
                        e.model = EquipmentModel.objects.get(name="DL380 G7")
                        e.purpose = eq
                    elif eq == "DS":
                        e.model = EquipmentModel.objects.get(name="DS2600")
                        e.purpose = eq
                    elif eq == "HN":
                        e.model = EquipmentModel.objects.get(name="PRIMERGY RX200 S5")
                        e.purpose = eq

                    e.serial = sn
                    e.rack = rack
                    e.unit = unit
                    e.save()

                    if eq == "VMC" or eq == "SC" or eq == "HN":
                        s = ServerManagement()
                        s.equipment = e
                        if eq == "HN":
                            s.method = "irmc"
                            s.username = "******"
                        elif eq == "VMC" or eq == "SC":
                            s.method = "ilo3"
                            s.username = "******"
                        s.hostname = dns
                        s.password = passwd
                        s.mac = mac
                        s.save()

                    print _("OK: %(eq)s %(sn)s") % {"eq": eq, "sn": sn}
                    count += 1
                print _("Total ") + str(count)
        except IOError:
            raise CommandError(_("No such file or directory"))