Esempio n. 1
0
    def __init__(self, *args, **kwargs):
        super(StakkrPostInstall, self).__init__(*args, **kwargs)

        try:
            package_utils.get_venv_basedir()
            _post_install(False)
        except OSError:
            msg = 'You must run setup.py from a virtualenv if you want to have '
            msg += 'the templates installed'
            print(msg)
Esempio n. 2
0
    def __init__(self, *args, **kwargs):
        super(StakkrPostInstall, self).__init__(*args, **kwargs)

        try:
            package_utils.get_venv_basedir()
            _post_install(False)
        except OSError:
            msg = 'You must run setup.py from a virtualenv if you want to have '
            msg += 'the templates installed'
            print(msg)
Esempio n. 3
0
    def __init__(self, *args, **kwargs):
        """Inherit from setup install class and ensure we are in a venv."""
        super(StakkrPostInstall, self).__init__(*args, **kwargs)

        try:
            package_utils.get_venv_basedir()
            _post_install(False)
        except OSError:
            msg = 'You must run setup.py from a virtualenv if you want to have'
            msg += ' templates installed'
            print(msg)
Esempio n. 4
0
def _post_install(force: bool = False):
    """Create templates (directories and files)."""
    print('Post Installation : create templates')

    venv_dir = package_utils.get_venv_basedir()
    # If already installed don't do anything
    if os.path.isfile(venv_dir + '/conf/compose.ini'):
        return

    required_dirs = [
        'conf/mysql-override', 'conf/php-fpm-override', 'conf/xhgui-override',
        'data', 'home/www-data', 'home/www-data/bin', 'logs', 'plugins',
        'services', 'www'
    ]
    for required_dir in required_dirs:
        _create_dir(venv_dir, required_dir, force)

    required_tpls = [
        '.env', 'bash_completion', 'conf/compose.ini.tpl',
        'conf/mysql-override/mysqld.cnf', 'conf/php-fpm-override/example.conf',
        'conf/php-fpm-override/README', 'conf/xhgui-override/config.php',
        'home/www-data/.bashrc'
    ]
    for required_tpl in required_tpls:
        _copy_file(venv_dir, required_tpl, force)
Esempio n. 5
0
 def __init__(self, config_file: str = None):
     """If no config given, read the default one"""
     self.errors = dict()
     self.config_file = package_utils.get_venv_basedir(
     ) + '/conf/compose.ini'
     if config_file is not None:
         self.config_file = config_file
Esempio n. 6
0
def _post_install(force: bool = False):
    print('Post Installation : create templates')

    venv_dir = package_utils.get_venv_basedir()
    # If already installed don't do anything
    if os.path.isfile(venv_dir + '/conf/compose.ini'):
        return

    required_dirs = [
        'conf/mysql-override',
        'conf/php-fpm-override',
        'conf/xhgui-override',
        'data',
        'home/www-data',
        'home/www-data/bin',
        'logs',
        'plugins',
        'services',
        'www'
    ]
    for required_dir in required_dirs:
        _create_dir(venv_dir, required_dir, force)

    required_tpls = [
        '.env',
        'bash_completion',
        'conf/compose.ini.tpl',
        'conf/mysql-override/mysqld.cnf',
        'conf/php-fpm-override/example.conf',
        'conf/php-fpm-override/README',
        'conf/xhgui-override/config.php',
        'home/www-data/.bashrc'
    ]
    for required_tpl in required_tpls:
        _copy_file(venv_dir, required_tpl, force)
Esempio n. 7
0
    def test_default_config(self):
        """Test the default config (exit if it exists)"""
        if os.path.isfile(package_utils.get_venv_basedir() + '/conf/compose.ini'):
            return

        c = Config()
        with self.assertRaisesRegex(IOError, "Config file .*compose.ini does not exist"):
            c.read()
Esempio n. 8
0
def set_env_from_main_config(config: list):
    """Define environment variables to be used in services yaml."""
    os.environ['DOCKER_UID'] = _get_uid(config.pop('uid'))
    os.environ['DOCKER_GID'] = _get_gid(config.pop('gid'))
    os.environ['COMPOSE_BASE_DIR'] = package_utils.get_venv_basedir()

    for parameter, value in config.items():
        parameter = 'DOCKER_{}'.format(parameter.replace('.', '_').upper())
        os.environ[parameter] = str(value)
Esempio n. 9
0
def add_local_services(available_services: list):
    """Get services in the virtualenv services/ directory, so specific to that stakkr."""
    services_dir = package_utils.get_venv_basedir() + '/services'

    conf_files = _get_services_from_dir(services_dir)
    for conf_file in conf_files:
        available_services[conf_file[:-4]] = services_dir + '/' + conf_file

    return available_services
Esempio n. 10
0
    def init(force: bool):
        """CLI Entry point, when initializing stakkr manually"""

        config_file = package_utils.get_venv_basedir() + '/conf/compose.ini'
        if os.path.isfile(config_file) and force is False:
            click.secho('Config file (conf/compose.ini) already present. Leaving.', fg='yellow')
            return

        msg = 'Config file (conf/compose.ini) not present, do not forget to create it'
        click.secho(msg, fg='yellow')
        _post_install(force)
def run(ct_name: str, relative_dir: str, sugarcli_cmd: str):
    home = get_venv_basedir() + '/home/www-data'
    if os.path.isdir(home) and not os.path.isdir(home + '/bin'):
        os.mkdir(home + '/bin')

    download_sugarcli(home + '/bin/sugarcli')

    tty = 't' if sys.stdin.isatty() else ''
    cmd = ['docker', 'exec', '-u', 'www-data', '-i' + tty, ct_name]
    cmd += ['bash', '-c', '--']
    cmd += ['cd /var/' + relative_dir + '; exec /usr/bin/php ~/bin/sugarcli {}'.format(sugarcli_cmd)]
    subprocess.call(cmd, stdin=sys.stdin, stderr=subprocess.STDOUT)
Esempio n. 12
0
    def init(force: bool):
        """CLI Entry point, when initializing stakkr manually."""
        config_file = package_utils.get_venv_basedir() + '/conf/compose.ini'
        if os.path.isfile(config_file) and force is False:
            click.secho(
                'Config file (conf/compose.ini) already present. Leaving.',
                fg='yellow')
            return

        msg = "Config (conf/compose.ini) not present, don't forget to create it"
        click.secho(msg, fg='yellow')
        _post_install(force)
Esempio n. 13
0
def stakkr(ctx, config, debug, verbose):
    """Click group, set context and main object."""
    from stakkr.actions import StakkrActions

    # Add the virtual env in the path
    venv_base = package_utils.get_venv_basedir()
    sys.path.append(venv_base)

    ctx.obj['CONFIG'] = config
    ctx.obj['DEBUG'] = debug
    ctx.obj['VERBOSE'] = verbose
    ctx.obj['STAKKR'] = StakkrActions(venv_base, ctx.obj)
    ctx.obj['CTS'] = get_running_containers_name(ctx.obj['STAKKR'].project_name)
Esempio n. 14
0
def stakkr(ctx, config, debug, verbose):
    """click group, set context and main object"""

    from stakkr.actions import StakkrActions

    # Add the virtual env in the path
    venv_base = package_utils.get_venv_basedir()
    sys.path.append(venv_base)

    ctx.obj['CONFIG'] = config
    ctx.obj['DEBUG'] = debug
    ctx.obj['VERBOSE'] = verbose
    ctx.obj['STAKKR'] = StakkrActions(venv_base, ctx.obj)
    ctx.obj['CTS'] = get_running_containers_name(ctx.obj['STAKKR'].project_name)
Esempio n. 15
0
def add_services_from_plugins(available_services: list):
    """Read plugin path and extract services in subdirectories services/."""
    from pkg_resources import iter_entry_points

    # Override services with plugins
    for entry in iter_entry_points('stakkr.plugins'):
        plugin_dir = str(entry).split('=')[0].strip()
        services_dir = package_utils.get_venv_basedir(
        ) + '/plugins/' + plugin_dir + '/services'

        conf_files = _get_services_from_dir(services_dir)
        for conf_file in conf_files:
            available_services[conf_file[:-4]] = services_dir + '/' + conf_file

    return available_services
Esempio n. 16
0
def run(stakkr, composer_cmd: str):
    ct_name = docker_actions.get_ct_item('php', 'name')
    relative_dir = stakkr.cwd_relative

    if relative_dir.startswith('www') is False:
        print(click.style('You can run composer only from a subdirectory of www', fg='red'))
        sys.exit(1)

    home = get_venv_basedir() + '/home/www-data'
    if os.path.isdir(home) and not os.path.isdir(home + '/bin'):
        os.mkdir(home + '/bin')

    download_composer(home + '/bin', ct_name)

    tty = 't' if sys.stdin.isatty() else ''
    cmd = ['docker', 'exec', '-u', 'www-data', '-i' + tty, ct_name]
    cmd += ['bash', '-c', '--']
    cmd += ['cd /var/' + relative_dir + '; exec /usr/bin/php ~/bin/composer {}'.format(composer_cmd)]
    subprocess.call(cmd, stdin=sys.stdin, stderr=subprocess.STDOUT)
Esempio n. 17
0
def run(stakkr, composer_cmd: str):
    ct_name = docker_actions.get_ct_item('php', 'name')
    relative_dir = stakkr.cwd_relative

    if relative_dir.startswith('www') is False:
        print(
            click.style('You can run composer only from a subdirectory of www',
                        fg='red'))
        sys.exit(1)

    home = get_venv_basedir() + '/home/www-data'
    if os.path.isdir(home) and not os.path.isdir(home + '/bin'):
        os.mkdir(home + '/bin')

    download_composer(home + '/bin', ct_name)

    tty = 't' if sys.stdin.isatty() else ''
    cmd = ['docker', 'exec', '-u', 'www-data', '-i' + tty, ct_name]
    cmd += ['bash', '-c', '--']
    cmd += [
        'cd /var/' + relative_dir +
        '; exec /usr/bin/php ~/bin/composer {}'.format(composer_cmd)
    ]
    subprocess.call(cmd, stdin=sys.stdin, stderr=subprocess.STDOUT)
Esempio n. 18
0
"""
Test usage of plugin
"""

import os
import subprocess
import sys
import unittest
from shutil import rmtree
from stakkr import package_utils
__base_dir__ = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, __base_dir__ + '/../')
__venv_dir__ = package_utils.get_venv_basedir()


class PluginsTest(unittest.TestCase):
    cmd_base = ['stakkr', '-c', __base_dir__ + '/static/config_valid.ini']

    def test_no_plugin_dir(self):
        """Make sure I have the right message when no plugin is present"""

        clean_plugin_dir()
        os.rmdir(__venv_dir__ + '/plugins')

        cmd = self.cmd_base + ['refresh-plugins']
        res = exec_cmd(cmd)
        self.assertRegex(res['stdout'], '.*Adding plugins from plugins.*')
        self.assertRegex(res['stdout'], '.*No plugin to add*')
        self.assertEqual(res['stderr'], '')
        self.assertIs(res['status'], 0)
Esempio n. 19
0
    def test_venv_basedir(self):
        """Make sure, even in another directory, the venv base dir is correct"""
        venv_base = pu.get_venv_basedir()

        self.assertEqual(os.path.abspath(get_config_vars()['exec_prefix'] + '/../'), venv_base)
Esempio n. 20
0
 def __init__(self, config_file: str = None):
     """If no config given, read the default one"""
     self.errors = dict()
     self.config_file = package_utils.get_venv_basedir() + '/conf/compose.ini'
     if config_file is not None:
         self.config_file = config_file
Esempio n. 21
0
"""
Test usage of plugin
"""

import os
import subprocess
import sys
import unittest
from shutil import rmtree
from stakkr import package_utils
__base_dir__ = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, __base_dir__ + '/../')
__venv_dir__ = package_utils.get_venv_basedir()


class PluginsTest(unittest.TestCase):
    cmd_base = ['stakkr', '-c', __base_dir__ + '/static/config_valid.ini']

    def test_no_plugin_dir(self):
        """Make sure I have the right message when no plugin is present"""

        clean_plugin_dir()
        os.rmdir(__venv_dir__ + '/plugins')

        cmd = self.cmd_base + ['refresh-plugins']
        res = exec_cmd(cmd)
        self.assertRegex(res['stdout'], '.*Adding plugins from plugins.*')
        self.assertRegex(res['stdout'], '.*No plugin to add*')
        self.assertEqual(res['stderr'], '')
        self.assertIs(res['status'], 0)