예제 #1
0
 def __init__(self, netns, flags=os.O_CREAT):
     self.netns = netns
     self.flags = flags
     self.cmdch, cmdch = MpPipe()
     self.brdch, brdch = MpPipe()
     self.server = MpProcess(target=NetNServer,
                             args=(self.netns, cmdch, brdch, self.flags))
     self.server.start()
     super(NetNS, self).__init__()
     self.marshal = MarshalRtnl()
예제 #2
0
파일: nslink.py 프로젝트: thezeep/pyroute2
 def __init__(self, *argv, **kwarg):
     self.cmdlock = threading.Lock()
     self.rcvch, rcvch = MpPipe()
     self.cmdch, cmdch = MpPipe()
     self.server = MpProcess(target=NetNServer,
                             args=(self.netns, rcvch, cmdch, self.flags))
     self.server.start()
     error = self.cmdch.recv()
     if error is not None:
         self.server.join()
         raise error
     else:
         atexit.register(self.close)
예제 #3
0
    def __init__(self, nsname, *argv, **kwarg):
        '''
        The only differences from the `subprocess.Popen` init are:
        * `nsname` -- network namespace name
        * `flags` keyword argument

        All other arguments are passed directly to `subprocess.Popen`.

        Flags usage samples. Create a network namespace, if it doesn't
        exist yet::

            import os
            nsp = NSPopen('nsname', ['command'], flags=os.O_CREAT)

        Create a network namespace only if it doesn't exist, otherwise
        fail and raise an exception::

            import os
            nsp = NSPopen('nsname', ['command'], flags=os.O_CREAT | os.O_EXCL)
        '''
        # create a child
        self.nsname = nsname
        if 'flags' in kwarg:
            self.flags = kwarg.pop('flags')
        else:
            self.flags = 0
        self.channel_out = MpQueue()
        self.channel_in = MpQueue()
        self.lock = threading.Lock()
        self.released = False
        self.server = MpProcess(target=NSPopenServer,
                                args=(self.nsname,
                                      self.flags,
                                      self.channel_out,
                                      self.channel_in,
                                      argv, kwarg))
        # start the child and check the status
        self.server.start()
        response = self.channel_in.get()
        if isinstance(response, Exception):
            self.server.join()
            raise response
        else:
            atexit.register(self.release)