Ejemplo n.º 1
0
def transform_backends_from_config(config):
    register = {}
    for entry in config.get('hosts', []):
        register.update({entry["host"]: [Server(endpoint) for endpoint in entry["servers"]]})
    for entry in config.get('paths', []):
        register.update({entry["path"]: [Server(endpoint) for endpoint in entry["servers"]]})
    return register
def test_weighted():
    backend1 = Server("localhost:8081")
    backend2 = Server("localhost:8082")
    backend3 = Server("localhost:8083")
    servers = [backend1, backend2, backend3]
    weights = [1, 2, 3]
    result = weighted(servers, weights)
    assert result in servers
Ejemplo n.º 3
0
def test_least_connections():
    backend1 = Server('localhost:8081')
    backend1.open_connections = 10
    backend2 = Server('localhost:8082')
    backend2.open_connections = 5
    backend3 = Server('localhost:8083')
    backend3.open_connections = 2
    servers = [backend1, backend2, backend3]
    result = least_connections(servers)
    assert result == backend3
Ejemplo n.º 4
0
def test_transform_backends_from_config():
    input = yaml.safe_load('''
        hosts:
          - host: www.mango.com
            servers:
              - localhost:8081
              - localhost:8082
          - host: www.apple.com
            servers:
              - localhost:9081
              - localhost:9082
        paths:
          - path: /mango
            servers:
              - localhost:8081
              - localhost:8082
          - path: /apple
            servers:
              - localhost:9081
              - localhost:9082
    ''')
    output = transform_backends_from_config(input)
    assert list(output.keys()) == [
        'www.mango.com', 'www.apple.com', '/mango', '/apple'
    ]
    assert output['www.mango.com'][0] == Server('localhost:8081')
    assert output['www.mango.com'][1] == Server('localhost:8082')
    assert output['www.apple.com'][0] == Server('localhost:9081')
    assert output['www.apple.com'][1] == Server('localhost:9082')
    assert output['/mango'][0] == Server('localhost:8081')
    assert output['/mango'][1] == Server('localhost:8082')
    assert output['/apple'][0] == Server('localhost:9081')
    assert output['/apple'][1] == Server('localhost:9082')
Ejemplo n.º 5
0
def test_transform_backends_from_config():
    input = yaml.safe_load('''
        hosts:
          - host: www.mango.com
            servers:
              - localhost:8081
              - localhost:8082
          - host: www.apple.com
            servers:
              - localhost:9081
              - localhost:9082
        paths:
          - path: /mango
            servers:
              - localhost:8081
              - localhost:8082
          - path: /apple
            servers:
              - localhost:9081
              - localhost:9082
    ''')
    output = transform_backends_from_config(input)
    assert list(output.keys()) == [
        "www.mango.com", "www.apple.com", "/mango", "/apple"
    ]
    assert output["www.mango.com"][0] == Server("localhost:8081")
    assert output["www.mango.com"][1] == Server("localhost:8082")
    assert output["www.apple.com"][0] == Server("localhost:9081")
    assert output["www.apple.com"][1] == Server("localhost:9082")
    assert output["/mango"][0] == Server("localhost:8081")
    assert output["/mango"][1] == Server("localhost:8082")
    assert output["/apple"][0] == Server("localhost:9081")
    assert output["/apple"][1] == Server("localhost:9082")
Ejemplo n.º 6
0
def test_get_healthy_server():
    host = "www.apple.com"
    healthy_server = Server("localhost:8081")
    unhealthy_server = Server("localhost:8082")
    unhealthy_server.healthy = False
    register = {"www.mango.com": [healthy_server, unhealthy_server], 
                "www.apple.com": [healthy_server, healthy_server],
                "www.orange.com": [unhealthy_server, unhealthy_server],
                "/mango": [healthy_server, unhealthy_server],
                "/apple": [unhealthy_server, unhealthy_server]}
    assert get_healthy_server("www.mango.com", register) == healthy_server
    assert get_healthy_server("www.apple.com", register) == healthy_server
    assert get_healthy_server("www.orange.com", register) == None
    assert get_healthy_server("/mango", register) == healthy_server
    assert get_healthy_server("/apple", register) == None
Ejemplo n.º 7
0
def servers_post():
    """
    CREATE new server from POST data.

    args:
        - none
    expected request json:
        - {'name': str, 'notes': str, 'cluster_id': int, 'is_active': bool}
    returns:
        - response with json payload and http status code
    """

    try:
        data = request.json
        keys = ['name', 'notes', 'is_active', 'cluster_id']
        server = Server()
        for key in keys:
            if key not in data:
                continue
            setattr(server, key, data[key])

        server.insert()
        return jsonify({'success': True, 'servers': [server.as_dict()]}), 200
    except Exception as err:
        abort(422, str(err))
Ejemplo n.º 8
0
def server_add():
    server_form = ServerForm()

    if request.method == 'POST' and server_form.validate():
        server = Server()

        server.server_ip = server_form.server_ip.data
        server.server_name = server_form.server_name.data
        server.database_port = server_form.database_port.data
        server.database_user = server_form.database_user.data

        # 密码hash加密
        server.database_pwd = generate_password_hash(
            server_form.database_pwd.data, method='pbkdf2:sha1', salt_length=8)
        # 添加对象到当前会话中
        db.session.add(server)
        # 提交当前会话中的对象
        db.session.commit()

        flash(u'服务器 %s 创建成功!' % server_form.server_name.data,
              category='success')

    return render_template('from_servers.html',
                           slidebar=False,
                           form=server_form)
Ejemplo n.º 9
0
def parseargs():
    parser = argparse.ArgumentParser()


    #Arguments
    #Must haves
    parser = argparse.ArgumentParser()
    parser.add_argument("-su","--serverUser", help= "server username", required = True, type = str)
    parser.add_argument("-sp","--serverPass", help= "server password", required = True, type = str)
    parser.add_argument("-sa","--serverAddress", help= "server addres",required = True, type = str)
    parser.add_argument("-db","--database", help= "the name of your database",required = True, type = str)
    parser.add_argument("-ak","--alpacaKey", help= "alpaca api key", required = True, type = str)
    parser.add_argument("-ask","--alpacaSKey", help= "alpaca secret api key", required = True, type = str)
    parser.add_argument("-akd","--alpacaKeydelta", help= "alpaca api key delta", required = True, type = str)
    parser.add_argument("-askd","--alpacaSKeydelta", help= "alpaca secret api key delta", required = True, type = str)
    parser.add_argument("-ake","--alpacaKeyecho", help= "alpaca api key echo", required = True, type = str)
    parser.add_argument("-aske","--alpacaSKeyecho", help= "alpaca secret api key echo", required = True, type = str)
    parser.add_argument("-ik","--iexKey", help= "Iex api key", required = True, type = str)
    
    
    #Optional 
    parser.add_argument("-s","--startup",help="fetches 3m data if present", action='store_true')
    parser.add_argument("-sprev","--startupPrevious",help="fetches previous data", action='store_true')
    parser.add_argument("-wl","--watchlists",help="run watchlists at boot", action='store_true')
    parser.add_argument("-pv","--posSize", help= "Max position size of a stock, default is  500", nargs = "?", default = 500, type = int)
    parser.add_argument("-ps","--posValue", help= "Max value of a position of a stock, default is 5000", nargs = "?", default = 5000, type = int)
    parser.add_argument("-ema","--exponentialmovingaverage", help= "ema for charlie/delta, default is 20", nargs = "?", default = 20, type = int)
    
    args = parser.parse_args()
    

    #Database variables    
    serverUser = args.serverUser    
    serverPass = args.serverPass
    serverAddress = args.serverAddress
    database = args.database
    alpacaKey = args.alpacaKey
    alpacaSKey = args.alpacaSKey
    alpacaKeydelta = args.alpacaKeydelta
    alpacaSKeydelta = args.alpacaSKeydelta
    alpacaKeyecho = args.alpacaKeyecho
    alpacaSKeyecho = args.alpacaSKeyecho
    iexKey = args.iexKey
    ema_time_period = args.exponentialmovingaverage
    
    #Position variables
    startup = args.startup
    startupPrevious = args.startupPrevious    
    watchlists = args.watchlists
    maxPosSize = args.posSize
    maxPosValue = args.posValue
    
    #important classes
    server = Server(user = serverUser, password = serverPass, address = serverAddress, database = database)
    apis = APIs(alpacaKey, alpacaSKey, iexKey)
    apis_delta = APIs(alpacaKeydelta, alpacaSKeydelta, iexKey)
    apis_echo = APIs(alpacaKeyecho, alpacaSKeyecho, iexKey)   

    
    return apis, server, startup, startupPrevious, watchlists, maxPosSize, maxPosValue, apis_delta, apis_echo, ema_time_period
Ejemplo n.º 10
0
    def nominate(self):
        if ServerManager.has_active_server():
            return post_message(
                'Someone else is already making tea, I\'ll save your nomination for later :smile:',
                self.channel)

        try:
            slack_id = MENTION_RE.search(self.command_body).groups()[0]
        except AttributeError:
            return post_message('You must nominate another user to brew!',
                                self.channel)

        nominated_user = UserManager.get_by_slack_id(slack_id)
        if self.request_user.nomination_points < NOMINATION_POINTS_REQUIRED:
            return post_message(
                'You can\'t nominate someone unless you brew tea %s times!' %
                NOMINATION_POINTS_REQUIRED, self.channel)

        # Subtract nomination points from request user.
        nominated_user.nomination_points -= NOMINATION_POINTS_REQUIRED

        server = Server(user_id=nominated_user.id)
        session.add(server)
        session.flush()
        session.add(Customer(user_id=self.request_user.id,
                             server_id=server.id))
        session.commit()
        brew_countdown.apply_async(countdown=BREW_COUNTDOWN,
                                   args=(self.channel, ))

        return post_message(
            '%s has nominated %s to make tea! Who wants in?' %
            (self.request_user.first_name, nominated_user.first_name),
            self.channel)
Ejemplo n.º 11
0
def create_and_add_server(rack_id, server_ip, ram):
    """
    Создание нового сервера и добавление его в серверную стойку.
    :param rack_id: id стойки
    :param server_ip: назначенный ip
    :param ram: объем оперативной памяти
    :return: id созданного сервера
    """
    tmp_session = get_session()
    try:
        rack = tmp_session.query(Rack).get(int(rack_id))
        check_free_slots(rack)
        new_server = Server(date_creation=datetime.now(),
                            date_last_changes=datetime.now(),
                            server_ip=check_ip(server_ip),
                            server_ram=ram,
                            server_core=SEVER_STANDART_CORE,
                            optical_port=False,
                            status=SEVER_STATUS_LIST[START_SERVER_STATUS])
        tmp_session.add(new_server)
        tmp_session.commit()
        module_logger.info('Создан новый сервер с id=%s' % str(new_server.id))
        add_server_to_rack(new_server.id, rack.id, new=True)
        return new_server.id
    except AttributeError:
        module_logger.error('Ошибка указания ip создаваемого сервера.')
        return False
    except Exception as err:
        module_logger.error(err)
        return False
    finally:
        tmp_session.close()
Ejemplo n.º 12
0
def test_get_healthy_server():
    healthy_server = Server('localhost:8081')
    unhealthy_server = Server('localhost:8082')
    unhealthy_server.healthy = False
    register = {
        'www.mango.com': [healthy_server, unhealthy_server],
        'www.apple.com': [healthy_server, healthy_server],
        'www.orange.com': [unhealthy_server, unhealthy_server],
        '/mango': [healthy_server, unhealthy_server],
        '/apple': [unhealthy_server, unhealthy_server]
    }
    assert get_healthy_server('www.mango.com', register) == healthy_server
    assert get_healthy_server('www.apple.com', register) == healthy_server
    assert get_healthy_server('www.orange.com', register) is None
    assert get_healthy_server('/mango', register) == healthy_server
    assert get_healthy_server('/apple', register) is None
Ejemplo n.º 13
0
def server_search(to_visit_sites, start_point):

    with session_scope() as session:
        while len(to_visit_sites):
            helper = to_visit_sites.pop(0)
            response = requests.get(helper)
            soup = BeautifulSoup(response.content, 'html.parser')

            r = requests.head(helper)
            server_info = r.headers.get("server")
            if server_info is None:
                server_info = "NO SERVER FOUND"
            info = session.query(Server).filter(Server.name == helper).one_or_none()
            if info is None:
                ser = Server(name=helper, server=server_info)
                session.add(ser)
            session.commit()

            for link in soup.find_all('a'):
                to_be_added = link.get('href')
                if to_be_added is not None and to_be_added not in to_visit_sites:
                    if to_be_added.startswith('http') and ".bg" in to_be_added:
                        to_visit_sites.append(to_be_added)
                        print(to_be_added)

                    elif to_be_added.startswith("link"):
                        to_be_added = start_point + to_be_added
                        to_visit_sites.append(to_be_added)
                        print(to_be_added)
Ejemplo n.º 14
0
def create_or_update_server_repository(db_session,
                                       hostname,
                                       server_type,
                                       server_url,
                                       username,
                                       vrf,
                                       server_directory,
                                       password,
                                       destination_on_host,
                                       created_by,
                                       server=None):

    hostname = check_acceptable_string(hostname)

    if server is None:
        server = Server(created_by=created_by)
        db_session.add(server)

    server.hostname = hostname
    server.server_type = server_type
    server.server_url = server_url
    server.username = username
    server.password = password
    server.vrf = vrf if (server_type == ServerType.TFTP_SERVER
                         or server_type == ServerType.FTP_SERVER) else ''
    server.server_directory = server_directory
    server.destination_on_host = destination_on_host if server_type == ServerType.SCP_SERVER else ''

    db_session.commit()

    return server
def test_get_healthy_server():
    healthy_server = Server("localhost:8081")
    unhealthy_server = Server("localhost:8082")
    unhealthy_server.healthy = False
    register = {
        "www.anthrax.com": [healthy_server, unhealthy_server],
        "www.metallica.com": [healthy_server, healthy_server],
        "www.slayer.com": [unhealthy_server, unhealthy_server],
        "/anthrax": [healthy_server, unhealthy_server],
        "/metallica": [unhealthy_server, unhealthy_server],
    }
    assert get_healthy_server("www.anthrax.com", register) == healthy_server
    assert get_healthy_server("www.metallica.com", register) == healthy_server
    assert get_healthy_server("www.slayer.com", register) is None
    assert get_healthy_server("/anthrax", register) == healthy_server
    assert get_healthy_server("/metallica", register) is None
Ejemplo n.º 16
0
    async def on_message(self, message):

        if isinstance(message.channel, discord.DMChannel
                      ) or message.author.bot or message.content is None:
            return

        if session.query(Server).filter_by(
                id=message.guild.id).first() is None:
            s = Server(id=message.guild.id, prefix='?', roles=['off'])
            session.add(s)
            session.commit()

        if session.query(User).filter_by(id=message.author.id).first() is None:
            s = User(id=message.author.id, last_vote=0)
            session.add(s)
            session.commit()

        try:
            if await self.get_cmd(message):
                session.commit()

        except Exception as e:
            traceback.print_exc()
            await message.channel.send(
                'Internal exception detected in command, {}'.format(e))
Ejemplo n.º 17
0
Archivo: main.py Proyecto: pgsvox/bot
    async def on_message(self, message):

        if message.author.bot or message.content is None:
            return

        u: User = session.query(User).filter(User.user == message.author.id)

        if u.count() < 1:

            user = User(user=message.author.id)

            session.add(user)
            session.commit()

        if message.guild is not None and session.query(Server).filter_by(server=message.guild.id).first() is None:

            server = Server(server=message.guild.id)

            session.add(server)
            session.commit()

        server = None if message.guild is None else session.query(Server).filter(Server.server == message.guild.id).first()
        user = session.query(User).filter(User.user == message.author.id).first()

        try:
            if await self.get_cmd(message, server, user):
                logger.info('Command: ' + message.content)

        except discord.errors.Forbidden:
            try:
                await message.channel.send(ENGLISH_STRINGS.get_string('no_perms_general'))
            except discord.errors.Forbidden:
                logger.info('Twice Forbidden')
Ejemplo n.º 18
0
    def post(self):
        if self.logged_in and self.current_user.admin is True:
            server_name = self.request.get('name')
            server_type = int(self.request.get('ah'))
            new_server = Server(name=server_name, ah_type=server_type)
            new_server.put()
            new_server.id = new_server.key.urlsafe()
            new_server.put()
            servers = self.get_server_dict()
            if server_type:
                for num in range(1, 4):
                    new_ah = AuctionHouse(server_name=server_name, faction=num)
                    new_ah.put()
                    new_ah.id = new_ah.key.urlsafe()
                    new_ah.put()
                    new_server.ah_id = new_server.ah_id + [new_ah.id]
            else:
                new_ah = AuctionHouse(server_name=server_name, faction=0)
                new_ah.put()
                new_ah.id = new_ah.key.urlsafe()
                new_ah.put()
                new_server.ah_id = new_server.ah_id + [new_ah.id]
            new_server.put()

            self.render('server-admin.html', {
                'name': self.current_user.name,
                'servers': servers,
            })
        else:
            self.abort(403)
Ejemplo n.º 19
0
def addServer():
    '''
    Добавление сервера
    :return:
    '''

    try:
        db_session.add(Server())
        db_session.commit()
        return getMessage('Server created.')
    except:
        return getMessage('Error creation of server.')
Ejemplo n.º 20
0
    async def registerServer(self, guild):
        muted_role = await self.makeMutedRole(guild)
        unmuted_role = await self.makeUnmutedRole(guild)

        # Add the new server, now with new muted and unmuted roles, to the database
        server = Server()
        server.server_id = guild.id
        server.name = guild.name
        server.muted_role_id = muted_role.id
        server.unmuted_role_id = unmuted_role.id
        session.add(server)
        session.commit()

        return server
Ejemplo n.º 21
0
 def add_servers(self):
     data = self.request.json
     with transaction.manager:
         server = Server(**data)
         DBSession.add(server)
         DBSession.flush()
         qs = DBSession.query(Server)
         qs.session.refresh(server)
         return {
             'uid': server.uid,
             'address': server.address,
             'port': server.port,
             'useSSL': server.useSSL
         }
Ejemplo n.º 22
0
    def brew(self):
        # Make sure the user is not brewing already
        if ServerManager.has_active_server():
            return post_message('Someone else is already making tea. Want in?',
                                self.channel)

        session.add(Server(user_id=self.request_user.id))
        session.commit()
        brew_countdown.apply_async(countdown=BREW_COUNTDOWN,
                                   args=(self.channel, ))
        return post_message(
            random.choice([
                '%s is making tea, who is in?' % self.request_user.first_name,
                'Who wants a cuppa?'
            ]), self.channel)
Ejemplo n.º 23
0
	def post(self):	
		server = Server(key_name=self.request.get('serverdomain'))
		server.serverdomain = self.request.get('serverdomain')
		if self.request.get('ssl') == "True":
			server.ssl = True
		else:
			server.ssl = False
		if self.request.get('notifywithprowl') == "True":
			server.notifywithprowl = True
		if self.request.get('notifywithemail') == "True":
			server.notifywithemail = True
		#server.notifywithprowl = self.request.get('notifywithtwitter')
		server.email = users.get_current_user().email()
		server.put()
		self.redirect('/admin')
Ejemplo n.º 24
0
    def parse_input(self, file_address):
        def parse_line(line):
            return map(lambda x: float(x.strip()),
                       line.split(self.INPUT_DELIMITER))

        with open(file_address) as input_file:
            servers_count, self.job_rate, self.job_deadline_rate, scheduler_rate = parse_line(
                input_file.readline())

            servers_count = int(servers_count)
            for i in range(servers_count):
                cores_rate = list(parse_line(input_file.readline()))[1:]
                server = Server(i, cores_rate)
                self.servers.append(server)

            self.scheduler = Scheduler(scheduler_rate, self.servers)
Ejemplo n.º 25
0
def create_server():
    form = ServerForm()
    if form.validate_on_submit():
        try:
            data = Data.query.filter_by(id=request.form['data']).all()[0]
        except:
            info = 'Нет такого Дата центра'
            return render_template('teh_info.html', info=info)
        server = Server(name_server=request.form['name_server'],
                        manufacturer=request.form['manufacturer'],
                        model_server=request.form['model_server'],
                        serial_number=request.form['serial_number'],
                        os=request.form['os'],
                        data=data)
        db.session.add(server)
        db.session.commit()
        return redirect(url_for('all_servers'))
    return render_template('create_server.html', form=form, title='Добавить')
Ejemplo n.º 26
0
def add_server():
    json_data = request.get_json()

    user = db.session.query(User).filter(
        User.steamid == json_data['owner']).first()

    if user.activated == False:
        return Response(status=401)

    server = Server(json_data['owner'])

    if 'description' in json_data:
        server.description = json_data['description']

    db.session.add(server)
    db.session.commit()

    return Response(status=201)
Ejemplo n.º 27
0
def log_server(res, db_session):
    server_data = res['server']
    server_id = int(server_data['id'])
    res_server = Server(id=server_id,
                        name=server_data['name'],
                        distance_km=server_data['d'],
                        country=server_data['country'],
                        host=server_data['host'],
                        latitude=server_data['lat'],
                        longitude=server_data['lon'],
                        sponsor=server_data['sponsor'],
                        url=server_data['url'],
                        country_code=server_data['cc'])

    if not db_session.query(Server).filter(Server.id == server_id).count():
        db_session.add(res_server)

    db_session.commit()
Ejemplo n.º 28
0
    def add_item(self, data: dict):
        new_data = ResultSchema(unknown=EXCLUDE).load(data)
        server_id = new_data.get("server").get("id")
        server = None
        if server_id:
            server = (self.db_session.query(Server).filter(
                Server.id == server_id).one_or_none())

        if server_id and not server:
            server_data = new_data.pop("server")
            server = Server(**server_data)
            self.db_session.add(server)

        new_data["server"] = server
        new_obj = Measurement(**new_data)
        self.db_session.add(new_obj)
        self.db_session.commit()
        return new_obj
Ejemplo n.º 29
0
def new_server():
    '''
    Create new server
    '''
    form = ServerForm()
    if form.validate_on_submit():
        my_server = Server()
        form.populate_obj(my_server)
        db.session.add(my_server)
        try:
            db.session.commit()
            # User info
            flash('Server created correctly', 'success')
            return redirect(url_for('servers'))
        except:
            db.session.rollback()
            flash('Error generating server.', 'danger')

    return render_template('web/new_server.html', form=form)
Ejemplo n.º 30
0
    async def constructMetadata(self, message):
        try:
            session = Session()
            session.expire_on_commit = False

            if (message.guild):
                current_server = Server(id=message.guild.id,
                                        service_id=self.service.id,
                                        server_name=message.guild.name)
            else:
                current_server = None

            current_user = User(id=message.author.id,
                                service_id=self.service.id,
                                username=message.author.display_name)

            if (message.channel.name):
                current_channel = Chat(id=message.channel.id,
                                       server_id=current_server.id,
                                       chat_name=message.channel.name,
                                       nsfw=message.channel.is_nsfw())
            else:
                current_channel = get_or_create(session,
                                                Chat,
                                                id=message.channel.id,
                                                server_id=current_server.id,
                                                chat_name=message.channel.id)

        except Exception as e:
            logger.error("Couldn't get data from message! " + str(e))
            return None

        # Metadata for use by commands and reactions
        metadata = {
            "service": self.service,
            "user": current_user,
            "server": current_server,
            "chat": current_channel,
            "message": message,
            "client": self
        }
        return metadata