예제 #1
0
 def _send(index, payload, delay):
     global _SlowSendIsWorking
     idurl = contactsdb.customer(index)
     if not idurl:
         _SlowSendIsWorking = False
         return
     # transport_control.ClearAliveTime(idurl)
     SendToID(idurl, Payload=payload, wide=True)
     reactor.callLater(delay, _send, index + 1, payload, delay)
예제 #2
0
def ListCustomerFiles1(customerNumber):
    """
    On the status form when clicking on a customer, find out what files we're
    holding for that customer.
    """
    idurl = contactsdb.customer(customerNumber)
    filename = nameurl.UrlFilename(idurl)
    customerDir = os.path.join(settings.getCustomersFilesDir(), filename)
    if os.path.exists(customerDir) and os.path.isdir(customerDir):
        backupFilesList = os.listdir(customerDir)
        if len(backupFilesList) > 0:
            return ListSummary(backupFilesList)
    return "No files stored for this customer"
예제 #3
0
def ListCustomerFiles1(customerNumber):
    """
    On the status form when clicking on a customer, find out what files we're
    holding for that customer.
    """
    idurl = contactsdb.customer(customerNumber)
    filename = nameurl.UrlFilename(idurl)
    customerDir = os.path.join(settings.getCustomersFilesDir(), filename)
    if os.path.exists(customerDir) and os.path.isdir(customerDir):
        backupFilesList = os.listdir(customerDir)
        if len(backupFilesList) > 0:
            return ListSummary(backupFilesList)
    return "No files stored for this customer"
예제 #4
0
def cmd_customers(opts, args, overDict):
    def _wait_remove_customer_and_stop(src, customer_name, count=0):
        customers = []
        for s in find_comments(src):
            if s.count('[online ]') or s.count('[offline]'):
                customers.append(s[18:38].strip())
        if customer_name not in customers:
            print '  customer %s is removed !' % customer_name
            print_and_stop(src)
            return
        if count >= 20:
            print ' time is out\n'
            reactor.stop()
            return
        else:
            def _check_again(customer_name, count):
                sys.stdout.write('.')
                run_url_command(webcontrol._PAGE_CUSTOMERS).addCallback(_wait_remove_customer_and_stop, customer_name, count)
            reactor.callLater(1, _check_again, customer_name, count+1)

    if len(args) < 2 or args[1] in [ 'list', 'ls', ]:
        url = webcontrol._PAGE_CUSTOMERS
        run_url_command(url).addCallback(print_and_stop)
        reactor.run()
        return 0

    elif args[1] in [ 'call', 'cl', ]:
        url = webcontrol._PAGE_CUSTOMERS + '?action=call'
        run_url_command(url).addCallback(print_and_stop)
        reactor.run()
        return 0

    elif args[1] in [ 'remove', 'rm', ] and len(args) >= 3:
        contactsdb.init()
        idurl = args[2].strip()
        if not idurl.startswith('http://'):
            try:
                idurl = contactsdb.customer(int(idurl))
            except:
                idurl = ''
        if not idurl:
            print 'customer IDURL is unknown\n'
            return 0
        name = nameurl.GetName(idurl)
        url = webcontrol._PAGE_CUSTOMERS + '?action=remove&idurl=%s' % misc.pack_url_param(idurl)
        run_url_command(url).addCallback(_wait_remove_customer_and_stop, name, 0)
        reactor.run()
        return 0
    
    return 2
예제 #5
0
    def doTestMyCapacity2(self, arg):
        """
        Here are some values.

        - donated_bytes : you set this in the config
        - spent_bytes : how many space is taken from you by other users right now
        - free_bytes = donated_bytes - spent_bytes : not yet allocated space
        - used_bytes : size of all files, which you store on your disk for your customers
        """
        current_customers = contactsdb.customers()
        removed_customers = []
        spent_bytes = 0
        donated_bytes = settings.getDonatedBytes()
        if os.path.isfile(settings.CustomersSpaceFile()):
            space_dict = bpio._read_dict(settings.CustomersSpaceFile(), {})
        else:
            space_dict = {'free': donated_bytes}
        used_dict = bpio._read_dict(settings.CustomersUsedSpaceFile(), {})
        lg.out(
            8,
            'customers_rejector.doTestMyCapacity donated=%d' % donated_bytes)
        try:
            int(space_dict['free'])
            for idurl, customer_bytes in space_dict.items():
                if idurl != 'free':
                    spent_bytes += int(customer_bytes)
        except:
            lg.exc()
            space_dict = {'free': donated_bytes}
            spent_bytes = 0
            removed_customers = list(current_customers)
            current_customers = []
            self.automat('space-overflow',
                         (space_dict, spent_bytes, current_customers,
                          removed_customers))
            return
        lg.out(8, '        spent=%d' % spent_bytes)
        if spent_bytes < donated_bytes:
            space_dict['free'] = donated_bytes - spent_bytes
            bpio._write_dict(settings.CustomersSpaceFile(), space_dict)
            lg.out(8, '        space is OK !!!!!!!!')
            self.automat('space-enough')
            return
        used_space_ratio_dict = {}
        for customer_pos in range(contactsdb.num_customers()):
            customer_idurl = contactsdb.customer(customer_pos)
            try:
                allocated_bytes = int(space_dict[customer_idurl])
            except:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                lg.warn('%s allocated space unknown' % customer_idurl)
                continue
            if allocated_bytes <= 0:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                lg.warn('%s allocated_bytes==0' % customer_idurl)
                continue
            try:
                files_size = int(used_dict.get(customer_idurl, 0))
                ratio = float(files_size) / float(allocated_bytes)
            except:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                lg.warn('%s used_dict have wrong value' % customer_idurl)
                continue
            if ratio > 1.0:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                spent_bytes -= allocated_bytes
                lg.warn('%s space overflow, where is bptester?' %
                        customer_idurl)
                continue
            used_space_ratio_dict[customer_idurl] = ratio
        customers_sorted = sorted(
            current_customers,
            key=lambda i: used_space_ratio_dict[i],
        )
        while len(customers_sorted) > 0:
            customer_idurl = customers_sorted.pop()
            allocated_bytes = int(space_dict[customer_idurl])
            spent_bytes -= allocated_bytes
            space_dict.pop(customer_idurl)
            current_customers.remove(customer_idurl)
            removed_customers.append(customer_idurl)
            lg.out(8, '        customer %s REMOVED' % customer_idurl)
            if spent_bytes < donated_bytes:
                break
        space_dict['free'] = donated_bytes - spent_bytes
        lg.out(8, '        SPACE NOT ENOUGH !!!!!!!!!!')
        self.automat(
            'space-overflow',
            (space_dict, spent_bytes, current_customers, removed_customers))
예제 #6
0
    def doTestMyCapacity2(self, arg):
        """
        Here are some values.

        - donated_bytes : you set this in the config
        - spent_bytes : how many space is taken from you by other users right now
        - free_bytes = donated_bytes - spent_bytes : not yet allocated space
        - used_bytes : size of all files, which you store on your disk for your customers
        """
        current_customers = contactsdb.customers()
        removed_customers = []
        spent_bytes = 0
        donated_bytes = settings.getDonatedBytes()
        if os.path.isfile(settings.CustomersSpaceFile()):
            space_dict = bpio._read_dict(settings.CustomersSpaceFile(), {})
        else:
            space_dict = {'free': donated_bytes}
        used_dict = bpio._read_dict(settings.CustomersUsedSpaceFile(), {})
        lg.out(8, 'customers_rejector.doTestMyCapacity donated=%d' % donated_bytes)
        try:
            int(space_dict['free'])
            for idurl, customer_bytes in space_dict.items():
                if idurl != 'free':
                    spent_bytes += int(customer_bytes)
        except:
            lg.exc()
            space_dict = {'free': donated_bytes}
            spent_bytes = 0
            removed_customers = list(current_customers)
            current_customers = []
            self.automat('space-overflow', (space_dict, spent_bytes, current_customers, removed_customers))
            return
        lg.out(8, '        spent=%d' % spent_bytes)
        if spent_bytes < donated_bytes:
            space_dict['free'] = donated_bytes - spent_bytes
            bpio._write_dict(settings.CustomersSpaceFile(), space_dict)
            lg.out(8, '        space is OK !!!!!!!!')
            self.automat('space-enough')
            return
        used_space_ratio_dict = {}
        for customer_pos in xrange(contactsdb.num_customers()):
            customer_idurl = contactsdb.customer(customer_pos)
            try:
                allocated_bytes = int(space_dict[customer_idurl])
            except:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                lg.warn('%s allocated space unknown' % customer_idurl)
                continue
            if allocated_bytes <= 0:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                lg.warn('%s allocated_bytes==0' % customer_idurl)
                continue
            try:
                files_size = int(used_dict.get(customer_idurl, 0))
                ratio = float(files_size) / float(allocated_bytes)
            except:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                lg.warn('%s used_dict have wrong value' % customer_idurl)
                continue
            if ratio > 1.0:
                if customer_idurl in current_customers:
                    current_customers.remove(customer_idurl)
                    removed_customers.append(customer_idurl)
                else:
                    lg.warn('%s not customers' % customer_idurl)
                spent_bytes -= allocated_bytes
                lg.warn('%s space overflow, where is bptester?' % customer_idurl)
                continue
            used_space_ratio_dict[customer_idurl] = ratio
        customers_sorted = sorted(current_customers,
                                  key=lambda i: used_space_ratio_dict[i],)
        while len(customers_sorted) > 0:
            customer_idurl = customers_sorted.pop()
            allocated_bytes = int(space_dict[customer_idurl])
            spent_bytes -= allocated_bytes
            space_dict.pop(customer_idurl)
            current_customers.remove(customer_idurl)
            removed_customers.append(customer_idurl)
            lg.out(8, '        customer %s REMOVED' % customer_idurl)
            if spent_bytes < donated_bytes:
                break
        space_dict['free'] = donated_bytes - spent_bytes
        lg.out(8, '        SPACE NOT ENOUGH !!!!!!!!!!')
        self.automat('space-overflow', (space_dict, spent_bytes, current_customers, removed_customers))