| 1 | 1 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 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 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
@Immutable |
| 54 | |
public final class BuildImage implements Agent { |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 1 | private static final Random RND = new SecureRandom(); |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
private final transient Script script; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 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 | |
|
| 80 | |
|
| 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 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 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 | |
} |