Coverage Report - com.thindeck.agents.DetectPorts
 
Classes in this File Line Coverage Branch Coverage Complexity
DetectPorts
18%
5/27
0%
0/6
1.75
 
 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.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  
  * Discover ports of Docker containers.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@teamed.io)
 51  
  * @version $Id$
 52  
  * @since 0.5
 53  
  * @checkstyle MultipleStringLiteralsCheck (500 lines)
 54  
  */
 55  
 @Immutable
 56  
 public final class DetectPorts implements Agent {
 57  
 
 58  
     /**
 59  
      * Pattern to find all ports.
 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  
      * Script to use.
 68  
      */
 69  
     private final transient Script script;
 70  
 
 71  
     /**
 72  
      * Ctor.
 73  
      * @throws IOException If fails
 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  
      * Ctor.
 85  
      * @param spt Script.
 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  
             // @checkstyle LineLength (1 line)
 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  
      * Detect all ports.
 123  
      * @param name Docker container name
 124  
      * @param host Host name of the tank
 125  
      * @return Ports
 126  
      * @throws IOException If fails
 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  
 }