Exemplo n.º 1
0
    def __init__(self, **kwds):
        '''
        Variant A:
        Script(name='AppPreStart', body='#!/usr/bin/python ...', asynchronous=True)

        Variant B:
        Script(id=43432234343, name='AppPreStart', pid=12145,
                        interpreter='/usr/bin/python', start_time=4342424324, asynchronous=True)
        '''
        for key, value in kwds.items():
            setattr(self, key, value)
        self.elapsed_time = 0
        self.return_code = 1
        assert self.name, '`name` required'
        assert self.exec_timeout, '`exec_timeout` required'

        if self.body or self.path:
            # time.time() can produce the same microseconds fraction in different async script execution threads,
            # and therefore produce the same id. solution is to seed random millisecods number
            random.seed()
            self.id = '%d.%d' % (time.time(), random.randint(0, 100))
        else:
            assert self.id, '`id` required'
            assert self.pid, '`pid` required'
            assert self.start_time, '`start_time` required'
            if self.interpreter:
                self.interpreter = split_strip(self.interpreter)[0]

        self.logger = logging.getLogger('%s.%s' % (__name__, self.id))
        self.exec_path = self.path or self._generate_exec_path()

        if self.exec_timeout:
            self.exec_timeout = int(self.exec_timeout)

        if self.execution_id:
            args = (self.name, self.event_name, self.execution_id)
            self.stdout_path = os.path.join(logs_dir,
                                            '%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir,
                                            '%s.%s.%s-err.log' % args)
        else:
            args = (self.name, self.event_name, self.role_name, self.id)
            self.stdout_path = os.path.join(logs_dir,
                                            '%s.%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir,
                                            '%s.%s.%s.%s-err.log' % args)
Exemplo n.º 2
0
    def __init__(self, **kwds):
        '''
        Variant A:
        Script(name='AppPreStart', body='#!/usr/bin/python ...', asynchronous=True)

        Variant B:
        Script(id=43432234343, name='AppPreStart', pid=12145,
                        interpreter='/usr/bin/python', start_time=4342424324, asynchronous=True)
        '''
        for key, value in kwds.items():
            setattr(self, key, value)
        self.elapsed_time = 0
        self.return_code = 1
        assert self.name, '`name` required'
        assert self.exec_timeout, '`exec_timeout` required'

        if self.body or self.path:
            # time.time() can produce the same microseconds fraction in different async script execution threads, 
            # and therefore produce the same id. solution is to seed random millisecods number
            random.seed()
            self.id = '%d.%d' % (time.time(), random.randint(0, 100))
        else:
            assert self.id, '`id` required'
            assert self.pid, '`pid` required'
            assert self.start_time, '`start_time` required'
            if self.interpreter:
                self.interpreter = split_strip(self.interpreter)[0]
                
        self.logger = logging.getLogger('%s.%s' % (__name__, self.id))
        self.exec_path = self.path or self._generate_exec_path()

        if self.exec_timeout:
            self.exec_timeout = int(self.exec_timeout)

        if self.execution_id:
            args = (self.name, self.event_name, self.execution_id)
            self.stdout_path = os.path.join(logs_dir, '%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir, '%s.%s.%s-err.log' % args)
        else:
            args = (self.name, self.event_name, self.role_name, self.id)
            self.stdout_path = os.path.join(logs_dir, '%s.%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir, '%s.%s.%s.%s-err.log' % args)
Exemplo n.º 3
0
    def __init__(self, **kwds):
        '''
        Variant A:
        Script(name='AppPreStart', body='#!/usr/bin/python ...', asynchronous=True)

        Variant B:
        Script(id=43432234343, name='AppPreStart', pid=12145,
                        interpreter='/usr/bin/python', start_time=4342424324, asynchronous=True)
        '''
        for key, value in kwds.items():
            setattr(self, key, value)

        assert self.name, '`name` required'
        assert self.exec_timeout, '`exec_timeout` required'

        if self.name and self.body:
            self.id = str(time.time())
            interpreter = read_shebang(script=self.body)
            if not interpreter:
                raise HandlerError(
                    "Can't execute script '%s' cause it hasn't shebang.\n"
                    "First line of the script should have the form of a shebang "
                    "interpreter directive is as follows:\n"
                    "#!interpreter [optional-arg]" % (self.name, ))
            self.interpreter = interpreter
        else:
            assert self.id, '`id` required'
            assert self.pid, '`pid` required'
            assert self.start_time, '`start_time` required'
            if self.interpreter:
                self.interpreter = split_strip(self.interpreter)[0]

        self.logger = logging.getLogger('%s.%s' % (__name__, self.id))
        self.exec_path = os.path.join(exec_dir_prefix + self.id, self.name)
        if self.exec_timeout:
            self.exec_timeout = int(self.exec_timeout)
        args = (self.name, self.event_name, self.role_name, self.id)
        self.stdout_path = os.path.join(logs_dir, '%s.%s.%s.%s-out.log' % args)
        self.stderr_path = os.path.join(logs_dir, '%s.%s.%s.%s-err.log' % args)
Exemplo n.º 4
0
    def __init__(self, **kwds):
        '''
        Variant A:
        Script(name='AppPreStart', body='#!/usr/bin/python ...', asynchronous=True)

        Variant B:
        Script(id=43432234343, name='AppPreStart', pid=12145,
                        interpreter='/usr/bin/python', start_time=4342424324, asynchronous=True)
        '''
        for key, value in kwds.items():
            setattr(self, key, value)

        assert self.name, '`name` required'
        assert self.exec_timeout, '`exec_timeout` required'

        if self.name and self.body:
            self.id = str(time.time())
            interpreter = read_shebang(script=self.body)
            if not interpreter:
                raise HandlerError("Can't execute script '%s' cause it hasn't shebang.\n"
                                                "First line of the script should have the form of a shebang "
                                                "interpreter directive is as follows:\n"
                                                "#!interpreter [optional-arg]" % (self.name, ))
            self.interpreter = interpreter
        else:
            assert self.id, '`id` required'
            assert self.pid, '`pid` required'
            assert self.start_time, '`start_time` required'
            if self.interpreter:
                self.interpreter = split_strip(self.interpreter)[0]

        self.logger = logging.getLogger('%s.%s' % (__name__, self.id))
        self.exec_path = os.path.join(exec_dir_prefix + self.id, self.name)
        if self.exec_timeout:
            self.exec_timeout = int(self.exec_timeout)
        args = (self.name, self.event_name, self.role_name, self.id)
        self.stdout_path = os.path.join(logs_dir, '%s.%s.%s.%s-out.log' % args)
        self.stderr_path = os.path.join(logs_dir, '%s.%s.%s.%s-err.log' % args)
Exemplo n.º 5
0
    def __init__(self, **kwds):
        '''
        Variant A:
        Script(name='AppPreStart', body='#!/usr/bin/python ...', asynchronous=True)

        Variant B:
        Script(id=43432234343, name='AppPreStart', pid=12145,
                        interpreter='/usr/bin/python', start_time=4342424324, asynchronous=True)
        '''
        for key, value in kwds.items():
            setattr(self, key, value)

        assert self.name, '`name` required'
        assert self.exec_timeout, '`exec_timeout` required'
        if linux.os['family'] == 'Windows' and self.run_as:
            raise HandlerError("Windows can't execute scripts remotely " \
                               "under user other than Administrator. " \
                               "Script '%s', given user: '******'" % (self.name, self.run_as))

        if self.name and (self.body or self.path):
            random.seed()
            # time.time() can produce the same microseconds fraction in different async script execution threads,
            # and therefore produce the same id. solution is to seed random millisecods number
            self.id = '%d.%d' % (time.time(), random.randint(0, 100))

            interpreter = read_shebang(path=self.path, script=self.body)
            if not interpreter:
                raise HandlerError(
                    "Can't execute script '%s' cause it hasn't shebang.\n"
                    "First line of the script should have the form of a shebang "
                    "interpreter directive is as follows:\n"
                    "#!interpreter [optional-arg]" % (self.name, ))
            self.interpreter = interpreter

            if linux.os['family'] == 'Windows' and self.body:
                # Erase first line with #!
                self.body = '\n'.join(self.body.splitlines()[1:])

        else:
            assert self.id, '`id` required'
            assert self.pid, '`pid` required'
            assert self.start_time, '`start_time` required'
            if self.interpreter:
                self.interpreter = split_strip(self.interpreter)[0]

        self.logger = logging.getLogger('%s.%s' % (__name__, self.id))
        self.exec_path = self.path or os.path.join(exec_dir_prefix + self.id,
                                                   self.name)

        if  self.interpreter == 'powershell' \
                and os.path.splitext(self.exec_path)[1] not in ('ps1', 'psm1'):
            self.exec_path += '.ps1'
        elif self.interpreter == 'cmd' \
                and os.path.splitext(self.exec_path)[1] not in ('cmd', 'bat'):
            self.exec_path += '.bat'

        if self.exec_timeout:
            self.exec_timeout = int(self.exec_timeout)

        if self.execution_id:
            args = (self.name, self.event_name, self.execution_id)
            self.stdout_path = os.path.join(logs_dir,
                                            '%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir,
                                            '%s.%s.%s-err.log' % args)
        else:
            args = (self.name, self.event_name, self.role_name, self.id)
            self.stdout_path = os.path.join(logs_dir,
                                            '%s.%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir,
                                            '%s.%s.%s.%s-err.log' % args)
Exemplo n.º 6
0
    def __init__(self, **kwds):
        '''
        Variant A:
        Script(name='AppPreStart', body='#!/usr/bin/python ...', asynchronous=True)

        Variant B:
        Script(id=43432234343, name='AppPreStart', pid=12145,
                        interpreter='/usr/bin/python', start_time=4342424324, asynchronous=True)
        '''
        for key, value in kwds.items():
            setattr(self, key, value)


        assert self.name, '`name` required'
        assert self.exec_timeout, '`exec_timeout` required'
        if linux.os['family'] == 'Windows' and self.run_as:
            raise HandlerError("Windows can't execute scripts remotely " \
                               "under user other than Administrator. " \
                               "Script '%s', given user: '******'" % (self.name, self.run_as))

        if self.name and (self.body or self.path):
            random.seed()
            # time.time() can produce the same microseconds fraction in different async script execution threads, 
            # and therefore produce the same id. solution is to seed random millisecods number
            self.id = '%d.%d' % (time.time(), random.randint(0, 100))

            interpreter = read_shebang(path=self.path, script=self.body)
            if not interpreter:
                raise HandlerError("Can't execute script '%s' cause it hasn't shebang.\n"
                                                "First line of the script should have the form of a shebang "
                                                "interpreter directive is as follows:\n"
                                                "#!interpreter [optional-arg]" % (self.name, ))
            self.interpreter = interpreter
            
            if linux.os['family'] == 'Windows' and self.body:
                # Erase first line with #!
                self.body = '\n'.join(self.body.splitlines()[1:])

        else:
            assert self.id, '`id` required'
            assert self.pid, '`pid` required'
            assert self.start_time, '`start_time` required'
            if self.interpreter:
                self.interpreter = split_strip(self.interpreter)[0]
                

        self.logger = logging.getLogger('%s.%s' % (__name__, self.id))
        self.exec_path = self.path or os.path.join(exec_dir_prefix + self.id, self.name)

        if  self.interpreter == 'powershell' \
                and os.path.splitext(self.exec_path)[1] not in ('ps1', 'psm1'):
            self.exec_path += '.ps1'
        elif self.interpreter == 'cmd' \
                and os.path.splitext(self.exec_path)[1] not in ('cmd', 'bat'):
            self.exec_path += '.bat'

        if self.exec_timeout:
            self.exec_timeout = int(self.exec_timeout)

        if self.execution_id:
            args = (self.name, self.event_name, self.execution_id)
            self.stdout_path = os.path.join(logs_dir, '%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir, '%s.%s.%s-err.log' % args)
        else:
            args = (self.name, self.event_name, self.role_name, self.id)
            self.stdout_path = os.path.join(logs_dir, '%s.%s.%s.%s-out.log' % args)
            self.stderr_path = os.path.join(logs_dir, '%s.%s.%s.%s-err.log' % args)