Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code-tidying - no functional changes #793

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public class DockerCloud extends Cloud {
* Default value for {@link #getEffectiveErrorDurationInMilliseconds()}
* used when {@link #errorDuration} is null.
*/
@Restricted(NoExternalUse.class)
private static final int ERROR_DURATION_DEFAULT_SECONDS = 300; // 5min

@CheckForNull
Expand Down Expand Up @@ -677,7 +676,7 @@ public static DockerCloud getCloudByName(String name) {
return (DockerCloud) Jenkins.getInstance().getCloud(name);
}

public Object readResolve() {
protected Object readResolve() {
//Xstream is not calling readResolve() for nested Describable's
for (DockerTemplate template : getTemplates()) {
template.readResolve();
Expand Down Expand Up @@ -871,14 +870,12 @@ public static AuthConfig getAuthConfig(DockerRegistryEndpoint registry, ItemGrou
// we can't use DockerRegistryEndpoint#getToken as this one do check domainRequirement based on registry URL
// but in some context (typically, passing registry auth for `docker build`) we just can't guess this one.

Credentials c = firstOrNull(CredentialsProvider.lookupCredentials(
final Credentials c = firstOrNull(CredentialsProvider.lookupCredentials(
IdCredentials.class, context, ACL.SYSTEM, Collections.EMPTY_LIST),
withId(registry.getCredentialsId()));

if (c == null) {
throw new IllegalArgumentException("Invalid Credential ID " + registry.getCredentialsId());
}

final DockerRegistryToken t = AuthenticationTokens.convert(DockerRegistryToken.class, c);
final String token = t.getToken();
// What docker-commons claim to be a "token" is actually configuration storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ private void cleanUpSuperfluousContainers(DockerClient client, Map<String, Node>

private boolean isStillTooYoung(Long created, Instant snapshotInstant) {
final Instant createdInstant = Instant.ofEpochSecond(created.longValue());

final Duration containerLifetime = Duration.between(createdInstant, snapshotInstant);

/*
* We allow containers to have a grace duration, during which is permitted for them
* to start/run without having a node attached.
Expand All @@ -348,9 +346,7 @@ private boolean isStillTooYoung(Long created, Instant snapshotInstant) {
* is allowed to kill any container.
*/
final Duration graceDurationForContainers = Duration.ofSeconds(JenkinsUtils.getSystemPropertyLong(DockerContainerWatchdog.class.getName()+".initialGraceDurationForContainersInSeconds", 60L));

final Duration untilMayBeCleanedUp = containerLifetime.minus(graceDurationForContainers);

return untilMayBeCleanedUp.isNegative();
}

Expand Down Expand Up @@ -607,7 +603,7 @@ private void addProcessingTimeout() {
private void addOverallRuntime(long runtime) {
overallRuntime += runtime;
}

private void addRetrieveContainerRuntime(long runtime) {
retrieveContainersRuntime += runtime;
retrieveContainersCalls++;
Expand All @@ -633,7 +629,7 @@ private String getContainerRemovalAverageDurationGracefully() {
}
return new Long(containersRemovedGracefullyRuntimeSum / containersRemovedGracefully).toString();
}

private String getAverageRetrieveContainerRuntime() {
if (retrieveContainersCalls == 0) {
return "0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@

import static com.nirima.jenkins.plugins.docker.utils.LogUtils.printResponseItemToListener;


public class DockerJobProperty extends OptionalJobProperty<AbstractProject<?, ?>> {

private static final Logger LOGGER = LoggerFactory.getLogger(DockerJobProperty.class.getName());


public final String additionalTag;

private boolean pushOnSuccess;
Expand Down Expand Up @@ -72,7 +69,7 @@ public boolean isPushOnSuccess() {
public boolean isCleanImages() {
return cleanImages;
}

public DockerRegistryEndpoint getRegistry() {
return registry;
}
Expand All @@ -96,7 +93,6 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return true;
}
DockerTransientNode dockerNode = (DockerTransientNode) node;

final String containerId = dockerNode.getContainerId();
final DockerAPI dockerAPI = dockerNode.getDockerAPI();
try(final DockerClient client = dockerAPI.getClient()) {
Expand All @@ -106,49 +102,37 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

private boolean perform(AbstractBuild<?, ?> build, BuildListener listener, String containerId, DockerAPI dockerAPI, DockerClient client) throws IOException {
final String dockerHost = dockerAPI.getDockerHost().getUri();

// Commit
String tag_image = client.commitCmd(containerId)
.withRepository(build.getParent().getDisplayName())
.withTag(build.getDisplayName().replace("#", "b")) // allowed only ([a-zA-Z_][a-zA-Z0-9_]*)
.withAuthor("Jenkins")
.withMessage(build.getFullDisplayName())
.exec();

// Tag it with the jenkins name
addJenkinsAction(build, dockerHost, containerId, tag_image);

// Should we add additional tags?
try {
String tagToken = getAdditionalTag(build, listener);

if (!Strings.isNullOrEmpty(tagToken)) {


final NameParser.ReposTag reposTag = NameParser.parseRepositoryTag(tagToken);
final String commitTag = StringUtils.isEmpty(reposTag.tag) ? "latest" : reposTag.tag;

client.tagImageCmd(tag_image, reposTag.repos, commitTag).withForce().exec();

addJenkinsAction(build, dockerHost, containerId, tagToken);

if (pushOnSuccess) {
Identifier identifier = Identifier.fromCompoundString(tagToken);

PushImageResultCallback resultCallback = new PushImageResultCallback() {
@Override
public void onNext(PushResponseItem item) {
printResponseItemToListener(listener, item);
super.onNext(item);
}
};
try {

PushImageCmd cmd = client.pushImageCmd(identifier);
DockerCloud.setRegistryAuthentication(cmd, registry, Jenkins.getInstance());
cmd.exec(resultCallback).awaitSuccess();

} catch(DockerException ex) {

LOGGER.error("Exception pushing docker image. Check that the destination registry is building.", ex);
throw ex;
}
Expand All @@ -157,14 +141,11 @@ public void onNext(PushResponseItem item) {
} catch (Exception ex) {
LOGGER.error("Could not add additional tags", ex);
}

if (cleanImages) {

client.removeImageCmd(tag_image)
.withForce(true)
.exec();
}

return true;
}

Expand All @@ -191,6 +172,7 @@ private void addJenkinsAction(AbstractBuild build, String dockerHost, String con

@Extension
public static final class DescriptorImpl extends OptionalJobPropertyDescriptor {
@Override
public String getDisplayName() {
return "Commit agent's Docker container";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
import java.util.Collection;
import java.util.List;



/**
* Manage the docker images.
* Docker page under "Manage Jenkins" page.
* Manage the docker images. Docker page under "Manage Jenkins" page.
*/
@Extension
public class DockerManagement extends ManagementLink implements StaplerProxy, Describable<DockerManagement>, Saveable {
Expand Down Expand Up @@ -51,7 +48,6 @@ public static DockerManagement get() {
return ManagementLink.all().get(DockerManagement.class);
}


@Override
public DescriptorImpl getDescriptor() {
return Jenkins.getInstance().getDescriptorByType(DescriptorImpl.class);
Expand All @@ -74,58 +70,56 @@ public String getDisplayName() {
}
}

public DockerManagementServer getServer(String serverName) {
return new DockerManagementServer(serverName);
}

@Override
public Object getTarget() {
Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
return this;
}

public Collection<String> getServerNames() {
return Collections2.transform(JenkinsUtils.getServers(), new Function<DockerCloud, String>() {
@Override
public String apply(@Nullable DockerCloud input) {
return input.getDisplayName();
}
});
}
public DockerManagementServer getServer(String serverName) {
return new DockerManagementServer(serverName);
}

public static class ServerDetail {
final DockerCloud cloud;
@Override
public Object getTarget() {
Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
return this;
}

public ServerDetail(DockerCloud cloud) {
this.cloud = cloud;
public Collection<String> getServerNames() {
return Collections2.transform(JenkinsUtils.getServers(), new Function<DockerCloud, String>() {
@Override
public String apply(DockerCloud input) {
return input.getDisplayName();
}
});
}

public String getName() {
return cloud.getDisplayName();
}
public static class ServerDetail {
final DockerCloud cloud;

public String getActiveHosts() {
try {
final DockerAPI dockerApi = cloud.getDockerApi();
final List<?> containers;
try(final DockerClient client = dockerApi.getClient()) {
containers = client.listContainersCmd().exec();
}
return "(" + containers.size() + ")";
} catch(Exception ex) {
return "Error";
}
}
public ServerDetail(DockerCloud cloud) {
this.cloud = cloud;
}

public String getName() {
return cloud.getDisplayName();
}

public Collection<ServerDetail> getServers() {
return Collections2.transform(JenkinsUtils.getServers(), new Function<DockerCloud, ServerDetail>() {
@Override
public ServerDetail apply(@Nullable DockerCloud input) {
return new ServerDetail(input);
public String getActiveHosts() {
try {
final DockerAPI dockerApi = cloud.getDockerApi();
final List<?> containers;
try (final DockerClient client = dockerApi.getClient()) {
containers = client.listContainersCmd().exec();
}
});
return "(" + containers.size() + ")";
} catch (Exception ex) {
return "Error";
}
}
}

public Collection<ServerDetail> getServers() {
return Collections2.transform(JenkinsUtils.getServers(), new Function<DockerCloud, ServerDetail>() {
@Override
public ServerDetail apply(@Nullable DockerCloud input) {
return new ServerDetail(input);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public String getJsUrl(String jsName) {
return Consts.PLUGIN_JS_URL + jsName;
}

@SuppressWarnings("unused")
@RequirePOST
public void doControlSubmit(@QueryParameter("stopId") String stopId, StaplerRequest req, StaplerResponse rsp) throws IOException {
Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public DockerRegistry getRegistryByName(String registryName) {
return null;
}



public void setRegistryList(List<DockerRegistry> registryList) {
this.registryList = registryList;
save();
Expand All @@ -54,15 +52,13 @@ public List<DockerRegistry> getRegistryList() {
return registryList;
}



// --- obsolete code goes here. kept for backward compatibility

public final boolean getPullFix() {
return false;
}

@SuppressWarnings("unused")
public final void setPullFix(boolean pullFix) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,19 @@ public Descriptor<DockerRegistry> getDescriptor() {
return (DescriptorImpl) Jenkins.getInstance().getDescriptor(getClass());
}


private Object readResolve() {
// TODO migrate to docker-commons' DockerRegistryEndpoint
// inspect all DockerTemplates
return this;
}


@Deprecated
public static final class DescriptorImpl extends Descriptor<DockerRegistry> {

@Override
public String getDisplayName() {
return "Docker Registry";
return "Docker Registry [DEPRECATED]";
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ public class DockerSlave extends Slave {

private transient String containerId;

@SuppressWarnings("unused")
private transient String cloudId;

private transient DockerAPI dockerAPI;
@SuppressWarnings("unused")
private transient DockerAPI dockerAPI;

private DockerSlave(@Nonnull String name, String remoteFS, ComputerLauncher launcher) throws Descriptor.FormException, IOException {
super(name, remoteFS, launcher);
}

@Override
protected Object readResolve() {
try {
return new DockerTransientNode(containerId, containerId, dockerTemplate.remoteFs, getLauncher());
Expand All @@ -36,8 +39,6 @@ protected Object readResolve() {
}
}



/* FIXME better move this to a io.jenkins.docker.DockerTransientNode.Callback
private void slaveShutdown(final TaskListener listener) throws DockerException, IOException {

Expand Down Expand Up @@ -150,5 +151,4 @@ private void addJenkinsAction(String tag_image) throws IOException {
theRun.save();
}
*/

}
Loading