Exemplo n.º 1
0
    def exec_command(self, cmd, tmp_path, become_user=None, sudoable=False, executable='/bin/sh', in_data=None):
        if in_data:
            raise errors.AnsibleError('Internal Error: this modules does not support optimized module pipelining')
       
        if executable:
            local_cmd = executable.split() + ['-c', cmd]
        else:
            local_cmd = cmd
        executable = executable.split()[0] if executable else None

        pipe_to_server = os.pipe()
        pipe_from_server = os.pipe()

        # os.environ is special, so we copy it into a dictionary and modify the dictionary instead
        env = dict()
        for key in os.environ.keys():
            env[key] = os.environ[key]
        env['SNMP_PIPE_IN'] = str(pipe_from_server[0])
        env['SNMP_PIPE_OUT'] = str(pipe_to_server[1])

        if 'PYTHONPATH' in env:
            env['PYTHONPATH'] = os.path.dirname(__file__) + ':' + env['PYTHONPATH']
        else:
            env['PYTHONPATH'] = os.path.dirname(__file__)

        vvv('EXEC %s' % (local_cmd), host=self.host)
        p = subprocess.Popen(local_cmd,
                             shell=isinstance(local_cmd, basestring),
                             cwd=self.runner.basedir,
                             executable=executable,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             env=env)

        # pysnmp insist on using its own socket map, so we use that instead
        conn = self._get_snmp_connection()
        sock_map = conn.dispatcher.getSocketMap()
        stdout = _BufferedDispatcher(asyncore.file_wrapper(p.stdout.fileno()), map=sock_map)
        stderr = _BufferedDispatcher(asyncore.file_wrapper(p.stderr.fileno()), map=sock_map)
        server = _Server(conn, pipe_to_server[0], pipe_from_server[1], map=sock_map)

        while stdout.readable() or stderr.readable():
            asyncore.poll(0.5, map=sock_map)
            conn.dispatcher.handleTimerTick(time.time())

        p.wait()

        os.close(pipe_to_server[0])
        os.close(pipe_to_server[1])
        os.close(pipe_from_server[0])
        os.close(pipe_from_server[1])
        
        return (p.returncode, '', stdout.data, stderr.data)
Exemplo n.º 2
0
 def test_close_twice(self):
     fd = os.open(support.TESTFN, os.O_RDONLY)
     f = asyncore.file_wrapper(fd)
     os.close(fd)
     f.close()
     self.assertEqual(f.fd, -1)
     f.close()
Exemplo n.º 3
0
 def test_resource_warning(self):
     # Issue #11453
     fd = os.open(support.TESTFN, os.O_RDONLY)
     f = asyncore.file_wrapper(fd)
     with support.check_warnings(('', ResourceWarning)):
         f = None
         support.gc_collect()
Exemplo n.º 4
0
 def test_close_twice(self):
     fd = os.open(support.TESTFN, os.O_RDONLY)
     f = asyncore.file_wrapper(fd)
     f.close()
     self.assertEqual(f.fd, -1)
     # calling close twice should not fail
     f.close()
Exemplo n.º 5
0
 def test_close_twice(self):
     fd = os.open(support.TESTFN, os.O_RDONLY)
     f = asyncore.file_wrapper(fd)
     f.close()
     self.assertEqual(f.fd, -1)
     # calling close twice should not fail
     f.close()
Exemplo n.º 6
0
 def test_resource_warning(self):
     # Issue #11453
     fd = os.open(support.TESTFN, os.O_RDONLY)
     f = asyncore.file_wrapper(fd)
     with support.check_warnings(('', ResourceWarning)):
         f = None
         support.gc_collect()
Exemplo n.º 7
0
 def test_recv(self):
     fd = os.open(support.TESTFN, os.O_RDONLY)
     w = asyncore.file_wrapper(fd)
     os.close(fd)
     self.assertNotEqual(w.fd, fd)
     self.assertNotEqual(w.fileno(), fd)
     self.assertEqual(w.recv(13), b"It's not dead")
     self.assertEqual(w.read(6), b", it's")
     w.close()
     self.assertRaises(OSError, w.read, 1)
Exemplo n.º 8
0
 def test_send(self):
     d1 = b'Come again?'
     d2 = b'I want to buy some cheese.'
     fd = os.open(support.TESTFN, os.O_WRONLY | os.O_APPEND)
     w = asyncore.file_wrapper(fd)
     os.close(fd)
     w.write(d1)
     w.send(d2)
     w.close()
     with open(support.TESTFN, 'rb') as file:
         self.assertEqual(file.read(), self.d + d1 + d2)
Exemplo n.º 9
0
    def test_send(self):
        d1 = "Come again?"
        d2 = "I want to buy some cheese."
        fd = os.open(TESTFN, os.O_WRONLY | os.O_APPEND)
        w = asyncore.file_wrapper(fd)
        os.close(fd)

        w.write(d1)
        w.send(d2)
        w.close()
        self.assertEqual(file(TESTFN).read(), self.d + d1 + d2)
Exemplo n.º 10
0
        def test_recv(self):
            fd = os.open(TESTFN, os.O_RDONLY)
            w = asyncore.file_wrapper(fd)
            os.close(fd)

            self.assertNotEqual(w.fd, fd)
            self.assertNotEqual(w.fileno(), fd)
            self.assertEqual(w.recv(13), "It's not dead")
            self.assertEqual(w.read(6), ", it's")
            w.close()
            self.assertRaises(OSError, w.read, 1)
Exemplo n.º 11
0
        def test_send(self):
            d1 = "Come again?"
            d2 = "I want to buy some cheese."
            fd = os.open(TESTFN, os.O_WRONLY | os.O_APPEND)
            w = asyncore.file_wrapper(fd)
            os.close(fd)

            w.write(d1)
            w.send(d2)
            w.close()
            self.assertEqual(file(TESTFN).read(), self.d + d1 + d2)
Exemplo n.º 12
0
    def test_close_twice(self):
        fd = os.open(support.TESTFN, os.O_RDONLY)
        f = asyncore.file_wrapper(fd)
        os.close(fd)

        os.close(f.fd)  # file_wrapper dupped fd
        with self.assertRaises(OSError):
            f.close()

        self.assertEqual(f.fd, -1)
        # calling close twice should not fail
        f.close()
Exemplo n.º 13
0
    def test_send(self):
        d1 = b"Come again?"
        d2 = b"I want to buy some cheese."
        fd = os.open(support.TESTFN, os.O_WRONLY | os.O_APPEND)
        w = asyncore.file_wrapper(fd)
        os.close(fd)

        w.write(d1)
        w.send(d2)
        w.close()
        with open(support.TESTFN, 'rb') as file:
            self.assertEqual(file.read(), self.d + d1 + d2)
Exemplo n.º 14
0
    def test_close_twice(self):
        fd = os.open(TESTFN, os.O_RDONLY)
        f = asyncore.file_wrapper(fd)
        os.close(fd)

        os.close(f.fd)  # file_wrapper dupped fd
        with self.assertRaises(OSError):
            f.close()

        self.assertEqual(f.fd, -1)
        # calling close twice should not fail
        f.close()
Exemplo n.º 15
0
    def init_file_dispatcher(self, fd):
        """
        Kludge to plug asyncore.file_dispatcher into asynchat.  Call from
        subclass's __init__() method, after calling
        PDUChannel.__init__(), and don't read this on a full stomach.
        """

        self.connected = True
        self._fileno = fd
        self.socket = asyncore.file_wrapper(fd)
        self.add_channel()
        flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
        flags = flags | os.O_NONBLOCK
        fcntl.fcntl(fd, fcntl.F_SETFL, flags)
Exemplo n.º 16
0
    def set_file(self, fd):
        try:
            # fd may be a file object
            fd = fd.fileno()
        except AttributeError:
            pass

        self.socket = asyncore.file_wrapper(fd)
        self._fileno = self.socket.fileno()
        self.add_channel()

        flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
        flags = flags | os.O_NONBLOCK
        fcntl.fcntl(fd, fcntl.F_SETFL, flags)
Exemplo n.º 17
0
  def init_file_dispatcher(self, fd):
    """
    Kludge to plug asyncore.file_dispatcher into asynchat.  Call from
    subclass's __init__() method, after calling
    PDUChannel.__init__(), and don't read this on a full stomach.
    """

    self.connected = True
    self._fileno = fd
    self.socket = asyncore.file_wrapper(fd)
    self.add_channel()
    flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
    flags = flags | os.O_NONBLOCK
    fcntl.fcntl(fd, fcntl.F_SETFL, flags)
Exemplo n.º 18
0
    def set_file(self, fd):
        try:
            # fd may be a file object
            fd = fd.fileno()
        except AttributeError:
            pass

        self.socket = asyncore.file_wrapper(fd)
        self._fileno = self.socket.fileno()
        self.add_channel()

        flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
        flags = flags | os.O_NONBLOCK
        fcntl.fcntl(fd, fcntl.F_SETFL, flags)
Exemplo n.º 19
0
 def __init__(self, event_to_raise):
     self._eventToRaise = event_to_raise
     self._in_fd, self._out_fd = os.pipe()
     self._pipe = asyncore.file_wrapper(self._in_fd)
     super().__init__(self._pipe)
Exemplo n.º 20
0
    def exec_command(self,
                     cmd,
                     tmp_path,
                     become_user=None,
                     sudoable=False,
                     executable='/bin/sh',
                     in_data=None):
        if in_data:
            raise errors.AnsibleError(
                'Internal Error: this modules does not support optimized module pipelining'
            )

        if executable:
            local_cmd = executable.split() + ['-c', cmd]
        else:
            local_cmd = cmd
        executable = executable.split()[0] if executable else None

        pipe_to_server = os.pipe()
        pipe_from_server = os.pipe()

        # os.environ is special, so we copy it into a dictionary and modify the dictionary instead
        env = dict()
        for key in os.environ.keys():
            env[key] = os.environ[key]
        env['SNMP_PIPE_IN'] = str(pipe_from_server[0])
        env['SNMP_PIPE_OUT'] = str(pipe_to_server[1])

        if 'PYTHONPATH' in env:
            env['PYTHONPATH'] = os.path.dirname(
                __file__) + ':' + env['PYTHONPATH']
        else:
            env['PYTHONPATH'] = os.path.dirname(__file__)

        vvv('EXEC %s' % (local_cmd), host=self.host)
        p = subprocess.Popen(local_cmd,
                             shell=isinstance(local_cmd, basestring),
                             cwd=self.runner.basedir,
                             executable=executable,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             env=env)

        # pysnmp insist on using its own socket map, so we use that instead
        conn = self._get_snmp_connection()
        sock_map = conn.dispatcher.getSocketMap()
        stdout = _BufferedDispatcher(asyncore.file_wrapper(p.stdout.fileno()),
                                     map=sock_map)
        stderr = _BufferedDispatcher(asyncore.file_wrapper(p.stderr.fileno()),
                                     map=sock_map)
        server = _Server(conn,
                         pipe_to_server[0],
                         pipe_from_server[1],
                         map=sock_map)

        while stdout.readable() or stderr.readable():
            asyncore.poll(0.5, map=sock_map)
            conn.dispatcher.handleTimerTick(time.time())

        p.wait()

        os.close(pipe_to_server[0])
        os.close(pipe_to_server[1])
        os.close(pipe_from_server[0])
        os.close(pipe_from_server[1])

        return (p.returncode, '', stdout.data, stderr.data)
Exemplo n.º 21
0
 def set_file(self, fd):
     # Like asyncore.file_dispatcher.set_file() but doesn't call
     # add_channel(). We'll call add_channel() in start() when the
     # download shall begin.
     self.socket = asyncore.file_wrapper(fd)
     self._fileno = self.socket.fileno()
Exemplo n.º 22
0
 def set_file(self, fd):
     self.socket = asyncore.file_wrapper(fd)
     self._fileno = self.socket.fileno()
Exemplo n.º 23
0
	def set_file(self, fd):
		self._fileno = fd
		self.socket = asyncore.file_wrapper(fd)
		if fd == 0:
			self.add_channel()
Exemplo n.º 24
0
	def set_file(self, fd):
		self._fileno = fd
		self.socket = asyncore.file_wrapper(fd)
		if fd == 0:
			self.add_channel()