Ejemplo n.º 1
0
def on_cmd_result(channel, method, properties, body):
    js = json.loads(body)
    if 'msgid' not in js:
        raise RuntimeError('Incorrect CMD result. Reesult = {}'.format(js))
    msgid = js.pop('msgid')
    log.info('Receive command result from client, msgid = %d', msgid)
    set_response(msgid, js)
Ejemplo n.º 2
0
def on_cmd(channel, method, properties, body):
    log.info('Receive command from server')
    js = json.loads(body)
    result = dispatch_cmd(js)
    result.msgid = js['msgid']
    channel.basic_publish(exchange='',
                          routing_key=QUEUE_CMD_RESULT,
                          body=json.dumps(result.__dict__))
Ejemplo n.º 3
0
def deregister_client(channel):
    info = xs_info.copy()
    info['type'] = 'leave'
    log.info('Deregistering the client, name=%s, routing-key=%s', info['name'],
             info['routing-key'])
    channel.basic_publish(exchange='',
                          routing_key=QUEUE_CONNECT,
                          body=json.dumps(info))
Ejemplo n.º 4
0
def pool_of_server():
    log.info('Pool of server')
    vms = []
    with connection() as s:
        pool = s.xenapi.pool.get_all()[0]
        uuid = s.xenapi.pool.get_uuid(pool)

    return Result(0, uuid)
Ejemplo n.º 5
0
 def run(self):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     server = loop.create_server(self.aiohttp_server(),
                                 host='0.0.0.0',
                                 port='8000')
     log.info('Starting aioHTTPServer')
     loop.run_until_complete(server)
     loop.run_forever()
Ejemplo n.º 6
0
def start(vm_uuid):
    log.info('Start VM %s', vm_uuid)
    try:
        with connection() as s:
            vm = s.xenapi.VM.get_by_uuid(vm_uuid)
            state = s.xenapi.VM.get_power_state(vm)
            if state != 'Running':
                s.xenapi.VM.start(vm, False, False)
    except Exception as e:
        return Result(101, error_message=str(e))
    return Result(0)
Ejemplo n.º 7
0
def on_connect(channel, method, properties, body):
    log.info('Receive connection from client on XS')
    xs_info = json.loads(body)
    tp = xs_info['type']
    if tp == 'join':
        add_agent(xs_info['routing-key'],
                  Agent(xs_info['name'], xs_info['routing-key']))
    elif tp == 'leave':
        del_agent(xs_info['routing-key'])
    else:
        log.warning('Unknown connect message')
Ejemplo n.º 8
0
def shutdown(vm_uuid):
    log.info('Stop VM %s', vm_uuid)
    try:
        with connection() as s:
            vm = s.xenapi.VM.get_by_uuid(vm_uuid)
            state = s.xenapi.VM.get_power_state(vm)
            if state != 'Halted':
                s.xenapi.VM.shutdown(vm)
    except Exception as e:
        return Result(101, error_message=str(e))
    return Result(0)
Ejemplo n.º 9
0
async def vms_of_server(channel, req):
    agent = req.query.get('server')
    state = req.query.get('state', None)

    log.info('Request CMD list_vms for %s', agent)
    cmd = {
        'type': 'server',
        'operation': 'list_vms',
        'state': state,
    }
    resp = await request_agent(channel, agent, cmd)
    return web.json_response(resp)
Ejemplo n.º 10
0
def dispatch_cmd(js):
    try:
        tp = js['type']
        operation = js['operation']
        log.info('CMD type=%s, operation=%s', tp, operation)
        if tp == 'vm':
            return vm.dispatch(js)
        elif tp == 'server':
            return server.dispatch(js)
        else:
            return Result(1, error_message='Not Implemented')
    except Exception as e:
        log.exception('Unable to dispatch cmd: {}'.format(js))
        return Result(999, error_message=str(e))
Ejemplo n.º 11
0
def wait_until_mq_ready(host, port):
    time.sleep(30)
    return

    for _ in range(30):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(1)
        try:
            log.info('Connecting %s:%d', host, port)
            s.connect((host, port))
            resp.shutdown()
            return
        except:
            pass

    raise RuntimeError('Unable to connect MQ')
Ejemplo n.º 12
0
def params():
    log.info('Params of server')
    with connection() as s:
        host_uuid = xs_info['uuid']
        host = s.xenapi.host.get_by_uuid(host_uuid)
        name_label = s.xenapi.host.get_name_label(host)
        description = s.xenapi.host.get_name_description(host)
        metrics = s.xenapi.host.get_metrics(host)
        metrics_record = s.xenapi.host_metrics.get_record(metrics)

        # make it serializble
        metrics_record['last_updated'] = str(metrics_record['last_updated'])

        result = {
            'UUID': host_uuid,
            'name_label': name_label,
            'description': description,
            'metrics': metrics_record,
        }

    return Result(0, result)
Ejemplo n.º 13
0
def list_vms(state):
    log.info('list vms')
    vms = []
    with connection() as s:
        all_vms = s.xenapi.VM.get_all()
        for vm in all_vms:
            is_template = s.xenapi.VM.get_is_a_template(vm)
            if not is_template:
                power_state = s.xenapi.VM.get_power_state(vm)
                if state is not None and power_state != state:
                    continue

                host = s.xenapi.VM.get_resident_on(vm)
                if host != 'OpaqueRef:NULL':
                    host_uuid = s.xenapi.host.get_uuid(host)
                    if host_uuid != xs_info['uuid']:
                        continue
                uuid = s.xenapi.VM.get_uuid(vm)
                vms.append(uuid)

    return Result(0, vms)
Ejemplo n.º 14
0
def params(vm_uuid):
    log.info('Params of VM')
    with connection() as s:
        vm = s.xenapi.VM.get_by_uuid(vm_uuid)
        name_label = s.xenapi.VM.get_name_label(vm)
        description = s.xenapi.VM.get_name_description(vm)
        power_state = s.xenapi.VM.get_power_state(vm)
        metrics = s.xenapi.VM.get_guest_metrics(vm)
        metrics_record = s.xenapi.VM_guest_metrics.get_record(metrics)

        # make it serializble
        metrics_record['last_updated'] = str(metrics_record['last_updated'])

        result = {
            'UUID': vm_uuid,
            'name_label': name_label,
            'description': description,
            'power_state': power_state,
            'metrics': metrics_record,
        }

    return Result(0, result)
Ejemplo n.º 15
0
def del_agent(routing_key):
    if routing_key in __agents:
        agent = __agents.pop(routing_key)
        log.info('Del agent %s', agent)
    else:
        log.error('Agent %s already deleted', routing_key)
Ejemplo n.º 16
0
def add_agent(routing_key, agent):
    log.info('Add agent %s', agent)
    __agents[routing_key] = agent
Ejemplo n.º 17
0
 def on_open(connection):
     log.info('MQ connected')
     connection.channel(on_open_callback=on_channel_open)
Ejemplo n.º 18
0
 def on_queue_declare(frame):
     queue_name = frame.method.queue
     log.info('QUEUE %s declared and bind to %s', queue_name, EXCHANGE_CMD)
     channel.queue_bind(queue_name, EXCHANGE_CMD, routing_key=routing_key)
     channel.basic_consume(queue_name, on_cmd, auto_ack=True)