Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 015ba283baa5b43e97a5b3bd3a356c48a87de3e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*******************************************************************************
 * Copyright (c) 2014, 2016 Red Hat.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.core;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.linuxtools.docker.core.DockerException;
import org.eclipse.linuxtools.docker.core.DockerImagePullFailedException;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerProgressDetail;
import org.eclipse.linuxtools.docker.core.IDockerProgressHandler;
import org.eclipse.linuxtools.docker.core.IDockerProgressMessage;

public class DefaultImagePullProgressHandler implements IDockerProgressHandler {

	private final static String IMAGE_DOWNLOAD_COMPLETE = "ImageDownloadComplete.msg"; //$NON-NLS-1$
	private final static String IMAGE_DOWNLOADING_JOBNAME = "ImageDownloadingJobName.msg"; //$NON-NLS-1$
	private final static String IMAGE_DOWNLOADING_IMAGE = "ImageDownloadingImage.msg"; //$NON-NLS-1$
	private final static String IMAGE_DOWNLOADING = "ImageDownloading.msg"; //$NON-NLS-1$
	private final static String IMAGE_VERIFYING_CHECKSUM = "ImageVerifyingChecksum.msg"; //$NON-NLS-1$
	private final static String IMAGE_EXTRACTING_JOBNAME = "ImageExtractingJobName.msg"; //$NON-NLS-1$
	private final static String IMAGE_EXTRACTING_IMAGE = "ImageExtractingImage.msg"; //$NON-NLS-1$
	private final static String IMAGE_EXTRACTING = "ImageExtracting.msg"; //$NON-NLS-1$
	private final static String IMAGE_PULLING = "ImagePulling.msg"; //$NON-NLS-1$
	private final static String IMAGE_PULL_COMPLETE = "ImagePullComplete.msg"; //$NON-NLS-1$
	private final static String IMAGE_DOWNLOADING_ALREADY_EXISTS = "ImageDownloadingAlreadyExists.msg"; //$NON-NLS-1$
	private final static String IMAGE_DOWNLOADING_VERIFIED = "ImageDownloadingVerified.msg"; //$NON-NLS-1$

	private String image;
	private DockerConnection connection;

	private Map<String, ProgressJob> progressJobs = new HashMap<>();

	public DefaultImagePullProgressHandler(IDockerConnection connection, String image) {
		this.image = image;
		this.connection = (DockerConnection) connection;
	}

	@Override
	public void processMessage(IDockerProgressMessage message)
			throws DockerException {
		if (message.error() != null) {
			stopAllJobs();
			throw new DockerImagePullFailedException(image, message.error());
		}
		String id = message.id();
		if (id != null) {
			ProgressJob p = progressJobs.get(id);
			if (p == null) {
				String status = message.status();
				if (status.contains(DockerMessages.getString(IMAGE_PULLING))) {
					// do nothing
				} else if (status
						.equals(DockerMessages.getString(IMAGE_DOWNLOAD_COMPLETE))
						|| status.contains(DockerMessages
								.getString(IMAGE_DOWNLOADING_ALREADY_EXISTS))
						|| status.contains(DockerMessages
								.getString(IMAGE_DOWNLOADING_VERIFIED))
						|| status.contains(DockerMessages
								.getString(IMAGE_VERIFYING_CHECKSUM))
						|| status.equals(
								DockerMessages.getString(IMAGE_PULL_COMPLETE))) {
					// an image is fully loaded, update the image list
					connection.getImages(true);
				} else if (status
						.startsWith(DockerMessages.getString(IMAGE_DOWNLOADING))) {
					IDockerProgressDetail detail = message.progressDetail();
					if (detail == null || detail.total() == 0) {
						// We have a new extraction in progress with no
						// details of what the total should be. Track it.
						ProgressJob2 newJob = new ProgressJob2(
								DockerMessages.getFormattedString(
										IMAGE_DOWNLOADING_JOBNAME, image),
								DockerMessages.getFormattedString(
										IMAGE_DOWNLOADING_IMAGE, id));
						// job.setUser(false) will show all pull job (one per
						// image layer) in the progress
						// view but not in multiple dialog
						newJob.setUser(false);
						newJob.setPriority(Job.LONG);
						newJob.schedule();
						progressJobs.put(id, newJob);

					} else {
						// We have a new download in progress and it
						// provides us with a total so we can calculate
						// percentage done. Track it.
						ProgressJob newJob = new ProgressJob(
								DockerMessages.getFormattedString(
										IMAGE_DOWNLOADING_JOBNAME, image),
								DockerMessages.getFormattedString(
										IMAGE_DOWNLOADING_IMAGE, id));
						// job.setUser(false) will show all pull job (one per
						// image layer) in the progress
						// view but not in multiple dialog
						newJob.setUser(false);
						newJob.setPriority(Job.LONG);
						newJob.schedule();
						progressJobs.put(id, newJob);
					}
				} else if (status.startsWith(
						DockerMessages.getString(IMAGE_EXTRACTING))) {
					IDockerProgressDetail detail = message.progressDetail();
					if (detail == null || detail.total() == 0) {
						// We have a new extraction in progress with no
						// details of what the total should be. Track it.
						ProgressJob2 newJob = new ProgressJob2(
								DockerMessages.getFormattedString(
										IMAGE_EXTRACTING_JOBNAME, image),
								DockerMessages.getFormattedString(
										IMAGE_EXTRACTING_IMAGE, id));
						// job.setUser(false) will show all pull job (one per
						// image layer) in the progress
						// view but not in multiple dialog
						newJob.setUser(false);
						newJob.setPriority(Job.LONG);
						newJob.schedule();
						progressJobs.put(id, newJob);
					} else {
						// We have a new extraction in progress and it
						// provides us with a total so we can calculate
						// percentage done. Track it.
						ProgressJob newJob = new ProgressJob(
								DockerMessages.getFormattedString(
										IMAGE_EXTRACTING_JOBNAME, image),
								DockerMessages.getFormattedString(
										IMAGE_EXTRACTING_IMAGE, id));
						// job.setUser(false) will show all pull job (one per
						// image
						// layer) in the progress
						// view but not in multiple dialog
						newJob.setUser(false);
						newJob.setPriority(Job.LONG);
						newJob.schedule();
						progressJobs.put(id, newJob);
					}
				}

			} else {
				String status = message.status();
				if (status.equals(DockerMessages.getString(IMAGE_DOWNLOAD_COMPLETE))
						|| status.contains(DockerMessages
								.getString(IMAGE_DOWNLOADING_ALREADY_EXISTS))
						|| status.contains(DockerMessages
								.getString(IMAGE_DOWNLOADING_VERIFIED))
						|| status.contains(DockerMessages
								.getString(IMAGE_VERIFYING_CHECKSUM))
						|| status.contains(DockerMessages
								.getString(IMAGE_PULL_COMPLETE))) {
					// Download or pull is complete for this id so set the job
					// percentage 100 and
					// remove the job from list. Removing the job allows
					// extraction job to be
					// created after a download is complete.
					p.setPercentageDone(100);
					progressJobs.put(id, null);
					connection.getImages(true);
				} else if (status
						.startsWith(DockerMessages.getString(IMAGE_DOWNLOADING))) {
					// Update download progress
					IDockerProgressDetail detail = message.progressDetail();
					if (detail != null) {
						if (p instanceof ProgressJob2) {
							((ProgressJob2) p)
									.setStatusMessage(message.progress());
						} else if (detail.current() > 0 && detail.total() > 0) {
							long percentage = (detail.current() * 100)
									/ detail.total();
							p.setPercentageDone((int) percentage);
						}
					}
				} else if (status.startsWith(
						DockerMessages.getString(IMAGE_EXTRACTING))) {
					// Update extracting progress
					IDockerProgressDetail detail = message.progressDetail();
					if (detail != null) {
						if (p instanceof ProgressJob2) {
							((ProgressJob2) p)
									.setStatusMessage(message.progress());
						} else if (detail.current() > 0 && detail.total() > 0) {
							long percentage = (detail.current() * 100)
									/ detail.total();
							p.setPercentageDone((int) percentage);
						}
					}
				}
			}
		}
	}

	private void stopAllJobs() {
		for (ProgressJob j : progressJobs.values()) {
			if (j != null) {
				j.cancel();
			}
		}

	}

}

Back to the top