-
Notifications
You must be signed in to change notification settings - Fork 55
/
PushImageToRegistryJob.java
106 lines (92 loc) · 4.2 KB
/
PushImageToRegistryJob.java
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
/*******************************************************************************
* Copyright (c) 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.jboss.tools.openshift.internal.ui.dockerutils;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.linuxtools.docker.core.DockerException;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IRegistryAccount;
import org.eclipse.linuxtools.internal.docker.ui.views.ImagePushProgressHandler;
import org.jboss.tools.openshift.internal.common.core.job.AbstractDelegatingMonitorJob;
import org.jboss.tools.openshift.internal.ui.OpenShiftUIActivator;
import com.spotify.docker.client.DockerCertificateException;
/**
* {@link Job} to push an image from a Docker daemon into the OpenShift registry
*/
public class PushImageToRegistryJob extends AbstractDelegatingMonitorJob {
private final IDockerConnection dockerConnection;
private final IRegistryAccount registryAccount;
private final String imageName;
private final String openshiftProject;
/**
* Constructor
* @param dockerConnection the Docker connection to use
* @param registryAccount the registry account to push the image into
* @param openshiftProject the name of the OpenShift project, because the image has to be into the same namespace
* @param imageName the name of the image
*/
public PushImageToRegistryJob(final IDockerConnection dockerConnection, final IRegistryAccount registryAccount, final String openshiftProject, final String imageName) {
super("Pushing Docker image to OpenShift registry...");
this.dockerConnection = dockerConnection;
this.registryAccount = registryAccount;
this.imageName = imageName;
this.openshiftProject = openshiftProject;
}
@Override
protected IStatus doRun(final IProgressMonitor monitor) {
monitor.beginTask("Pushing image to registry", 1);
final String tmpImageName = getPushToRegistryImageName();
try {
// first, we need to tag the image with the OpenShift target
// project
this.dockerConnection.tagImage(imageName, tmpImageName);
// then we can push that image with the new name
this.dockerConnection.pushImage(tmpImageName, registryAccount,
new ImagePushProgressHandler(this.dockerConnection, tmpImageName));
}
// FIXME: needs to catch DockerCertificateException until
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=495249 is fixed
catch (DockerException | InterruptedException | DockerCertificateException e) {
return new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
"Failed to push the selected Docker image into OpenShift registry", e);
} finally {
// we need to untag the image, even if the push operation failed
try {
this.dockerConnection.removeTag(tmpImageName);
} catch (DockerException | InterruptedException e) {
return new Status(IStatus.WARNING, OpenShiftUIActivator.PLUGIN_ID,
"Pushed the selected Docker image into OpenShift registry but failed to untag it afterwards",
e);
}
monitor.done();
}
// TODO Auto-generated method stub
return Status.OK_STATUS;
}
/**
* @return the name used to push to the registry.
*/
public String getPushToRegistryImageName() {
try {
final String registryHostname = new URL(this.registryAccount.getServerAddress()).getHost();
final String tmpImageName = registryHostname + '/' + this.openshiftProject + '/' + DockerImageUtils.extractImageNameAndTag(this.imageName);
return tmpImageName;
} catch (MalformedURLException e) {
OpenShiftUIActivator.getDefault().getLog().log(new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
"Failed to push the selected Docker image into OpenShift registry", e));
return null;
}
}
}