11package com .docker .infrastructure .service ;
22
33import com .docker .exception .DockerImageException ;
4+ import com .docker .model .ImageRepository ;
5+ import com .docker .model .RegistryAuthConfig ;
46import com .docker .service .DockerImageOperations ;
57import com .github .dockerjava .api .DockerClient ;
68import com .github .dockerjava .api .command .BuildImageCmd ;
3234public class InternalDockerImageOperations implements DockerImageOperations {
3335
3436 private static final Logger logger = LoggerFactory .getLogger (InternalDockerImageOperations .class );
35-
3637 private DockerClient dockerClient ;
37- private String registryRepository = "localhost:8081" ;
3838 private long pullImagesAwaitSeconds = 30 ;
3939 private long pushImagesAwaitSeconds = 30 ;
4040
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-
4941 public InternalDockerImageOperations setPullImagesAwaitSeconds (long pullImagesAwaitSeconds ) {
5042 if (pullImagesAwaitSeconds <= 0 ) {
5143 throw new DockerImageException ("pullImage-init" , String .format ("镜像拉取延迟时间(%秒)不合法。" , pullImagesAwaitSeconds ));
@@ -70,91 +62,113 @@ public InternalDockerImageOperations(DockerClient dockerClient) {
7062 }
7163
7264 @ Override
73- public void pullImageFrom (String imageRepository , AuthConfig authConfig ) throws DockerImageException {
74- if (StringUtils .isBlank (imageRepository )) {
75- throw new DockerImageException ("pullImage-001" , "镜像地址不能为NULL或者空字符串。" );
65+ public void pullImageFrom (ImageRepository imageNameWithRepository , RegistryAuthConfig registryAuthConfig ) throws DockerImageException {
66+ if (imageNameWithRepository == null ) {
67+ throw new DockerImageException ("pullImageFrom-001" , "镜像地址不能为NULL。" );
68+ }
69+
70+ if (registryAuthConfig == null ) {
71+ throw new DockerImageException ("pullImageFrom-002" , "镜像仓库认证信息不能为NULL。" );
7672 }
73+
74+ AuthConfig authConfig = new AuthConfig ()
75+ .withUsername (registryAuthConfig .getUsername ())
76+ .withPassword (registryAuthConfig .getPassword ())
77+ .withRegistryAddress (registryAuthConfig .getRegistryAddress ());
78+
79+ String imageName = imageNameWithRepository .toString ();
7780 try {
78- dockerClient .pullImageCmd (imageRepository ).
81+ dockerClient .pullImageCmd (imageName ).
7982 withAuthConfig (authConfig ).
8083 exec (new PullImageResultCallback ()).
8184 awaitCompletion (pullImagesAwaitSeconds , TimeUnit .SECONDS );
8285 } catch (InterruptedException e ) {
83- throw new DockerImageException ("pullImage-004 " , String .format ("下载镜像 %s 失败。" , imageRepository ));
86+ throw new DockerImageException ("pullImageFrom-003 " , String .format ("下载镜像 %s 失败。" , imageName ));
8487 }
8588 }
8689
8790 @ Override
88- public void pullImage (String imageRepository ) throws DockerImageException {
89- if (StringUtils . isBlank ( imageRepository ) ) {
90- throw new DockerImageException ("pullImage-001" , "镜像地址不能为NULL或者空字符串 。" );
91+ public void pullImage (ImageRepository imageNameWithRepository ) throws DockerImageException {
92+ if (imageNameWithRepository == null ) {
93+ throw new DockerImageException ("pullImage-001" , "镜像地址不能为NULL 。" );
9194 }
95+ this .pullImage (imageNameWithRepository .toString ());
96+ }
97+
98+ @ Override
99+ public void pullImage (String imageNameWithRepository ) throws DockerImageException {
92100 try {
93101 dockerClient
94- .pullImageCmd (imageRepository )
102+ .pullImageCmd (imageNameWithRepository )
95103 .exec (new PullImageResultCallback ())
96104 .awaitCompletion (pullImagesAwaitSeconds , TimeUnit .SECONDS );
97105 } catch (InterruptedException e ) {
98- throw new DockerImageException ("pullImage-004 " , String .format ("下载镜像 %s 失败。" , imageRepository ));
106+ throw new DockerImageException ("pullImage-002 " , String .format ("下载镜像 %s 失败。" , imageNameWithRepository ));
99107 }
100108 }
101109
102- /**
103- * @param dockerFilePath
104- * @param imageRepository 仓库名为两段式路径,比如 jwilder/nginx-proxy
105- * @param tag
106- * @return
107- */
108110 @ Override
109- public String buildImage (String dockerFilePath , String imageRepository , String tag ) throws DockerImageException {
110- if (StringUtils . isBlank ( imageRepository ) ) {
111+ public String buildImage (String dockerFilePath , ImageRepository imageNameWithRepository ) throws DockerImageException {
112+ if (imageNameWithRepository == null ) {
111113 throw new DockerImageException ("buildImage-002" , "镜像仓库名不能为NULL或者空字符串。" );
112114 }
113- if (StringUtils .isBlank (tag )) {
114- throw new DockerImageException ("buildImage-003" , "镜像标签名不能为NULL或者空字符串。" );
115- }
116115
116+ String imageName = imageNameWithRepository .toString ();
117117 InputStream tarFile = fetchImageBuildFilesToTarFile (dockerFilePath );
118- String imageName = String .format ("%s/%s:%s" , registryRepository , imageRepository , tag );
119118 return dockerfileBuild (tarFile , imageName );
120119 }
121120
122-
123121 @ Override
124- public void pushImage (String imageRepository , String tag ) throws DockerImageException {
125- String imageName = String .format ("%s/%s:%s" , registryRepository , imageRepository , tag );
122+ public void pushImageToLocal (ImageRepository imageNameWithRepository ) throws DockerImageException {
123+ if (imageNameWithRepository == null ) {
124+ throw new DockerImageException ("pushImageToLocal-001" , "镜像地址不能为NULL。" );
125+ }
126+
127+ String imageName = imageNameWithRepository .toString ();
126128 try {
127- dockerClient .pushImageCmd (imageName )
128- . withAuthConfig (dockerClient .authConfig ())
129- . exec (new PushImageResultCallback ())
130- . awaitCompletion (pushImagesAwaitSeconds , TimeUnit .SECONDS );
129+ dockerClient .pushImageCmd (imageName ).
130+ withAuthConfig (dockerClient .authConfig ()).
131+ exec (new PushImageResultCallback ()).
132+ awaitCompletion (pushImagesAwaitSeconds , TimeUnit .SECONDS );
131133 } catch (InterruptedException e ) {
132134 logger .error ("推送镜像失败。" , e );
133- throw new DockerImageException ("pushImage-001 " , "镜像制作文件读取失败。" );
135+ throw new DockerImageException ("pushImageToLocal-002 " , "镜像制作文件读取失败。" );
134136 } catch (Exception e ) {
135137 logger .error ("推送镜像失败。" , e );
136- throw new DockerImageException ("pushImage-002 " , "推送镜像失败。" );
138+ throw new DockerImageException ("pushImageToLocal-003 " , String . format ( "推送镜像 %s 失败。" , imageName ) );
137139 }
138140 }
139141
142+ @ Override
143+ public void tagImage (ImageRepository imageNameWithRepository , ImageRepository newImageNameWithRepository ) throws DockerImageException {
144+ if (imageNameWithRepository == null ) {
145+ throw new DockerImageException ("tagImage-001" , "原镜像地址不能为NULL。" );
146+ }
147+ if (newImageNameWithRepository == null ) {
148+ throw new DockerImageException ("tagImage-002" , "新镜像地址不能为NULL。" );
149+ }
150+
151+ dockerClient .tagImageCmd (imageNameWithRepository .toString (), newImageNameWithRepository .toStringWithoutTag (), newImageNameWithRepository .getTag ()).exec ();
152+ }
153+
140154 private InputStream fetchImageBuildFilesToTarFile (String dockerFilePath ) {
141155 if (StringUtils .isBlank (dockerFilePath )) {
142- throw new DockerImageException ("buildImage -001" , "镜像制作文件目录不能为NULL或者空字符串。" );
156+ throw new DockerImageException ("fetchImageBuildFilesToTarFile -001" , "镜像制作文件目录不能为NULL或者空字符串。" );
143157 }
144158 File dockerFile = FileUtils .getFile (dockerFilePath );
145159 if (dockerFile .exists () == false ) {
146- throw new DockerImageException ("buildImage-004 " , "镜像制作文件目录不存在。" );
160+ throw new DockerImageException ("fetchImageBuildFilesToTarFile-002 " , "镜像制作文件目录不存在。" );
147161 }
148162 Collection <File > files = FileUtils .listFiles (dockerFile , TrueFileFilter .INSTANCE , TrueFileFilter .INSTANCE );
149163 if (CollectionUtils .isEmpty (files )) {
150- throw new DockerImageException ("buildImage-005 " , "镜像制作文件不存在。" );
164+ throw new DockerImageException ("fetchImageBuildFilesToTarFile-003 " , "镜像制作文件不存在。" );
151165 }
152166 try {
153167 File file = CompressArchiveUtil .archiveTARFiles (dockerFile , files , UUID .randomUUID ().toString ());
154168 return new FileInputStream (file );
155169 } catch (IOException e ) {
156170 logger .error ("镜像制作文件读取失败。" , e );
157- throw new DockerImageException ("buildImage-006 " , "镜像制作失败。" );
171+ throw new DockerImageException ("fetchImageBuildFilesToTarFile-004 " , "镜像制作失败。" );
158172 }
159173 }
160174
@@ -166,5 +180,4 @@ private String dockerfileBuild(InputStream inputStream, String imageName) {
166180 return buildImageCmd .withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
167181 }
168182
169-
170183}
0 commit comments