예제 #1
0
 def install_nginx_config(self):
     static_paths = self.options.get('static_paths', '').strip()
     www_root = utils.realpath(self.var_dir, 'www')
     rmtree(www_root)
     locations = []
     if static_paths:
         static_paths = [p.split('=') for p in static_paths.split('\n')
                                      if p.strip() and '=' in p]
         for location, path in static_paths:
             location = location.strip()
             path = path.strip()
             path = os.path.realpath(path.replace('%(here)s', self.curdir))
             if path.startswith(self.curdir) and \
                '/' not in location.strip('/'):
                 location = '/%s/' % location.strip('/')
                 www_root = utils.realpath(self.var_dir, 'www')
                 os.symlink(path, os.path.realpath(join(www_root,
                                                   location.strip('/'))))
                 locations.append(location)
     self.install_config('nginx.conf', www=www_root, locations=locations)
예제 #2
0
def admin():

    root = os.path.join(os.getcwd(), 'root')

    parser.add_option("-i", "--interpreter", dest="interpreter",
                      action="store", default=sys.executable,
                      help='Default to: ' + sys.executable)
    parser.add_option("-d", "--deploy", dest="source",
                      action="store", metavar='SOURCE',
                      default=os.environ.get('SOURCE', None),
                      help='Default to $SOURCE if exist.')
    parser.add_option("-b", "--branch", dest="branch",
                      action="store",
                      default=os.environ.get('BRANCH', None),
                      help='Default to $BRANCH if exist')
    parser.add_option("-a", "--app-name", dest="app_name",
                      action="store", default=None)
    parser.add_option("--destroy", dest="destroy",
                      action="store")
    parser.add_option("--host", dest="host",
                      action="append",
                      default=os.environ.get('HOSTS', '').split(';'))
    parser.add_option("-r", "--root", dest="root",
                      action="store", default=root,
                      help='Default to %s' % root)
    parser.add_option("-e", "--eggs", dest="eggs",
                      action="store", default=os.path.expanduser('~/eggs'),
                      help='Default to: ' + os.path.expanduser('~/eggs'))
    parser.add_option("--develop", dest="develop",
                      action="append", default=[], help="Testing only")
    parser.add_option("--develop-dir", dest="develop_dir",
                      default=os.environ.get('DEVELOP_DIR', None),
                      help=("Used for buildout:develop-dir. "
                            "Default to $DEVELOP_DIR if exist"))

    (options, args) = parser.parse_args()

    if 'GIT_DIR' in os.environ:
        del os.environ['GIT_DIR']

    if not options.root:
        parser.error('Invalid root option')

    root = utils.realpath(options.root)

    if not os.path.isdir(options.root):
        parser.error('Invalid root option')

    os.chdir(root)

    def guess_app():
        for ext in ('.hg', '.git'):
            app_dirs = glob(join(root, '*', ext))
            if len(app_dirs) == 1:
                app_dir = os.path.dirname(app_dirs[0])
                return app_dir

    app_dir = guess_app()

    if options.destroy:
        if not app_dir:
            parser.error('No application in %s' % options.root)
        supervisor = join(app_dir, 'bin', 'supervisorctl')
        if os.path.isfile(supervisor):
            utils.call(supervisor, 'shutdown', silent=True)
            time.sleep(0.5)
        shutil.rmtree(options.root)
        sys.exit(0)

    if options.source:
        if app_dir and os.path.isdir(app_dir):
            shutil.rmtree(app_dir)
        if not options.source:
            parser.error('No source option')
        log.info(options.source)
        # .git url or local path are git repositories
        if options.source.endswith('.git') or options.source.startswith('/'):
            if options.app_name:
                utils.call('git', 'clone', '-q', options.source,
                                                 options.app_name)
            else:
                utils.call('git', 'clone', '-q', options.source)
            app_dir = guess_app()
            log.info(app_dir)
            os.chdir(app_dir)
            if not options.branch:
                options.branch = 'master'
            utils.call('git', 'checkout', options.branch)
        else:
            if options.app_name:
                utils.call('hg', 'clone', '-q', options.source,
                                                options.app_name)
            else:
                utils.call('hg', 'clone', '-q', options.source)
            app_dir = guess_app()
            log.info(app_dir)
            os.chdir(app_dir)
            if options.branch and options.branch != 'master':
                # dont take care of hg branch for now
                pass
    elif app_dir is None:
        parser.error('You must have an existing repository or a deploy url')

    os.chdir(app_dir)
    app_name = os.path.basename(app_dir)

    if os.path.isfile('setup.py'):
        options.develop.insert(0, '.')

    for setup in glob(join(app_dir, '*', 'setup.py')):
        options.develop.append(os.path.dirname(setup))

    for setup in glob(join(app_dir, 'src', '*', 'setup.py')):
        options.develop.append(os.path.dirname(setup))

    var = utils.realpath(root, 'var', 'buildout')

    config = Config.from_template('pytheon.cfg')
    config.buildout['dump-picked-versions-file'] = join(root, 'etc',
                                                              'versions.cfg')
    config.buildout['eggs-directory'] = options.eggs or join(var, 'eggs')
    config.buildout['parts-directory'] = join(var, 'parts')
    config.buildout['develop-eggs-directory'] = join(var, 'develop-eggs')
    config.buildout.develop = options.develop
    config.deploy.recipe = 'pytheon.deploy'
    config.deploy['deploy-dir'] = root
    if options.develop_dir:
        config.buildout['develop-dir'] = options.develop_dir
    if options.host:
        config.deploy.host = options.host
    config.deploy.environ = ['PYTHEON=1', 'PRODUCTION=1'] + list(args)

    if os.path.isfile('requirements.txt'):
        config.buildout.requirements = 'requirements.txt'

    extends = []
    for filename in ('buildout.cfg', 'versions.cfg',
                     'deploy-%s.ini' % app_name, 'deploy.ini'):
        filename = utils.realpath(os.getcwd(), filename)
        if os.path.isfile(filename):
            extends.append(filename)
    config.buildout.extends = extends

    buildout = 'pytheon.cfg'
    config.write(buildout)

    env = os.environ
    env.update(dict([v.split('=', 1) for v in args if '=' in v]))

    utils.buildout(options.interpreter, buildout,
                   eggs=CONFIG.pytheon.eggs_dir, env=env)

    if os.path.isfile('post_install.sh'):
        lib_dir = join(root, 'lib')
        if os.path.isfile(os.path.join(lib_dir, 'environ.py')):
            execfile(os.path.join(lib_dir, 'environ.py'))
        utils.call('/bin/bash', 'post_install.sh', env=os.environ)
예제 #3
0
    def __init__(self, buildout, name, options):
        self.buildout, self.name, self.options = buildout, name, options
        self.logger = logging.getLogger(name.title())

        if 'inherit' in self.options:
            inherit_options = self.buildout[self.options['inherit']]
            for k, v in inherit_options.items():
                if k not in self.options:
                    self.options[k] = v

        if 'project_name' not in self.options:
            self.options['project_name'] = os.path.basename(os.getcwd())
        if 'uuid' not in self.options:
            self.options['uuid'] = str(uuid.uuid4())
        if 'password' not in self.options:
            if utils.PY3:
                id = str(uuid.uuid4()).encode('ascii')
            else:
                id = str(uuid.uuid4())
            self.options['password'] = str(sha1(id).hexdigest())

        self.options['uid'] = getpass.getuser()
        self.options['gid'] = grp.getgrgid(os.getegid())[0]
        self.options['eggs'] = self.options.get('eggs', 'PasteDeploy')
        self.options['eggs'] += '\n' + self.buildout['buildout'].get(
                                                    'requirements-eggs', '')

        self.options['include'] = self.options.get('include', '')

        self.curdir = os.path.realpath(buildout['buildout']['directory'])
        self.deploy_dir = utils.realpath(options.get('deploy-dir',
                             join(self.buildout['buildout']['parts-directory'],
                             self.name)))
        self.bin_dir = self.buildout['buildout']['bin-directory']
        self.lib_dir = utils.realpath(self.deploy_dir, 'lib')
        self.var_dir = utils.realpath(self.deploy_dir, 'var')
        self.etc_dir = utils.realpath(self.deploy_dir, 'etc')
        self.run_dir = utils.realpath(self.var_dir, 'run')
        dirnames = dict(
            curdir=self.curdir,
            deploy_dir=self.deploy_dir,
            lib_dir=self.lib_dir,
            etc_dir=self.etc_dir,
            var_dir=self.var_dir,
            log_dir=utils.realpath(self.var_dir, 'log'),
            run_dir=self.run_dir,
          )
        for k, v in dirnames.items():
            self.options[k] = v

        if os.path.isfile(join(self.lib_dir, 'environ.py')):
            execfile(join(self.lib_dir, 'environ.py'))

        environ_string = ''
        for envvar in options.get('environ', '').split('\n'):
            envvar = envvar.strip()
            if envvar and '=' in envvar:
                k, v = envvar.split('=', 1)
                environ_string += '\nos.environ[%r] = %r' % (k.upper(), v)
                os.environ[k.upper()] = str(v)
        initialization = self.options.get('initialization', '')
        initialization += INITIALIZATION % dict(environ_string=environ_string,
                                                **dirnames)
        self.options['initialization'] = initialization