Package com.nirima.docker.client

Examples of com.nirima.docker.client.DockerClient


    }

    @Override
    public void execute(AbstractBuild<?, ?> build) throws DockerException {
        LOGGER.info("Stopping container " + containerId);
        DockerClient client = getClient(build);
        client.container(containerId).stop();
        getLaunchAction(build).stopped(client, containerId);
        if( remove )
            client.container(containerId).remove();
    }
View Full Code Here


        DockerTemplate template = getCloud(build).getTemplate(templateId);

        String containerId = template.provisionNew().getId();

        LOGGER.info("Starting container " + containerId);
        DockerClient client = getClient(build);
        client.container(containerId).start();
        getLaunchAction(build).started(client, containerId);
    }
View Full Code Here

    @Override
    public void execute(AbstractBuild<?, ?> build) throws DockerException {

        LOGGER.info("Starting container " + containerId);
        DockerClient client = getClient(build);
        client.container(containerId).start();
        getLaunchAction(build).started(client, containerId);

    }
View Full Code Here

        FilePath fpChild = new FilePath(build.getWorkspace(), dockerFileDirectory);

        final String tagToUse = getTag(build, launcher, listener);
        final String url = getUrl(build);
        // Marshal the builder across the wire.
        DockerClient client = getDockerClient(build);
        final DockerClient.Builder builder = DockerClient.builder().fromClient(client);

        BuildCommandResponse response = fpChild.act(new FilePath.FileCallable<BuildCommandResponse>() {
            public BuildCommandResponse invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
                try {
                    listener.getLogger().println("Docker Build : build with tag " + tagToUse + " at path " + f.getAbsolutePath());
                    DockerClient client = builder
                            .readTimeout(3600000).build();

                    File dockerFile;

                    // Be lenient and allow the user to just specify the path.
                    if( f.isFile() )
                        dockerFile = f;
                    else
                        dockerFile = new File(f, "Dockerfile");

                    return client.createBuildCommand()
                            .dockerFile(dockerFile)
                            .tag(tagToUse)
                            .execute();

                } catch (DockerException e) {
                    throw Throwables.propagate(e);
                }

            }
        });


        listener.getLogger().println("Docker Build Response : " + response);

        Optional<String> id = response.imageId();
        if( !id.isPresent() )
           return false;

        build.addAction( new DockerBuildImageAction(url, id.get(), tagToUse, cleanupWithJenkinsJobDelete, pushOnSuccess) );
        build.save();


        if( pushOnSuccess ) {

            listener.getLogger().println("Pushing " + tagToUse);
            if( !tagToUse.toLowerCase().equals(tagToUse) ) {
                listener.getLogger().println("ERROR: Docker will refuse to push tag name " + tagToUse + " because it uses upper case.");
            }

            Identifier identifier = Identifier.fromCompoundString(tagToUse);

            String repositoryName = identifier.repository.name;

            PushCommandResponse pushResponse = client.createPushCommand()
                    .name(repositoryName)
                    .execute();

            listener.getLogger().println("Docker Push Response : " + pushResponse);
        }

        if (cleanImages) {

            // For some reason, docker delete doesn't delete all tagged
            // versions, despite force = true.
            // So, do it multiple times (protect against infinite looping).
            listener.getLogger().println("Cleaning local images");

            int delete = 100;
            while (delete != 0) {
                int count = client.image(id.get()).removeCommand()
                        .force(true)
                        .execute().size();
                if (count == 0)
                    delete = 0;
                else
View Full Code Here

        this.bindAllPorts = bindAllPorts;
    }

    @Override
    public void execute(AbstractBuild<?, ?> build) throws DockerException, IOException {
        DockerClient client = getClient(build);

        // Expand some token macros

        String xImage    = expand(build, image);
        String xCommand  = expand(build, dockerCommand);
        String xHostname = expand(build, hostname);


        LOGGER.info("Pulling image " + xImage);

        InputStream result = client.createPullCommand()
                .image( Identifier.fromCompoundString(xImage))
                .execute();

        String strResult = IOUtils.toString(result);
        LOGGER.info("Pull result = " + strResult);
View Full Code Here

        return new DockerComputer(this);
    }

    public boolean containerExistsInCloud() {
        try {
            DockerClient client = getClient();
            client.container(containerId).inspect();
            return true;
        } catch(Exception ex) {
            return false;
        }
    }
View Full Code Here

        try {
            toComputer().disconnect(null);

            try {
                DockerClient client = getClient();
                client.container(containerId).stop();
            } catch(Exception ex) {
                LOGGER.log(Level.SEVERE, "Failed to stop instance " + containerId + " for slave " + name + " due to exception", ex);
            }

            // If the run was OK, then do any tagging here
            if( theRun != null ) {
                try {
                    slaveShutdown(listener);
                } catch (Exception e) {
                    LOGGER.log(Level.SEVERE, "Failure to slaveShutdown instance " + containerId+ " for slave " + name , e);
                }
            }

            try {
                DockerClient client = getClient();
                client.container(containerId).remove();
            } catch(Exception ex) {
                LOGGER.log(Level.SEVERE, "Failed to remove instance " + containerId + " for slave " + name + " due to exception",ex);
            }

        } catch (Exception e) {
View Full Code Here

        if(!getJobProperty().tagOnCompletion) {
            addJenkinsAction(null);
            return;
        }

        DockerClient client = getClient();


         // Commit
        String tag_image = client.container(containerId).createCommitCommand()
                    .repo(theRun.getParent().getDisplayName())
                    .tag(theRun.getDisplayName())
                    .author("Jenkins")
                    .execute();

        // Tag it with the jenkins name
        addJenkinsAction(tag_image);

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

            if( !Strings.isNullOrEmpty(tagToken) ) {
                client.image(tag_image).tag(tagToken, false);
                addJenkinsAction(tagToken);

                if( getJobProperty().pushOnSuccess ) {
                    client.image(tagToken).push(null);
                }
            }
        }
        catch(Exception ex) {
            LOGGER.log(Level.SEVERE, "Could not add additional tags");
        }

        if( getJobProperty().cleanImages ) {

            // For some reason, docker delete doesn't delete all tagged
            // versions, despite force = true.
            // So, do it multiple times (protect against infinite looping).

            int delete = 100;
            while(delete != 0 ) {
                int count = client.image(tag_image).removeCommand()
                                   .force(true)
                                   .execute().size();
                if( count == 0 )
                    delete = 0;
                else
View Full Code Here

     * @param ami If AMI is left null, then all instances are counted.
     * <p>
     * This includes those instances that may be started outside Hudson.
     */
    public int countCurrentDockerSlaves(String ami) throws Exception {
        final DockerClient dockerClient = connect();

        List<Container> containers = dockerClient.containers().finder().allContainers(false).list();

        if (ami == null)
            return containers.size();

        List<Image> images = dockerClient.images().finder().allImages(true).filter(ami).list();
        LOGGER.log(Level.INFO, "Images found: " + images);

        if (images.size() == 0) {
            LOGGER.log(Level.INFO, "Pulling image " + ami + " since one was not found.  This may take awhile...");
            Identifier amiId = Identifier.fromCompoundString(ami);
            InputStream imageStream = dockerClient.createPullCommand().image(amiId).execute();
            int streamValue = 0;
            while (streamValue != -1) {
                streamValue = imageStream.read();
            }
            imageStream.close();
            LOGGER.log(Level.INFO, "Finished pulling image " + ami);
        }

        final ImageInspectResponse ir = dockerClient.image(ami).inspect();

        Collection<Container> matching = Collections2.filter(containers, new Predicate<Container>() {
            public boolean apply(@Nullable Container container) {
                ContainerInspectResponse cis = dockerClient.container(container.getId()).inspect();
                return (cis.getImage().equalsIgnoreCase(ir.getId()));
            }
        });
        return matching.size();
    }
View Full Code Here

        public FormValidation doTestConnection(
                @QueryParameter URL serverUrl
                ) throws IOException, ServletException, DockerException {

            DockerClient dc = DockerClient.builder().withUrl(serverUrl.toString()).build();

            Version version = dc.system().version();

            if( version.getVersionComponents()[0] < 1 )
                return FormValidation.error("Docker host is " + version.getVersion() + " which is not supported.");

            return FormValidation.ok("Version = " + version.getVersion());
View Full Code Here

TOP

Related Classes of com.nirima.docker.client.DockerClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.