Esempio n. 1
0
def send_updates(host_obj, db, cloud, updates):
    _log = get_mylog()
    _log.info('[{}] has updates {}'.format(cloud.my_id_from_remote, updates))
    # connect to remote

    rd = setup_remote_socket(cloud)
    if not rd.success:
        msg = 'Failed to connect to remote: {}'.format(rd.data)
        _log.error(msg)
        # TODO: At this point, the local updates will not be reflected in the
        #   nebula. This is a spot to come back to when we add support for
        #   multiple hosts.
        return
    remote_sock = rd.data
    raw_connection = RawConnection(remote_sock)
    # get hosts list
    msg = GetActiveHostsRequestMessage(cloud.my_id_from_remote, cloud.uname(),
                                       cloud.cname())
    raw_connection.send_obj(msg)
    response = raw_connection.recv_obj()
    check_response(GET_ACTIVE_HOSTS_RESPONSE, response.type)
    hosts = response.hosts
    updated_peers = 0
    for host in hosts:
        if host['id'] == cloud.my_id_from_remote:
            continue
        update_peer(host_obj, db, cloud, host, updates)
        updated_peers += 1
    _log.info('[{}] updated {} peers'.format(cloud.my_id_from_remote,
                                             updated_peers))
Esempio n. 2
0
    def main(self):

        if self.testing:
            print('please enter username for {}:'.format(self.cname))
            self.username = stdin.readline()[:-1]
            print('Enter the password for ' + self.cname + ':')
            password = stdin.readline()[:-1]  # todo this is yea, bad.
        else:
            self.username = raw_input('Enter the username for ' + self.cname +
                                      ':').lower()
            password = getpass.getpass('Enter the password for ' + self.cname +
                                       ':')

        rem_sock = setup_remote_socket(self.rem_addr, self.rem_port)
        self.rem_conn = RawConnection(rem_sock)
        # request = make_client_session_request(self.cname, self.username, password)
        request = ClientSessionRequestMessage(self.cname, self.username,
                                              password)
        # send_msg(request, self.rem_sock)
        self.rem_conn.send_obj(request)
        response = self.rem_conn.recv_obj()
        # response = recv_msg(self.rem_sock)
        # print '__ resp:{}__'.format(response)
        if not (response.type == CLIENT_SESSION_RESPONSE):
            raise Exception('remote did not respond with success')
        self.session_id = response.sid
        self.tgt_host_ip = response.ip
        self.tgt_host_port = response.port
        self.main_loop()
Esempio n. 3
0
    def get_remote_conn(self):
        # type: () -> ResultAndData
        # type: () -> ResultAndData(True, RawConnection)
        # type: () -> ResultAndData(False, Exception)
        from host.util import setup_remote_socket

        rd = ResultAndData(False, None)
        try:
            rd = setup_remote_socket(self)
            if rd.success:
                conn = RawConnection(rd.data)
                rd = ResultAndData(True, conn)
            else:
                return rd
        except Exception, e:
            rd = ResultAndData(False, e)
Esempio n. 4
0
def complete_mirroring(db, cloud):
    # type: (SimpleDB, Cloud) -> ResultAndData
    _log = get_mylog()
    rd = setup_remote_socket(cloud)
    if rd.success:
        new_rem_sock = rd.data
        remote_conn = RawConnection(new_rem_sock)
        msg = MirroringCompleteMessage(cloud.my_id_from_remote, cloud.uname(),
                                       cloud.cname())
        remote_conn.send_obj(msg)

        cloud.completed_mirroring = True
        db.session.commit()

        new_rem_sock.close()
    else:
        msg = 'Failed to complete_mirroring {}'.format(rd.data)
        _log.error(msg)
    return rd
Esempio n. 5
0
def client_setup_test_actual():

    print '#' * 80
    print '# testing setting up a client session'
    print '#' * 80
    global host_proc, remote_proc
    test_root = 'client_test'
    neb_1_path = os.path.join(test_root, 'tmp0')
    neb_2_path = os.path.join(test_root, 'tmp1')

    if not os.path.exists(test_root):
        os.makedirs(test_root)
    if not os.path.exists(neb_1_path):
        os.makedirs(neb_1_path)
    if not os.path.exists(neb_2_path):
        os.makedirs(neb_2_path)

    host_proc, host_1_proc, remote_proc = start_nebs_and_nebr(test_root)

    rem_sock = setup_remote_socket(REMOTE_HOST, REMOTE_PORT)
    rem_conn = RawConnection(rem_sock)
    print '__ setup remote socket __'
    # request = make_client_session_request('qwer', 'asdf', 'asdf')
    request = ClientSessionRequestMessage('asdf', 'asdf')
    # send_msg(request, rem_sock)
    rem_conn.send_obj(request)
    print '__ sent client session request message __'
    print '__ msg:{}__'.format(request.__dict__)
    # response = recv_msg(rem_sock)
    response = rem_conn.recv_obj()
    print '__ resp:{}__'.format(response.__dict__)

    if not (response.type == CLIENT_SESSION_RESPONSE):
        raise Exception('remote did not respond with success')

    session_id = response.sid

    ############
    rem_sock = setup_remote_socket(REMOTE_HOST, REMOTE_PORT)
    rem_conn = RawConnection(rem_sock)

    msg = ClientGetCloudsRequestMessage(session_id)
    rem_conn.send_obj(msg)
    resp = rem_conn.recv_obj()
    if not (resp.type == CLIENT_GET_CLOUDS_RESPONSE):
        raise Exception('remote did not respond with success CGCR')
    print resp.__dict__

    cloudname = resp.owned[0].cname

    ############
    rem_sock = setup_remote_socket(REMOTE_HOST, REMOTE_PORT)
    rem_conn = RawConnection(rem_sock)

    msg = ClientGetCloudHostRequestMessage(session_id, cloudname)
    rem_conn.send_obj(msg)
    resp = rem_conn.recv_obj()
    if not (resp.type == CLIENT_GET_CLOUD_HOST_RESPONSE):
        raise Exception('remote did not respond with success CGCR')
    print resp.__dict__

    tgt_host_ip = resp.ip
    tgt_host_port = resp.port

    ############
    print '__ setting up host socket __'
    msg = ListFilesRequestMessage(session_id, 'qwer', '.')
    response = create_sock_msg_get_response(tgt_host_ip, tgt_host_port, msg)

    print response
    verify_type_or_teardown(response, LIST_FILES_RESPONSE)

    resp0 = response
    ls0 = resp0.ls

    if not os.path.exists(neb_2_path):
        os.makedirs('test_out/tmp0/qwer')
    if not os.path.exists(neb_2_path):
        os.makedirs('test_out/tmp0/asdf')
        pass
    if not os.path.exists(neb_2_path):
        os.makedirs('test_out/tmp0/qwer')
        pass
    if not os.path.exists(neb_2_path):
        os.makedirs('test_out/tmp0/qwer')
        pass

    # fix all the paths to use actual constants and shit.
    os.makedirs('test_out/tmp0/asdf')
    os.makedirs('test_out/tmp0/asdf/foo')
    os.makedirs('test_out/tmp0/zxcv')
    print '__ made some new dirs__'
    sleep(1)
    print '__ checking for new dirs__'
    # host_sock.close()  #todo this is dumb
    # cont we should be able to reuse the connection.
    # but there's a lot of ways that could go wrong... right?
    # host_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # host_sock.connect((tgt_host_ip, tgt_host_port))

    # send_msg(
    #     make_list_files_request('qwer', session_id, '.')
    #     , host_sock
    # )
    # response = recv_msg(host_sock)

    print '__ setting up host socket __'
    msg = ListFilesRequestMessage(session_id, 'qwer', '.')

    response = create_sock_msg_get_response(tgt_host_ip, tgt_host_port, msg)
    print response
    verify_type_or_teardown(response, LIST_FILES_RESPONSE)

    resp1 = response
    ls1 = resp1.ls
    print '__these should be different {}!={}__'.format(len(ls0), len(ls1))

    ls_path(session_id, tgt_host_ip, tgt_host_port, './')
    ls_path(session_id, tgt_host_ip, tgt_host_port, './asdf')
    ls_path(session_id, tgt_host_ip, tgt_host_port, './tmp0')

    sleep(5)
    teardown()