Ejemplo n.º 1
0
    def test_pip_install(self):
        pip_satchel = get_satchel('pip')
        try:
            # Initialize tmp directory.
            d = '/tmp/test_pip_install'
            if os.path.isdir(d):
                shutil.rmtree(d)
            os.makedirs(d)

            # Install pip requirements.
            with set_cwd(d):

                # Create requirements file.
                os.makedirs('roles/all')
                with open('roles/all/pip-requirements.txt', 'w') as fout:
                    print('PyYAML\n', file=fout)

                # Install without the quiet flag
                pip_satchel.verbose = GLOBAL_VERBOSE
                pip_satchel.env.quiet_flag = ''
                pip_satchel.configure()

                self.assertTrue(os.path.isdir('.env'))
                ret = getoutput('.env/bin/pip freeze | grep -i yaml')
                print('pip freeze:\n', ret)
                self.assertTrue('PyYAML' in ret)

                # Delete the virtualenv.
                shutil.rmtree(os.path.join(d, '.env'))

                # Install with the quiet flag
                pip_satchel.verbose = GLOBAL_VERBOSE
                pip_satchel.env.quiet_flag = ' -q '
                pip_satchel.configure()

                self.assertTrue(os.path.isdir('.env'))
                ret = getoutput('.env/bin/pip freeze | grep -i yaml')
                print('pip freeze:\n', ret)
                self.assertTrue('PyYAML' in ret)

        finally:
            shutil.rmtree(d)
Ejemplo n.º 2
0
    def test_diff(self):
        """
        Confirm on a multi-site multi-host environment, apache correctly reports change.
        """

        test = self.get_test_satchel()

        print('Setting paths...')
        #env.plan_storage = STORAGE_LOCAL
        env.disable_known_hosts = True
        burlap_dir = os.path.abspath(os.path.split(burlap.__file__)[0])

        print('Initializing tmp directory...')
        d = '/tmp/test_apache_change'
        if os.path.isdir(d):
            shutil.rmtree(d)
        os.makedirs(d)

        activate_cmd = '. {d}/.env/bin/activate;'.format(d=d)
        with set_cwd(d):
            print('Creating project skeleton...')
            project.create_skeleton(
                project_name='test_apache_change',
                roles='prod',
                components='apache',
            )

            assert not os.path.isfile('%s/plans/prod/000/thumbprints/test-dj-migrate-1' % d)
            assert not os.path.isfile('%s/plans/prod/000/thumbprints/test-dj-migrate-2' % d)

            # Simulate multiple remote hosts by creating aliases of localhost.
            # Note, for testing this on your localhost for a user without passwordless sudo,
            # you may have to run: `sudo chmod 777 /etc/hosts`
            # This won't work on Travis, where these will instead be set in .travis.yml.
            print('Modifying /etc/hosts...')
            env.host_string = 'localhost'
            env.hosts = [env.host_string]
            env.user = getpass.getuser()
            hosts_updated = False
            with settings(warn_only=True):
                output = test.run('cat /etc/hosts')
                if 'test-dj-migrate-1' not in output:
                    for use_sudo in (False, True):
                        print('Attempting to append to /etc/hosts with use_sudo=%s...' % use_sudo)
                        #ret = append(filename='/etc/hosts', text='127.0.0.1 test-dj-migrate-1\n127.0.0.1 test-dj-migrate-2', use_sudo=use_sudo)
                        ret = test.append(filename='/etc/hosts', text='127.0.0.1 test-dj-migrate-1\n127.0.0.1 test-dj-migrate-2', use_sudo=use_sudo)
                        print('ret:', ret)
                        print('Checking /etc/hosts content...')
                        output = test.run('cat /etc/hosts')
                        print('output:', output)
                        if 'test-dj-migrate-1' in output:
                            hosts_updated = True
                            print('Success!')
                            break
                else:
                    hosts_updated = True
            assert hosts_updated

            os.system('ln -s %s %s/' % (burlap_dir, d))

            project.update_settings({
                    'plan_storage': STORAGE_LOCAL,
                    'plan_data_dir': os.path.join(d, 'plans'),
                    'services': ['apache'],
                    'default_site': 'testsite1',
                    'default_role': 'prod',
                    # This is necessary to stop get_current_hostname() from attempting to lookup our actual hostname.
                    '_ip_to_hostname': {
                        'test-dj-migrate-1': 'test-dj-migrate-1',
                        'test-dj-migrate-2': 'test-dj-migrate-2',
                    },
                    'apache_application_name': 'testsite1',
                    'apache_server_admin_email': '*****@*****.**',
                    'apache_server_aliases_template': '{apache_locale}.mydomain.com',
                    'apache_wsgi_dir_template': '/usr/local/{apache_application_name}/wsgi',
                    'apache_wsgi_processes': 1,
                    'apache_wsgi_threads': 0,
                },
                role='all')

            project.update_settings({
                    'hosts': ['test-dj-migrate-1', 'test-dj-migrate-2'],
                    'available_sites_by_host':{
                        'test-dj-migrate-1': [
                            'testsite1',
                        ],
                        'test-dj-migrate-2': [
                            'testsite2',
                        ]
                    },
                    'sites': {
                        'testsite1': {
                            'apache_domain_template': 'testsite1.test-dj-migrate-1.com',
                        },
                        'testsite2': {
                            'apache_domain_template': 'testsite2.test-dj-migrate-2.com',
                        },
                    },
                },
                role='prod')

            # Run a deployment preview.
            kwargs = dict(
                activate_cmd=activate_cmd,
            )
            status, output = self.getstatusoutput('{activate_cmd} fab prod deploy.preview'.format(**kwargs))
            print('output:\n%s' % output)
            assert not status
            # The deployment preview should include both hosts.
            assert "[test-dj-migrate-1] Executing task 'deploy.preview'" in output
            assert "[test-dj-migrate-2] Executing task 'deploy.preview'" in output
            assert not os.path.isfile('%s/plans/prod/000/thumbprints/test-dj-migrate-1' % d)
            assert not os.path.isfile('%s/plans/prod/000/thumbprints/test-dj-migrate-2' % d)

            status, output = self.getstatusoutput(
                '{activate_cmd} '
                'fab prod debug.set_satchel_value:apache,site,xyz '
                'debug.show_satchel_items:apache'.format(**kwargs))
            assert not status
            assert ' = xyz' in output
Ejemplo n.º 3
0
    def test_migrate(self):

        burlap_dir = os.path.abspath(os.path.split(burlap.__file__)[0])

        src_dir = '/tmp/test_dj_migrate/src'

        dj_version = [1, 10, 6]

        d = '/tmp/test_dj_migrate'
        if os.path.isdir(d):
            shutil.rmtree(d)
        os.makedirs(d)

        activate_cmd = '. {d}/.env/bin/activate;'.format(d=d)
        with set_cwd(d):
            project.create_skeleton(
                project_name='test_dj_migrate',
                roles='prod',
                components='dj',
                dj_version='.'.join(map(str, dj_version)),
            )

            assert not os.path.isfile('/tmp/test_dj_migrate/plans/prod/000/thumbprints/test-dj-migrate-1')
            assert not os.path.isfile('/tmp/test_dj_migrate/plans/prod/000/thumbprints/test-dj-migrate-2')

            # Simulate multiple remote hosts my creating aliases of localhost.
            # Note, for testing this on your localhost for a user without passwordless sudo,
            # you may have to run: `sudo chmod 777 /etc/hosts`
            # This won't work on Travis, where these will instead be set in .travis.yml.
            print('Modifying /etc/hosts...')
            env.host_string = 'localhost'
            env.hosts = [env.host_string]
            env.user = getpass.getuser()
            with settings(warn_only=True):
                for use_sudo in (False, True):
                    print('Trying with use_sudo:', use_sudo)
                    try:
                        ret = append(filename='/etc/hosts', text='127.0.0.1 test-dj-migrate-1', use_sudo=use_sudo)
                        ret = append(filename='/etc/hosts', text='127.0.0.1 test-dj-migrate-2', use_sudo=use_sudo)
                        if ret is None:
                            break
                    except NetworkError:
                        print('Error modifying /etc/hosts using use_sudo=%s:' % use_sudo)
                        traceback.print_exc()

            os.system('ln -s %s %s/' % (burlap_dir, d))

            project.update_settings({
                    'plan_storage': STORAGE_LOCAL,
                    'plan_data_dir': os.path.join(d, 'plans'),
                    'services': ['dj'],
                    'dj_settings_module': 'test_dj_migrate.settings',
                    'default_site': 'testsite1',
                    'default_role': 'prod',
                    'dj_local_project_dir': 'src',
                    'dj_project_dir': '%s/src' % d,
                    'dj_manage_media': False,
                    'dj_manage_migrations': True,
                    'dj_manage_cmd': '%s/.env/bin/python manage.py' % d,
                    'dj_version': dj_version,
                    # This is necessary to stop get_current_hostname() from attempting to lookup our actual hostname.
                    '_ip_to_hostname': {
                        'test-dj-migrate-1': 'test-dj-migrate-1',
                        'test-dj-migrate-2': 'test-dj-migrate-2',
                    },
                },
                role='all')

            project.update_settings({
                    'hosts': ['test-dj-migrate-1', 'test-dj-migrate-2'],
                    'available_sites_by_host':{
                        'test-dj-migrate-1': [
                            'testsite1',
                        ],
                        'test-dj-migrate-2': [
                            'testsite2',
                        ]
                    },
                    'sites': {
                        'testsite1': {
                            'apache_domain_template': 'testsite1.test-dj-migrate-1.com',
                        },
                        'testsite2': {
                            'apache_domain_template': 'testsite2.test-dj-migrate-2.com',
                        },
                    },
                },
                role='prod')

            # Confirm both hosts are shown.
            kwargs = dict(
                activate_cmd=activate_cmd,
            )
            status, output = self.getstatusoutput('{activate_cmd} fab prod debug.list_hosts'.format(**kwargs))
            print('output:', output)
            assert 'test-dj-migrate-1' in output
            assert 'test-dj-migrate-2' in output
Ejemplo n.º 4
0
def test_deploy():
    """
    Creates a multi-site Apache Django powered web server with a MySQL backend.
    """

    try:

        # Delete any old tmp files
        PROJECT_DIR = '/tmp/burlap_test'
        if os.path.exists(PROJECT_DIR):
            shutil.rmtree(PROJECT_DIR)
        os.makedirs(PROJECT_DIR)

        # Create our test virtualenv.
        PYTHON_EXE = os.path.split(sys.executable)[-1]
        VIRTUALENV_DIR = os.path.join(PROJECT_DIR, '.env')
        BURLAP_DIR = os.path.abspath('./burlap')
        BURLAP_BIN = os.path.abspath('./bin/burlap')
        SITE_PACKAGES = os.path.join(VIRTUALENV_DIR,
                                     'lib/%s/site-packages' % PYTHON_EXE)

        # Initialize project.
        kwargs = dict(
            project_dir=PROJECT_DIR,
            burlap_bin=BURLAP_BIN,
        )
        assert not os.system(
            'cd {project_dir}; {burlap_bin} skel multitenant'.format(**kwargs))

        # Symlink burlap.
        assert not os.system('ln -s %s %s' % (BURLAP_DIR, SITE_PACKAGES))

        # Add production role.
        VIRTUALENV_ACTIVATE = '. %s/bin/activate' % VIRTUALENV_DIR
        kwargs = dict(
            project_dir=PROJECT_DIR,
            activate=VIRTUALENV_ACTIVATE,
            burlap_bin=BURLAP_BIN,
        )
        assert not os.system(
            'cd {project_dir}; {burlap_bin} add-role prod'.format(**kwargs))

        # Test logging in to VM.
        print('env.host_string:', env.host_string, env.host)
        print('env.user:'******'env.key_filename:', env.key_filename)
        env.ROLE = 'prod'
        prod_settings = Shelf(filename=PROJECT_DIR +
                              '/roles/{role}/settings.yaml')
        prod_settings['hosts'] = [env.host] if isinstance(
            env.host, basestring) else env.host
        #prod_settings['host_string']
        prod_settings['user'] = env.user
        prod_settings['key_filename'] = env.key_filename
        prod_settings['is_local'] = False
        prod_settings['app_name'] = 'multitenant'
        kwargs = dict(project_dir=PROJECT_DIR, )
        assert not os.system(
            'cd {project_dir}; . ./shell; fab prod shell:command="echo hello"'.
            format(**kwargs))
        out = getoutput(
            'cd {project_dir}; . ./shell; fab prod shell:command="ifconfig"'.
            format(**kwargs))
        print('out:', out)
        assert 'inet addr:10.0.2.15' in out

        # Add services.
        services = prod_settings.get('services', [])
        services.extend([
            'apache',
            #'hostname',
            'mysql',
            'mysqlclient',
            'ntpclient',
            'packager',
            'pip',
            'sshnice',
            'tarball',
            'timezone',
            'ubuntumultiverse',
            'unattendedupgrades',
            #'user',
        ])
        prod_settings.set('services', services)
        prod_settings.set(
            'sites', {
                'multitenant': {
                    'apache_domain_template': 'multitenant.test.com',
                    'apache_domain_with_sub_template': 'multitenant.test.com',
                    'apache_domain_without_sub_template':
                    'multitenant.test.com',
                    'apache_server_aliases_template': 'multitenant.test.com',
                    'apache_ssl': False,
                    'apache_auth_basic': False,
                    'apache_enforce_subdomain': False,
                }
            })

        # Confirm deployment changes are detected.
        with set_cwd(PROJECT_DIR):
            #from burlap import role_prod as prod
            prod = load_role_handler('prod')
            #         print('A', prod)
            prod()
            assert 'app_name' in env
            assert 'sites' in env
            env.host_string = env.hosts[0]
            #         print('B')
            #         print('env.host_string:', env.host_string, env.host)
            #         print('env.user:'******'env.key_filename:', env.key_filename)

            changed_components, deploy_funcs = deploy_preview()
            print('changed_components:', changed_components)
            assert changed_components == [
                'MYSQLCLIENT',
                'UBUNTUMULTIVERSE',
                'PACKAGER',
                'PIP',
                'SSHNICE',
                'TIMEZONE',
                'UNATTENDEDUPGRADES',
                'APACHE',
                'MYSQL',
                'NTPCLIENT',
            ]
            print('deploy_funcs:', deploy_funcs)
            assert deploy_funcs == [
                ('mysqlclient.configure', None),
                ('ubuntumultiverse.configure', None),
                ('packager.configure', None),
                ('pip.configure', None),
                ('sshnice.configure', None),
                ('timezone.configure', None),
                ('unattendedupgrades.configure', None),
                ('apache.configure', None),
                ('mysql.configure', None),
                ('ntpclient.configure', None),
            ]

        # Deploy changes.

        # Confirm changes have been cleared.

        # Add Django site.

    finally:
        # Undo changes to the VM.
        pass
Ejemplo n.º 5
0
    def test_diff(self):
        """
        Confirm on a multi-site multi-host environment, apache correctly reports change.
        """
        print('Setting paths...')
        #env.plan_storage = STORAGE_LOCAL
        env.disable_known_hosts = True
        burlap_dir = os.path.abspath(os.path.split(burlap.__file__)[0])

        print('Initializing tmp directory...')
        d = '/tmp/test_apache_change'
        if os.path.isdir(d):
            shutil.rmtree(d)
        os.makedirs(d)

        activate_cmd = '. {d}/.env/bin/activate;'.format(d=d)
        with set_cwd(d):
            print('Creating project skeleton...')
            project.create_skeleton(
                project_name='test_apache_change',
                roles='prod',
                components='apache',
            )

            assert not os.path.isfile(
                '%s/plans/prod/000/thumbprints/test-dj-migrate-1' % d)
            assert not os.path.isfile(
                '%s/plans/prod/000/thumbprints/test-dj-migrate-2' % d)

            # Simulate multiple remote hosts my creating aliases of localhost.
            # Note, for testing this on your localhost for a user without passwordless sudo,
            # you may have to run: `sudo chmod 777 /etc/hosts`
            # This won't work on Travis, where these will instead be set in .travis.yml.
            print('Modifying /etc/hosts...')
            env.host_string = 'localhost'
            env.hosts = [env.host_string]
            env.user = getpass.getuser()
            with settings(warn_only=True):
                for use_sudo in (False, True):
                    ret = append(
                        filename='/etc/hosts',
                        text=
                        '127.0.0.1 test-dj-migrate-1\n127.0.0.1 test-dj-migrate-2',
                        use_sudo=use_sudo)
                    if ret is None:
                        break

            os.system('ln -s %s %s/' % (burlap_dir, d))

            project.update_settings(
                {
                    'plan_storage': STORAGE_LOCAL,
                    'plan_data_dir': os.path.join(d, 'plans'),
                    'services': ['apache'],
                    'default_site': 'testsite1',
                    'default_role': 'prod',
                    # This is necessary to stop get_current_hostname() from attempting to lookup our actual hostname.
                    '_ip_to_hostname': {
                        'test-dj-migrate-1': 'test-dj-migrate-1',
                        'test-dj-migrate-2': 'test-dj-migrate-2',
                    },
                    'apache_application_name': 'testsite1',
                    'apache_server_admin_email': '*****@*****.**',
                    'apache_server_aliases_template':
                    '{apache_locale}.mydomain.com',
                    'apache_wsgi_dir_template':
                    '/usr/local/{apache_application_name}/wsgi',
                    'apache_wsgi_processes': 1,
                    'apache_wsgi_threads': 0,
                },
                role='all')

            project.update_settings(
                {
                    'hosts': ['test-dj-migrate-1', 'test-dj-migrate-2'],
                    'available_sites_by_host': {
                        'test-dj-migrate-1': [
                            'testsite1',
                        ],
                        'test-dj-migrate-2': [
                            'testsite2',
                        ]
                    },
                    'sites': {
                        'testsite1': {
                            'apache_domain_template':
                            'testsite1.test-dj-migrate-1.com',
                        },
                        'testsite2': {
                            'apache_domain_template':
                            'testsite2.test-dj-migrate-2.com',
                        },
                    },
                },
                role='prod')

            # Run a deployment preview.
            kwargs = dict(activate_cmd=activate_cmd, )
            status, output = self.getstatusoutput(
                '{activate_cmd} fab prod deploy.preview'.format(**kwargs))
            assert not status
            # The deployment preview should include both hosts.
            assert "[test-dj-migrate-1] Executing task 'deploy.preview'" in output
            assert "[test-dj-migrate-2] Executing task 'deploy.preview'" in output
            assert not os.path.isfile(
                '%s/plans/prod/000/thumbprints/test-dj-migrate-1' % d)
            assert not os.path.isfile(
                '%s/plans/prod/000/thumbprints/test-dj-migrate-2' % d)

            status, output = self.getstatusoutput(
                '{activate_cmd} '
                'fab prod debug.set_satchel_value:apache,site,xyz '
                'debug.show_satchel_items:apache'.format(**kwargs))
            assert not status
            assert ' = xyz' in output
Ejemplo n.º 6
0
    def _test_deploy(self):
        """
        Creates a multi-site Apache Django powered web server with a MySQL backend.
        """
        try:
            set_verbose(True)
            assert 'apache_specifics' in env

            print('all_satchels:', sorted(all_satchels.keys()))
            assert len(all_satchels.keys()) == 61
            print('env.host_string:', env.host_string)
            print('env.hosts:', env.hosts)
            print('env.user:'******'/tmp/burlap_test'
            if os.path.exists(PROJECT_DIR):
                #shutil.rmtree(PROJECT_DIR)
                os.system('rm -Rf %s/*' % PROJECT_DIR)
            else:
                os.makedirs(PROJECT_DIR)

            # Create our test virtualenv.
            PYTHON_EXE = os.path.split(sys.executable)[-1]
            VIRTUALENV_DIR = os.path.join(PROJECT_DIR, '.env')
            BURLAP_DIR = os.path.abspath(os.path.join(BASE_DIR, 'burlap'))
            BURLAP_BIN = os.path.abspath(
                os.path.join(BASE_DIR, 'bin/burlap-admin.py'))
            SITE_PACKAGES = os.path.join(VIRTUALENV_DIR,
                                         'lib/%s/site-packages' % PYTHON_EXE)

            # Initialize project.
            kwargs = dict(
                project_dir=PROJECT_DIR,
                burlap_bin=BURLAP_BIN,
            )
            print('Initializing project skeleton...')
            assert os.path.isdir(PROJECT_DIR)
            with set_cwd(PROJECT_DIR):

                _status, _output = self.bash(
                    '{burlap_bin} skel multitenant'.format(**kwargs))
                print('_status, _output:', _status, _output)
                assert not _status

                # Symlink burlap.
                _status, _output = self.bash('ln -s %s %s' %
                                             (BURLAP_DIR, SITE_PACKAGES))
                #assert not _status

                # Add production role.
                VIRTUALENV_ACTIVATE = '. %s/bin/activate' % VIRTUALENV_DIR
                kwargs = dict(
                    project_dir=PROJECT_DIR,
                    activate=VIRTUALENV_ACTIVATE,
                    burlap_bin=BURLAP_BIN,
                )
                assert os.path.isdir(PROJECT_DIR)

                _status, _output = self.bash(
                    '{burlap_bin} add-role prod'.format(**kwargs))
                assert not _status

                # Test logging in to VM.
                print('env.host_string:', env.host_string)
                print('env.user:'******'env.key_filename:', env.key_filename)
                env.ROLE = 'prod'
                prod_settings = Shelf(
                    filename='%s/roles/{role}/settings.yaml' % PROJECT_DIR)
                prod_settings['hosts'] = [env.host_string]
                assert prod_settings['hosts'][0]
                #prod_settings['host_string']
                prod_settings['user'] = env.user
                prod_settings['key_filename'] = env.key_filename
                prod_settings['is_local'] = False
                prod_settings['app_name'] = 'multitenant'
                kwargs = dict(project_dir=PROJECT_DIR, )
                self.bash('ls -lah .')
                assert os.path.isdir(PROJECT_DIR)

                print('Testing hello world...')
                _status, _output = self.bash(
                    '.env/bin/fab prod:verbose=1 shell:command="echo hello"'.
                    format(**kwargs))
                print('_status, _output:', _status, _output)
                assert not _status
                _status, _output = self.bash('ls -lah .')
                print('_status, _output:', _status, _output)

                print('Testing ifconfig...')
                _status, _output = self.bash(
                    '.env/bin/fab prod:verbose=1 shell:command="ifconfig"'.
                    format(**kwargs))
                print('_status, _output:', _status, _output)
                assert 'inet addr:127.0.0.1' in _output

                # Add services.
                services = prod_settings.get('services', [])
                services.extend([
                    'apache',
                    #'hostname',
                    'mysql',
                    'mysqlclient',
                    'ntpclient',
                    'packager',
                    'pip',
                    'sshnice',
                    'tarball',
                    'timezone',
                    'ubuntumultiverse',
                    'unattendedupgrades',
                    #'user',
                ])
                prod_settings.set('services', services)
                prod_settings.set(
                    'sites', {
                        'multitenant': {
                            'apache_domain_template': 'multitenant.test.com',
                            'apache_domain_with_sub_template':
                            'multitenant.test.com',
                            'apache_domain_without_sub_template':
                            'multitenant.test.com',
                            'apache_server_aliases_template':
                            'multitenant.test.com',
                            'apache_ssl': False,
                            'apache_auth_basic': False,
                            'apache_enforce_subdomain': False,
                        },
                    })
                prod_settings.set('pip_requirements', 'pip-requirements.txt')
                prod_settings.set('packager_initial_upgrade', False)

                # Confirm deployment changes are detected.
                #from burlap import role_prod as prod
                prod = load_role_handler('prod')
                prod()
                assert 'app_name' in env
                assert 'sites' in env
                env.host_string = env.hosts[0]

                print('-' * 80)
                set_verbose(1)
                assert 'apache_specifics' in env
                print('Getting changed_components.verbose:', get_verbose())
                changed_components, deploy_funcs = deploy_satchel.get_component_funcs(
                )
                changed_components = sorted(changed_components)
                expected_components = [
                    'APACHE',
                    'MYSQL',
                    'MYSQLCLIENT',
                    'NTPCLIENT',
                    'PACKAGER',
                    'PIP',
                    'SSHNICE',
                    'TARBALL',
                    'TIMEZONE',
                    'UBUNTUMULTIVERSE',
                    'UNATTENDEDUPGRADES',
                ]
                print('changed_components:', changed_components)
                print('expected_components:', expected_components)
                assert changed_components == expected_components
                deploy_funcs = sorted(deploy_funcs)
                print('deploy_funcs:', deploy_funcs)
                assert deploy_funcs == [
                    ('apache.configure', None),
                    ('mysql.configure', None),
                    ('mysqlclient.configure', None),
                    ('ntpclient.configure', None),
                    ('packager.configure', None),
                    ('pip.configure', None),
                    ('sshnice.configure', None),
                    ('tarball.configure', None),
                    ('timezone.configure', None),
                    ('ubuntumultiverse.configure', None),
                    ('unattendedupgrades.configure', None),
                ]

            # Deploy changes.

            # Confirm changes have been cleared.

            # Add Django site.

        finally:
            # Undo changes to the VM.
            pass
Ejemplo n.º 7
0
    def _test_project(self):
        try:
            project_dir = '/tmp/burlap_test_project'  #tempfile.mkdtemp()
            if not os.path.isdir(project_dir):
                os.makedirs(project_dir)
            bin_dir = os.path.split(sys.executable)[0]

            with set_cwd(project_dir):
                cmd = ('. {bin_dir}/activate; '
                       'burlap-admin.py skel myproject').format(**locals())
                print(cmd)
                ret = os.system(cmd)
                print('ret:', ret)
                assert not ret

                cmd = ('. {bin_dir}/activate; '
                       'burlap-admin.py add-role prod dev').format(**locals())
                print(cmd)
                ret = os.system(cmd)
                print('ret:', ret)
                assert not ret

                if not os.path.isdir('satchels'):
                    os.makedirs('satchels')
                os.system('touch satchels/__init__.py')
                open('satchels/junk.py', 'w').write("""
from burlap import Satchel
from burlap.constants import *
from burlap.decorators import task

class JunkSatchel(Satchel):

    name = 'junk'

    def set_defaults(self):
        self.env.param = 'default'

    @task
    def show_param(self):
        print('param:%s' % self.env.param)

    @task
    def configure(self):
        self.show_param()

junk = JunkSatchel()
""")

                open('roles/all/settings.yaml', 'w').write(
                    yaml.dump(
                        dict(
                            app_name='myproject_site',
                            default_site='myproject',
                            services=['junk'],
                            sites={},
                            junk_param='allvalue',
                        )))

                open('roles/prod/settings.yaml', 'w').write(
                    yaml.dump(
                        dict(
                            inherits='all',
                            hosts=['localhost'],
                            junk_enabled=True,
                            junk_param='prodvalue',
                        )))

                open('roles/dev/settings.yaml', 'w').write(
                    yaml.dump(
                        dict(
                            inherits='all',
                            hosts=['localhost'],
                            junk_enabled=True,
                            junk_param='devvalue',
                        )))

                ## Check prod role.

                os.system('rm -Rf .burlap')
                cmd = ('. {bin_dir}/activate; '
                       'fab prod junk.show_param').format(**locals())
                print(cmd)
                status, output = getstatusoutput(cmd)
                print('output:', output)
                assert 'param:prodvalue' in output

                os.system('rm -Rf .burlap')
                cmd = ('. {bin_dir}/activate; '
                       'fab prod deploy.preview:verbose=1').format(**locals())
                print(cmd)
                status, output = getstatusoutput(cmd)
                print('output:', output)
                assert 'junk.configure' in output

                os.system('rm -Rf .burlap')
                cmd = ('. {bin_dir}/activate; '
                       'fab prod deploy.run:yes=1').format(**locals())
                print(cmd)
                status, output = getstatusoutput(cmd)
                print('output:', output)
                assert 'param:prodvalue' in output

                ## Check dev role.

                os.system('rm -Rf .burlap')
                cmd = ('. {bin_dir}/activate; '
                       'fab dev junk.show_param').format(**locals())
                print(cmd)
                status, output = getstatusoutput(cmd)
                print('output:', output)
                assert 'param:devvalue' in output

                os.system('rm -Rf .burlap')
                cmd = ('. {bin_dir}/activate; '
                       'fab dev deploy.preview').format(**locals())
                print(cmd)
                status, output = getstatusoutput(cmd)
                print('output:', output)
                assert 'junk.configure' in output

                os.system('rm -Rf .burlap')
                cmd = ('. {bin_dir}/activate; '
                       'fab dev deploy.run:yes=1').format(**locals())
                print(cmd)
                status, output = getstatusoutput(cmd)
                print('output:', output)
                assert 'param:devvalue' in output

        finally:
            #shutil.rmtree(project_dir)
            pass
Ejemplo n.º 8
0
def test_deploy():
    """
    Creates a multi-site Apache Django powered web server with a MySQL backend.
    """
    
    try:
        
        # Delete any old tmp files
        PROJECT_DIR = '/tmp/burlap_test'
        if os.path.exists(PROJECT_DIR):
            shutil.rmtree(PROJECT_DIR)
        os.makedirs(PROJECT_DIR)
     
        # Create our test virtualenv.
        PYTHON_EXE = os.path.split(sys.executable)[-1]
        VIRTUALENV_DIR = os.path.join(PROJECT_DIR, '.env')
        BURLAP_DIR = os.path.abspath('./burlap')
        BURLAP_BIN = os.path.abspath('./bin/burlap')
        SITE_PACKAGES = os.path.join(VIRTUALENV_DIR, 'lib/%s/site-packages' % PYTHON_EXE)
     
        # Initialize project.
        kwargs = dict(
            project_dir=PROJECT_DIR,
            burlap_bin=BURLAP_BIN,
        )
        assert not os.system('cd {project_dir}; {burlap_bin} skel multitenant'.format(**kwargs))
     
        # Symlink burlap.
        assert not os.system('ln -s %s %s' % (BURLAP_DIR, SITE_PACKAGES))
        
        # Add production role.
        VIRTUALENV_ACTIVATE = '. %s/bin/activate' % VIRTUALENV_DIR
        kwargs = dict(
            project_dir=PROJECT_DIR,
            activate=VIRTUALENV_ACTIVATE,
            burlap_bin=BURLAP_BIN,
        )
        assert not os.system('cd {project_dir}; {burlap_bin} add-role prod'.format(**kwargs))
         
        # Test logging in to VM.
        print('env.host_string:', env.host_string, env.host)
        print('env.user:'******'env.key_filename:', env.key_filename)
        env.ROLE = 'prod'
        prod_settings = Shelf(filename=PROJECT_DIR+'/roles/{role}/settings.yaml')
        prod_settings['hosts'] = [env.host] if isinstance(env.host, basestring) else env.host
        #prod_settings['host_string']
        prod_settings['user'] = env.user
        prod_settings['key_filename'] = env.key_filename
        prod_settings['is_local'] = False
        prod_settings['app_name'] = 'multitenant'
        kwargs = dict(
            project_dir=PROJECT_DIR,
        )
        assert not os.system('cd {project_dir}; . ./shell; fab prod shell:command="echo hello"'.format(**kwargs))
        out = getoutput('cd {project_dir}; . ./shell; fab prod shell:command="ifconfig"'.format(**kwargs))
        print('out:', out)
        assert 'inet addr:10.0.2.15' in out
        
        # Add services.
        services = prod_settings.get('services', [])
        services.extend([
            'apache',
            #'hostname',
            'mysql',
            'mysqlclient',
            'ntpclient',
            'packager',
            'pip',
            'sshnice',
            'tarball',
            'timezone',
            'ubuntumultiverse',
            'unattendedupgrades',
            #'user',
        ])
        prod_settings.set('services', services)
        prod_settings.set('sites', {
            'multitenant': {
                'apache_domain_template': 'multitenant.test.com',
                'apache_domain_with_sub_template': 'multitenant.test.com',
                'apache_domain_without_sub_template': 'multitenant.test.com',
                'apache_server_aliases_template': 'multitenant.test.com',
                'apache_ssl': False,
                'apache_auth_basic': False,
                'apache_enforce_subdomain': False,
            }
        })
        
        # Confirm deployment changes are detected.
        with set_cwd(PROJECT_DIR):
            #from burlap import role_prod as prod
            prod = load_role_handler('prod')
    #         print('A', prod)
            prod()
            assert 'app_name' in env
            assert 'sites' in env
            env.host_string = env.hosts[0]
    #         print('B')
    #         print('env.host_string:', env.host_string, env.host)
    #         print('env.user:'******'env.key_filename:', env.key_filename)
            
            changed_components, deploy_funcs = deploy_preview()
            print('changed_components:', changed_components)
            assert changed_components == [
                'MYSQLCLIENT',
                'UBUNTUMULTIVERSE',
                'PACKAGER',
                'PIP',
                'SSHNICE',
                'TIMEZONE',
                'UNATTENDEDUPGRADES',
                'APACHE',
                'MYSQL',
                'NTPCLIENT',
            ]
            print('deploy_funcs:', deploy_funcs)
            assert deploy_funcs == [
                ('mysqlclient.configure', None),
                ('ubuntumultiverse.configure', None),
                ('packager.configure', None),
                ('pip.configure', None),
                ('sshnice.configure', None),
                ('timezone.configure', None),
                ('unattendedupgrades.configure', None),
                ('apache.configure', None),
                ('mysql.configure', None),
                ('ntpclient.configure', None),
            ]
        
        # Deploy changes.
        
        # Confirm changes have been cleared.
        
        # Add Django site.
        
    finally:
        # Undo changes to the VM.
        pass