<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 os
import sys

from collections import namedtuple

from fabric.api import local
from fabric.context_managers import lcd
from fabric.contrib import console

IDM_ROOT = ''
KEYSTONE_ROOT = IDM_ROOT + 'keystone/'
HORIZON_ROOT = IDM_ROOT + 'horizon/'
FIWARECLIENT_ROOT = IDM_ROOT + 'fiwareclient/'

# TODO(garcianavalon) sync this with the extension,
# see https://trello.com/c/rTsUMnjw
INTERNAL_ROLES = {
    'provider': [0, 1, 2, 3, 4],
    'purchaser': [3],
}
INTERNAL_PERMISSIONS = [
    'Manage the application',
    'Manage roles',
    'Get and assign all application roles',
    'Manage Authorizations',
    'Get and assign only owned roles',
]
CONTROLLER_PUBLIC_ADDRESS = '127.0.0.1'
CONTROLLER_ADMIN_ADDRESS = '127.0.0.1'
CONTROLLER_INTERNAL_ADDRESS = '127.0.0.1'


def deploy(dev=False):
    """Fully installs the IdM."""
    # TODO(garcianavalon) PARAMETERS!!!
    fiwareclient_install(dev=dev)
    keystone_deploy(dev=dev)
    horizon_deploy(dev=dev)
    print 'IdM successfully deployed! :)'


def fiwareclient_install(fiwareclient_path=FIWARECLIENT_ROOT,
                         dev=False):
    """ Download and install locally the fiwareclient."""
    print 'Installing the custom keystoneclient aka fiwareclient'
    if os.path.isdir(fiwareclient_path[:-1]):
        print 'already downloaded'
    else:
        local('git clone https://github.com/ging/python-keystoneclient \
        {0}'.format(fiwareclient_path))
    with lcd(fiwareclient_path):
        if dev:
            local('git checkout development')

    local('sudo pip install -e {0}'.format(fiwareclient_path[:-1]))
    print 'Done!'


def _fiwareclient_check_installation(fiwareclient_path=FIWARECLIENT_ROOT):
    """Check if fiwareclient has been correctly set up"""
    # NOTE(garcianavalon) add the fiwareclient to PYTHONPATH
    try:
        from keystoneclient.v3 import client
        return client
    except Exception, e:
        # TODO(garcianavalon) custom message/exception
        raise e


# HORIZON
def horizon_deploy(dev=False):
    """Fully installs the IdM frontend"""
    # TODO(garcianavalon) PARAMETERS!!!
    horizon_install(dev=dev)
    if dev:
        horizon_dev_runserver()
    else:
        # TODO(garcianavalon) production server!
        pass


def horizon_install(horizon_path=HORIZON_ROOT,
                    fiwareclient_path=FIWARECLIENT_ROOT,
                    dev=False):
    """Download and install Horizon and its dependencies."""
    print 'Installing frontend (Horizon)'
    local('sudo apt-get install git python-dev python-virtualenv \
        libssl-dev libffi-dev libjpeg8-dev')

    if os.path.isdir(horizon_path[:-1]):
        print 'already downloaded'
    else:
        local('git clone https://github.com/ging/horizon.git \
            {0}'.format(horizon_path))

    # NOTE(garcianavalon) lets make sure the fiwareclient is correctly set up
    fiwareclient_relative_path = ('../' + fiwareclient_path[:-1]
                                  ).replace('/', '\/')
    local("sed -i 's/-e fiwareclient/-e {fiwareclient}/g' \
        {horizon}requirements.txt".format(
        horizon=horizon_path,
        fiwareclient=fiwareclient_relative_path))

    _fiwareclient_check_installation(fiwareclient_path)
    with lcd(horizon_path):
        if dev:
            local('git checkout development')

        local('sudo python tools/install_venv.py')
        local('cp openstack_dashboard/local/local_settings.py.example \
            openstack_dashboard/local/local_settings.py')
    print 'Done!'


def horizon_dev_runserver(address='127.0.0.1:8000',
                          horizon_path=HORIZON_ROOT):
    """Run horizon server for development purposes"""
    with lcd(horizon_path):
        local('sudo tools/with_venv.sh python manage.py runserver \
            {0}'.format(address))


# KEYSTONE
def keystone_deploy(dev=False):
    """Fully installs the IdM backend"""
    # TODO(garcianavalon) PARAMETERS!!!
    keystone_install(dev=dev)
    keystone_database_create()
    if not dev:
        keystone_service_create()
        keystone_service_start()
        keystone_database_init()
    if dev:
        print 'Run fab keystone_dev_server on another terminal to \<commit_msg>removed fiwareclient in requirements.txt editing<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 os
import sys

from collections import namedtuple

from fabric.api import local
from fabric.context_managers import lcd
from fabric.contrib import console

IDM_ROOT = ''
KEYSTONE_ROOT = IDM_ROOT + 'keystone/'
HORIZON_ROOT = IDM_ROOT + 'horizon/'
FIWARECLIENT_ROOT = IDM_ROOT + 'fiwareclient/'

# TODO(garcianavalon) sync this with the extension,
# see https://trello.com/c/rTsUMnjw
INTERNAL_ROLES = {
    'provider': [0, 1, 2, 3, 4],
    'purchaser': [3],
}
INTERNAL_PERMISSIONS = [
    'Manage the application',
    'Manage roles',
    'Get and assign all application roles',
    'Manage Authorizations',
    'Get and assign only owned roles',
]
CONTROLLER_PUBLIC_ADDRESS = '127.0.0.1'
CONTROLLER_ADMIN_ADDRESS = '127.0.0.1'
CONTROLLER_INTERNAL_ADDRESS = '127.0.0.1'


def deploy(dev=False):
    """Fully installs the IdM."""
    # TODO(garcianavalon) PARAMETERS!!!
    fiwareclient_install(dev=dev)
    keystone_deploy(dev=dev)
    horizon_deploy(dev=dev)
    print 'IdM successfully deployed! :)'


def fiwareclient_install(fiwareclient_path=FIWARECLIENT_ROOT,
                         dev=False):
    """ Download and install locally the fiwareclient."""
    print 'Installing the custom keystoneclient aka fiwareclient'
    if os.path.isdir(fiwareclient_path[:-1]):
        print 'already downloaded'
    else:
        local('git clone https://github.com/ging/python-keystoneclient \
        {0}'.format(fiwareclient_path))
    with lcd(fiwareclient_path):
        if dev:
            local('git checkout development')

    local('sudo pip install -e {0}'.format(fiwareclient_path[:-1]))
    print 'Done!'


def _fiwareclient_check_installation(fiwareclient_path=FIWARECLIENT_ROOT):
    """Check if fiwareclient has been correctly set up"""
    # NOTE(garcianavalon) add the fiwareclient to PYTHONPATH
    try:
        from keystoneclient.v3 import client
        return client
    except Exception, e:
        # TODO(garcianavalon) custom message/exception
        raise e


# HORIZON
def horizon_deploy(dev=False):
    """Fully installs the IdM frontend"""
    # TODO(garcianavalon) PARAMETERS!!!
    horizon_install(dev=dev)
    if dev:
        horizon_dev_runserver()
    else:
        # TODO(garcianavalon) production server!
        pass


def horizon_install(horizon_path=HORIZON_ROOT,
                    fiwareclient_path=FIWARECLIENT_ROOT,
                    dev=False):
    """Download and install Horizon and its dependencies."""
    print 'Installing frontend (Horizon)'
    local('sudo apt-get install git python-dev python-virtualenv \
        libssl-dev libffi-dev libjpeg8-dev')

    if os.path.isdir(horizon_path[:-1]):
        print 'already downloaded'
    else:
        local('git clone https://github.com/ging/horizon.git \
            {0}'.format(horizon_path))

    # NOTE(garcianavalon) lets make sure the fiwareclient is correctly set up
    _fiwareclient_check_installation(fiwareclient_path)
    with lcd(horizon_path):
        if dev:
            local('git checkout development')

        local('sudo python tools/install_venv.py')
        local('cp openstack_dashboard/local/local_settings.py.example \
            openstack_dashboard/local/local_settings.py')
    print 'Done!'


def horizon_dev_runserver(address='127.0.0.1:8000',
                          horizon_path=HORIZON_ROOT):
    """Run horizon server for development purposes"""
    with lcd(horizon_path):
        local('sudo tools/with_venv.sh python manage.py runserver \
            {0}'.format(address))


# KEYSTONE
def keystone_deploy(dev=False):
    """Fully installs the IdM backend"""
    # TODO(garcianavalon) PARAMETERS!!!
    keystone_install(dev=dev)
    keystone_database_create()
    if not dev:
        keystone_service_create()
        keystone_service_start()
        keystone_database_init()
    if dev:
        print 'Run fab keystone_dev_server on another terminal to \
{'fraction_non_alphanumeric': 0.09784798049401039, 'fraction_numerical': 0.01091911374960246, 'mean_word_length': 3.295992714025501, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4568561', 'n_tokens_mistral': 2988, 'n_tokens_neox': 2874, 'n_words': 757}