1313import org .slf4j .LoggerFactory ;
1414
1515import dev .snowdrop .buildpack .config .RegistryAuthConfig ;
16+ import dev .snowdrop .buildpack .config .HostAndSocketConfig ;
1617import dev .snowdrop .buildpack .utils .OperatingSytem ;
1718
1819import com .github .dockerjava .api .DockerClient ;
@@ -34,24 +35,23 @@ public static DockerClient getDockerClient() {
3435 return getDockerClient (probeContainerRuntime (null ));
3536 }
3637
37- public static DockerClient getDockerClient (HostAndSocket runtimeInfo ) {
38+ public static DockerClient getDockerClient (HostAndSocketConfig runtimeInfo ) {
3839 return getDockerClient (runtimeInfo , new ArrayList <RegistryAuthConfig >(){});
3940 }
4041
4142 /**
4243 * Simple util to get a DockerClient for the platform. probably needs more work
4344 * for other platforms, and we may want a way to configure authentication etc.
4445 */
45- public static DockerClient getDockerClient (HostAndSocket runtimeInfo , List <RegistryAuthConfig > authConfigs ) {
46- if (runtimeInfo == null || runtimeInfo .host == null || runtimeInfo .host .isEmpty () ||
47- runtimeInfo .socket == null || runtimeInfo .socket .isEmpty ()) {
46+ public static DockerClient getDockerClient (HostAndSocketConfig runtimeInfo , List <RegistryAuthConfig > authConfigs ) {
47+ if (runtimeInfo == null || !runtimeInfo .getHost ().isPresent () || !runtimeInfo .getSocket ().isPresent ()) {
4848 log .warn ("Supplied host/socket was null, attempting to use auto-configured defaults" );
4949 return getDockerClient (probeContainerRuntime (runtimeInfo ), authConfigs );
5050 }
5151
52- log .debug ("Using dockerhost " + runtimeInfo .host );
52+ log .debug ("Using dockerhost " + runtimeInfo .getHost (). get () );
5353 DockerClientConfig config = DefaultDockerClientConfig .createDefaultConfigBuilder ()
54- .withDockerHost (runtimeInfo .host )
54+ .withDockerHost (runtimeInfo .getHost (). get () )
5555 .build ();
5656
5757 AuthDelegatingDockerClientConfig addcc = new AuthDelegatingDockerClientConfig (config );
@@ -67,55 +67,41 @@ public static DockerClient getDockerClient(HostAndSocket runtimeInfo, List<Regis
6767 return dockerClient ;
6868 }
6969
70- public static class HostAndSocket {
71- public String host ;
72- public String socket ;
73- public HostAndSocket (String host , String socket ){
74- this .host = host ; this .socket = socket ;
75- }
76- public HostAndSocket (String host ){
77- this .host = host ;
78- this .socket = null ;
79- }
80- }
81-
82- public static HostAndSocket probeContainerRuntime (HostAndSocket overrides ) {
70+ public static HostAndSocketConfig probeContainerRuntime (HostAndSocketConfig overrides ) {
71+ log .debug ("Probing for container runtime... " +(overrides !=null ? "H:" +overrides .getHost ()+" S:" +overrides .getSocket () : "no overrides" ));
8372 if (overrides !=null ){
8473 //if user has supplied both host & socket, we don't need to probe at all, use their values.
8574 //(using isEmpty as isBlank is jdk11 onwards)
86- if (overrides .host !=null && overrides .socket !=null && !overrides .host .isEmpty () && !overrides .socket .isEmpty ()){
87- return overrides ;
88- }
89-
90- //sanitize blank/empty values to null. (also using isEmpty because isBlank is only from jdk11 onwards)
91- if (overrides .host !=null && overrides .host .isEmpty ()){
92- overrides .host =null ;
93- }
94- if (overrides .socket !=null && overrides .socket .isEmpty ()){
95- overrides .socket =null ;
75+ if (overrides .getHost ().isPresent () && overrides .getSocket ().isPresent ()){
76+ return new HostAndSocketConfig (overrides .getHost ().get (),overrides .getSocket ().get ());
9677 }
97- }else {
98- //no overrides at all? make an empty one.
99- overrides = new HostAndSocket (null , null );
10078 }
10179
10280 try {
10381 //configure the override values as Optionals.
104- Optional <String > dockerHost = Optional .ofNullable ( overrides .host );
82+ Optional <String > dockerHost = overrides == null ? Optional .empty () : overrides .getHost ( );
10583 //for dockerhost, if user override was null, try to honor the env var
10684 if (!dockerHost .isPresent ()){
10785 dockerHost = Optional .ofNullable (System .getenv ("DOCKER_HOST" ));
10886 }
109- Optional <String > dockerSocket = Optional .ofNullable (overrides .socket );
87+ Optional <String > dockerSocket = overrides ==null ? Optional .empty () : overrides .getSocket ();
88+
89+ //if we now have a host & socket, we are done.
90+ if (dockerHost .isPresent () && dockerSocket .isPresent ()){
91+ log .debug ("Using docker host " +dockerHost .get ()+" and socket " +dockerSocket .get ());
92+ return new HostAndSocketConfig (dockerHost .get (),dockerSocket .get ());
93+ }
11094
11195 //if dockerhost is specified, but docker socket is not, test if dockerhost is podman rootful,
11296 //and autoconfigure dockersocket.. otherwise invoking podman as the user may result in the
11397 //user socket being selected for use with the rootful host, leading to failure.
11498 if ( dockerHost .isPresent () && !dockerSocket .isPresent () &&
11599 ( "unix:///var/run/podman/podman.sock" .equals (dockerHost .get ()) || "unix:///run/podman/podman.sock" .equals (dockerHost .get ()) )){
116- return new HostAndSocket (dockerHost .get (), dockerHost .get ().substring ("unix://" .length ()));
100+ log .debug ("Using podman rootful host " +dockerHost .get ()+" and socket " +dockerHost .get ().substring ("unix://" .length ()));
101+ return new HostAndSocketConfig (dockerHost .get (), dockerHost .get ().substring ("unix://" .length ()));
117102 }
118103
104+ //we are still missing a host or socket, so we need to probe for them.
119105 //try to obtain podman socket path..
120106 log .info ("Testing for podman/docker..." );
121107 DockerClientUtils .CmdResult cr = DockerClientUtils .start (PODMAN_SOCKET );
@@ -124,58 +110,59 @@ public static HostAndSocket probeContainerRuntime(HostAndSocket overrides) {
124110 String socket = cr .output .get (0 );
125111 if (socket .startsWith ("unix://" )){
126112 socket = socket .substring ("unix://" .length ());
127- }
113+ }
114+ log .debug ("Using derived socket path value of " +socket +" from podman cli invocation." );
128115 //podman was present, use podman to retrieve dockerhost value
129116 switch (OperatingSytem .getOperationSystem ()) {
130117 case WIN :{
131118 DockerClientUtils .CmdResult scmd = DockerClientUtils .start (WIN_PODMAN_HOST );
132119 if (scmd .rc ==0 ){
133- String fixedhost = scmd .output .get (0 ).replaceAll ("\\ " , "/" );
134- return new HostAndSocket (dockerHost .orElse (fixedhost ), dockerSocket .orElse (cr . output . get ( 0 ) ));
120+ String fixedhost = scmd .output .get (0 ).replaceAll ("\\ \\ " , "/" );
121+ return new HostAndSocketConfig (dockerHost .orElse (fixedhost ), dockerSocket .orElse (socket ));
135122 }else {
136123 log .warn ("Unable to obtain podman socket path from podman, using internal default" );
137- return new HostAndSocket (dockerHost .orElse ("npipe:////./pipe/docker_engine" ),dockerSocket .orElse ("/var/run/docker.sock" ));
124+ return new HostAndSocketConfig (dockerHost .orElse ("npipe:////./pipe/docker_engine" ),dockerSocket .orElse ("/var/run/docker.sock" ));
138125 }
139126 }
140127 case LINUX :{
141128 DockerClientUtils .CmdResult scmd = DockerClientUtils .start (LIN_PODMAN_HOST );
142129 if (scmd .rc ==0 ){
143- return new HostAndSocket (dockerHost .orElse (scmd .output .get (0 )), dockerSocket .orElse (socket ));
130+ return new HostAndSocketConfig (dockerHost .orElse (scmd .output .get (0 )), dockerSocket .orElse (socket ));
144131 }else {
145132 log .warn ("Unable to obtain podman socket path from podman, using internal default" );
146- return new HostAndSocket (dockerHost .orElse ("unix:///var/run/podman.sock" ), dockerSocket .orElse ("/var/run/podman.sock" ));
133+ return new HostAndSocketConfig (dockerHost .orElse ("unix:///var/run/podman.sock" ), dockerSocket .orElse ("/var/run/podman.sock" ));
147134 }
148135 }
149136 case MAC :{
150137 DockerClientUtils .CmdResult scmd = DockerClientUtils .start (MAC_PODMAN_HOST );
151138 if (scmd .rc ==0 ){
152- return new HostAndSocket (dockerHost .orElse (scmd .output .get (0 )), dockerSocket .orElse (socket ));
139+ return new HostAndSocketConfig (dockerHost .orElse (scmd .output .get (0 )), dockerSocket .orElse (socket ));
153140 }else {
154141 log .warn ("Unable to obtain podman socket path from podman, using internal default" );
155- return new HostAndSocket (dockerHost .orElse ("unix:///var/run/podman.sock" ), dockerSocket .orElse ("/var/run/podman.sock" ));
142+ return new HostAndSocketConfig (dockerHost .orElse ("unix:///var/run/podman.sock" ), dockerSocket .orElse ("/var/run/podman.sock" ));
156143 }
157144 }
158145 case UNKNOWN :{
159146 log .warn ("Unable to identify Operating System, you may need to specify docker host / docker socket manually" );
160- return new HostAndSocket (dockerHost .orElse ("unix:///var/run/podman.sock" ), dockerSocket .orElse ("/var/run/podman.sock" ));
147+ return new HostAndSocketConfig (dockerHost .orElse ("unix:///var/run/podman.sock" ), dockerSocket .orElse ("/var/run/podman.sock" ));
161148 }
162149 }
163150 }else {
164151 log .info ("Assuming docker, configuring." );
165152 //failed to obtain podman socket path, assuming docker..
166153 switch (OperatingSytem .getOperationSystem ()) {
167154 case WIN :{
168- return new HostAndSocket (dockerHost .orElse ("npipe:////./pipe/docker_engine" ),dockerSocket .orElse ("/var/run/docker.sock" ));
155+ return new HostAndSocketConfig (dockerHost .orElse ("npipe:////./pipe/docker_engine" ),dockerSocket .orElse ("/var/run/docker.sock" ));
169156 }
170157 case LINUX :{
171- return new HostAndSocket (dockerHost .orElse ("unix:///var/run/docker.sock" ), dockerSocket .orElse ("/var/run/docker.sock" ));
158+ return new HostAndSocketConfig (dockerHost .orElse ("unix:///var/run/docker.sock" ), dockerSocket .orElse ("/var/run/docker.sock" ));
172159 }
173160 case MAC :{
174- return new HostAndSocket (dockerHost .orElse ("unix:///var/run/docker.sock" ), dockerSocket .orElse ("/var/run/docker.sock" ));
161+ return new HostAndSocketConfig (dockerHost .orElse ("unix:///var/run/docker.sock" ), dockerSocket .orElse ("/var/run/docker.sock" ));
175162 }
176163 case UNKNOWN :{
177164 log .warn ("Unable to identify Operating System, you may need to specify docker host / docker socket manually" );
178- return new HostAndSocket (dockerHost .orElse ("unix:///var/run/docker.sock" ), dockerSocket .orElse ("/var/run/docker.sock" ));
165+ return new HostAndSocketConfig (dockerHost .orElse ("unix:///var/run/docker.sock" ), dockerSocket .orElse ("/var/run/docker.sock" ));
179166 }
180167 }
181168 }
0 commit comments