|
1 | 1 | package com.github.dockerjava.netty;
|
2 | 2 |
|
| 3 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.InetAddress; |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.net.SocketAddress; |
| 9 | +import java.nio.channels.spi.SelectorProvider; |
| 10 | +import java.security.Security; |
| 11 | + |
| 12 | +import javax.net.ssl.SSLEngine; |
| 13 | +import javax.net.ssl.SSLParameters; |
| 14 | + |
| 15 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 16 | + |
3 | 17 | import com.github.dockerjava.api.command.AttachContainerCmd;
|
4 | 18 | import com.github.dockerjava.api.command.AuthCmd;
|
5 | 19 | import com.github.dockerjava.api.command.BuildImageCmd;
|
|
39 | 53 | import com.github.dockerjava.api.command.RemoveImageCmd;
|
40 | 54 | import com.github.dockerjava.api.command.RemoveNetworkCmd;
|
41 | 55 | import com.github.dockerjava.api.command.RemoveVolumeCmd;
|
| 56 | +import com.github.dockerjava.api.command.RenameContainerCmd; |
42 | 57 | import com.github.dockerjava.api.command.RestartContainerCmd;
|
43 | 58 | import com.github.dockerjava.api.command.SaveImageCmd;
|
44 | 59 | import com.github.dockerjava.api.command.SearchImagesCmd;
|
|
51 | 66 | import com.github.dockerjava.api.command.UpdateContainerCmd;
|
52 | 67 | import com.github.dockerjava.api.command.VersionCmd;
|
53 | 68 | import com.github.dockerjava.api.command.WaitContainerCmd;
|
54 |
| -import com.github.dockerjava.api.command.RenameContainerCmd; |
55 | 69 | import com.github.dockerjava.core.DockerClientConfig;
|
56 | 70 | import com.github.dockerjava.core.DockerClientImpl;
|
57 | 71 | import com.github.dockerjava.core.SSLConfig;
|
|
93 | 107 | import com.github.dockerjava.netty.exec.RemoveImageCmdExec;
|
94 | 108 | import com.github.dockerjava.netty.exec.RemoveNetworkCmdExec;
|
95 | 109 | import com.github.dockerjava.netty.exec.RemoveVolumeCmdExec;
|
| 110 | +import com.github.dockerjava.netty.exec.RenameContainerCmdExec; |
96 | 111 | import com.github.dockerjava.netty.exec.RestartContainerCmdExec;
|
97 | 112 | import com.github.dockerjava.netty.exec.SaveImageCmdExec;
|
98 | 113 | import com.github.dockerjava.netty.exec.SearchImagesCmdExec;
|
|
105 | 120 | import com.github.dockerjava.netty.exec.UpdateContainerCmdExec;
|
106 | 121 | import com.github.dockerjava.netty.exec.VersionCmdExec;
|
107 | 122 | import com.github.dockerjava.netty.exec.WaitContainerCmdExec;
|
108 |
| -import com.github.dockerjava.netty.exec.RenameContainerCmdExec; |
109 | 123 |
|
110 | 124 | import io.netty.bootstrap.Bootstrap;
|
| 125 | +import io.netty.channel.ChannelFactory; |
111 | 126 | import io.netty.channel.ChannelInitializer;
|
112 | 127 | import io.netty.channel.EventLoopGroup;
|
113 |
| -import io.netty.channel.epoll.EpollDomainSocketChannel; |
114 |
| -import io.netty.channel.epoll.EpollEventLoopGroup; |
115 | 128 | import io.netty.channel.nio.NioEventLoopGroup;
|
116 | 129 | import io.netty.channel.socket.DuplexChannel;
|
117 | 130 | import io.netty.channel.socket.SocketChannel;
|
118 | 131 | import io.netty.channel.socket.nio.NioSocketChannel;
|
119 |
| -import io.netty.channel.unix.DomainSocketAddress; |
120 |
| -import io.netty.channel.unix.UnixChannel; |
121 | 132 | import io.netty.handler.codec.http.HttpClientCodec;
|
122 | 133 | import io.netty.handler.logging.LoggingHandler;
|
123 | 134 | import io.netty.handler.ssl.SslHandler;
|
124 | 135 | import io.netty.util.concurrent.DefaultThreadFactory;
|
125 |
| - |
126 |
| -import org.bouncycastle.jce.provider.BouncyCastleProvider; |
127 |
| - |
128 |
| -import javax.net.ssl.SSLEngine; |
129 |
| -import javax.net.ssl.SSLParameters; |
130 |
| - |
131 |
| -import java.io.IOException; |
132 |
| -import java.net.InetAddress; |
133 |
| -import java.net.InetSocketAddress; |
134 |
| -import java.net.SocketAddress; |
135 |
| -import java.security.Security; |
136 |
| - |
137 |
| -import static com.google.common.base.Preconditions.checkNotNull; |
| 136 | +import jnr.enxio.channels.NativeSelectorProvider; |
| 137 | +import jnr.unixsocket.UnixSocketAddress; |
| 138 | +import jnr.unixsocket.UnixSocketChannel; |
138 | 139 |
|
139 | 140 | /**
|
140 | 141 | * Experimental implementation of {@link DockerCmdExecFactory} that supports http connection hijacking that is needed to pass STDIN to the
|
@@ -215,22 +216,50 @@ private interface NettyInitializer {
|
215 | 216 | }
|
216 | 217 |
|
217 | 218 | private class UnixDomainSocketInitializer implements NettyInitializer {
|
| 219 | + |
| 220 | + final java.io.File path = new java.io.File("/var/run/docker.sock"); |
| 221 | + |
218 | 222 | @Override
|
219 | 223 | public EventLoopGroup init(Bootstrap bootstrap, DockerClientConfig dockerClientConfig) {
|
220 |
| - EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, new DefaultThreadFactory(threadPrefix)); |
221 |
| - bootstrap.group(epollEventLoopGroup).channel(EpollDomainSocketChannel.class) |
222 |
| - .handler(new ChannelInitializer<UnixChannel>() { |
| 224 | + final SelectorProvider nativeSelectorProvider = NativeSelectorProvider.getInstance(); |
| 225 | + |
| 226 | + EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0, |
| 227 | + new DefaultThreadFactory(threadPrefix), nativeSelectorProvider); |
| 228 | + |
| 229 | + ChannelFactory<NioSocketChannel> factory = new ChannelFactory<NioSocketChannel>() { |
| 230 | + |
| 231 | + @Override |
| 232 | + public NioSocketChannel newChannel() { |
| 233 | + try { |
| 234 | + return new NioSocketChannel(UnixSocketChannel.create()); |
| 235 | + } catch (IOException e) { |
| 236 | + throw new RuntimeException(); |
| 237 | + } |
| 238 | + } |
| 239 | + }; |
| 240 | + |
| 241 | + bootstrap.group(nioEventLoopGroup).channelFactory(factory) |
| 242 | + .handler(new ChannelInitializer<SocketChannel>() { |
223 | 243 | @Override
|
224 |
| - protected void initChannel(final UnixChannel channel) throws Exception { |
| 244 | + protected void initChannel(final SocketChannel channel) throws Exception { |
| 245 | + channel.pipeline().addLast(new LoggingHandler(getClass())); |
225 | 246 | channel.pipeline().addLast(new HttpClientCodec());
|
226 | 247 | }
|
227 | 248 | });
|
228 |
| - return epollEventLoopGroup; |
| 249 | + |
| 250 | + return nioEventLoopGroup; |
229 | 251 | }
|
230 | 252 |
|
231 | 253 | @Override
|
232 | 254 | public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException {
|
233 |
| - return (DuplexChannel) bootstrap.connect(new DomainSocketAddress("/var/run/docker.sock")).sync().channel(); |
| 255 | + |
| 256 | + if (!path.exists()) { |
| 257 | + throw new RuntimeException("socket not found: " + path); |
| 258 | + } |
| 259 | + |
| 260 | + UnixSocketAddress address = new UnixSocketAddress(path); |
| 261 | + |
| 262 | + return (DuplexChannel) bootstrap.connect(address).sync().channel(); |
234 | 263 | }
|
235 | 264 | }
|
236 | 265 |
|
|
0 commit comments