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

Add support to configure Initial Image from global configuration #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -7,6 +7,7 @@
import hudson.model.Computer;
import hudson.model.TaskListener;
import hudson.util.ArgumentListBuilder;
import jenkins.model.Jenkins;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.output.TeeOutputStream;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -255,11 +256,16 @@ private String getDocker0Ip(Launcher launcher, String image) throws IOException,
// On some distributions, docker doesn't start docker0 bridge until a container do require it
// So let's run the container once, running /bin/true so it terminates immediately

DockerBuildWrapper.DescriptorImpl descriptor = (DockerBuildWrapper.DescriptorImpl)
Jenkins.getInstance().getDescriptor(DockerBuildWrapper.class);

final String initImage = descriptor.getInitImage();

ArgumentListBuilder args = dockerCommand()
.add("run", "--rm")
.add("--entrypoint")
.add("/bin/true")
.add("alpine:3.6");
.add(initImage);

int status = launcher.launch()
.envs(getEnvVars())
Expand Down Expand Up @@ -290,7 +296,7 @@ private String getDocker0Ip(Launcher launcher, String image) throws IOException,
.add("run", "--tty", "--rm")
.add("--entrypoint")
.add("/sbin/ip")
.add("alpine:3.6")
.add(initImage)
.add("route");

ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
Expand All @@ -18,16 +19,20 @@
import hudson.remoting.Callable;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryToken;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -259,6 +264,36 @@ private String whoAmI(Launcher launcher) throws IOException, InterruptedExceptio
@Extension
public static class DescriptorImpl extends BuildWrapperDescriptor {

private String initImage;

public DescriptorImpl(){
super(DockerBuildWrapper.class);
load();
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
req.bindParameters(this);
String paramValue = json.getString("initImage");
this.initImage = !paramValue.trim().isEmpty() ? paramValue : "alpine:3.6";
save();
return super.configure(req,json);
}

public String getInitImage() {
return initImage;
}

public void setInitImage(String initImage) {
this.initImage = initImage;
}

public FormValidation doCheckInitImage(@QueryParameter String initImage) throws IOException, ServletException {
if (Util.fixEmptyAndTrim(initImage) == null)
return FormValidation.error("Please set a Init Image name. Default: alpine:3.6");
return FormValidation.ok();
}

@Override
public String getDisplayName() {
return "Build inside a Docker container";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License

Copyright 2015 CloudBees Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:d="/lib/docker/commons" xmlns:c="/lib/credentials">
<f:section title="Build In Docker Configuration">
<f:entry field="initImage" title="Init image">
<f:textbox default="alpine:3.6"/>
</f:entry>
</f:section>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
This image is used to create two temporary containers to start docker0 bridge and to discover gateway IP from the container.
<p> DEFAULT: `alpine:3.6` (since it has a size of 2MB).
</div>