Ejemplo n.º 1
0
    def run(self):
        if cond.acquire():
            while 1:
                if len(running_list) < options.running_dict_size \
                and not waitting_queue.empty():

                    # 从等待队列中获取元素
                    item = waitting_queue.get()

                    try:
                        # 创建实例
                        uuid = nova_api.create_vm(item)
                        item['uuid'] = uuid
                    except Exception, e:
                        waitting_t.delete(_id=item['_oid'])
                    else:

                        # 为实例增加端口映射
                        os = item['instance_data']['os']
                        balancer = addNginxHost(uuid, os)
                        balancer.start()

                        # 实例加入到running_dict中
                        insert_vm_in_mem(item)

                        # 实例的到期时间
                        expired_time = from_now_to_datetime(seconds=seconds)
                        # 启动定时器
                        timer = Timer(uuid=uuid,
                                      item=item,
                                      seconds=cal_seconds_to(expired_time))
                        timer.start()
                        timer_list.append(timer)

                        # 在running_t中插入记录
                        obj_id = running_t.insert(
                            uuid=uuid,
                            user_id=item['user_id'],
                            expired_time=expired_time,
                            instance_data=item['instance_data'])

                        # 从等待的waitting_t中删除对应的记录
                        # 2013/02/20: 根据_id删除,而不是user_id
                        waitting_t.delete(_id=item['_oid'])
                        LOG.info("Get item from waitting_queue: %s" % item)

                        # 排队的请求被处理,根据waitting_id更新processed_time
                        # 并增加running_id,用于删除时例时更新deleted_time
                        # NOTICE: 目前使用uuid更新backup_t中deleted_time的条件
                        processed_time = time.time()
                        backup_t.update(dict(waitting_id=item['_oid']),
                                        processed_time=processed_time,
                                        uuid=uuid,
                                        running_id=obj_id)
                else:
                    cond.wait()
Ejemplo n.º 2
0
 def run(self):
     if cond.acquire():
         while 1:
             if len(running_list) < options.running_dict_size \
             and not waitting_queue.empty():
                 
                 # 从等待队列中获取元素
                 item = waitting_queue.get()
                 
                 try:
                     # 创建实例
                     uuid = nova_api.create_vm(item)
                     item['uuid'] = uuid
                 except Exception, e:
                     waitting_t.delete(_id=item['_oid'])
                 else:
                 
                     # 为实例增加端口映射
                     os = item['instance_data']['os']
                     balancer = addNginxHost(uuid, os)
                     balancer.start()
                     
                     # 实例加入到running_dict中
                     insert_vm_in_mem(item)
                     
                     # 实例的到期时间            
                     expired_time = from_now_to_datetime(seconds=seconds)
                     # 启动定时器
                     timer = Timer(uuid=uuid, item=item,
                                   seconds=cal_seconds_to(expired_time))
                     timer.start()
                     timer_list.append(timer)
                     
                     # 在running_t中插入记录
                     obj_id = running_t.insert(uuid=uuid, user_id=item['user_id'],
                                      expired_time=expired_time,
                                      instance_data=item['instance_data'])
                     
                     # 从等待的waitting_t中删除对应的记录
                     # 2013/02/20: 根据_id删除,而不是user_id
                     waitting_t.delete(_id=item['_oid'])
                     LOG.info("Get item from waitting_queue: %s" % item)
                     
                     # 排队的请求被处理,根据waitting_id更新processed_time
                     # 并增加running_id,用于删除时例时更新deleted_time
                     # NOTICE: 目前使用uuid更新backup_t中deleted_time的条件
                     processed_time = time.time()
                     backup_t.update(dict(waitting_id=item['_oid']),
                                     processed_time=processed_time,
                                     uuid=uuid, running_id=obj_id)
             else:
                 cond.wait()
Ejemplo n.º 3
0
def process_request(user_id, instance_data):
    '''
    处理用户的请求
    如果运行队列未满,则创建虚拟机实例、启动定时器、在running_t中插入记录
    否则将请求投入排队的队列中,并在waitting_t中插入记录
    '''
    item = dict(user_id=user_id, instance_data=instance_data)
    os = instance_data['os']

    # 如果运行队列未满
    if len(running_list) < options.running_dict_size:
        # 创建实例
        uuid = nova_api.create_vm(item)
        item['uuid'] = uuid

        # 为实例增加端口映射
        balancer = addNginxHost(uuid, os)
        balancer.start()

        # 实例加入到running_dict中
        insert_vm_in_mem(item)

        # 实例的到期时间
        expired_time = from_now_to_datetime(seconds=seconds)

        # 启动定时器
        timer = Timer(uuid=uuid,
                      item=item,
                      seconds=cal_seconds_to(expired_time))
        timer.start()
        timer_list.append(timer)

        # 在running_t中插入一条记录
        obj_id = running_t.insert(uuid=uuid,
                                  user_id=user_id,
                                  expired_time=expired_time,
                                  instance_data=instance_data)

        # 请求被立即处理
        # 日志表里插入created_time和processed_time
        # 并增加running_id,用于删除实例时更新deleted_time
        # user_id用于查询用
        processed_time = time.time()
        backup_t.insert(user_id=user_id,
                        created_time=processed_time,
                        processed_time=processed_time,
                        instance_data=instance_data,
                        running_id=obj_id,
                        uuid=uuid)
        LOG.info("Start timer for {0}: {1}".format(user_id, instance_data))
        # 返回0
        return 0
    else:
        # 创建时间
        created_time = from_now_to_datetime()
        item.setdefault("created_time", created_time)

        # 在waitting_t中加入请求
        obj_id = waitting_t.insert(**item)

        # 请求被排队
        # 日志表里插入created_time,并增加waitting_id
        # waitting_id是在waitting_t中获得的id
        # 用于在RunningPutter线程中更新processed_time
        # NOTICE: 目前使用uuid作为删除实例时更新deleted_time的条件
        # user_id用于查询
        created_time = time.time()
        backup_t.insert(user_id=user_id,
                        created_time=created_time,
                        waitting_id=obj_id,
                        instance_data=instance_data)

        # 将请求加入到等待队列
        # 2013/02/20: 将db的_objectid也压入队列
        # 在waitting_t中删除元素的时候,就根据_objectid删除
        # 而不是user_id
        item['_oid'] = obj_id
        waitting_queue.put(item)

        LOG.info("put data to waitting queue {0}: {1}".format(
            user_id, instance_data))
        result = waitting_t.query(_id=obj_id)

        # 返回排队的序号
        for line in result:
            return line['auto_id']
Ejemplo n.º 4
0
def process_request(user_id, instance_data):
    '''
    处理用户的请求
    如果运行队列未满,则创建虚拟机实例、启动定时器、在running_t中插入记录
    否则将请求投入排队的队列中,并在waitting_t中插入记录
    '''
    item = dict(user_id=user_id, instance_data=instance_data)
    os = instance_data['os']
    
    # 如果运行队列未满
    if len(running_list) < options.running_dict_size:
        # 创建实例
        uuid = nova_api.create_vm(item)
        item['uuid'] = uuid
        
        # 为实例增加端口映射
        balancer = addNginxHost(uuid, os)
        balancer.start()
        
        # 实例加入到running_dict中
        insert_vm_in_mem(item)
        
        # 实例的到期时间
        expired_time = from_now_to_datetime(seconds=seconds)
        
        # 启动定时器
        timer = Timer(uuid=uuid, item=item,
                      seconds=cal_seconds_to(expired_time))
        timer.start()
        timer_list.append(timer)
        
        # 在running_t中插入一条记录
        obj_id = running_t.insert(uuid=uuid, user_id=user_id,
                         expired_time=expired_time, instance_data=instance_data)
        
        # 请求被立即处理
        # 日志表里插入created_time和processed_time
        # 并增加running_id,用于删除实例时更新deleted_time
        # user_id用于查询用
        processed_time = time.time()
        backup_t.insert(user_id=user_id, created_time=processed_time, processed_time=processed_time,
                        instance_data=instance_data, running_id=obj_id, uuid=uuid)
        LOG.info("Start timer for {0}: {1}".format(user_id, instance_data))
        # 返回0
        return 0
    else:
        # 创建时间
        created_time = from_now_to_datetime()
        item.setdefault("created_time", created_time)
        
        # 在waitting_t中加入请求
        obj_id = waitting_t.insert(**item)
        
        # 请求被排队
        # 日志表里插入created_time,并增加waitting_id
        # waitting_id是在waitting_t中获得的id
        # 用于在RunningPutter线程中更新processed_time
        # NOTICE: 目前使用uuid作为删除实例时更新deleted_time的条件
        # user_id用于查询
        created_time = time.time()
        backup_t.insert(user_id=user_id, created_time=created_time, waitting_id=obj_id,
                        instance_data=instance_data)
        
        # 将请求加入到等待队列
        # 2013/02/20: 将db的_objectid也压入队列
        # 在waitting_t中删除元素的时候,就根据_objectid删除
        # 而不是user_id
        item['_oid'] = obj_id
        waitting_queue.put(item)
        
        LOG.info("put data to waitting queue {0}: {1}".format(user_id, instance_data))
        result = waitting_t.query(_id=obj_id)
        
        # 返回排队的序号
        for line in result:
            return line['auto_id']