Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BuildImage |
|
| 1.5;1.5 |
1 | 1 | /** |
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.apache.commons.lang3.StringUtils; | |
42 | import org.xembly.Directive; | |
43 | import org.xembly.Directives; | |
44 | ||
45 | /** | |
46 | * Builds image from repo. | |
47 | * | |
48 | * @author Yegor Bugayenko (yegor@teamed.io) | |
49 | * @version $Id$ | |
50 | * @since 0.1 | |
51 | * @checkstyle MultipleStringLiteralsCheck (500 lines) | |
52 | */ | |
53 | @Immutable | |
54 | public final class BuildImage implements Agent { | |
55 | ||
56 | /** | |
57 | * Random. | |
58 | */ | |
59 | 1 | private static final Random RND = new SecureRandom(); |
60 | ||
61 | /** | |
62 | * Script to use. | |
63 | */ | |
64 | private final transient Script script; | |
65 | ||
66 | /** | |
67 | * Ctor. | |
68 | * @throws IOException If fails | |
69 | */ | |
70 | public BuildImage() throws IOException { | |
71 | 0 | this( |
72 | new Script.Default( | |
73 | BuildImage.class.getResource("build-image.sh") | |
74 | ) | |
75 | ); | |
76 | 0 | } |
77 | ||
78 | /** | |
79 | * Ctor. | |
80 | * @param spt Script. | |
81 | */ | |
82 | 1 | public BuildImage(final Script spt) { |
83 | 1 | this.script = spt; |
84 | 1 | } |
85 | ||
86 | @Override | |
87 | public Iterable<Directive> exec(final XML deck) throws IOException { | |
88 | 0 | final Collection<XML> repos = deck.nodes( |
89 | "/deck/repo[not(name=/deck/images/image/repo)]" | |
90 | ); | |
91 | 0 | final String name = deck.xpath("/deck/@name").get(0); |
92 | 0 | final Directives dirs = new Directives().xpath("/deck").addIf("images"); |
93 | 0 | for (final XML repo : repos) { |
94 | 0 | final String rname = repo.xpath("name/text()").get(0); |
95 | 0 | Logger.info( |
96 | this, "There is no Docker image for %s, time to build it", | |
97 | rname | |
98 | ); | |
99 | 0 | final String image = this.build(name, repo); |
100 | 0 | dirs.xpath("/deck/images").add("image") |
101 | .add("name").set(image).up() | |
102 | .add("repo").set(rname).up() | |
103 | .add("uri").set(repo.xpath("uri/text() ").get(0)).up() | |
104 | .attr("type", "blue") | |
105 | .xpath("/deck/repo") | |
106 | .remove(); | |
107 | 0 | } |
108 | 0 | return dirs; |
109 | } | |
110 | ||
111 | /** | |
112 | * Build a new image. | |
113 | * @param deck Deck name | |
114 | * @param repo Repo | |
115 | * @return Image name | |
116 | * @throws IOException If fails | |
117 | */ | |
118 | private String build(final String deck, final XML repo) | |
119 | throws IOException { | |
120 | 0 | final String name = String.format( |
121 | "%s-%08x", deck, | |
122 | BuildImage.RND.nextInt() | |
123 | ); | |
124 | 0 | final String hash = "#"; |
125 | 0 | String uri = repo.xpath("uri/text()").get(0); |
126 | final String branch; | |
127 | final String path; | |
128 | 0 | if (uri.contains(hash)) { |
129 | 0 | final String[] tail = StringUtils.substringAfter(uri, hash) |
130 | .split(":", 2); | |
131 | 0 | branch = tail[0]; |
132 | 0 | path = tail[1]; |
133 | 0 | uri = StringUtils.substringBefore(uri, hash); |
134 | 0 | } else { |
135 | 0 | branch = "master"; |
136 | 0 | path = "."; |
137 | } | |
138 | 0 | final long start = System.currentTimeMillis(); |
139 | 0 | this.script.exec( |
140 | "t1.thindeck.com", | |
141 | new ArrayMap<String, String>() | |
142 | .with("image", name) | |
143 | .with("uri", uri) | |
144 | .with("branch", branch) | |
145 | .with("path", path) | |
146 | ); | |
147 | 0 | Logger.info( |
148 | this, "Docker image %s built in %[ms]s", | |
149 | name, System.currentTimeMillis() - start | |
150 | ); | |
151 | 0 | return name; |
152 | } | |
153 | ||
154 | } |