44import com .docker .service .DockerImageOperations ;
55import com .github .dockerjava .api .DockerClient ;
66import com .github .dockerjava .api .command .BuildImageCmd ;
7+ import com .github .dockerjava .api .model .AuthConfig ;
78import com .github .dockerjava .core .command .BuildImageResultCallback ;
9+ import com .github .dockerjava .core .command .PullImageResultCallback ;
810import com .github .dockerjava .core .command .PushImageResultCallback ;
911import com .github .dockerjava .core .util .CompressArchiveUtil ;
1012import org .apache .commons .io .FileUtils ;
1113import org .apache .commons .io .filefilter .TrueFileFilter ;
1214import org .apache .commons .lang .StringUtils ;
1315import org .slf4j .Logger ;
1416import org .slf4j .LoggerFactory ;
15- import org .springframework .beans .factory .annotation .Autowired ;
16- import org .springframework .beans .factory .annotation .Value ;
17- import org .springframework .stereotype .Service ;
1817import org .springframework .util .CollectionUtils ;
1918
2019import java .io .File ;
2726import java .util .UUID ;
2827import java .util .concurrent .TimeUnit ;
2928
30- @ Service ("dockerClient.dockerImageOperations" )
29+ /**
30+ * Docker镜像操作服务
31+ */
3132public class InternalDockerImageOperations implements DockerImageOperations {
3233
33- @ Value ("${dockerClient.pushImagesAwaitCompletionSeconds:30}" )
34- private int pushImagesAwaitCompletionSeconds = 30 ;
35-
3634 private static final Logger logger = LoggerFactory .getLogger (InternalDockerImageOperations .class );
3735
38- @ Autowired
3936 private DockerClient dockerClient ;
37+ private String registryRepository = "localhost:8081" ;
38+ private long pullImagesAwaitSeconds = 30 ;
39+ private long pushImagesAwaitSeconds = 30 ;
40+
41+ public InternalDockerImageOperations setRegistryRepository (String registryRepository ) {
42+ if (StringUtils .isBlank (registryRepository )) {
43+ throw new DockerImageException ("pullImage-init" , String .format ("镜像仓库地址不能为NULL或者空字符串。" , pullImagesAwaitSeconds ));
44+ }
45+ this .registryRepository = registryRepository ;
46+ return this ;
47+ }
48+
49+ public InternalDockerImageOperations setPullImagesAwaitSeconds (long pullImagesAwaitSeconds ) {
50+ if (pullImagesAwaitSeconds <= 0 ) {
51+ throw new DockerImageException ("pullImage-init" , String .format ("镜像拉取延迟时间(%秒)不合法。" , pullImagesAwaitSeconds ));
52+ }
53+ this .pullImagesAwaitSeconds = pullImagesAwaitSeconds ;
54+ return this ;
55+ }
56+
57+ public InternalDockerImageOperations setPushImagesAwaitSeconds (long pushImagesAwaitSeconds ) {
58+ if (pullImagesAwaitSeconds <= 0 ) {
59+ throw new DockerImageException ("pullImage-init" , String .format ("镜像推动延迟时间(%秒)不合法。" , pushImagesAwaitSeconds ));
60+ }
61+ this .pushImagesAwaitSeconds = pushImagesAwaitSeconds ;
62+ return this ;
63+ }
64+
65+ public InternalDockerImageOperations (DockerClient dockerClient ) {
66+ if (dockerClient == null ) {
67+ throw new DockerImageException ("pullImage-init" , String .format ("docker连接器不能为NULL或空字符串。" ));
68+ }
69+ this .dockerClient = dockerClient ;
70+ }
71+
72+ @ Override
73+ public void pullImageFrom (String imageRepository , AuthConfig authConfig ) throws DockerImageException {
74+ if (StringUtils .isBlank (imageRepository )) {
75+ throw new DockerImageException ("pullImage-001" , "镜像地址不能为NULL或者空字符串。" );
76+ }
77+ try {
78+ dockerClient .pullImageCmd (imageRepository ).
79+ withAuthConfig (authConfig ).
80+ exec (new PullImageResultCallback ()).
81+ awaitCompletion (pullImagesAwaitSeconds , TimeUnit .SECONDS );
82+ } catch (InterruptedException e ) {
83+ throw new DockerImageException ("pullImage-004" , String .format ("下载镜像 %s 失败。" , imageRepository ));
84+ }
85+ }
4086
41- @ Value ("${registryRepository:localhost:9005}" )
42- private String registryRepository = "localhost:9005" ;
87+ @ Override
88+ public void pullImage (String imageRepository ) throws DockerImageException {
89+ if (StringUtils .isBlank (imageRepository )) {
90+ throw new DockerImageException ("pullImage-001" , "镜像地址不能为NULL或者空字符串。" );
91+ }
92+ try {
93+ dockerClient
94+ .pullImageCmd (imageRepository )
95+ .exec (new PullImageResultCallback ())
96+ .awaitCompletion (pullImagesAwaitSeconds , TimeUnit .SECONDS );
97+ } catch (InterruptedException e ) {
98+ throw new DockerImageException ("pullImage-004" , String .format ("下载镜像 %s 失败。" , imageRepository ));
99+ }
100+ }
43101
44102 /**
45103 * @param dockerFilePath
46104 * @param imageRepository 仓库名为两段式路径,比如 jwilder/nginx-proxy
47105 * @param tag
48106 * @return
49107 */
108+ @ Override
50109 public String buildImage (String dockerFilePath , String imageRepository , String tag ) throws DockerImageException {
51110 if (StringUtils .isBlank (imageRepository )) {
52111 throw new DockerImageException ("buildImage-002" , "镜像仓库名不能为NULL或者空字符串。" );
@@ -68,18 +127,21 @@ public void pushImage(String imageRepository, String tag) throws DockerImageExce
68127 dockerClient .pushImageCmd (imageName )
69128 .withAuthConfig (dockerClient .authConfig ())
70129 .exec (new PushImageResultCallback ())
71- .awaitCompletion (30 , TimeUnit .SECONDS );
130+ .awaitCompletion (pushImagesAwaitSeconds , TimeUnit .SECONDS );
72131 } catch (InterruptedException e ) {
73132 logger .error ("推送镜像失败。" , e );
74- throw new DockerImageException ("buildImage-006" , "镜像制作文件读取失败。" );
133+ throw new DockerImageException ("pushImage-001" , "镜像制作文件读取失败。" );
134+ } catch (Exception e ) {
135+ logger .error ("推送镜像失败。" , e );
136+ throw new DockerImageException ("pushImage-002" , "推送镜像失败。" );
75137 }
76138 }
77139
78140 private InputStream fetchImageBuildFilesToTarFile (String dockerFilePath ) {
79141 if (StringUtils .isBlank (dockerFilePath )) {
80142 throw new DockerImageException ("buildImage-001" , "镜像制作文件目录不能为NULL或者空字符串。" );
81143 }
82- File dockerFile = new File (dockerFilePath );
144+ File dockerFile = FileUtils . getFile (dockerFilePath );
83145 if (dockerFile .exists () == false ) {
84146 throw new DockerImageException ("buildImage-004" , "镜像制作文件目录不存在。" );
85147 }
@@ -99,7 +161,7 @@ private InputStream fetchImageBuildFilesToTarFile(String dockerFilePath) {
99161 private String dockerfileBuild (InputStream inputStream , String imageName ) {
100162 BuildImageCmd buildImageCmd = dockerClient
101163 .buildImageCmd ()
102- .withTags (new HashSet < String > (Arrays .asList (imageName )))
164+ .withTags (new HashSet (Arrays .asList (imageName )))
103165 .withTarInputStream (inputStream );
104166 return buildImageCmd .withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
105167 }
0 commit comments