Пример #1
0
    def run(self, args):
        server = args.server[0]
        try:
#           Lookup server name or ipaddress
            server = clusto.get(server)
            if not server:
#               Acount for fqdn
                server = clusto.get(server.split('.')[0])
                if not server:
                    raise LookupError('The object "%s" does not seem to exist' % args.server[0])
        except Exception as e:
            self.debug(e)
            self.error('"%s" does not exist' % args.server[0])
            return -1
        server = server[0]

        if args.attribute:
            self.debug('Grabbing attribute from parameter')
            attribute = args.attribute
        else:
            self.debug('Grabbing attribute from config file or default')
            attribute = self.get_conf('puppet.attribute', 'puppet')
        self.debug('Attribute is "%s"' % attribute)

        if args.base:
            self.debug('Grabbing base-class from parameter')
            base_class = args.base
        else:
            self.debug('Grabbing base-class from config file or default')
            base_class = self.get_conf('puppet.base', 'base::node')
        self.debug('Base class is "%s"' % base_class)

        result = {
            'classes': [base_class],
            'parameters': {},
        }

        for attr in server.attrs(key=attribute, merge_container_attrs=True):

            if attr.subkey == 'class':
                val = str(attr.value)
                if not val in result['classes']:
                    result['classes'].append(val)
                continue

            if attr.subkey == 'environment' and not 'environment' in result:
                result['environment'] = str(attr.value)
                continue

            if isinstance(attr.value, int):
                val = attr.value
            else:
                val = str(attr.value)

            subkey = str(attr.subkey)
            if not subkey in result['parameters']:
                result['parameters'][subkey] = val

#       Print the YAML object to stdout
        yaml.dump(result, sys.stdout, default_flow_style=False, explicit_start=True, indent=2)
Пример #2
0
    def run(self, args):
        try:
            obj = clusto.get(args.object[0])
        except Exception as e:
            self.debug(e)
            self.error('"%s" does not exist' % args.object[0])
            return -1
        obj = obj[0]
        if not isinstance(obj, drivers.BasicServer):
            self.error('Seems like "%s" is not a server (type=%s), you cannot FAI this' % (args.object[0], obj.type))
            return 1
        self.debug('Working with %s' % obj)
        print 'Parents: %s' % ' '.join([ _.name for _ in obj.parents() ])
        sys.stdout.write('Are you sure you want to reboot %s (yes/No)? ' % args.object[0])
        sys.stdout.flush()

        try:
            line = sys.stdin.readline().rstrip('\r\n')
        except KeyboardInterrupt:
            line = False

        if line != 'yes':
            print 'Aborting'
            return
        else:
            obj.power_reboot(captcha=False)
            print '%s is rebooting now.' % obj.name
        return
Пример #3
0
    def run(self, args):
        try:
            obj = clusto.get(args.object[0])
        except Exception as e:
            self.debug(e)
            self.error('"%s" does not exist' % args.object[0])
            return -1
        obj = obj[0]
        if not isinstance(obj, drivers.BasicServer):
            self.error(
                'Seems like "%s" is not a server (type=%s), you cannot FAI this'
                % (args.object[0], obj.type))
            return 1
        self.debug('Working with %s' % obj)
        print 'Parents: %s' % ' '.join([_.name for _ in obj.parents()])
        sys.stdout.write('Are you sure you want to reboot %s (yes/No)? ' %
                         args.object[0])
        sys.stdout.flush()

        try:
            line = sys.stdin.readline().rstrip('\r\n')
        except KeyboardInterrupt:
            line = False

        if line != 'yes':
            print 'Aborting'
            return
        else:
            obj.power_reboot(captcha=False)
            print '%s is rebooting now.' % obj.name
        return
Пример #4
0
    def run(self, args):
        try:
            obj = clusto.get(args.object[0])
        except Exception as e:
            self.debug(e)
            self.error('"%s" does not exist' % args.object[0])
            return -1
        obj = obj[0]
        if not hasattr(obj, 'reboot'):
            self.error('"%s" does not have a reboot() method.')
            return 1
        self.debug('Rebooting %s' % obj)
        print 'Parents: %s' % ' '.join([ _.name for _ in obj.parents() ])
        sys.stdout.write('Are you sure you want to reboot %s (yes/no)? ' % args.object[0])
        sys.stdout.flush()

        try:
            line = sys.stdin.readline().rstrip('\r\n')
        except KeyboardInterrupt:
            line = False

        if line != 'yes':
            print 'Aborting'
            return
        else:
            obj.reboot(captcha=False)
            print '%s is rebooting now.' % obj.name
        return
Пример #5
0
    def testGet(self):
        s1 = BasicServer('s1')
        s2 = BasicServer('s2')
        s3 = BasicServer('s3')
        ipm = IPManager('testnet', baseip='10.0.0.0', netmask='255.255.255.0')

        s1.set_attr(key='system', subkey='serial', value='P0000000000')
        s2.set_port_attr('nic-eth', 1, 'mac', '00:11:22:33:44:55')
        s3.bind_ip_to_osport('10.0.0.1', 'eth0')

        self.assertEqual(clusto.get('s1')[0], s1)
        self.assertEqual(clusto.get('00:11:22:33:44:55')[0], s2)
        self.assertEqual(clusto.get('10.0.0.1')[0], s3)
        self.assertEqual(clusto.get('P0000000000')[0], s1)
        self.assertEqual(clusto.get('foo'), None)
        self.assertRaises(ValueError, clusto.get, None)
Пример #6
0
    def run(self, args):
#       get the 'unallocated' pool
        if args.pool:
            self.debug('Grabbing pool from parameter')
            pool = args.pool
        else:
            self.debug('Grabbing pool from config file or default')
            pool = self.get_conf('deallocate.pool', 'unallocated')
        self.debug('Unallocated pool is "%s"' % pool)

#       Load the attributes from the conf file
        for attr in self.get_conf('deallocate.keep_attrs', '').split(','):
            if attr.split():
                self.keep_attrs.append(attr.strip())
#       Append attributes from the command line, if any
        if args.keep_attrs:
            for attr in args.keep_attrs.split(','):
                if attr not in self.keep_attrs:
                    self.keep_attrs.append(attr)
        self.debug('Final list of attrs to keep: %s' % self.keep_attrs)
        for obj in args.objects:
            obj = clusto.get(obj)
            if obj not in self.hosts:
                self.hosts.extend(obj)
        self.info('This is the list of servers/ipaddresses that you will deallocate')
        for host in self.hosts:
            if host.get_ips():
                print '%s >> %s' % (host.name, ' '.join(host.get_ips()))
            else:
                print host.name

#       Ask for confirmation
        if not args.force_yes:
            sys.stdout.write('Are you absolutely sure you want to continue %s (yes/No)? ')
            sys.stdout.flush()

            try:
                line = sys.stdin.readline().strip()
            except KeyboardInterrupt:
                line = False

            if line != 'yes':
                print 'Aborting'
                return 7

        for host in self.hosts:
            if not isinstance(host, drivers.servers.BasicServer):
                self.warn('Cannot deallocate "%s" because is not a server' % host.name)
                continue
            self._deallocate(host)
            self._clean_attrs(host)
            if host not in pool:
                pool.insert(host)
            if args.reboot:
                host.power_reboot(captcha=False)
                continue
            if args.shutdown:
                host.power_off(captcha=False)
                continue
        self.info('Done.')
Пример #7
0
    def testGet(self):
        s1 = BasicServer('s1')
        s2 = BasicServer('s2')
        s3 = BasicServer('s3')
        ipm = IPManager('testnet', baseip='10.0.0.0', netmask='255.255.255.0')

        s1.set_attr(key='system', subkey='serial', value='P0000000000')
        s2.set_port_attr('nic-eth', 1, 'mac', '00:11:22:33:44:55')
        s3.bind_ip_to_osport('10.0.0.1', 'eth0')

        self.assertEqual(clusto.get('s1')[0], s1)
        self.assertEqual(clusto.get('00:11:22:33:44:55')[0], s2)
        self.assertEqual(clusto.get('10.0.0.1')[0], s3)
        self.assertEqual(clusto.get('P0000000000')[0], s1)
        self.assertEqual(clusto.get('foo'), None)
        self.assertRaises(ValueError, clusto.get, None)
Пример #8
0
    def run(self, args):
#       get the 'unallocated' pool
        if args.pool:
            self.debug('Grabbing pool from parameter')
            pool = args.pool
        else:
            self.debug('Grabbing pool from config file or default')
            pool = self.get_conf('deallocate.pool', 'unallocated')
        self.debug('Unallocated pool is "%s"' % pool)

#       Load the attributes from the conf file
        for attr in self.get_conf('deallocate.keep_attrs', '').split(','):
            if attr.split():
                self.keep_attrs.append(attr.strip())
#       Append attributes from the command line, if any
        if args.keep_attrs:
            for attr in args.keep_attrs.split(','):
                if attr not in self.keep_attrs:
                    self.keep_attrs.append(attr)
        self.debug('Final list of attrs to keep: %s' % self.keep_attrs)
        for obj in args.objects:
            obj = clusto.get(obj)
            if obj not in self.hosts:
                self.hosts.extend(obj)
        self.info('This is the list of servers/ipaddresses that you will deallocate')
        for host in self.hosts:
            if host.get_ips():
                print '%s >> %s' % (host.name, ' '.join(host.get_ips()))
            else:
                print host.name

#       Ask for confirmation
        if not args.force_yes:
            sys.stdout.write('Are you absolutely sure you want to continue %s (yes/No)? ')
            sys.stdout.flush()

            try:
                line = sys.stdin.readline().strip()
            except KeyboardInterrupt:
                line = False

            if line != 'yes':
                print 'Aborting'
                return 7

        for host in self.hosts:
            if not isinstance(host, drivers.servers.BasicServer):
                self.warn('Cannot deallocate "%s" because is not a server' % host.name)
                continue
            self._deallocate(host)
            self._clean_attrs(host)
            if host not in pool:
                pool.insert(host)
            if args.reboot:
                host.power_reboot(captcha=False)
                continue
            if args.shutdown:
                host.power_off(captcha=False)
                continue
        self.info('Done.')
Пример #9
0
    def get(self, request):
        if not 'name' in request.params:
            return Response(status=400, body='400 Bad Request\nYou must specifiy a "name" parameter\n')
        name = request.params['name']

        result = clusto.get(name)
        if result is None:
            return Response(status=404, body='404 Not Found\n')

        result = [EntityAPI(x)._dict() for x in result]
        return dumps(request, result)
Пример #10
0
    def get(self, request):
        if not 'name' in request.params:
            return Response(status=400, body='400 Bad Request\nYou must specifiy a "name" parameter\n')
        name = request.params['name']

        result = clusto.get(name)
        if result is None:
            return Response(status=404, body='404 Not Found\n')

        result = [EntityAPI(x)._dict() for x in result]
        return dumps(request, result)
Пример #11
0
 def run(self, args):
     obj = clusto.get(args.obj[0])
     if not obj:
         self.error('Object %s does not exist' % args.obj[0])
         return -1
     self.obj = obj[0]
     opts = {}
     kwargs = dict(args.__dict__.items())
     self.format = args.format
     for k in ['key', 'subkey', 'value', 'merge_container_attrs']:
         if kwargs[k] != None:
             opts[k] = kwargs[k]
     return (getattr(self, 'run_%s' % args.action[0])(opts))
Пример #12
0
    def run(self, args):
        if not args.items:
            print 'You need to provide at least one item. Use --help'
            return 0
        item_list = []
        self.debug('Fetching the list of items: %s' % ','.join(args.items))
        for item in args.items:
            obj = clusto.get(item)
            if not obj:
                self.warn("The item %s couldn't be found" % item)
                continue
            obj = obj[0]
            self.debug('Object found! %s' % obj)
            item_attrs = {
                'name': obj.name,
                'type': obj.type,
            }
            #           Fetch system attrs
            for attr in obj.attrs(key='system'):
                item_attrs[attr.subkey] = attr.value
#           fetch description(s)
            values = obj.attrs(key='description')
            if values:
                item_attrs['description'] = [_.value for _ in values]
            else:
                item_attrs['description'] = ''
#           fetch parent(s)
            values = obj.parents()
            if values:
                item_attrs['parents'] = [_.name for _ in values]
#           fetch content(s)
            values = obj.contents()
            if values:
                item_attrs['contents'] = [_.name for _ in values]
#           fetch ip(s)
            if 'get_ips' in dir(obj) and callable(getattr(obj, 'get_ips')):
                values = obj.get_ips()
                if values:
                    item_attrs['ip'] = [_ for _ in values]


#           fetch mac(s)
            values = [
                _ for _ in obj.attrs(key='port-nic-eth')
                if _.subkey.find('mac') != -1
            ]
            if values:
                for value in values:
                    item_attrs['mac%d' % value.number] = value.value
            item_list.append(item_attrs)
        getattr(self, 'print_%s' % args.format)(item_list)
Пример #13
0
 def run(self, args):
     obj = clusto.get(args.obj[0])
     if not obj:
         self.error("Object %s does not exist" % args.obj[0])
         return -1
     self.obj = obj[0]
     opts = {}
     kwargs = dict(args.__dict__.items())
     self.format = args.format
     if args.csv:
         self.format = "csv"
     for k in ["key", "subkey", "value", "merge_container_attrs"]:
         if kwargs[k] != None:
             opts[k] = kwargs[k]
     return getattr(self, "run_%s" % args.action[0])(opts)
Пример #14
0
 def run(self, args):
     obj = clusto.get(args.obj[0])
     if not obj:
         self.error('Object %s does not exist' % args.obj[0])
         return -1
     self.obj = obj[0]
     opts = {}
     kwargs = dict(args.__dict__.items())
     self.format = args.format
     for k in ['key', 'subkey', 'value', 'number', 'merge_container_attrs']:
         if k == 'number' and kwargs[k] is not None:
             kwargs[k] = int(kwargs[k])
         if kwargs[k] != None:
             opts[k] = kwargs[k]
     return (getattr(self, 'run_%s' % args.action[0])(opts))
Пример #15
0
    def run(self, args):
        for name in args.items:
            obj = clusto.get(name)
            if not obj:
                sys.stderr.write('Object does not exist: %s\n' % name)
                continue
            obj = obj[0]

            for label, key in PROPERTY_MAP:
                value = getattr(obj, key, None)
                self.print_line(label, value)

            #sys.stdout.write('\n')

            self.print_line('Parents', [x.name for x in obj.parents()])
            self.print_line('Contents', [x.name for x in obj.contents()])
            
            sys.stdout.write('\n')

            self.print_line('IP', obj.attr_values(key='ip', subkey='ipstring'))
            self.print_line('Public DNS', obj.attr_values(key='ec2', subkey='public-dns'))
            self.print_line('Private DNS', obj.attr_values(key='ec2', subkey='private-dns'))

            sys.stdout.write('\n')

            self.print_line('Owner', ['%s: %s' % (x.subkey, x.value) for x in obj.attrs(key='owner')])
            self.print_line('Serial',
                [x.rstrip('\r\n') for x in obj.attr_values(
                key='system', subkey='serial')])

            memory = obj.attr_value(key='system', subkey='memory')
            if memory:
                self.print_line('Memory', '%i GB' % (int(memory) / 1000))

            disk = obj.attr_value(key='system', subkey='disk')
            if disk:
                self.print_line('Disk', '%i GB (%i)' % (int(disk),
                len([int(x) for x in obj.attr_values(key='disk', subkey='size')])))

            cpucount = obj.attr_value(key='system', subkey='cpucount')
            if cpucount:
                self.print_line('CPU Cores', int(cpucount))

            self.print_line('Description', obj.attr_values(key='description'))
            sys.stdout.write('\n')

            if len(args.items) > 1:
                sys.stdout.write(('-' * 40) + '\n\n')
Пример #16
0
    def run(self, args):
        if not args.items:
            print 'You need to provide at least one item. Use --help'
            return 0
        item_list = []
        self.debug('Fetching the list of items: %s' % ','.join(args.items))
        for item in args.items:
            obj = clusto.get(item)
            if not obj:
                self.warn("The item %s couldn't be found" % item)
                continue
            obj = obj[0]
            self.debug('Object found! %s' % obj)
            item_attrs = {
                'name': obj.name,
                'type': obj.type,
            }
#           Fetch system attrs
            for attr in obj.attrs(key='system'):
                item_attrs[attr.subkey] = attr.value
#           fetch description(s)
            values = obj.attrs(key='description')
            if values:
                item_attrs['description'] = [ _.value for _ in values]
            else:
                item_attrs['description'] = ''
#           fetch parent(s)
            values = obj.parents()
            if values:
                item_attrs['parents'] = [ _.name for _ in values ]
#           fetch content(s)
            values = obj.contents()
            if values:
                item_attrs['contents'] = [ _.name for _ in values ]
#           fetch ip(s)
            if 'get_ips' in dir(obj) and callable(getattr(obj, 'get_ips')):
                values = obj.get_ips()
                if values:
                    item_attrs['ip'] = [ _ for _ in values ]
#           fetch mac(s)
            values = [ _ for _ in obj.attrs(key='port-nic-eth') if _.subkey.find('mac') != -1 ]
            if values:
                for value in values:
                    item_attrs['mac%d' % value.number] = value.value
            item_list.append(item_attrs)
        getattr(self, 'print_%s' % args.format)(item_list)
Пример #17
0
    def run(self, args):
        for name in args.server:
            server = clusto.get(name)
            if not server:
                self.error('%s does not exist' % name)
                continue

            server = server[0]

            if not hasattr(server, 'reboot'):
                self.error('%s does not implement reboot()' % server.name)
                return -1

            if not args.batch:
                if not self.confirm(server):
                    return -1

            kwargs = {}
            if args.method is not None:
                kwargs['method'] = args.method
            server.reboot(**kwargs)
Пример #18
0
    def run(self, args):
        for name in args.server:
            server = clusto.get(name)
            if not server:
                self.error('%s does not exist' % name)
                continue

            server = server[0]

            if not hasattr(server, 'reboot'):
                self.error('%s does not implement reboot()' % server.name)
                return -1

            if not args.batch:
                if not self.confirm(server):
                    return -1

            kwargs = {}
            if args.method is not None:
                kwargs['method'] = args.method
            server.reboot(**kwargs)
Пример #19
0
    def run(self, args):
        try:
            server = clusto.get(args.server[0])
            if not server:
                raise LookupError('Object "%s" does not exist' % args.server)
        except Exception as e:
            self.debug(e)
            self.error('No object like "%s" was found' % args.server)
            return 1
        server = server[0]

        if not hasattr(server, 'console'):
            self.error('The object %s lacks a console method' % server.name)
            return 2

        user = os.environ.get('USER')
        if args.user:
            self.debug('Grabbing user from parameter')
            user = args.user
        else:
            self.debug('Grabbing user from config file or default')
            user = self.get_conf('console.user', user)
        self.debug('User is "%s"' % user)
        return(server.console(ssh_user=user))
Пример #20
0
    def run(self, args):
        try:
            server = clusto.get(args.server[0])
            if not server:
                raise LookupError('Object "%s" does not exist' % args.server)
        except Exception as e:
            self.debug(e)
            self.error('No object like "%s" was found' % args.server)
            return 1
        server = server[0]

        if not hasattr(server, 'console'):
            self.error('The object %s lacks a console method' % server.name)
            return 2

        user = os.environ.get('USER')
        if args.user:
            self.debug('Grabbing user from parameter')
            user = args.user
        else:
            self.debug('Grabbing user from config file or default')
            user = self.get_conf('console.user', user)
        self.debug('User is "%s"' % user)
        return(server.console(ssh_user=user))
Пример #21
0
    def run(self, args):
        number = args.number

        if args.from_pool:
            self.debug('Grabbing pool from parameter')
            pool = args.from_pool
        else:
            self.debug('Grabbing pool from config file or default')
            pool = self.get_conf('allocate.pool', 'unallocated')
        self.debug('Unallocated pool is "%s"' % pool)

        pools = []
        try:
            if args.create_pools:
                pools = [
                    clusto.get_or_create(_, drivers.pool.Pool)
                    for _ in args.pool
                ]
            else:
                pools = [
                    clusto.get_by_name(_, assert_driver=drivers.pool.Pool)
                    for _ in args.to_pool
                ]
        except Exception as e:
            self.debug(e)
            self.error(
                'There was an error when fetching/creating the pools to allocate the servers to'
            )
            return 4
        self.debug('Target pool list: %s' % pools)

        try:
            pool = clusto.get_by_name(pool, assert_driver=drivers.pool.Pool)
        except Exception as e:
            self.debug(e)
            self.error('The pool "%s" does not exist' % pool)
            return 1

        try:
            parent = clusto.get(args.parent)
            if not parent:
                raise LookupError("Parent object is %s" % parent)
        except Exception as e:
            self.debug(e)
            self.error('The parent object "%s" does not exist' % args.parent)
            return 2
        parent = parent[0]
        if not isinstance(parent, drivers.racks.BasicRack) and not isinstance(
                parent, drivers.datacenters.BasicDatacenter):
            self.error('The parent "%s" is not a rack or a datacenter' %
                       args.parent)
            return 2

        self.info('Searching for servers in "%s", this may take a while' %
                  parent.name)

        unallocated = [
            _ for _ in parent.contents(
                clusto_types=[drivers.servers.BasicServer],
                search_children=True)
        ]
        unallocated = [_ for _ in unallocated if _ in pool and _.get_ips()]
        self.debug('The unallocated list size is %d' % len(unallocated))
        if len(unallocated) < number:
            self.error(
                'There are not enough servers in "%s" to fulfill your request'
                % args.parent)
            return 3

        filters = []
        if args.memory:
            filters.append(self.__make_filter('memory', args.memory * 1000))
        if args.disk:
            filters.append(self.__make_filter('disk', args.disk))
        if args.cores:
            filters.append(self.__make_filter('cpucount', args.cores))
        if args.spindles:
            filters.append(self.__make_filter('spindles', args.spindles))

        self.debug('Applying filters: %s' % filters)
        servers = []
        if not filters:
            servers = self.__sort_servers(unallocated)[:number]
        else:
            for func in filters:
                servers = filter(func, unallocated)[:number]
        self.debug('Server list: %s' % servers)

        for s in servers:
            pool.remove(s)
            for p in pools:
                p.insert(s)

        self.info(
            'Allocated the following list of servers matching your filters '
            'were allocated from the pool "%s"' % pool.name)
        self.info('The servers were also added to the pools %s' %
                  ','.join([_.name for _ in pools]))
        for s in servers:
            if s.get_ips():
                print s.get_ips()[0]
            else:
                print s.name
Пример #22
0
    def run(self, args):
        try:
            obj = clusto.get(args.object[0])
        except Exception as e:
            self.debug(e)
            self.error('"%s" does not exist' % args.object[0])
            return -1
        obj = obj[0]
        if not isinstance(obj, drivers.BasicServer):
            self.error('Seems like "%s" is not a server (type=%s), you cannot FAI this' % (obj.name, obj.type))
            return 1
        self.debug('Working with %s' % obj)
        if args.disk_class:
            obj.set_attr(key='fai', subkey='class', value=args.disk_class)
        disk_class = obj.attrs(key='fai', subkey='class', merge_container_attrs=True)
        if not disk_class:
            self.error('The server "%s" lacks fai class attribute, please '
                       'set one (hint: --disk-class at least)' % obj.name)
            return 2
        ip = obj.get_ips()
        if not ip:
            self.error('"%s" lacks an IP address, cannot FAI' % obj.name)
            return 3
        ip = ip[0]
        mac = obj.attrs(key='port-nic-eth', subkey='mac', number=1)
        if not mac:
            self.error('"%s" lacks a MAC Address, cannot FAI' % obj.name)
            return 4
        mac = mac[0].value

        if args.pool:
            self.debug('Grabbing pool from parameter')
            pool = args.pool
        else:
            self.debug('Grabbing pool from config file or default')
            pool = self.get_conf('fai.pool', 'fai')
        self.debug('FAI pool is "%s"' % pool)
        try:
            pool = clusto.get_by_name(pool)
        except Exception as e:
            self.debug(e)
            self.error('The pool "%s" does not exist' % pool)
            return 5
        if not isinstance(pool, drivers.Pool):
            self.error('Looks like "%s" is not a pool' % pool.name)
            return 6

        print 'IP: %s' % ip
        print 'MAC: %s' % mac
        print 'FAI classes: %s' % ' '.join([ _.value for _ in disk_class ])
        print 'Parents: %s' % ' '.join([ _.name for _ in obj.parents() ])

        sys.stdout.write('Are you absolutely sure you want to FAI %s (yes/No)? ' % obj.name)
        sys.stdout.flush()

        try:
            line = sys.stdin.readline().rstrip('\r\n')
        except KeyboardInterrupt:
            line = False

        if line != 'yes':
            print 'Aborting'
            return
        else:
            if obj not in pool:
                pool.insert(obj)
            if not args.no_reboot:
                obj.power_reboot(captcha=False)
                print '%s is rebooting now.' % obj.name
            print 'Done.'
        return
Пример #23
0
                    except ConfigParser.ParsingError, cppe:
                        logger.warn(cppe)
                    except Exception, e:
                        logger.debug(e)

    if dsn:
        config.set('clusto', 'dsn', dsn)
    elif 'CLUSTODSN' in os.environ:
        config.set('clusto', 'dsn', os.environ['CLUSTODSN'])

    if not config.has_option('clusto', 'dsn'):
        raise CmdLineError("No database given for clusto data.")

    plugins = []
    if config.has_option('clusto', 'plugins'):
        plugins += clusto.get('clusto', 'plugins').split(',')
    if 'CLUSTOPLUGINS' in os.environ:
        plugins += os.environ['CLUSTOPLUGINS'].split(',')
    for plugin in plugins:
        logger.debug('Loading plugin %s' % plugin)
        module = __import__(plugin)

    return config

def demodule(module):
    '''
    Returns a class out of a given module name by doing:
    some_name_in_this_form => SomeNameInThisForm()
    '''

    klass = ''.join([_.capitalize() for _ in module.split('_')])
Пример #24
0
    def run(self, args):
        number = args.number

        if args.from_pool:
            self.debug('Grabbing pool from parameter')
            pool = args.from_pool
        else:
            self.debug('Grabbing pool from config file or default')
            pool = self.get_conf('allocate.pool', 'unallocated')
        self.debug('Unallocated pool is "%s"' % pool)

        pools = []
        try:
            if args.create_pools:
                pools = [ clusto.get_or_create(_, drivers.pool.Pool) for _ in args.pool ]
            else:
                pools = [ clusto.get_by_name(_, assert_driver=drivers.pool.Pool) for _ in args.to_pool ]
        except Exception as e:
            self.debug(e)
            self.error('There was an error when fetching/creating the pools to allocate the servers to')
            return 4
        self.debug('Target pool list: %s' % pools)

        try:
            pool = clusto.get_by_name(pool, assert_driver=drivers.pool.Pool)
        except Exception as e:
            self.debug(e)
            self.error('The pool "%s" does not exist' % pool)
            return 1

        try:
            parent = clusto.get(args.parent)
            if not parent:
                raise LookupError("Parent object is %s" % parent)
        except Exception as e:
            self.debug(e)
            self.error('The parent object "%s" does not exist' % args.parent)
            return 2
        parent = parent[0]
        if not isinstance(parent, drivers.racks.BasicRack) and not isinstance(parent, drivers.datacenters.BasicDatacenter):
            self.error('The parent "%s" is not a rack or a datacenter' % args.parent)
            return 2

        self.info('Searching for servers in "%s", this may take a while' % parent.name)

        unallocated = [ _ for _ in parent.contents(clusto_types=[drivers.servers.BasicServer], search_children=True) ]
        unallocated = [ _ for _ in unallocated if _ in pool and _.get_ips() ]
        self.debug('The unallocated list size is %d' % len(unallocated))
        if len(unallocated) < number:
            self.error('There are not enough servers in "%s" to fulfill your request' % args.parent)
            return 3

        filters = []
        if args.memory:
            filters.append(self.__make_filter('memory', args.memory * 1000))
        if args.disk:
            filters.append(self.__make_filter('disk', args.disk))
        if args.cores:
            filters.append(self.__make_filter('cpucount', args.cores))
        if args.spindles:
            filters.append(self.__make_filter('spindles', args.spindles))

        self.debug('Applying filters: %s' % filters)
        servers = []
        if not filters:
            servers = self.__sort_servers(unallocated)[:number]
        else:
            for func in filters:
                servers = filter(func, unallocated)[:number]
        self.debug('Server list: %s' % servers)

        for s in servers:
            pool.remove(s)
            for p in pools:
                p.insert(s)

        self.info('Allocated the following list of servers matching your filters '
            'were allocated from the pool "%s"' % pool.name)
        self.info('The servers were also added to the pools %s' % ','.join([ _.name for _ in pools ]))
        for s in servers:
            if s.get_ips():
                print s.get_ips()[0]
            else:
                print s.name
Пример #25
0
def barker_callback(body):
    if not 'ec2' in body:
        return
    if not 'instance-id' in body['ec2']:
        return
    ec2 = body['ec2']
    log.debug(ec2['instance-id'])

    try:
        clusto.begin_transaction()
        server = clusto.get_or_create(ec2['instance-id'], SGServer)

        if not server.attr_values(key='ec2', subkey='instance-id'):
            server.set_attr(key='ec2', subkey='instance-id', value=ec2['instance-id'])

        zone = clusto.get(ec2['placement'])
        if not zone:
            zone = EC2Zone(ec2['placement'])
        else:
            zone = zone[0]
        if not server in zone:
            zone.insert(server)

        for key, subkey in EC2_SUBKEYS.items():
            server.set_attr(key='ec2', subkey=subkey, value=ec2[key])

        previous_ec2sg = server.attr_values(key='ec2',subkey='security-group')
        for group in set(previous_ec2sg).difference(set(ec2['security-groups'])):
            server.del_attrs(key='ec2',subkey='security-group', value=group)

        for i,group in enumerate(sorted(ec2['security-groups'])):
            server.set_attr(key='ec2', subkey='security-group', number=i, value=group)
            if group.find('_') != -1:
                environment, role = group.lower().split('_', 1)
                p = clusto.get_or_create(environment, Pool)
                if not p.attrs(key='pooltype', value='environment'):
                    p.set_attr(key='pooltype', value='environment')
                if not server in p:
                    p.insert(server)

        #server.bind_ip_to_osport(ec2['local-ipv4'], 'nic-eth', 0)
        #server.bind_ip_to_osport(ec2['public-ipv4'], 'nic-eth', 0)
        if len(server.attrs(key='ip', subkey='ipstring')) != 2:
            server.del_attrs(key='ip', subkey='ipstring')
            server.add_attr(key='ip', subkey='ipstring', value=ec2['local-ipv4'], number=0)
            server.add_attr(key='ip', subkey='ipstring', value=ec2['public-ipv4'], number=0)

        system = body['os']
        server.set_attr(key='system', subkey='memory',
                        value=int(system['memory']['MemTotal']) / 1024)
        server.set_attr(key='system', subkey='hostname',
                        value=system['hostname'])
        server.set_attr(key='system', subkey='os',
                        value=system['operatingsystemrelease'])
        if 'cpu' in system and len(system['cpu']) > 0:
            server.set_attr(key='system', subkey='cputype',
                            value=system['cpu'][0]['model name'])
            server.set_attr(key='system', subkey='cpucount',
                            value=len(system['cpu']))
            server.set_attr(key='system', subkey='cpucache',
                            value=system['cpu'][0]['cache size'])

        if 'kernelrelease' in system:
            server.set_attr(key='system', subkey='kernelrelease',
                            value=system['kernelrelease'])

        previous_disk = server.attr_key_tuples(key='disk')
        incoming_disk = []
        blockmap = [(v.replace('/dev/', ''), k) for k, v in ec2['block-device-mapping'].items() if k != 'root']
        blockmap = dict(blockmap)
        total_disk = 0
        for i, disk in enumerate(system['disks']):
            for subkey in disk.keys():
                server.set_attr(key='disk', subkey=subkey, number=i, value=str(disk[subkey]))
                incoming_disk.append(('disk',i,subkey))
            if disk['osname'] in blockmap:
                server.set_attr(key='disk', subkey='ec2-type', number=i, value=blockmap[disk['osname']])
                incoming_disk.append(('disk',i,'ec2-type'))
            total_disk += disk['size']
        total_disk = total_disk / 1073741824
        server.set_attr(key='system', subkey='disk', value=total_disk)

        for attr in set(previous_disk).difference(set(incoming_disk)):
            server.del_attrs(key=attr[0],subkey=attr[2],number=attr[1])

        for subkey, value in body.get('sgmetadata', {}).items():
            server.set_attr(key='sgmetadata', subkey=subkey, value=value)
            if subkey == 'clusterid' and value:
                cluster = clusto.get_or_create(value, Pool)
                if not cluster.attrs(key='pooltype', value='clusterid'):
                    cluster.set_attr(key='pooltype', value='clusterid')
                if not server in cluster:
                    cluster.insert(server)
            if subkey == 'role' and value:
                if len(server.attr_values(key='puppet', subkey='class', merge_container_attrs=True)) == 0:
                    server.set_attr(key='puppet', subkey='class',
                                    value='site::role::%s' % value)

                p = clusto.get_or_create(value, Pool)
                if not p.attrs(key='pooltype', value='role'):
                    p.set_attr(key='pooltype', value='role')
                if not server in p:
                    p.insert(server)

        if len(server.attr_values(key='puppet', subkey='class', merge_container_attrs=True)) == 0:
            log.warning('Found host %s with no role set, using site::role::base' % ec2['instance-id'])
            server.set_attr(key='puppet', subkey='class',
                            value='site::role::base')

        #server.set_attr(key='barker', subkey='last_updated', value=int(time()))

        try:
            owners = body['owners']
            for owner, reason in owners.iteritems():
                server.set_attr(key='owner', subkey=owner, value=reason)
        except KeyError:
            pass

        clusto.commit()
    except:
        log.warning('Exception from %s: %s' % (ec2['instance-id'], format_exc()))
        clusto.rollback_transaction()