コード例 #1
0
def do_stop_bot():
	data = request.get_json(force=True)
	if not data.get('name', False):
		return error_response("Specify a bot name.")
	bot_name = get_bot_name(github.get('user').get('login'), data.get('name'))
	deployer.stop_bot(bot_name)
	return success_response()
コード例 #2
0
def do_delete_bot():
	data = request.get_json(force=True)
	if not data.get('name', False):
		return error_response("Specify a bot name")
	username = github.get('user').get('login')
	bot_name = get_bot_name(username, data.get('name'))
	if not deployer.delete_bot(bot_name):
		return error_response()
	return success_response()
コード例 #3
0
def do_get_log(botname, **kwargs):
	data = request.get_json(force=True)
	lines = data.get('lines', None)
	if not data.get('name', False):
		return error_response("Specify a bot name.")
	username = github.get('user').get('login')
	bot_name = get_bot_name(username, data.get('name'))
	logs = deployer.bot_log(bot_name, lines=lines)
	return success_response(logs=dict(content=logs))
コード例 #4
0
def do_process_bot():
	data = request.get_json(force=True)
	if not data.get('name', False):
		return error_response("Specify a bot name.")
	username = github.get('user').get('login')
	bot_name = get_bot_name(username, data.get('name'))
	extracted = deployer.extract_file(bot_name)
	if not extracted:
		return error_response("Failure. Bot zip file not found.")
	if not deployer.check_and_load_structure(bot_name):
		return error_response("Failure. Something's wrong with your zip file.")
	deployer.create_docker_image(bot_name)
	return success_response()
コード例 #5
0
def get_user_bots(username):
    bots = []
    bot_name_prefix = get_bot_name(username, '')
    bot_status_by_name = _get_bot_statuses(bot_name_prefix)
    for bot_name, bot_status in bot_status_by_name.items():
        zuliprc = _read_bot_zuliprc(bot_name)
        bot_info = dict(
            name=bot_name[len(bot_name_prefix):],  # remove 'username-' prefix
            status=bot_status,
            email=zuliprc['email'],
            site=zuliprc['site'],
        )
        bots.append(bot_info)
    return bots
コード例 #6
0
def upload_file():
	# check if the post request has the file part
	if 'file' not in request.files:
		flash('No file part')
		return redirect(request.url)
	file = request.files['file']
	if file.filename == '':
		flash('No selected file')
		return redirect(request.url)
	if file and allowed_file(file.filename):
		username = github.get('user').get('login')
		name, file_ext = os.path.splitext(file.filename)
		bot_name = get_bot_name(username, name)
		bot_filename = bot_name + file_ext
		file.save(os.path.join(deployer.get_bots_dir(), bot_filename))
		return success_response(message="Bot uploaded successfully. Now you need to process it.")
コード例 #7
0
    def test_get_user_bots_success(self):
        user_name = 'user1'
        bot1_name, bot1_status = get_bot_name(user_name, 'bot1'), 'running'
        bot2_name, bot2_status = get_bot_name(user_name, 'bot2'), 'paused'
        bot3_name, bot3_status = get_bot_name(user_name, 'bot3'), 'exited'
        bot_zuliprc_configs = {
            bot1_name:
            dict(email='{}@domain'.format(bot1_name),
                 site='http://{}.com'.format(bot1_name)),
            bot2_name:
            dict(email='{}@domain'.format(bot2_name),
                 site='http://{}.com'.format(bot2_name)),
            bot3_name:
            dict(email='{}@domain'.format(bot3_name),
                 site='http://{}.com'.format(bot3_name)),
        }
        bot_name_prefix = get_bot_name(user_name, '')
        expected_bot_configs = [
            {
                'name': bot1_name[len(bot_name_prefix):],
                'status': bot1_status,
                **bot_zuliprc_configs[bot1_name]
            },
            {
                'name': bot2_name[len(bot_name_prefix):],
                'status': bot2_status,
                **bot_zuliprc_configs[bot2_name]
            },
            {
                'name': bot3_name[len(bot_name_prefix):],
                'status': bot3_status,
                **bot_zuliprc_configs[bot3_name]
            },
        ]

        docker_client = test_docker_client(
            containers=[
                dict(id='c1', image_id='i1', status=bot1_status),
                dict(id='c2', image_id='i2', status=bot2_status),
                dict(id='c3', image_id='i3', status=bot3_status),
                dict(id='c4', image_id='i4', status='running'),
            ],
            images=[
                dict(id='i1',
                     tags=['{}:latest'.format(get_bot_image_name(bot1_name))]),
                dict(id='i2',
                     tags=['{}:latest'.format(get_bot_image_name(bot2_name))]),
                dict(id='i3',
                     tags=['{}:latest'.format(get_bot_image_name(bot3_name))]),
                dict(id='i4', tags=['zulip-user2-bot4:latest'])
            ])
        with patch('deployer.docker_client', new=docker_client):
            read_bot_zuliprc_mock = lambda bot_name: bot_zuliprc_configs[
                bot_name]
            with patch('deployer._read_bot_zuliprc',
                       new=read_bot_zuliprc_mock):
                actual_bot_configs = deployer.get_user_bots(user_name)
            bot_config_key = lambda bot_config: bot_config['name']
            self.assertListEqual(
                sorted(actual_bot_configs, key=bot_config_key),
                sorted(expected_bot_configs, key=bot_config_key))