Ejemplo n.º 1
0
class BaseCommand(object):
    def add_arguments(self, parser):
        pass

    def run(self):
        pass

    def connect_and_run(self, host, run, hosts, keyfile, certfile, timeout,
                        **args):
        self.connection = XMLRPCClient(
            'https://{0}/'.format(host if ':' in host else ':'.
                                  join([host, str(DEFAULT_PORT)])), keyfile,
            certfile, timeout)
        result = self.run(**args)

        check_std = lambda x: isinstance(x, str) or x == None

        result_ok = False
        if len(result) == 3:
            retval, stdout, stderr = result
            if isinstance(retval, int) and \
                check_std(stdout) and check_std(stderr):
                result_ok = True

        if result_ok:
            return result
        else:
            raise ViriError(
                'command Local class must return a tuple of '
                '(int, str, str), representing (retval, stdout, stderr)')

    def send_file(self, filename):
        with open(filename, 'rb') as f:
            content = f.read()
        file_id = sha1(content).hexdigest()
        success, exists_value = self.connection.exists({'file_id': file_id})
        if success:
            if not exists_value:
                success, put_value = self.connection.put({
                    'file_name':
                    os.path.basename(filename),
                    'file_content':
                    Binary(content)
                })
                if not success:
                    raise ViriError(
                        'Could not send file: {}'.format(put_value))
        else:
            raise ViriError(
                'Could not check if file exists: {}'.format(exists_value))

        return file_id
Ejemplo n.º 2
0
class BaseCommand(object):
    def add_arguments(self, parser):
        pass

    def run(self):
        pass
        
    def connect_and_run(self, host, run, hosts, keyfile, certfile, timeout, **args):        
        self.connection = XMLRPCClient(
            'https://{0}/'.format(
                host if ':' in host else ':'.join(
                    [host, str(DEFAULT_PORT)])),
            keyfile,
            certfile,
            timeout)
        result = self.run(**args)

        check_std = lambda x: isinstance(x, str) or x == None

        result_ok = False
        if len(result) == 3:
            retval, stdout, stderr = result
            if isinstance(retval, int) and \
                check_std(stdout) and check_std(stderr):
                result_ok = True

        if result_ok:
            return result
        else:
            raise ViriError('command Local class must return a tuple of '
                '(int, str, str), representing (retval, stdout, stderr)')

    def send_file(self, filename):
        with open(filename, 'rb') as f:
            content = f.read()
        file_id = sha1(content).hexdigest()
        success, exists_value = self.connection.exists({'file_id': file_id})
        if success:
            if not exists_value:
                success, put_value = self.connection.put({
                    'file_name': os.path.basename(filename),
                    'file_content': Binary(content)})
                if not success:
                    raise ViriError('Could not send file: {}'.format(
                        put_value))
        else:
            raise ViriError('Could not check if file exists: {}'.format(
                exists_value))

        return file_id