Beispiel #1
0
def write_script(loc):
    '''Write an empty script to the filesystem'''
    logger.info('Creating script ' + loc)
    with open(loc, mode='x') as filehandle:
        filehandle.write('#!/bin/sh\n')
        filehandle.close()
        chmod(loc, 0o777)
Beispiel #2
0
 def script_exec(self, script, envarg):
     '''Actually execute the script/command'''
     try:
         result = check_output(script,
                               stderr=STDOUT,
                               universal_newlines=True,
                               env=envarg)
         return result
     except CalledProcessError as err:
         logger.error('Script '+script+' Failed! Exit='+ \
                      str(err.returncode)+' Message="'+str(err.output)+'"')
Beispiel #3
0
 def script_exec(self, script, envarg):
     '''Actually execute the script/command'''
     try:
         result = check_output(
             script,
             stderr=STDOUT,
             env=envarg,
             shell=True)
         return result
     except CalledProcessError as err:
         logger.error('Script '+script+' Failed! Exit='+ \
                      str(err.returncode)+' Message="'+str(err.output)+'"')
Beispiel #4
0
    def wait_event(self, action):
        '''Wait for events'''

        if not callable(action):
            raise ValueError

        logger.info("watching " + self.endpoint + " for container events")
        for event in self.client.events(since=datetime.utcnow(), decode=True):
            event_key = event['Type'] + os.path.sep + event['Action']
            logger.debug("Detected actionable event " + event_key + ": " + str(event))
            try:
                output = action(event)
                logger.debug('Finished handling ' + event_key)
                if output:
                    logger.debug("SCRIPT OUTPUT: " + str(output))
            except RuntimeError as err:
                logger.error('Failed to execute task in reponse to "' +
                             event_key + '" :' + str(err))
Beispiel #5
0
    def wait_event(self, action):
        '''Wait for events'''

        if not callable(action):
            raise ValueError

        logger.info("watching " + self.endpoint + " for container events")
        for event in self.client.events(since=datetime.utcnow(), decode=True):
            event_key = event['Type'] + os.path.sep + event['Action']
            logger.debug("Detected actionable event " + event_key + ": " + str(event))
            try:
                output = action(event)
                logger.debug('Finished handling ' + event_key)
                if output:
                    logger.debug("SCRIPT OUTPUT: " + str(output))
            except RuntimeError as err:
                logger.error('Failed to execute task in reponse to "' +
                             event_key + '" :' + str(err))
Beispiel #6
0
def main():
    '''Main CLI entrypoint for des'''
    args = parser.parse_args()

    if args.version:
        print(__version__)
        exit(0)

    if args.verbose:
        logger.setLevel(DEBUG)

    if args.create:
        logger.info('Creating template script directory at ' + args.script_dir)
        create_dirs(args.script_dir)
        exit(0)

    watcher = Watcher()
    runner = ScriptRunner(args.script_dir)

    watcher.wait_event(runner.run)
Beispiel #7
0
    def run(self, event_dict):
        '''Handle a docker event'''

        script = self.basedir + sep + event_dict.get('Type') + \
                 sep + str(event_dict.get('Action')).split(':')[0]

        if exists(script) == False:
            logger.warning('Unable to handle event! No script exists at ' + script)
            return
        
        env_dict = self.build_env(event_dict)

        logger.info('Running script ' + script)
        logger.debug('Script ENV: ' + str(env_dict))
        self.script_exec(script, env_dict)