Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4491501dd610567af22b83d22f54993bfc4e1daf (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
273
274
/*******************************************************************************
 * Copyright (c) 2017, 2019 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * 		Red Hat Inc. - modified for use with Docker Container launching
 *******************************************************************************/
package org.eclipse.cdt.docker.launcher;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.build.ICBuildConfigurationManager;
import org.eclipse.cdt.core.build.ICBuildConfigurationManager2;
import org.eclipse.cdt.core.build.IToolChain;
import org.eclipse.cdt.core.build.IToolChainManager;
import org.eclipse.cdt.core.build.IToolChainProvider;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.internal.docker.launcher.ui.launchbar.ContainerGCCToolChain;
import org.eclipse.cdt.internal.docker.launcher.ui.launchbar.ContainerGCCToolChainProvider;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.launchbar.core.ILaunchBarManager;
import org.eclipse.launchbar.core.target.ILaunchTarget;
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
import org.eclipse.launchbar.core.target.ILaunchTargetManager2;
import org.eclipse.launchbar.core.target.ILaunchTargetProvider;
import org.eclipse.launchbar.core.target.ILaunchTargetWorkingCopy;
import org.eclipse.launchbar.core.target.TargetStatus;
import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.docker.core.EnumDockerConnectionState;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerConnectionManagerListener;
import org.eclipse.linuxtools.docker.core.IDockerImage;

/**
 * @since 1.2
 * @author jjohnstn
 *
 */
public class ContainerTargetTypeProvider implements ILaunchTargetProvider, IDockerConnectionManagerListener {

	public static final String TYPE_ID = "org.eclipse.cdt.docker.launcher.launchTargetType.container"; //$NON-NLS-1$
	public static final String CONTAINER_LINUX = "linux-container"; //$NON-NLS-1$

	private ILaunchTargetManager targetManager;

	@Override
	public synchronized void init(ILaunchTargetManager targetManager) {
		this.targetManager = targetManager;
		ILaunchBarManager launchbarManager = CDebugCorePlugin.getService(ILaunchBarManager.class);
		ILaunchTarget defaultTarget = null;
		try {
			defaultTarget = launchbarManager.getActiveLaunchTarget();
		} catch (CoreException e) {
			// ignore
		}
		IDockerConnection[] connections = DockerConnectionManager.getInstance().getConnections();
		Map<String, IDockerConnection> establishedConnectionMap = new HashMap<>();
		Set<String> imageNames = new HashSet<>();
		for (IDockerConnection connection : connections) {
			// Get Images before checking state as the state may be
			// unknown until a request is made
			List<IDockerImage> images = connection.getImages();
			if (connection.getState() == EnumDockerConnectionState.ESTABLISHED) {
				establishedConnectionMap.put(connection.getUri(), connection);
			}
			for (IDockerImage image : images) {
				if (!image.isDangling() && !image.isIntermediateImage()) {
					String imageName = "[" //$NON-NLS-1$
							+ image.repoTags().get(0).replace(':', '_')
							// .replace('/', '_')
							+ "]"; //$NON-NLS-1$
					if (imageNames.contains(imageName)) {
						imageName += "[" + connection.getName() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
					}
					imageNames.add(imageName);
					ILaunchTarget target = targetManager.getLaunchTarget(TYPE_ID, imageName);
					if (target == null) {
						target = targetManager.addLaunchTarget(TYPE_ID, imageName);
					}
					ILaunchTargetWorkingCopy wc = target.getWorkingCopy();
					wc.setAttribute(ILaunchTarget.ATTR_OS, CONTAINER_LINUX);
					wc.setAttribute(ILaunchTarget.ATTR_ARCH, Platform.getOSArch());
					wc.setAttribute(IContainerLaunchTarget.ATTR_CONNECTION_URI, connection.getUri());
					wc.setAttribute(IContainerLaunchTarget.ATTR_IMAGE_ID, image.repoTags().get(0));

					wc.save();
				}
			}
		}

		// remove any launch targets for closed/disabled connections
		ILaunchTarget[] targets = targetManager.getLaunchTargetsOfType(TYPE_ID);
		for (ILaunchTarget target : targets) {
			try {
				String uri = target.getAttribute(IContainerLaunchTarget.ATTR_CONNECTION_URI, ""); //$NON-NLS-1$
				if (!establishedConnectionMap.containsKey(uri)) {
					targetManager.removeLaunchTarget(target);
				}
			} catch (IllegalStateException e) {
				// ignore
			}
		}

		// add a Docker Connection listener to handle enablement/disablement of
		// Connections
		DockerConnectionManager.getInstance().addConnectionManagerListener(this);

		// re-check configs in case an enabled Connection has made old configs
		// valid again do this in a separate job to prevent a possible
		// deadlock trying to get the lock on the CBuildConfigurationManager
		// "configs" map (Bug 540085)
		Job checkConfigs = new Job("Check configs") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				// call the recheckConfigs method in case any disabled targets
				// are now
				// ok
				ICBuildConfigurationManager mgr = CCorePlugin.getService(ICBuildConfigurationManager.class);
				ICBuildConfigurationManager2 cbuildmanager = (ICBuildConfigurationManager2) mgr;
				cbuildmanager.recheckConfigs();
				return Status.OK_STATUS;
			}
		};
		checkConfigs.setUser(true);
		checkConfigs.schedule();

		try {
			if (defaultTarget != null)
				launchbarManager.setActiveLaunchTarget(defaultTarget);
		} catch (CoreException e) {
			DockerLaunchUIPlugin.log(e);
		}

	}

	@Override
	public TargetStatus getStatus(ILaunchTarget target) {
		// Always OK
		return TargetStatus.OK_STATUS;
	}

	@Override
	public synchronized void changeEvent(final IDockerConnection connection, final int type) {
		Job checkConfigs = new Job("Check configs") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {

				ICBuildConfigurationManager mgr = CCorePlugin.getService(ICBuildConfigurationManager.class);
				ICBuildConfigurationManager2 manager = (ICBuildConfigurationManager2) mgr;

				if (type == IDockerConnectionManagerListener.ADD_EVENT
						|| type == IDockerConnectionManagerListener.ENABLE_EVENT) {
					ILaunchBarManager launchbarManager = CDebugCorePlugin.getService(ILaunchBarManager.class);
					ILaunchTarget defaultTarget = null;
					try {
						defaultTarget = launchbarManager.getActiveLaunchTarget();
					} catch (CoreException e) {
						// ignore
					}

					List<IDockerImage> images = connection.getImages();
					IToolChainProvider provider = new ContainerGCCToolChainProvider();
					for (IDockerImage image : images) {
						if (!image.isDangling() && !image.isIntermediateImage()) {

							String imageName = "[" //$NON-NLS-1$
									+ image.repoTags().get(0).replace(':', '_')
							// .replace('/', '_')
									+ "]"; //$NON-NLS-1$
							String imageName2 = imageName + "[" //$NON-NLS-1$
									+ connection.getName() + "]"; //$NON-NLS-1$
							ILaunchTarget target = targetManager.getLaunchTarget(TYPE_ID, imageName2);
							if (target != null) {
								continue;
							}
							target = targetManager.getLaunchTarget(TYPE_ID, imageName);
							if (target != null) {
								if (target.getAttribute(IContainerLaunchTarget.ATTR_CONNECTION_URI, "") //$NON-NLS-1$
										.equals(connection.getUri())) {
									continue;
								}
								imageName = imageName2;
							}
							target = ((ILaunchTargetManager2) targetManager).addLaunchTargetNoNotify(TYPE_ID,
									imageName);
							ILaunchTargetWorkingCopy wc = target.getWorkingCopy();
							wc.setAttribute(ILaunchTarget.ATTR_OS, CONTAINER_LINUX);
							wc.setAttribute(ILaunchTarget.ATTR_ARCH, Platform.getOSArch());
							wc.setAttribute(IContainerLaunchTarget.ATTR_CONNECTION_URI, connection.getUri());
							wc.setAttribute(IContainerLaunchTarget.ATTR_IMAGE_ID, image.repoTags().get(0));

							wc.save();

							Map<String, String> properties = new HashMap<>();

							properties.put(ILaunchTarget.ATTR_OS, ContainerTargetTypeProvider.CONTAINER_LINUX);
							properties.put(ILaunchTarget.ATTR_ARCH, Platform.getOSArch());
							properties.put(IContainerLaunchTarget.ATTR_CONNECTION_URI, connection.getUri());
							properties.put(IContainerLaunchTarget.ATTR_IMAGE_ID, image.repoTags().get(0));
							// following can be used for naming build
							// configurations
							properties.put(ContainerGCCToolChainProvider.CONTAINER_LINUX_CONFIG_ID,
									image.repoTags().get(0).replace(':', '_'));
							// .replace('/', '_'));

							IToolChainManager toolChainManager = MakeCorePlugin.getService(IToolChainManager.class);

							Collection<IToolChain> toolChains;
							try {
								toolChains = toolChainManager.getToolChainsMatching(properties);
								if (toolChains.isEmpty()) {
									ContainerGCCToolChain toolChain = new ContainerGCCToolChain(
											"gcc-img-" + image.id().substring(0, //$NON-NLS-1$
													19),
											provider, properties, null);
									toolChainManager.addToolChain(toolChain);
								}
							} catch (CoreException e) {
								DockerLaunchUIPlugin.log(e);
							}

						}
					}

					// reset the default target back again
					if (defaultTarget != null) {
						try {
							launchbarManager.setActiveLaunchTarget(defaultTarget);
						} catch (CoreException e) {
							DockerLaunchUIPlugin.log(e);
						}

					}

					// Re-evaluate config list in case a build config was marked
					// invalid and is now enabled
					manager.recheckConfigs();
				} else if (type == IDockerConnectionManagerListener.REMOVE_EVENT
						|| type == IDockerConnectionManagerListener.DISABLE_EVENT) {
					String connectionURI = connection.getUri();
					ILaunchTarget[] targets = targetManager.getLaunchTargetsOfType(TYPE_ID);
					for (ILaunchTarget target : targets) {
						String uri = target.getAttribute(IContainerLaunchTarget.ATTR_CONNECTION_URI, ""); //$NON-NLS-1$
						if (connectionURI.equals(uri)) {
							targetManager.removeLaunchTarget(target);
						}
					}
				}
				return Status.OK_STATUS;
			}
		};
		checkConfigs.setUser(true);
		checkConfigs.schedule();
	}

}

Back to the top