Beispiel #1
0
def install(args):
    " Install site from sources or module "

    # Deactivate virtualenv
    if 'VIRTUAL_ENV' in environ:
        LOGGER.warning('Virtualenv enabled: %s' % environ['VIRTUAL_ENV'])

    # Install from base modules
    if args.module:
        args.src = op.join(settings.MOD_DIR, args.module)
        assert op.exists(args.src), "Not found module: %s" % args.module

    # Fix project name
    args.PROJECT = args.PROJECT.replace('-', '_')

    args.home = op.abspath(args.path)

    # Create engine
    engine = Installer(args)
    args.deploy_dir = engine.target_dir

    # Check dir exists
    assert args.info or args.repeat or not op.exists(
        engine.target_dir), "Path %s exists. Stop deploy." % args.deploy_dir

    try:
        if args.repeat:
            site = Site(engine.target_dir)
            site.run_install()
            return site

        site = engine.clone_source()
        if not site:
            return True

        engine.build()
        site.run_install()
        return site

    except (CalledProcessError, AssertionError):
        print "Installation failed"
        print "Fix errors and repeat installation with (-r) or run 'makesite uninstall %s' for cancel." % args.deploy_dir
        raise
Beispiel #2
0
    def clone_source(self):
        " Clone source and prepare templates "

        print_header('Clone src: %s' % self.src, '-')

        # Get source
        source_dir = self._get_source()

        # Append settings from source
        self.read(op.join(source_dir, settings.CFGNAME))

        self.templates += (self.args.template or self.template).split(',')
        self.templates = OrderedSet(self._gen_templates(self.templates))
        self['template'] = ','.join(str(x[0]) for x in self.templates)

        print_header('Deploy templates: %s' % self.template, sep='-')
        with open(op.join(self.deploy_dir, settings.TPLNAME), 'w') as f:
            f.write(self.template)

        with open(op.join(self.deploy_dir, settings.CFGNAME), 'w') as f:
            self['deploy_dir'], tmp_dir = self.target_dir, self.deploy_dir
            self.write(f)
            self['deploy_dir'] = tmp_dir

        # Create site
        site = Site(self.deploy_dir)

        # Prepare templates
        for template_name, template in self.templates:
            site.paste_template(template_name, template, tmp_dir)

        # Create site
        if self.args.info:
            print_header('Project context', sep='-')
            LOGGER.info(site.get_info(full=True))
            return None

        # Check requirements
        call('sudo chmod +x %s/*.sh' % self.service_dir)
        site.run_check(service_dir=self.service_dir)

        # Save options
        site.write()

        return site
Beispiel #3
0
    def test_site(self):
        args = FakeArgs(
            template='django',
            branch='feature/red-alert',
            src=op.join(settings.MOD_DIR, 'django'),
        )

        # Init engine
        engine = Installer(args)
        self.assertTrue(op.isdir(engine.deploy_dir))
        self.assertEqual(engine.templates, ['base'])

        # Clone source
        engine.clone_source()
        self.assertTrue(op.isdir(op.join(engine.deploy_dir, 'source')))
        self.assertTrue(op.isfile(op.join(engine.deploy_dir, 'source', 'Makefile')))
        self.assertEqual(engine.template, 'base,src-dir,virtualenv,django')
        self.assertEqual(engine.target_dir, op.join(args.home, args.PROJECT, args.branch.replace('/', '-')))
        self.assertEqual(engine.django_settings, 'settings.dev')
        self.assertEqual(engine.templates, [
            ('base', op.join(settings.TPL_DIR, 'base')),
            ('src-dir', op.join(settings.TPL_DIR, 'src-dir')),
            ('virtualenv', op.join(settings.TPL_DIR, 'virtualenv')),
            ('django', op.join(settings.TPL_DIR, 'django')),
        ])
        self.assertTrue(op.isfile(op.join(engine.deploy_dir, settings.TPLNAME)))
        self.assertTrue(op.isfile(op.join(engine.deploy_dir, 'service', 'django_install.sh')))
        self.assertTrue(op.isfile(op.join(engine.deploy_dir, settings.CFGNAME)))

        # Build site
        engine.build()

        # Init site
        site = Site(engine.target_dir)
        self.assertEqual(site.get_name(), u'main.feature-red-alert')
        self.assertEqual(site.get_info(), u'main.feature-red-alert [base,src-dir,virtualenv,django]')
        self.assertTrue('www-data' in site.get_info(full=True))

        with self.assertRaises(AssertionError):
            site.add_template('django')
        self.assertEqual(site._get_template_path('zeta'), op.join(settings.TPL_DIR, 'zeta'))
        site.add_template('zeta')
        site.run_install('zeta')
        zeta_scripts = list(site._gen_scripts('install', template_name='zeta'))
        self.assertEqual(zeta_scripts, [u'/tmp/main/feature-red-alert/service/zeta_install_update.sh'])
        self.assertEqual(site.get_info(), u'main.feature-red-alert [base,src-dir,virtualenv,django,zeta]')

        self.assertEqual(site['safe_branch'], 'feature-red-alert')

        site.remove_template('zeta')
        site.run_remove('zeta')
        self.assertEqual(site.get_info(), u'main.feature-red-alert [base,src-dir,virtualenv,django]')

        # Find site
        self.assertTrue(find_site(site.deploy_dir))
        self.assertTrue(find_site(site.get_name()))

        # Gen scripts
        self.assertTrue(op.join(site.service_dir, 'django_install.sh') in site._gen_scripts('install'))

        sites = list(gen_sites(op.dirname(engine.deploy_dir)))
        self.assertTrue(sites)
        for s in sites:
            s.clean()
Beispiel #4
0
def template(args):
    " Add or remove templates from site. "
    site = Site(args.PATH)
    if args.ACTION == "add":
        return site.add_template(args.TEMPLATE)
    return site.remove_template(args.TEMPLATE)
Beispiel #5
0
    def test_site(self):
        from makesite import settings

        args = FakeArgs(
            template='django',
            branch='feature/red-alert',
            home=settings.MAKESITE_HOME,
            src=op.join(settings.MOD_DIR, 'django'),
        )

        # Init engine
        engine = Installer(args)
        self.assertTrue(op.isdir(engine.deploy_dir))
        self.assertEqual(engine.templates, ['base'])

        # Clone source
        engine.clone_source()
        self.assertTrue(op.isdir(op.join(engine.deploy_dir, 'source')))
        self.assertTrue(
            op.isfile(op.join(engine.deploy_dir, 'source', 'Makefile')))
        self.assertEqual(engine.template, 'base,src-dir,virtualenv,django')
        self.assertEqual(
            engine.target_dir,
            op.join(args.home, args.PROJECT, args.branch.replace('/', '-')))
        self.assertEqual(engine.django_settings, 'settings.dev')
        self.assertEqual(engine.templates, [
            ('base', op.join(settings.TPL_DIR, 'base')),
            ('src-dir', op.join(settings.TPL_DIR, 'src-dir')),
            ('virtualenv', op.join(settings.TPL_DIR, 'virtualenv')),
            ('django', op.join(settings.TPL_DIR, 'django')),
        ])
        self.assertTrue(op.isfile(op.join(engine.deploy_dir,
                                          settings.TPLNAME)))
        self.assertTrue(
            op.isfile(
                op.join(engine.deploy_dir, 'service', 'django_install.sh')))
        self.assertTrue(op.isfile(op.join(engine.deploy_dir,
                                          settings.CFGNAME)))

        # Build site
        engine.build()

        # Init site
        site = Site(engine.target_dir)
        self.assertEqual(site.get_name(), u'main.feature-red-alert')
        self.assertEqual(
            site.get_info(),
            u'main.feature-red-alert [base,src-dir,virtualenv,django]')
        self.assertTrue('www-data' in site.get_info(full=True))

        if version_info >= (2, 7):
            with self.assertRaises(AssertionError):
                site.add_template('django')

        self.assertEqual(site._get_template_path('zeta'),
                         op.join(settings.TPL_DIR, 'zeta'))
        site.add_template('zeta')
        site.run_install('zeta')
        zeta_scripts = list(site._gen_scripts('install', template_name='zeta'))
        self.assertEqual(
            zeta_scripts,
            [u'/tmp/main/feature-red-alert/service/zeta_install_update.sh'])
        self.assertEqual(
            site.get_info(),
            u'main.feature-red-alert [base,src-dir,virtualenv,django,zeta]')

        self.assertEqual(site['safe_branch'], 'feature-red-alert')

        site.remove_template('zeta')
        site.run_remove('zeta')
        self.assertEqual(
            site.get_info(),
            u'main.feature-red-alert [base,src-dir,virtualenv,django]')

        # Find site
        self.assertTrue(find_site(site.deploy_dir))
        self.assertTrue(find_site(site.get_name()))

        # Gen scripts
        self.assertTrue(
            op.join(site.service_dir, 'django_install.sh') in
            site._gen_scripts('install'))

        sites = list(gen_sites(op.dirname(engine.deploy_dir)))
        self.assertTrue(sites)
        for s in sites:
            s.clean()