Exemple #1
0
class LogEcho(threading.Thread):
    def __init__(self, user, server, filename, target_lines=None, filters=[]):
        self.logfile = filename
        threading.Thread.__init__(self)
        self.remote = RemoteConnection(user, server)
        self.count = 0
        self.target_lines = target_lines
        self.show_progress_bar = self.target_lines is not None
        self.process = None
        self.finished = False
        self.filters = filters

    def stop(self):
        print
        try:
            self.process.kill()
        except:
            print padded_log('An error occurred when stopping logger')

    def run(self):
        def tail_log(line, stdin, process):
            # At first line reception, store echo logger process
            # and instantiate progress bar if needed
            if self.process is None:
                self.process = process
                if self.show_progress_bar:
                    self.pacman = Pacman(text='    Progress')

            # Determine if we want to count/print the line based on filters setting
            matched_filter = re.search(r'({})'.format('|'.join(self.filters)), line)
            this_line_is_good = matched_filter or self.filters == []

            # If line is good and we're not reached the 100%
            if this_line_is_good and not self.finished:
                # Update progress bar if in progressbar mode
                if self.show_progress_bar:
                    self.count += 1
                    percent = (100 * self.count) / self.target_lines
                    percent = percent if percent <= 100 else 100
                    self.finished = percent == 100
                    self.pacman.progress(percent)
                # or print line
                else:
                    print '    ' + line.rstrip()

        try:
            code, stdout = self.remote.execute(
                'tail -F -n0 {}'.format(self.logfile),
                _out=tail_log
            )
        except:
            pass
Exemple #2
0
class MaxServer(SupervisorHelpers, NginxHelpers, CommonSteps, PyramidServer):

    def __init__(self, config, *args, **kwargs):
        self.config = config

        self.process_prefix = 'max_'
        self._client = None
        self._instances = {}
        self.instance = None
        self.remote = RemoteConnection(self.config.ssh_user, self.config.server)
        self.buildout = RemoteBuildoutHelper(self.remote, self.config.python_interpreter, self)

        if not self.remote.file_exists(self.config.instances_root):
            self.remote.mkdir(self.config.instances_root)

    def get_client(self, instance_name, username='', password=''):
        instance_info = self.get_instance(instance_name)
        client = MaxClient(instance_info['server']['dns'])
        if username and password:
            client.login(username=username, password=password)
        return client

    def get_running_version(self, instance_name):
        instance_info = self.get_instance(instance_name)
        return requests.get('{}/info'.format(instance_info['server']['dns'])).json().get('version', '???')

    def get_expected_version(self, instance_name):
        versions = self.remote.get_file('{}/versions.cfg'.format(self.buildout.folder))
        return re.search(r'\smax\s=\s(.*?)\s', versions, re.MULTILINE).groups()[0]

    def get_instance(self, instance_name):
        if instance_name not in self._instances:
            max_ini = self.buildout.config_files.get(instance_name, {}).get('max.ini', '')
            if not max_ini:
                return {}

            maxconfig = parse_ini_from(max_ini)
            port_index = int(maxconfig['server:main']['port']) - MAX_BASE_PORT

            instance = OrderedDict()
            instance['name'] = instance_name
            instance['port_index'] = port_index
            instance['mongo_database'] = maxconfig['app:main']['mongodb.db_name']
            instance['server'] = {
                'direct': 'http://{}:{}'.format(self.config.server, maxconfig['server:main']['port']),
                'dns': maxconfig['app:main']['max.server']
            }
            instance['oauth'] = maxconfig['app:main']['max.oauth_server']
            self._instances[instance_name] = instance
        return self._instances[instance_name]

    def test_activity(self, instance_name, ldap_branch, restricted_user_password):

        progress_log('Testing UTalk activity notifications')

        # Get a maxclient for this instance
        padded_log("Getting instance information")
        instance_info = self.get_instance(instance_name)
        restricted_password = restricted_user_password
        client = self.get_client(instance_name, username='******', password=restricted_password)

        padded_log("Setting up test clients")
        test_users = [
            ('ulearn.testuser1', 'UTestuser1'),
            ('ulearn.testuser2', 'UTestuser2')
        ]

        utalk_clients = []
        max_clients = []

        # Syncronization primitives
        wait_for_others = AsyncResult()
        counter = ReadyCounter(wait_for_others)

        for user, password in test_users:
            max_clients.append(self.get_client(
                instance_name,
                username=user,
                password=password)
            )

            # Create websocket clients
            utalk_clients.append(getUtalkClient(
                instance_info['server']['dns'],
                instance_name,
                user,
                password,
                quiet=False
            ))
            counter.add()

        # Create users
        padded_log("Creating users and conversations")
        client.people['ulearn.testuser1'].post()
        client.people['ulearn.testuser2'].post()

        # Admin creates context with notifications enabled and subscribes users to it
        context = client.contexts.post(url='http://testcontext', displayName='Test Context', notifications=True)
        client.people['ulearn.testuser1'].subscriptions.post(object_url='http://testcontext')
        client.people['ulearn.testuser2'].subscriptions.post(object_url='http://testcontext')

        def post_activity():
            max_clients[0].people['ulearn.testuser1'].activities.post(
                object_content='Hola',
                contexts=[{"url": "http://testcontext", "objectType": "context"}]
            )

        # Prepare test messages for clients
        # First argument are messages to send (conversation, message)
        # Second argument are messages to expect (conversation, sender, message)
        # Third argument is a syncronization event to wait for all clients to be listening
        # Fourth argument is a method to trigger when client ready

        arguments1 = [
            [

            ],
            [
                ('test', 'test')
            ],
            counter,
            post_activity
        ]

        arguments2 = [
            [

            ],
            [
                ('test', 'test')
            ],
            counter,
            None
        ]

        padded_log("Starting websockets and waiting for messages . . .")

        greenlets = [
            gevent.spawn(utalk_clients[0].test, *arguments1),
            gevent.spawn(utalk_clients[1].test, *arguments2)
        ]

        gevent.joinall(greenlets, timeout=20, raise_error=True)

        success = None not in [g.value for g in greenlets]

        if success:
            padded_success('Websocket test passed')
        else:
            padded_error('Websocket test failed, Timed out')

    # Steps used in commands. Some of them defined in gummanager.libs.mixins

    def configure_instance(self):

        customizations = {
            'mongodb-config': {
                'replica_set': self.config.mongodb.replica_set,
                'cluster_hosts': self.config.mongodb.cluster
            },
            'max-config': {
                'name': self.instance.name,
            },
            'ports': {
                'port_index': '{:0>2}'.format(self.instance.index),
            },
            'rabbitmq-config': {
                'username': self.config.rabbitmq.username,
                'password': self.config.rabbitmq.password
            },
            'hosts': {
                'max': self.config.server_dns,
                'oauth': self.config.oauth.server_dns,
                'rabbitmq': self.config.rabbitmq.server

            }
        }

        self.buildout.configure_file('customizeme.cfg', customizations),
        return success_log('Succesfully configured {}/customizeme.cfg'.format(self.buildout.folder))

    def set_mongodb_indexes(self):
        new_instance_folder = '{}/{}'.format(
            self.config.instances_root,
            self.instance.name
        )
        code, stdout = self.remote.execute('cd {0} && ./bin/max.mongoindexes'.format(new_instance_folder))
        added = 'Creating' in stdout or 'already exists' in stdout

        if added:
            return success_log("Succesfully added indexes")
        else:
            return error_log("Error on adding indexes")

    def configure_max_security_settings(self):
        new_instance_folder = '{}/{}'.format(
            self.config.instances_root,
            self.instance.name
        )
        self.buildout.folder = new_instance_folder

        self.remote.execute('cd {} && ./bin/max.security reset'.format(new_instance_folder))
        code, stdout = self.remote.execute('cd {} && ./bin/max.security add {}'.format(new_instance_folder, self.config.authorized_user))
        added = 'restart max process' in stdout
        # Force read the new configuration files

        if added:
            return success_log("Succesfully configured security settings")
        else:
            return error_log("Error configuring security settings")

    def create_max_nginx_entry(self):
        nginx_params = {
            'instance_name': self.instance.name,
            'server': self.config.server,
            'server_dns': self.config.server_dns,
            'bigmax_port': BIGMAX_BASE_PORT,
            'max_port': int(self.instance.index) + MAX_BASE_PORT,
            'nginx_root_folder': self.config.nginx.root,
            'max_root_folder': self.buildout.folder
        }
        nginxentry = MAX_NGINX_ENTRY.format(**nginx_params)

        nginx_remote = RemoteConnection(self.config.nginx.ssh_user, self.config.nginx.server)

        nginx_file_location = "{}/config/max-instances/{}.conf".format(self.config.nginx.root, self.instance.name)
        nginx_remote.put_file(nginx_file_location, nginxentry)
        return success_log("Succesfully created {}".format(nginx_file_location))

    def commit_local_changes(self):
        self.buildout.commit_to_local_branch(
            self.config.local_git_branch,
            files=[
                'customizeme.cfg',
                'mongoauth.cfg'
            ])
        return success_log("Succesfully commited local changes")

    def add_instance_to_bigmax(self):
        instances_file = ''
        if self.remote.file_exists(self.config.bigmax_instances_list):
            instances_file = self.remote.get_file(self.config.bigmax_instances_list, do_raise=True)
        if '[{}]'.format(self.instance.name) not in instances_file:
            linebreak = '\n' if instances_file else ''
            instances_file += linebreak + BIGMAX_INSTANCE_ENTRY.format(**{
                "server_dns": self.config.server_dns,
                "instance_name": self.instance.name,
            })
            self.remote.put_file(self.config.bigmax_instances_list, instances_file, do_raise=True)
        else:
            return success_log("Instance {} already in bigmax instance list".format(self.instance.name))

        return success_log("Succesfully added {} to bigmax instance list".format(self.instance.name))

    def update_buildout(self):
        result = self.buildout.upgrade(self.config.maxserver_buildout_branch, self.config.local_git_branch)
        return success(result, "Succesfully commited local changes")

    def reload_instance(self):
        self.restart(self.instance.name)
        sleep(1)
        status = self.get_status(self.instance.name)
        if status['status'] == 'running':
            return success_log("Succesfully restarted max {}".format(self.instance.name))
        else:
            return error_log('Max instance {} is not running'.format(self.instance.name))

    def check_version(self):
        running_version = self.get_running_version(self.instance.name)
        expected_version = self.get_expected_version(self.instance.name)

        if running_version == expected_version:
            return success_log("Max {} is running on {}".format(self.instance.name, running_version))
        else:
            return success_log("Max {} is running on {}, but {} was expected".format(self.instance.name, running_version, expected_version))

    # Commands

    @command
    def new_instance(self, instance_name, port_index, oauth_instance=None, logecho=None, rabbitmq_url=None):

        self.buildout.cfgfile = self.config.max.cfg_file
        self.buildout.logecho = logecho
        self.buildout.folder = '{}/{}'.format(
            self.config.instances_root,
            instance_name
        )

        self.set_instance(
            name=instance_name,
            index=port_index,
            oauth=oauth_instance if oauth_instance is not None else instance_name,
        )

        yield step_log('Cloning buildout')
        yield self.clone_buildout()

        yield step_log('Bootstraping buildout')
        yield self.bootstrap_buildout()

        yield step_log('Configuring customizeme.cfg')
        yield self.configure_instance()

        yield step_log('Configuring mongoauth.cfg')
        yield self.configure_mongoauth()

        yield step_log('Executing buildout')
        yield self.execute_buildout()

        yield step_log('Adding indexes to mongodb')
        yield self.set_mongodb_indexes()

        yield step_log('Configuring default permissions settings')
        yield self.configure_max_security_settings()

        yield step_log('Creating nginx entry for max')
        yield self.create_max_nginx_entry()

        yield step_log('Commiting to local branch')
        yield self.commit_local_changes()

        yield step_log('Changing permissions')
        yield self.set_filesystem_permissions()

        yield step_log('Adding instance to supervisor config')
        yield self.configure_supervisor()

    @command
    def upgrade(self, instance_name, logecho=None):
        self.buildout.cfgfile = self.config.max.cfg_file
        self.buildout.logecho = logecho
        self.buildout.folder = '{}/{}'.format(
            self.config.instances_root,
            instance_name
        )

        self.set_instance(
            name=instance_name,
        )

        yield step_log('Updating buildout')
        yield self.update_buildout()

        yield step_log('Executing buildout')
        yield self.execute_buildout(update=True)

        yield step_log('Changing permissions')
        yield self.set_filesystem_permissions()

        # yield step_log('Reloading max')
        yield self.reload_instance()

        yield step_log('Checking running version')
        yield self.check_version()
Exemple #3
0
class ULearnServer(GenwebServer):
    _remote_config_files = {}

    def __init__(self, *args, **kwargs):
        super(ULearnServer, self).__init__(*args, **kwargs)
        self.prefes = RemoteConnection(self.config.prefe_ssh_user, self.config.prefe_server)

    def reload_nginx_configuration(self):
        progress_log("Reloading nginx configuration")
        padded_log("Testing configuration")
        code, stdout = self.prefes.execute("/etc/init.d/nginx configtest")
        if code == 0 and "done" in stdout:
            padded_success("Configuration test passed")
        else:
            padded_error("Configuration test failed")
            return None

        code, stdout = self.prefes.execute("/etc/init.d/nginx reload")
        if code == 0 and "done" in stdout:
            padded_success("Nginx reloaded succesfully")
        else:
            padded_error("Error reloading nginx")
            return None

    def setup_nginx(self, site, max_url):

        nginx_params = {"instance_name": site.plonesite, "max_server": max_url, "mountpoint_id": site.mountpoint}
        nginxentry = ULEARN_NGINX_ENTRY.format(**nginx_params)

        success = self.prefes.put_file(
            "{}/config/ulearn-instances/{}.conf".format(self.config.prefe_nginx_root, site.plonesite), nginxentry
        )

        if success:
            return success_log(
                "Succesfully created {}/config/ulearn-instances/{}.conf".format(
                    self.config.prefe_nginx_root, site.plonesite
                )
            )
        else:
            return error_log("Error when generating nginx config file for ulean")

    def get_instance(self, environment, mountpoint, plonesite):
        site = UlearnSite(environment, mountpoint, plonesite, "", "")
        settings = site.get_settings()
        return settings

    def add_user(self, instance, user):
        """
        """
        pass

    @staticmethod
    def check_users(users):
        """
            Check user list consistency, raises on validation errors
        """

        # Look for repeated users
        usernames = [a["username"] for a in users]
        duplicates = [k for k, v in Counter(usernames).items() if v > 1]
        if duplicates:
            raise Exception("Found duplicated users: {}".format(", ".join(duplicates)))

        # Look for repeated emails
        emails = [a["email"] for a in users]
        duplicates = [k for k, v in Counter(emails).items() if v > 1]
        if duplicates:
            raise Exception("Found duplicated emails: {}".format(", ".join(duplicates)))

        # Look for users withuot password
        users_without_password = [a["username"] for a in users if a["password"].strip() == ""]
        if users_without_password:
            raise Exception("Found users without password: {}".format(", ".join(users_without_password)))

    # COMMANDS

    def batch_add_users(self, instance, usersfile):
        site = UlearnSite(self.get_environment(instance["environment"]), instance["mountpoint"], instance["plonesite"])
        try:
            users = read_users_file(usersfile, required_fields=["username", "fullname", "email", "password"])
        except Exception as exc:
            error_message = "Error parsing users file {}: {{}}".format(usersfile)
            yield raising_error_log(error_message.format(exc.message))

        try:
            self.check_users(users)
        except Exception as exc:
            yield raising_error_log(exc.message)

        yield step_log("Creating {} users ".format(len(users)))
        for count, user in enumerate(users, start=1):
            if not user:
                yield error_log("Error parsing user at line #{}".format(count))
                continue
            succeeded = site.add_user(**user)
            if not succeeded.get("error", False):
                yield success_log(succeeded["message"])
            else:
                yield error_log(succeeded["message"])

    def batch_subscribe_users(self, instance, subscriptionsfile):
        site = UlearnSite(self.get_environment(instance["environment"]), instance["mountpoint"], instance["plonesite"])
        try:
            communities = read_subscriptions_file(subscriptionsfile, required_fields=["owners", "readers", "editors"])
        except Exception as exc:
            error_message = "Error parsing subscriptionsfile file {}: {{}}".format(subscriptionsfile)
            yield raising_error_log(error_message.format(exc.message))

        for community in communities:
            yield step_log("Subscribing users to {}".format(community["url"]))

            succeeded = site.subscribe_users(**community)
            if not succeeded.get("error", False):
                yield success_log(succeeded["message"])
            else:
                yield error_log(succeeded["message"])

    def new_instance(
        self,
        instance_name,
        environment,
        mountpoint,
        title,
        language,
        max_name,
        max_direct_url,
        oauth_name,
        ldap_branch,
        ldap_password,
        logecho,
    ):

        environment = self.get_environment(environment)
        site = UlearnSite(environment, mountpoint, instance_name, title, language, logecho)

        yield step_log("Creating Plone site")
        yield site.create(packages=["ulearn.core:default"])

        yield step_log("Setting up homepage")
        yield site.setup_homepage()

        yield step_log("Setting up ldap")
        yield site.setup_ldap(ldap_branch, self.config.ldap)

        yield step_log("Setting up max")
        yield site.setup_max(max_name, oauth_name, ldap_branch)

        yield step_log("Rebuilding catalog")
        yield site.rebuild_catalog()

        yield step_log("Setting up nginx entry @ {}".format(self.config.prefe_server))
        yield self.setup_nginx(site, max_direct_url)