Esempio n. 1
0
def dump():
    assert isinstance(application.options, OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.writeConfigurations: return
    if not __debug__:
        print('Cannot dump configuration file if python is run with "-O" or "-OO" option', file=sys.stderr)
        sys.exit(1)
    try:
        context.activate(dumpAssembly())
        try: 
            loadPlugins()
            configFile = configurations_file_path()
        
            if os.path.isfile(configFile):
                with open(configFile, 'r') as f: config = load(f)
            else: config = {}
            
            context.open(aop.modulesIn('__plugin__.**'), config=config, included=True)
            try:
                if os.path.isfile(configFile): os.rename(configFile, configFile + '.bak')
                with open(configFile, 'w') as f: save(context.configurations(force=True), f)
                print('Created "%s" configuration file' % configFile)
            finally: context.deactivate()
        finally: context.deactivate()
    
    except SystemExit: raise
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while dumping configurations', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
Esempio n. 2
0
def repair():
    openPlugins()

    used = set()
    try:
        for call, name, trigger in support.eventsFor(event.REPAIR, app.POPULATE):
            trigger = (trigger,)
            if app.POPULATE.isTriggered(trigger):
                used.add(name)
                executed = markers().get(name)
                if app.DEVEL.isTriggered(trigger) or app.REPAIR.isTriggered(trigger): executed = None  # If in devel then we execute regardless
                if executed is None:
                    executed = call()
                    log.info('Executed populate event call \'%s\' for the first time and got %s', name, executed)
                elif not executed:
                    executed = call()
                    log.info('Executed populate event call \'%s\' again and got %s', name, executed)
                else:
                    log.info('No need to execute populate event call \'%s\'', name)
                markers()[name] = executed

            elif app.REPAIR.isTriggered(trigger):
                log.info('Executing plugins repair event call \'%s\'', name)
                call()
    finally:
        persistMarkers(used)
        context.deactivate()
Esempio n. 3
0
def testUpdatePlugins():
    assert isinstance(application.options, OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.test: return
    try:
        openSetups()
        loadPlugins()
        
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while opening setups for testing', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
        return
    finally: context.deactivate()
Esempio n. 4
0
def __deploy__():
    # Deploy the application
    try:
        import package_extender
        package_extender.PACKAGE_EXTENDER.addFreezedPackage('__deploy__.')
        from ally.container import aop, context
    except ImportError:
        print(
            'Corrupted or missing ally component, make sure that this component is not missing from python path '
            'or components eggs',
            file=sys.stderr)
        sys.exit(1)

    application = sys.modules['application'] = ModuleType('application')
    try:
        # We create the parser to be prepared.
        application.parser = argparse.ArgumentParser(
            description='The ally distribution application deployer.')
        application.Options = object  # Prepare the option class

        # In the first stage we prepare the application deployment.
        context.open(aop.modulesIn('__deploy__.*.prepare'))
        try:
            context.processStart()
        finally:
            context.deactivate()

        # In the second stage we parse the application arguments.
        application.options = application.parser.parse_args(
            namespace=application.Options())

        # In the final stage we deploy the application.
        context.open(aop.modulesIn('__deploy__.*.deploy'))
        try:
            context.processStart()
        finally:
            context.deactivate()

    except SystemExit:
        raise
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while deploying', file=sys.stderr)
        traceback.print_exc()
        print('-' * 150, file=sys.stderr)
        sys.exit(1)
Esempio n. 5
0
def deploy():
    loadPlugins()
    if not os.path.isfile(configurations_file_path()):
        print('The configuration file "%s" does not exist, create one by running the the application '
              'with "-dump" option' % configurations_file_path())
        sys.exit(1)
    with open(configurations_file_path(), 'r') as f: config = load(f)

    PACKAGE_EXTENDER.addFreezedPackage('__plugin__.')
    pluginModules = aop.modulesIn('__plugin__.**')
    for module in pluginModules.load().asList():
        if not isPackage(module) and re.match('__plugin__\\.[^\\.]+$', module.__name__):
            raise SetupError('The plugin setup module \'%s\' is not allowed directly in the __plugin__ package it needs '
                             'to be in a sub package' % module.__name__)

    context.open(pluginModules, config=config, included=True)
    try: context.processStart()
    finally: context.deactivate()
Esempio n. 6
0
def testUpdatePlugins():
    assert isinstance(
        application.options,
        OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.test: return
    try:
        openSetups()
        loadPlugins()

    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while opening setups for testing',
              file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
        return
    finally:
        context.deactivate()
Esempio n. 7
0
def openSetups():
    ''' Open the assembly for setups '''
    if not os.path.isfile(application.options.configurationPath):
        print('The configuration file "%s" doesn\'t exist, create one by running the the application '
              'with "-dump" option' % application.options.configurationPath, file=sys.stderr)
        sys.exit(1)
    with open(application.options.configurationPath, 'r') as f: config = load(f)

    context.open(aop.modulesIn('__setup__.ally.**'), config=config)
    
    logging.basicConfig(format=format())
    for name in warning_for(): logging.getLogger(name).setLevel(logging.WARN)
    for name in info_for(): logging.getLogger(name).setLevel(logging.INFO)
    for name in debug_for(): logging.getLogger(name).setLevel(logging.DEBUG)
    
    context.deactivate()
    
    context.open(aop.modulesIn('__setup__.**'), config=config)
Esempio n. 8
0
def dump():
    assert isinstance(
        application.options,
        OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.writeConfigurations: return
    if not __debug__:
        print(
            'Cannot dump configuration file if python is run with "-O" or "-OO" option',
            file=sys.stderr)
        sys.exit(1)
    try:
        context.activate(dumpAssembly())
        try:
            loadPlugins()
            configFile = configurations_file_path()

            if os.path.isfile(configFile):
                with open(configFile, 'r') as f:
                    config = load(f)
            else:
                config = {}

            context.open(aop.modulesIn('__plugin__.**'),
                         config=config,
                         included=True)
            try:
                if os.path.isfile(configFile):
                    os.rename(configFile, configFile + '.bak')
                with open(configFile, 'w') as f:
                    save(context.configurations(force=True), f)
                print('Created "%s" configuration file' % configFile)
            finally:
                context.deactivate()
        finally:
            context.deactivate()

    except SystemExit:
        raise
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while dumping configurations',
              file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
Esempio n. 9
0
def deploy():
    assert isinstance(application.options, OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.start: return
    try:
        openSetups()
        context.processStart()
        if log_file(): logging.getLogger().addHandler(FileHandler(log_file()))

    except SystemExit: raise
    except (SetupError, ConfigError):
        print('-' * 150, file=sys.stderr)
        print('A setup or configuration error occurred while deploying, try to rebuild the application properties by '
              'running the the application with "-dump" option', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while deploying', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
    finally: context.deactivate()
Esempio n. 10
0
def repair():
    assert isinstance(application.options, OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.repair: return
    try:
        openSetups()
        for call, name, _trigger in support.eventsFor(event.REPAIR):
            log.info('Executing repair event call \'%s\'', name)
            call()
            
    except SystemExit: raise
    except (SetupError, ConfigError):
        print('-' * 150, file=sys.stderr)
        print('A setup or configuration error occurred while deploying, try to rebuild the application properties by '
              'running the the application with "-dump" options', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while repairing', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
    finally: context.deactivate()
Esempio n. 11
0
def __deploy__():
    # Deploy the application
    try:
        import package_extender
        package_extender.PACKAGE_EXTENDER.addFreezedPackage('__deploy__.')
        from ally.container import aop, context
    except ImportError:
        print('Corrupted or missing ally component, make sure that this component is not missing from python path '
              'or components eggs', file=sys.stderr)
        sys.exit(1)

    application = sys.modules['application'] = ModuleType('application')
    try:
        # We create the parser to be prepared.
        application.parser = argparse.ArgumentParser(description='The ally distribution application deployer.')
        application.Options = object  # Prepare the option class

        # In the first stage we prepare the application deployment.
        context.open(aop.modulesIn('__deploy__.*.prepare'))
        try: context.processStart()
        finally: context.deactivate()

        # In the second stage we parse the application arguments.
        application.options = application.parser.parse_args(namespace=application.Options())

        # In the final stage we deploy the application.
        context.open(aop.modulesIn('__deploy__.*.deploy'))
        try: context.processStart()
        finally: context.deactivate()

    except SystemExit: raise
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while deploying', file=sys.stderr)
        traceback.print_exc()
        print('-' * 150, file=sys.stderr)
        sys.exit(1)
Esempio n. 12
0
def dump():
    assert isinstance(application.options, OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.writeConfigurations: return
    if not __debug__:
        print('Cannot dump configuration file if python is run with "-O" or "-OO" option', file=sys.stderr)
        sys.exit(1)
    assembly, configFile = dumpAssembly(), application.options.configurationPath
    try:
        context.activate(assembly)
        try:
            if os.path.isfile(configFile): os.rename(configFile, configFile + '.bak')
            for config in assembly.configurations:
                try: assembly.processForName(config)
                except ConfigError as e: print('Failed to fetch a value for configuration \'%s\': %s' % (config, e))
            # Forcing the processing of all configurations
            with open(configFile, 'w') as f: save(assembly.trimmedConfigurations(), f)
            print('Created "%s" configuration file' % configFile)
        finally: context.deactivate()
    except SystemExit: raise
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while dumping configurations', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
Esempio n. 13
0
def deploy():
    assert isinstance(application.options, OptionsCore), 'Invalid application options %s' % application.options
    if not application.options.start: return
    try:
        if not os.path.isfile(application.options.configurationPath):
            print('The configuration file "%s" doesn\'t exist, create one by running the the application '
                  'with "-dump" option' % application.options.configurationPath, file=sys.stderr)
            sys.exit(1)
        with open(application.options.configurationPath, 'r') as f: config = load(f)

        context.open(aop.modulesIn('__setup__.ally_utilities.**'), config=config)

        import logging
        logging.basicConfig(format=format())
        if log_file(): logging.getLogger().addHandler(FileHandler(log_file()))
        for name in warning_for(): logging.getLogger(name).setLevel(logging.WARN)
        for name in info_for(): logging.getLogger(name).setLevel(logging.INFO)
        for name in debug_for(): logging.getLogger(name).setLevel(logging.DEBUG)

        context.deactivate()

        context.open(aop.modulesIn('__setup__.**'), config=config)
        try: context.processStart()
        finally: context.deactivate()
    except SystemExit: raise
    except (SetupError, ConfigError):
        print('-' * 150, file=sys.stderr)
        print('A setup or configuration error occurred while deploying, try to rebuild the application properties by '
              'running the the application with "configure components" options', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while deploying', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
Esempio n. 14
0
def config():
    assert isinstance(application.options, OptionsMongrel2), 'Invalid application options %s' % application.options
    if not application.options.configMongrel2: return
    workspace = application.options.configMongrel2
    folders = [path.join('mongrel2', name) for name in ('logs', 'run', 'tmp')]
    folders.append(path.join('shared', 'upload'))

    for name in folders:
        folder = path.join(workspace, name)
        if not path.isdir(folder): makedirs(folder)
    
    configFile = application.options.configurationPath
    if path.isfile(configFile):
        with open(configFile, 'r') as f: config = load(f)
    else:
        print('The configuration file "%s" doesn\'t exist, create one by running the the application '
              'with "-dump" option, also change the application properties "server_type" configuration to "mongrel2" '
              'and also adjust the "recv_spec", "send_spec" and "server_port" accordingly' % configFile, file=sys.stderr)
        sys.exit(1)
        
    try:
        context.open(aop.modulesIn('__setup__.**'), config=config)
    
        updateConfig = False
        if server_type() != 'mongrel2':
            updateConfig = True
            support.persist(server_type, 'mongrel2')
        
        sendIdent = send_ident()
        if sendIdent is None:
            updateConfig = True
            sendIdent = str(uuid4())
            support.persist(send_ident, sendIdent)
        
        replace = {}
        try:
            replace['${send_spec}'] = send_spec()
            replace['${send_ident}'] = sendIdent
            replace['${recv_spec}'] = recv_spec()
            replace['${recv_ident}'] = recv_ident()
            replace['${server_port}'] = str(server_port())
        
            if updateConfig:
                if path.isfile(configFile): renames(configFile, configFile + '.bak')
                with open(configFile, 'w') as f: save(context.configurations(force=True), f)
                print('Updated the "%s" configuration file' % configFile)
        finally: context.deactivate()
    except SystemExit: raise
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while configuring Mongrel2', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
    else:
        conf = openURI(path.join(pythonPath(), 'resources', 'ally.conf'))
        conf = codecs.getreader('utf8')(conf)
        conf = ReplaceInFile(conf, replace)
        with open(path.join(workspace, 'ally.conf'), 'w') as f: pipe(conf, f)
        with open(path.join(workspace, 'README-Mongrel2.txt'), 'wb') as f:
            pipe(openURI(path.join(pythonPath(), 'resources', 'README-Mongrel2.txt')), f)
        
        print('Configured "%s" mongrel2 workspace' % workspace)
Esempio n. 15
0
def config():
    assert isinstance(application.options, OptionsMongrel2), 'Invalid application options %s' % application.options
    if not application.options.configMongrel2: return
    workspace = application.options.configMongrel2
    folders = [path.join('mongrel2', name) for name in ('logs', 'run', 'tmp')]
    folders.append(path.join('shared', 'upload'))

    for name in folders:
        folder = path.join(workspace, name)
        if not path.isdir(folder): makedirs(folder)
    
    configFile = application.options.configurationPath
    if path.isfile(configFile):
        with open(configFile, 'r') as f: config = load(f)
    else:
        print('The configuration file "%s" doesn\'t exist, create one by running the the application '
              'with "-dump" option, also change the application properties "server_type" configuration to "mongrel2" '
              'and also adjust the "recv_spec", "send_spec" and "server_port" accordingly' % configFile, file=sys.stderr)
        sys.exit(1)
        
    try:
        context.open(aop.modulesIn('__setup__.**'), config=config)
    
        updateConfig = False
        if server_type() != 'mongrel2':
            updateConfig = True
            support.persist(server_type, 'mongrel2')
        
        sendIdent = send_ident()
        if sendIdent is None:
            updateConfig = True
            sendIdent = str(uuid4())
            support.persist(send_ident, sendIdent)
        
        replace = {}
        try:
            replace['${send_spec}'] = send_spec()
            replace['${send_ident}'] = sendIdent
            replace['${recv_spec}'] = recv_spec()
            replace['${recv_ident}'] = recv_ident()
            replace['${server_port}'] = str(server_port())
        
            if updateConfig:
                if path.isfile(configFile): renames(configFile, configFile + '.bak')
                with open(configFile, 'w') as f: save(context.configurations(force=True), f)
                print('Updated the "%s" configuration file' % configFile)
        finally: context.deactivate()
    except SystemExit: raise
    except:
        print('-' * 150, file=sys.stderr)
        print('A problem occurred while configuring Mongrel2', file=sys.stderr)
        traceback.print_exc(file=sys.stderr)
        print('-' * 150, file=sys.stderr)
    else:
        conf = openURI(path.join(pythonPath(), 'resources', 'ally.conf'))
        conf = codecs.getreader('utf8')(conf)
        conf = ReplaceInFile(conf, replace)
        with open(path.join(workspace, 'ally.conf'), 'w') as f: pipe(conf, f)
        with open(path.join(workspace, 'README-Mongrel2.txt'), 'wb') as f:
            pipe(openURI(path.join(pythonPath(), 'resources', 'README-Mongrel2.txt')), f)
        
        print('Configured "%s" mongrel2 workspace' % workspace)
Esempio n. 16
0
def deploy():
    openPlugins()

    try: context.processStart()
    finally: context.deactivate()