| 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.util.Collection; |
| 39 | |
import java.util.Map; |
| 40 | |
import java.util.concurrent.ConcurrentHashMap; |
| 41 | |
import java.util.concurrent.ConcurrentMap; |
| 42 | |
import java.util.regex.Matcher; |
| 43 | |
import java.util.regex.Pattern; |
| 44 | |
import org.xembly.Directive; |
| 45 | |
import org.xembly.Directives; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
@Immutable |
| 56 | |
public final class DetectPorts implements Agent { |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 1 | private static final Pattern PTN = Pattern.compile( |
| 62 | |
"thindeck_([a-z]+)=(?:[\\d+\\.]+):(\\d+)", |
| 63 | |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE |
| 64 | |
); |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
private final transient Script script; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public DetectPorts() throws IOException { |
| 76 | 0 | this( |
| 77 | |
new Script.Default( |
| 78 | |
DetectPorts.class.getResource("docker-ports.sh") |
| 79 | |
) |
| 80 | |
); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | 1 | public DetectPorts(final Script spt) { |
| 88 | 1 | this.script = spt; |
| 89 | 1 | } |
| 90 | |
|
| 91 | |
@Override |
| 92 | |
public Iterable<Directive> exec(final XML deck) throws IOException { |
| 93 | 0 | final Directives dirs = new Directives(); |
| 94 | 0 | final Collection<XML> containers = deck.nodes( |
| 95 | |
|
| 96 | |
"/deck/containers/container[not(@waste) and (not(http) or not(https))]" |
| 97 | |
); |
| 98 | 0 | for (final XML ctr : containers) { |
| 99 | 0 | final String name = ctr.xpath("name/text()").get(0); |
| 100 | 0 | final String host = ctr.xpath("host/text()").get(0); |
| 101 | 0 | Logger.info( |
| 102 | |
this, "Exposed ports of container %s at %s must be found", |
| 103 | |
name, host |
| 104 | |
); |
| 105 | 0 | final Map<String, Integer> ports = this.ports(name, host); |
| 106 | 0 | dirs.xpath( |
| 107 | |
String.format( |
| 108 | |
"/deck/containers/container[name='%s']", |
| 109 | |
name |
| 110 | |
) |
| 111 | |
); |
| 112 | 0 | for (final Map.Entry<String, Integer> port : ports.entrySet()) { |
| 113 | 0 | dirs.addIf(port.getKey()) |
| 114 | |
.set(Integer.toString(port.getValue())) |
| 115 | |
.up(); |
| 116 | 0 | } |
| 117 | 0 | } |
| 118 | 0 | return dirs; |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
private Map<String, Integer> ports(final String name, final String host) |
| 129 | |
throws IOException { |
| 130 | 0 | final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(0); |
| 131 | 0 | final String stdout = this.script.exec( |
| 132 | |
host, |
| 133 | |
new ArrayMap<String, String>().with("container", name) |
| 134 | |
); |
| 135 | 0 | final Matcher matcher = DetectPorts.PTN.matcher(stdout); |
| 136 | 0 | while (matcher.find()) { |
| 137 | 0 | map.put( |
| 138 | |
matcher.group(1), |
| 139 | |
Integer.parseInt(matcher.group(2)) |
| 140 | |
); |
| 141 | |
} |
| 142 | 0 | Logger.info( |
| 143 | |
this, "Docker container %s at %s exposes these ports: %s", |
| 144 | |
name, host, map |
| 145 | |
); |
| 146 | 0 | return map; |
| 147 | |
} |
| 148 | |
|
| 149 | |
} |