<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2010 Asidev s.r.l.

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 plac
from . interface import BaseInterface
from . archive import ArchiveInterface


class InstanceInterface(BaseInterface):

    commands = ['list', 'deploy', 'delete', 'enable', 'disable', 'flush',
                'switch_env', 'reload', 'reload_all', 'rewrite', 'rewrite_all',
                'delete', 'archive', 'restore', 'info', 'migrate', 'kill',
                'force_reload']
    name = 'instances'

    @plac.annotations(
        domain=('Domain to deploy', 'positional', None),
        environment=('Env to deploy to', 'positional'),
        owner=('Owner of the instance', 'positional'),
        technical_contact=('Tech contact of the instance', 'option', 'c', str,
                          None, 'EMAIL'),
        theme=('Theme name', 'option', 't', str, None, 'THEME_NAME'),
        default_language=('Default language for autoredirect', 'option', 'l'),
        disabled=('Mark the instance as disabled once deployed', 'flag', 'd'),
        verbose=('Enable debugging messages', 'flag', 'v')
    )
    def deploy(self, domain, environment, owner, technical_contact=None,
               theme='', default_language='it', disabled=False, verbose=False):
        """ deploy a new instance for a given domain. """
        data = dict(
            domain=domain,
            environment_name=environment,
            owner_email=owner,
            theme_name=theme,
            default_language=default_language,
            verbose='true' if verbose else '',
            enabled='true' if not disabled else '')
        if technical_contact:
            data['technical_contact_email'] = technical_contact,
        self.api.execute_sync_task('post', self.root_url, data=data)

    @plac.annotations(domain=('The instance to enable', 'positional'))
    def enable(self, domain):
        """ Reenable a previously disabled instance """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'enable'})

    @plac.annotations(domain=('The instance to disable', 'positional'))
    def disable(self, domain):
        """ Disable an instance, i.e. it stops the vassal but keep data and db
            in place"""
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'disable'})

    @plac.annotations(domain=('The instance to flush', 'positional'))
    def flush(self, domain):
        """ Flush cache for an instance """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'flush_cache'})

    @plac.annotations(domain=('The instance to reload', 'positional'))
    def reload(self, domain):
        """ Reload the vassal for an instance """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'reload'})

    def reload_all(self):
        """ Reload all instances at once"""
        self.api.execute_sync_task('put', self.root_url,
                                   data={'action': 'reload'})

    @plac.annotations(domain=('The instance to rewrite', 'positional'))
    def rewrite(self, domain):
        """ Rewrite instance config (thus reloading the uwsgi vassal) """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'rewrite'})

    def rewrite_all(self):
        """ Rewrite all instances at once"""
        self.api.execute_sync_task('put', self.root_url,
                                   data={'action': 'rewrite'})

    @plac.annotations(
        domain=('The instance to kill', 'positional'),
    )
    def force_reload(self, domain):
        """ Kill an instance's vassal sending SIGTERM to the process """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'reload',
                                         'force': 'true'})

    @plac.annotations(domain=('The instance to kill', 'positional'))
    def kill(self, domain):
        """ Kill an instance's vassal sending a SIGKILL to the process """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'reload',
                                         'kill': 'true'})

    @plac.annotations(
        domain=('The instance to archive', 'positional'),
        name=('Archive name', 'option', 'n')
    )
    def archive(self, domain, name=None):
        """ Create an archive of an instance. """
        archives_url = "/{}".format(ArchiveInterface.name)
        params = dict(domain=domain)
        if name:
            params['name'] = name
        self.api.execute_sync_task('post', archives_url, data=params)

    @plac.annotations(
        domain=('The instance to restore', 'positional'),
        archive_file=('Archive from file', 'option', 'f', str, None, 'FILE'),
        archive=('Archive name', 'option', 'n', str, None, 'NAME')
    )
    def restore(self, domain, archive_file=None, archive=None):
        """ Restore an instance using a pre-created archive """
        params = dict(action='restore')
        # TODO handle archive upload
        if archive:
            params['archive'] = archive
        self.api.execute_sync_task('post', self.get_url(domain), data=params)

    @plac.annotations(
        domain=('The instance to delete', 'positional'),
        disable=('Disable instance before deleting', 'flag', 'd'),
        noarchive=('Do not archive before deleting', 'flag', 'n')
    )
    def delete(self, domain, disable=False, noarchive=False):
        """ Delete the selected instance """
        if disable:
            self.disable(domain)
        params = {'archive': 'true' if not noarchive else ''}
        self.api.execute_sync_task('delete', self.get_url(domain),
                                   data=params)

    @plac.annotations(
        domain=('The instance to modify', 'positional'),
        environment=('The new environment to switch to', 'positional')
    )<commit_msg>Added change_domain action to instance interface<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2010 Asidev s.r.l.

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 plac
from . interface import BaseInterface
from . archive import ArchiveInterface


class InstanceInterface(BaseInterface):

    commands = ['list', 'deploy', 'delete', 'enable', 'disable', 'flush',
                'switch_env', 'reload', 'reload_all', 'rewrite', 'rewrite_all',
                'delete', 'archive', 'restore', 'info', 'migrate', 'kill',
                'force_reload', 'change_domain']
    name = 'instances'

    @plac.annotations(
        domain=('Domain to deploy', 'positional', None),
        environment=('Env to deploy to', 'positional'),
        owner=('Owner of the instance', 'positional'),
        technical_contact=('Tech contact of the instance', 'option', 'c', str,
                          None, 'EMAIL'),
        theme=('Theme name', 'option', 't', str, None, 'THEME_NAME'),
        default_language=('Default language for autoredirect', 'option', 'l'),
        disabled=('Mark the instance as disabled once deployed', 'flag', 'd'),
        verbose=('Enable debugging messages', 'flag', 'v')
    )
    def deploy(self, domain, environment, owner, technical_contact=None,
               theme='', default_language='it', disabled=False, verbose=False):
        """ deploy a new instance for a given domain. """
        data = dict(
            domain=domain,
            environment_name=environment,
            owner_email=owner,
            theme_name=theme,
            default_language=default_language,
            verbose='true' if verbose else '',
            enabled='true' if not disabled else '')
        if technical_contact:
            data['technical_contact_email'] = technical_contact,
        self.api.execute_sync_task('post', self.root_url, data=data)

    @plac.annotations(domain=('The instance to enable', 'positional'))
    def enable(self, domain):
        """ Reenable a previously disabled instance """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'enable'})

    @plac.annotations(domain=('The instance to disable', 'positional'))
    def disable(self, domain):
        """ Disable an instance, i.e. it stops the vassal but keep data and db
            in place"""
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'disable'})

    @plac.annotations(domain=('The instance to flush', 'positional'))
    def flush(self, domain):
        """ Flush cache for an instance """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'flush_cache'})

    @plac.annotations(domain=('The instance to reload', 'positional'))
    def reload(self, domain):
        """ Reload the vassal for an instance """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'reload'})

    def reload_all(self):
        """ Reload all instances at once"""
        self.api.execute_sync_task('put', self.root_url,
                                   data={'action': 'reload'})

    @plac.annotations(domain=('The instance to rewrite', 'positional'))
    def rewrite(self, domain):
        """ Rewrite instance config (thus reloading the uwsgi vassal) """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'rewrite'})

    def rewrite_all(self):
        """ Rewrite all instances at once"""
        self.api.execute_sync_task('put', self.root_url,
                                   data={'action': 'rewrite'})

    @plac.annotations(
        domain=('The instance to kill', 'positional'),
    )
    def force_reload(self, domain):
        """ Kill an instance's vassal sending SIGTERM to the process """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'reload',
                                         'force': 'true'})

    @plac.annotations(domain=('The instance to kill', 'positional'))
    def kill(self, domain):
        """ Kill an instance's vassal sending a SIGKILL to the process """
        self.api.execute_sync_task('put', self.get_url(domain),
                                   data={'action': 'reload',
                                         'kill': 'true'})

    @plac.annotations(
        domain=('The instance to archive', 'positional'),
        name=('Archive name', 'option', 'n')
    )
    def archive(self, domain, name=None):
        """ Create an archive of an instance. """
        archives_url = "/{}".format(ArchiveInterface.name)
        params = dict(domain=domain)
        if name:
            params['name'] = name
        self.api.execute_sync_task('post', archives_url, data=params)

    @plac.annotations(
        domain=('The instance to restore', 'positional'),
        archive_file=('Archive from file', 'option', 'f', str, None, 'FILE'),
        archive=('Archive name', 'option', 'n', str, None, 'NAME')
    )
    def restore(self, domain, archive_file=None, archive=None):
        """ Restore an instance using a pre-created archive """
        params = dict(action='restore')
        # TODO handle archive upload
        if archive:
            params['archive'] = archive
        self.api.execute_sync_task('put', self.get_url(domain), data=params)

    @plac.annotations(
        domain=('The instance to rename', 'positional'),
        new_domain=('New instance domain', 'positional')
    )
    def change_domain(self, domain, new_domain):
        """ Restore an instance using a pre-created archive """
        params = dict(action='change_domain', domain=new_domain)
        self.api.execute_sync_task('put', self.get_url(domain), data=params)

    @plac.annotations(
        domain=('The instance to delete', 'positional'),
        disable=('Disable instance before deleting', 'flag', 'd'),
        noarchive=('Do not archive before deleting', 'flag', 'n')
    )
    def delete(self, domain, disable=False, noarchive=False):
        """ Delete the selected instance """
        if disable:
            self.disable(domain)
        params = {'archive': 'true' if not noarchive else ''}
        self.api.execute_sync_task('delete', self.get_url(domain),
                                   data=params)

    @plac.annotations(
        domain=('The instance to modify', 'positional'),
        environment=('The new environment to switch to', 'positional')
    )
{'fraction_non_alphanumeric': 0.13357558139534884, 'fraction_numerical': 0.001308139534883721, 'mean_word_length': 2.517638036809816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '897233', 'n_tokens_mistral': 3725, 'n_tokens_neox': 3611, 'n_words': 1059}