Esempio n. 1
0
def albumlist_url(app_url, team_id, form_data, *args, **kwargs):
    try:
        app_url = scrape_links_from_text(form_data['text'])[0]
    except IndexError:
        return app_url or 'No albumlist mapped to this team (admins: use `/albumlist create` or `/albumlist url [url]` to get started)'
    flask.current_app.logger.info(f'[router]: registering {team_id} with {app_url}')
    try:
        mapping.set_mapping_for_team(team_id, app_url)
    except DatabaseError as e:
        flask.current_app.logger.error(f'[db]: {e}')
        return 'Team not authed or already registered'
    return 'Registered your Slack team with the provided Albumlist'
Esempio n. 2
0
def is_managed(team_id, app_url_or_name, heroku_token, session=requests):
    if not heroku_token or not app_url_or_name:
        return
    app_name = urlparse(app_url_or_name).hostname.split(
        '.')[0] if scrape_links_from_text(app_url_or_name) else app_url_or_name
    flask.current_app.logger.info(
        f'[heroku]: checking if {app_name} is managed...')
    url = f"{urljoin(constants.HEROKU_API_URL, 'apps')}/{app_name}"
    headers = set_heroku_headers(heroku_token)
    response = session.get(url, headers=headers, timeout=1.5)
    flask.current_app.logger.debug(
        f'[heroku][{response.status_code}]: {response.json()}')
    if response.status_code == 401:
        flask.current_app.logger.info(f'[heroku]: heroku auth failed...')
        return refresh_heroku(team_id, session)
    return heroku_token
Esempio n. 3
0
def route_commands_to_albumlist(team_id, app_url, uri, form_data, *args, **kwargs):
    if not app_url:
        return 'Failed (use `/albumlist set [url]` first to use Albumlist commands)'
    if not scrape_links_from_text(app_url):
        return 'Failed (try `/albumlist check`)'
    full_url = f'{urljoin(app_url, "slack")}/{uri}'
    flask.current_app.logger.info(f'[router]: connecting {team_id} to {full_url}...')
    try:
        response = requests.post(full_url, data=form_data, timeout=2.0)
    except requests.exceptions.Timeout:
        return 'The connection to the albumlist timed out'
    if not response.ok:
        flask.current_app.logger.error(f'[router]: connection error for {team_id} to {full_url}: {response.status_code}')
        return 'Failed'
    try:
        return flask.jsonify(response.json())
    except ValueError:
        return response.text
Esempio n. 4
0
def scale_formation(app_url_or_name,
                    heroku_token,
                    quantity=None,
                    session=requests):
    if scrape_links_from_text(app_url_or_name):
        app_url_or_name = urlparse(app_url_or_name).hostname.split('.')[0]
    url = f"{urljoin(constants.HEROKU_API_URL, 'apps')}/{app_url_or_name}/formation"
    headers = set_heroku_headers(heroku_token)
    try:
        quantity = int(quantity)
        flask.current_app.logger.info(
            f'[heroku]: scaling dyno formation to {quantity} for {app_url_or_name}...'
        )
        payload = {
            "updates": [
                {
                    "quantity": 1,
                    "size": "standard-1X" if quantity > 1 else "hobby",
                    "type": "web"
                },
                {
                    "quantity": quantity,
                    "size": "standard-1X" if quantity > 1 else "hobby",
                    "type": "worker"
                },
            ]
        }
        response = session.patch(url, headers=headers, json=payload)
    except ValueError:
        response = session.get(url, headers=headers)
    if response.ok:
        response_json = response.json()
        flask.current_app.logger.debug(
            f'[heroku]: current scale for {app_url_or_name}: {response_json}')
        return "\n".join([
            f"{dyno['quantity']} x {dyno['type']} ({dyno['size']})"
            for dyno in response_json
        ])
    flask.current_app.logger.error(
        f'[heroku]: failed to scale formation for {app_url_or_name}: {response.status_code}'
    )
    flask.current_app.logger.debug(f'[heroku]: {response.text}')
    return f':red_circle: failed to scale'
Esempio n. 5
0
def get_config_variable_for_albumlist(app_url_or_name,
                                      heroku_token,
                                      config_name,
                                      session=requests):
    if scrape_links_from_text(app_url_or_name):
        app_url_or_name = urlparse(app_url_or_name).hostname.split('.')[0]
    flask.current_app.logger.info(
        f'[heroku]: retrieving config variables for {app_url_or_name}...')
    url = f"{urljoin(constants.HEROKU_API_URL, 'apps')}/{app_url_or_name}/config-vars"
    headers = set_heroku_headers(heroku_token)
    response = session.get(url, headers=headers)
    if response.ok:
        flask.current_app.logger.info(
            f'[heroku]: retrieved config variables {app_url_or_name}: {response.json()}'
        )
        return response.json()[config_name]
    flask.current_app.logger.error(
        f'[heroku]: failed to retrieve config variables for {app_url_or_name}: {response.status_code}'
    )
Esempio n. 6
0
def check_albumlist(team_id, app_url, heroku_token, *args, **kwargs):
    if not app_url:
        return 'No albumlist mapped to this team (admins: use `/albumlist create` to get started)'
    if scrape_links_from_text(app_url):
        flask.current_app.logger.info(
            f'[router]: checking connection to {app_url} for {team_id}')
        try:
            response = requests.head(app_url, timeout=2.0)
        except requests.exceptions.Timeout:
            return 'The connection to the albumlist timed out'
        if response.ok:
            return 'OK'
        flask.current_app.logger.info(
            f'[router]: connection to {app_url} failed: {response.status_code}'
        )
        return f'Failed ({response.status_code})'
    if not heroku_token:
        return 'Missing Heroku OAuth (admins: use `/albumlist heroku`)'
    if check_and_update(team_id, app_url, heroku_token):
        return 'OK'
    return 'Failed. (admins: try running `/albumlist check` again)'