Coverage Report - com.thindeck.bosses.CleanNginx
 
Classes in this File Line Coverage Branch Coverage Complexity
CleanNginx
0%
0/7
N/A
2
CleanNginx$1
0%
0/4
N/A
2
CleanNginx$1$1
0%
0/2
N/A
2
 
 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.bosses;
 31  
 
 32  
 import com.google.common.base.Function;
 33  
 import com.google.common.base.Joiner;
 34  
 import com.google.common.collect.Iterables;
 35  
 import com.jcabi.immutable.ArrayMap;
 36  
 import com.jcabi.ssh.Shell;
 37  
 import com.thindeck.agents.Remote;
 38  
 import com.thindeck.agents.Script;
 39  
 import com.thindeck.api.Boss;
 40  
 import com.thindeck.api.Deck;
 41  
 import java.io.ByteArrayOutputStream;
 42  
 import java.io.IOException;
 43  
 import org.apache.commons.io.IOUtils;
 44  
 
 45  
 /**
 46  
  * Remove un-used expired domains from nginx load balancer.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@teamed.io)
 49  
  * @version $Id$
 50  
  */
 51  0
 public final class CleanNginx implements Boss {
 52  
 
 53  
     @Override
 54  
     public void exec(final Iterable<Deck> decks) throws IOException {
 55  0
         final Iterable<String> confs = Iterables.concat(
 56  
             Iterables.transform(
 57  
                 decks,
 58  0
                 new Function<Deck, Iterable<String>>() {
 59  
                     @Override
 60  
                     public Iterable<String> apply(final Deck deck) {
 61  
                         try {
 62  0
                             return Iterables.transform(
 63  
                                 new Deck.Smart(deck).xml().xpath(
 64  
                                     "/deck/domains/domain/text()"
 65  
                                 ),
 66  0
                                 new Function<String, String>() {
 67  
                                     @Override
 68  
                                     public String apply(final String dmn) {
 69  0
                                         return String.format("%s.conf", dmn);
 70  
                                     }
 71  
                                 }
 72  
                             );
 73  0
                         } catch (final IOException ex) {
 74  0
                             throw new IllegalStateException(ex);
 75  
                         }
 76  
                     }
 77  
                 }
 78  
             )
 79  
         );
 80  0
         final String host = "t1.thindeck.com";
 81  0
         final Shell shell = new Remote(host);
 82  0
         shell.exec(
 83  
             "cat > ~/domains",
 84  
             IOUtils.toInputStream(Joiner.on('\n').join(confs)),
 85  
             new ByteArrayOutputStream(),
 86  
             new ByteArrayOutputStream()
 87  
         );
 88  0
         new Script.Default(SetupNginx.class.getResource("clean-nginx.sh")).exec(
 89  
             host, new ArrayMap<String, String>()
 90  
         );
 91  0
     }
 92  
 
 93  
 }