<commit_before># Copyright (C) 2014 Universidad Politecnica de Madrid
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import ConfigParser
import json
import os
import string

from conf import settings

from keystoneclient.v3 import client

from fabric.api import task
from fabric.tasks import Task
from fabric.state import env
from fabric.api import execute


@task
def install(keystone_path=settings.KEYSTONE_ROOT):
    """Download and install the Back-end and its dependencies."""
    if env.exists(keystone_path[:-1]):
        print 'Already downloaded.'
    else:
        env.run('git clone https://github.com/ging/keystone.git \
            {0}'.format(keystone_path))
    with env.cd(keystone_path):
        dependencies = ' '.join(settings.UBUNTU_DEPENDENCIES['keystone'])
        
        env.run('sudo apt-get install {0}'.format(dependencies))
        env.run('sudo cp etc/keystone.conf.sample etc/keystone.conf')
        env.run('sudo python tools/install_venv.py')

        # Uncomment config file
        with env.cd('etc/'):
            env.run(("sudo sed -i "
                "'s/#admin_token=ADMIN/admin_token={0}/g' " 
                "keystone.conf").format(settings.KEYSTONE_ADMIN_TOKEN))
            env.run(("sudo sed -i "
                "'s/#admin_port=35357/admin_port={0}/g' "
                "keystone.conf").format(settings.KEYSTONE_ADMIN_PORT))
            env.run(("sudo sed -i "
                "'s/#public_port=5000/public_port={0}/g' "
                "keystone.conf").format(settings.KEYSTONE_PUBLIC_PORT))
    # TODO(garcianavalon) add the endpoint filter extension to pipeline
    # and the driver in the [catalog] section
    # driver = keystone.contrib.endpoint_filter.backends.
    # catalog_sql.EndpointFilterCatalog 
    print 'Done!'

@task
def database_create(keystone_path=settings.KEYSTONE_ROOT, verbose=True,
                    mysql_user=False):
    if mysql_user:
        env.run('mysql -u {0} -p '.format(mysql_user))
        # TODO(garcianavalon) this is not executing inside mysql shell
        env.run('CREATE DATABASE {0};'.format(
            settings.KEYSTONE_PROD_DATABASE))
        # TODO(garcianavalon) grant all privileges!
        # TODO(garcianavlaon) connection string!!

    add_verbose = '-v' if verbose else ''
    with env.cd(keystone_path):
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension endpoint_filter').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension=oauth2').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension=roles').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension=user_registration').format(v=add_verbose))

@task
def database_delete(keystone_path=settings.KEYSTONE_ROOT,
                    keystone_db=settings.KEYSTONE_DEV_DATABASE,
                    mysql_user=False):
    if mysql_user:
        env.run('mysql -u {0} -p '.format(mysql_user))
        env.run('DROP DATABASE {0};'.format(settings.KEYSTONE_PROD_DATABASE))
    else:
        db_path = keystone_path + keystone_db
        if os.path.isfile(db_path):
            env.run('sudo rm ' + db_path)

@task
def database_reset(keystone_path=settings.KEYSTONE_ROOT, mysql_user=False):
    """Deletes keystone's database and create a new one, populated with
    the base data needed by the IdM. Requires a keystone instance running.
    """
    execute(database_delete, keystone_path=keystone_path,
            mysql_user=mysql_user)
    execute(database_create, keystone_path=keystone_path,
            mysql_user=mysql_user)
    execute('keystone.populate', keystone_path=keystone_path)


@task
def set_up_as_service(absolute_keystone_path=None):
    if not absolute_keystone_path:
        absolute_keystone_path = os.getcwd() + '/' + settings.KEYSTONE_ROOT
    in_file = open('conf/keystone_idm.conf')
    src = string.Template(in_file.read())
    out_file = open("tmp_keystone_idm.conf", "w")
    out_file.write(src.substitute({
        'absolute_keystone_path': absolute_keystone_path}))
    out_file.close()
    env.run('sudo cp tmp_keystone_idm.conf /etc/init/keystone_idm.conf')
    env.run('sudo rm tmp_keystone_idm.conf')

@task
def start():
    """Runs the service."""
    env.run('sudo service keystone_idm start')

@task
def stop():
    """Stops the service."""
    env.run('sudo service keystone_idm stop')

@task
def restart():<commit_msg>removed mysql database support for now<commit_after># Copyright (C) 2014 Universidad Politecnica de Madrid
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import ConfigParser
import json
import os
import string

from conf import settings

from keystoneclient.v3 import client

from fabric.api import task
from fabric.tasks import Task
from fabric.state import env
from fabric.api import execute


@task
def install(keystone_path=settings.KEYSTONE_ROOT):
    """Download and install the Back-end and its dependencies."""
    if env.exists(keystone_path[:-1]):
        print 'Already downloaded.'
    else:
        env.run('git clone https://github.com/ging/keystone.git \
            {0}'.format(keystone_path))
    with env.cd(keystone_path):
        dependencies = ' '.join(settings.UBUNTU_DEPENDENCIES['keystone'])
        
        env.run('sudo apt-get install {0}'.format(dependencies))
        env.run('sudo cp etc/keystone.conf.sample etc/keystone.conf')
        env.run('sudo python tools/install_venv.py')

        # Uncomment config file
        with env.cd('etc/'):
            env.run(("sudo sed -i "
                "'s/#admin_token=ADMIN/admin_token={0}/g' " 
                "keystone.conf").format(settings.KEYSTONE_ADMIN_TOKEN))
            env.run(("sudo sed -i "
                "'s/#admin_port=35357/admin_port={0}/g' "
                "keystone.conf").format(settings.KEYSTONE_ADMIN_PORT))
            env.run(("sudo sed -i "
                "'s/#public_port=5000/public_port={0}/g' "
                "keystone.conf").format(settings.KEYSTONE_PUBLIC_PORT))
    # TODO(garcianavalon) add the endpoint filter extension to pipeline
    # and the driver in the [catalog] section
    # driver = keystone.contrib.endpoint_filter.backends.
    # catalog_sql.EndpointFilterCatalog 
    print 'Done!'

@task
def database_create(keystone_path=settings.KEYSTONE_ROOT, verbose=True,
                    mysql_user=False):
    if mysql_user:
        env.run('mysql -u {0} -p '.format(mysql_user))
        # TODO(garcianavalon) this is not executing inside mysql shell
        env.run('CREATE DATABASE {0};'.format(
            settings.KEYSTONE_PROD_DATABASE))
        # TODO(garcianavalon) grant all privileges!
        # TODO(garcianavlaon) connection string!!

    add_verbose = '-v' if verbose else ''
    with env.cd(keystone_path):
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension endpoint_filter').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension=oauth2').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension=roles').format(v=add_verbose))
        env.run(('sudo tools/with_venv.sh bin/keystone-manage {v}'
            ' db_sync --extension=user_registration').format(v=add_verbose))

@task
def database_delete(keystone_path=settings.KEYSTONE_ROOT):
    db_path = keystone_path + settings.KEYSTONE_DEV_DATABASE
    if os.path.isfile(db_path):
        env.run('sudo rm ' + db_path)

@task
def database_reset(keystone_path=settings.KEYSTONE_ROOT):
    """Deletes keystone's database and create a new one, populated with
    the base data needed by the IdM. Requires a keystone instance running.
    """
    execute('keystone.database_delete', keystone_path=keystone_path)
    execute('keystone.database_create', keystone_path=keystone_path)
    execute('keystone.populate', keystone_path=keystone_path)


@task
def set_up_as_service(absolute_keystone_path=None):
    if not absolute_keystone_path:
        absolute_keystone_path = os.getcwd() + '/' + settings.KEYSTONE_ROOT
    in_file = open('conf/keystone_idm.conf')
    src = string.Template(in_file.read())
    out_file = open("tmp_keystone_idm.conf", "w")
    out_file.write(src.substitute({
        'absolute_keystone_path': absolute_keystone_path}))
    out_file.close()
    env.run('sudo cp tmp_keystone_idm.conf /etc/init/keystone_idm.conf')
    env.run('sudo rm tmp_keystone_idm.conf')

@task
def start():
    """Runs the service."""
    env.run('sudo service keystone_idm start')

@task
def stop():
    """Stops the service."""
    env.run('sudo service keystone_idm stop')

@task
def restart():
{'fraction_non_alphanumeric': 0.11533107973752237, 'fraction_numerical': 0.005567707297673493, 'mean_word_length': 3.228247162673392, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2060818', 'n_tokens_mistral': 3321, 'n_tokens_neox': 3112, 'n_words': 728}