| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StartDocker |
|
| 1.5;1.5 |
| 1 | 0 | /** |
| 2 | * Copyright (c) 2014-2015, Thindeck.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the thindeck.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.thindeck.agents; | |
| 31 | ||
| 32 | import com.jcabi.aspects.Immutable; | |
| 33 | import com.jcabi.immutable.ArrayMap; | |
| 34 | import com.jcabi.log.Logger; | |
| 35 | import com.jcabi.xml.XML; | |
| 36 | import com.thindeck.api.Agent; | |
| 37 | import java.io.IOException; | |
| 38 | import java.security.SecureRandom; | |
| 39 | import java.util.Collection; | |
| 40 | import java.util.Random; | |
| 41 | import org.xembly.Directive; | |
| 42 | import org.xembly.Directives; | |
| 43 | ||
| 44 | /** | |
| 45 | * Start containers. | |
| 46 | * | |
| 47 | * @author Yegor Bugayenko (yegor@teamed.io) | |
| 48 | * @version $Id$ | |
| 49 | * @since 0.1 | |
| 50 | * @checkstyle MultipleStringLiteralsCheck (500 lines) | |
| 51 | */ | |
| 52 | @Immutable | |
| 53 | public final class StartDocker implements Agent { | |
| 54 | ||
| 55 | /** | |
| 56 | * Random. | |
| 57 | */ | |
| 58 | 0 | private static final Random RND = new SecureRandom(); |
| 59 | ||
| 60 | /** | |
| 61 | * Script to use. | |
| 62 | */ | |
| 63 | private final transient Script script; | |
| 64 | ||
| 65 | /** | |
| 66 | * Ctor. | |
| 67 | * @throws IOException If fails | |
| 68 | */ | |
| 69 | public StartDocker() throws IOException { | |
| 70 | 0 | this( |
| 71 | new Script.Default( | |
| 72 | StartDocker.class.getResource("start-docker.sh") | |
| 73 | ) | |
| 74 | ); | |
| 75 | 0 | } |
| 76 | ||
| 77 | /** | |
| 78 | * Ctor. | |
| 79 | * @param spt Script. | |
| 80 | */ | |
| 81 | 0 | public StartDocker(final Script spt) { |
| 82 | 0 | this.script = spt; |
| 83 | 0 | } |
| 84 | ||
| 85 | @Override | |
| 86 | public Iterable<Directive> exec(final XML deck) throws IOException { | |
| 87 | 0 | final Collection<XML> images = deck.nodes( |
| 88 | "/deck/images/image[not(@waste)]" | |
| 89 | ); | |
| 90 | 0 | final Directives dirs = new Directives() |
| 91 | .xpath("/deck").addIf("containers"); | |
| 92 | 0 | for (final XML image : images) { |
| 93 | 0 | final String img = image.xpath("name/text()").get(0); |
| 94 | 0 | final Collection<String> tanks = deck.xpath( |
| 95 | String.format( | |
| 96 | // @checkstyle LineLength (1 line) | |
| 97 | "/deck/tanks/tank[not(host=/deck/containers/container[not(@waste) and image='%s']/host)]/host/text()", | |
| 98 | img | |
| 99 | ) | |
| 100 | ); | |
| 101 | 0 | for (final String tank : tanks) { |
| 102 | 0 | Logger.info( |
| 103 | this, | |
| 104 | "There are no Docker containers at %s for image %s yet", | |
| 105 | tank, img | |
| 106 | ); | |
| 107 | 0 | final String cid = this.start(img, tank); |
| 108 | 0 | dirs.xpath("/deck/containers").add("container") |
| 109 | .add("name").set(cid).up() | |
| 110 | .add("image").set(img).up() | |
| 111 | .add("host").set(tank).up() | |
| 112 | .attr("state", "unknown") | |
| 113 | .attr("type", image.xpath("@type").get(0)); | |
| 114 | 0 | } |
| 115 | 0 | } |
| 116 | 0 | return dirs; |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Run docker in this tank. | |
| 121 | * @param image Docker image name | |
| 122 | * @param host Host name of the tank | |
| 123 | * @return Docker container name | |
| 124 | * @throws IOException If fails | |
| 125 | */ | |
| 126 | private String start(final String image, final String host) | |
| 127 | throws IOException { | |
| 128 | 0 | final String name = String.format("%08x", StartDocker.RND.nextInt()); |
| 129 | 0 | final long start = System.currentTimeMillis(); |
| 130 | 0 | this.script.exec( |
| 131 | host, | |
| 132 | new ArrayMap<String, String>() | |
| 133 | .with("image", image) | |
| 134 | .with("container", name) | |
| 135 | ); | |
| 136 | 0 | Logger.info( |
| 137 | this, "Docker container %s started at %s in %[ms]s", | |
| 138 | name, host, | |
| 139 | System.currentTimeMillis() - start | |
| 140 | ); | |
| 141 | 0 | return name; |
| 142 | } | |
| 143 | ||
| 144 | } |