Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4aca2704472f18fb8a5350152f8f5e15cc0c0b16 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*******************************************************************************
 * Copyright (c) 2017 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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
 *
 * Contributor:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.linuxtools.docker.reddeer.ui.resources;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.linuxtools.docker.reddeer.ui.AbstractDockerExplorerItem;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.common.wait.WaitWhile;
import org.eclipse.reddeer.jface.exception.JFaceLayerException;
import org.eclipse.reddeer.swt.api.Combo;
import org.eclipse.reddeer.swt.api.Shell;
import org.eclipse.reddeer.swt.api.TreeItem;
import org.eclipse.reddeer.swt.condition.ControlIsEnabled;
import org.eclipse.reddeer.swt.condition.ShellIsAvailable;
import org.eclipse.reddeer.swt.impl.button.FinishButton;
import org.eclipse.reddeer.swt.impl.button.PushButton;
import org.eclipse.reddeer.swt.impl.combo.DefaultCombo;
import org.eclipse.reddeer.swt.impl.menu.ContextMenu;
import org.eclipse.reddeer.swt.impl.shell.DefaultShell;
import org.eclipse.reddeer.swt.impl.text.LabeledText;
import org.eclipse.reddeer.swt.impl.toolbar.DefaultToolItem;
import org.eclipse.reddeer.workbench.core.condition.JobIsRunning;

/**
 * 
 * @author jkopriva@redhat.com, mlabuda@redhat.com
 *
 */

public class DockerConnection extends AbstractDockerExplorerItem {

	// image name label in dialog
	private static final String IMAGE_NAME_LABEL_DIALOG = "Image name:";

	public DockerConnection(TreeItem treeItem) {
		super(treeItem);
	}

	public void enableConnection() {
		select();
		new DefaultToolItem("Enable Connection").click();
		new WaitWhile(new JobIsRunning());
	}

	/**
	 * Gets docker image with specified image name and latest tag.
	 * 
	 * @param imageName
	 *            docker image name
	 * @return tree item of docker image or null if does not exist
	 */
	public DockerImage getImage(String imageName) {
		return getImage(imageName, "latest");
	}

	/**
	 * Gets docker image with specified image name and specified tag.
	 * 
	 * @param imageName
	 *            docker image name
	 * @param tag
	 *            image tag
	 * @return tree item of docker image or null if does not exist
	 */
	public DockerImage getImage(String imageName, String tag) {
		try {
			List<TreeItem> images = treeViewerHandler.getTreeItems(item, "Images", imageName + ":");

			for (TreeItem item : images) {
				if (hasTag(tag, item)) {
					return new DockerImage(item);
				}
			}
		} catch (JFaceLayerException ex) {
		}
		return null;
	}

	private boolean hasTag(String tag, TreeItem item) {
		String[] styledTexts = treeViewerHandler.getStyledTexts(item);
		if (styledTexts == null || styledTexts.length == 0) {
			return false;
		}
		return StringUtils.contains(styledTexts[0].trim(), tag);
	}

	/**
	 * Refresh images.
	 */
	public void refreshImages() {
		treeViewerHandler.getTreeItem(item, "Images").select();
		new ContextMenu().getItem("Refresh").select();
		new WaitWhile(new JobIsRunning());
	}

	/**
	 * Refresh containers.
	 */
	public void refreshContainers() {
		treeViewerHandler.getTreeItem(item, "Containers").select();
		new ContextMenu().getItem("Refresh").select();
		new WaitWhile(new JobIsRunning());
	}

	/**
	 * Refresh images and containers.
	 */
	public void refresh() {
		refreshImages();
		refreshContainers();
	}

	public void pullImage(String imageName) {
		pullImage(imageName, null, null);
	}

	/**
	 * Pull docker image with specified name and tag. If tag is null, latest tag
	 * is assumed. If image exists, nothing happens.
	 * 
	 * @param imageName
	 *            name of docker image to pull
	 * @param tag
	 *            tag of docker image to null
	 */
	public void pullImage(String imageName, String imageTag) {
		pullImage(imageName, imageTag, null);
	}

	public void pullImage(String imageName, String imageTag, String dockerRegister) {
		if (getImage(imageName, imageTag) == null) {
			refreshImages();

			treeViewerHandler.getTreeItem(item, "Images").select();
			new ContextMenu().getItem("Pull...").select();

			new WaitUntil(new ShellIsAvailable("Pull Image"), TimePeriod.DEFAULT);
			Shell pullShell = new DefaultShell("Pull Image");

			// select register
			if (dockerRegister != null) {
				Combo combo = new DefaultCombo();
				combo.setSelection(dockerRegister);
			}

			new LabeledText(IMAGE_NAME_LABEL_DIALOG).setFocus();
			new LabeledText(IMAGE_NAME_LABEL_DIALOG).setText(imageTag == null ? imageName : imageName + ":" + imageTag);

			new WaitUntil(new ControlIsEnabled(new FinishButton()));
			new FinishButton(pullShell).click();

			new WaitWhile(new ShellIsAvailable(pullShell));
			new WaitWhile(new JobIsRunning(), TimePeriod.VERY_LONG);
		}
	}

	public void openImageSearchDialog(String imageName, String imageTag, String dockerRegister) {
		refreshImages();

		treeViewerHandler.getTreeItem(item, "Images").select();
		new ContextMenu().getItem("Pull...").select();

		new WaitUntil(new ShellIsAvailable("Pull Image"), TimePeriod.DEFAULT);

		// select register
		if (dockerRegister != null) {
			Combo combo = new DefaultCombo();
			combo.setSelection(dockerRegister);
		}

		new LabeledText(IMAGE_NAME_LABEL_DIALOG).setFocus();
		new LabeledText(IMAGE_NAME_LABEL_DIALOG).setText(imageTag == null ? imageName : imageName + ":" + imageTag);
		new PushButton("Search...").click();
	}

	public boolean imageIsDeployed(String imageName) {
		return deployedImagesCount(imageName) >= 1;
	}

	public int deployedImagesCount(String imageName) {
		int count = 0;
		select();
		List<String> imagesNames = getImagesNames(true);
		for (String imageNameFromList : imagesNames) {
			if (imageNameFromList.contains(imageName)) {
				count++;
			}
		}
		return count;
	}

	public boolean containerIsDeployed(String containerName) {
		return getContainer(containerName) != null;
	}

	public void removeConnection() {
		select();
		new DefaultToolItem("Remove Connection").click();
		new WaitWhile(new JobIsRunning());
	}

	public DockerContainer getContainer(String containerName) {
		try {
			List<TreeItem> containers = treeViewerHandler.getTreeItems(item, "Containers", containerName);
			return new DockerContainer(containers.get(0));
		} catch (JFaceLayerException ex) {
			// Container does not exist.
			return null;
		}
	}

	/**
	 * Returns all the names of all image, without the tag.
	 * 
	 * @return
	 */
	public List<String> getImagesNames() {
		return getImagesNames(false);
	}

	public List<String> getImagesNames(boolean withTag) {
		select();
		List<String> imagesNames = new ArrayList<String>();
		List<TreeItem> images = treeViewerHandler.getTreeItem(item, "Images").getItems();
		for (TreeItem item : images) {
			String imageName = treeViewerHandler.getNonStyledText(item);
			if (withTag) {
				String imageTag = getImageTag(item);
				imagesNames.add(imageName + imageTag);
			} else {
				imagesNames.add(imageName);
			}

		}
		return imagesNames;
	}

	private String getImageTag(TreeItem item) {
		String[] styledTexts = treeViewerHandler.getStyledTexts(item);
		if (styledTexts == null || styledTexts.length == 0) {
			return null;
		}
		return styledTexts[0];
	}

	public List<String> getContainersNames() {
		select();
		List<String> containersNames = new ArrayList<String>();
		List<TreeItem> containers = treeViewerHandler.getTreeItem(item, "Containers").getItems();
		for (TreeItem item : containers) {
			containersNames.add(treeViewerHandler.getNonStyledText(item));
		}
		return containersNames;
	}

	public String getName() {
		return treeViewerHandler.getNonStyledText(this.item);
	}

}

Back to the top