Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UpdateNginx |
|
| 1.2;1.2 | ||||
UpdateNginx$1 |
|
| 1.2;1.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.agents; | |
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.xml.XML; | |
37 | import com.thindeck.api.Agent; | |
38 | import java.io.IOException; | |
39 | import org.xembly.Directive; | |
40 | import org.xembly.Directives; | |
41 | ||
42 | /** | |
43 | * Update nginx load balancer. | |
44 | * | |
45 | * <p>Assumption is that ngnix configuration loaded from {@code ngnix.conf} | |
46 | * and each host has a conf file named {@code www.example.com.conf}, | |
47 | * which will contain load balancing group, e.g: | |
48 | * | |
49 | * <pre> upstream example_servers { | |
50 | * server 10.0.0.1:80; | |
51 | * server 10.0.0.2:80; | |
52 | * } | |
53 | * server { | |
54 | * listen 80; | |
55 | * server_name www.example.com; | |
56 | * location / { | |
57 | * proxy_pass http://example_servers; | |
58 | * } | |
59 | * }</pre> | |
60 | * | |
61 | * <p>This file is loaded from main {@code ngnix.conf} | |
62 | * using {@code include} directive. | |
63 | * | |
64 | * <p>To install nginx on a clean server, just install it first using | |
65 | * "apt-get" or "yum" and that's it.</> | |
66 | * | |
67 | * @author Krzysztof Krason (Krzysztof.Krason@gmail.com) | |
68 | * @author Yegor Bugayenko (yegor@teamed.io) | |
69 | * @version $Id$ | |
70 | * @checkstyle MultipleStringLiterals (300 lines) | |
71 | */ | |
72 | public final class UpdateNginx implements Agent { | |
73 | ||
74 | /** | |
75 | * Script to use. | |
76 | */ | |
77 | private final transient Script script; | |
78 | ||
79 | /** | |
80 | * Ctor. | |
81 | * @throws IOException If fails | |
82 | */ | |
83 | public UpdateNginx() throws IOException { | |
84 | 0 | this( |
85 | new Script.Default( | |
86 | UpdateNginx.class.getResource("update-nginx.sh") | |
87 | ) | |
88 | ); | |
89 | 0 | } |
90 | ||
91 | /** | |
92 | * Ctor. | |
93 | * @param spt Script. | |
94 | */ | |
95 | 1 | public UpdateNginx(final Script spt) { |
96 | 1 | this.script = spt; |
97 | 1 | } |
98 | ||
99 | @Override | |
100 | public Iterable<Directive> exec(final XML deck) throws IOException { | |
101 | 0 | for (final String domain : deck.xpath("/deck/domains/domain/text()")) { |
102 | 0 | this.update(domain, deck); |
103 | 0 | } |
104 | 0 | return new Directives(); |
105 | } | |
106 | ||
107 | /** | |
108 | * Update for one domain. | |
109 | * @param domain Domain name | |
110 | * @param deck The deck | |
111 | * @throws IOException If fails | |
112 | */ | |
113 | private void update(final String domain, final XML deck) | |
114 | throws IOException { | |
115 | // @checkstyle LineLength (1 line) | |
116 | 0 | final String terms = "not(@waste) and @type='green' and @state='alive' and http"; |
117 | 0 | final String servers = Joiner.on(' ').join( |
118 | Iterables.transform( | |
119 | deck.nodes(String.format("//container[%s]", terms)), | |
120 | 0 | new Function<XML, String>() { |
121 | @Override | |
122 | public String apply(final XML ctr) { | |
123 | 0 | return String.format( |
124 | "server %s:%s; ", | |
125 | ctr.xpath("host/text()").get(0), | |
126 | ctr.xpath("http/text()").get(0) | |
127 | ); | |
128 | } | |
129 | } | |
130 | ) | |
131 | ); | |
132 | 0 | this.script.exec( |
133 | "t1.thindeck.com", | |
134 | new ArrayMap<String, String>() | |
135 | .with("deck", deck.xpath("/deck/@name").get(0)) | |
136 | .with( | |
137 | "images", | |
138 | Joiner.on(',').join( | |
139 | deck.xpath( | |
140 | String.format("//container[%s]/image/text()", terms) | |
141 | ) | |
142 | ) | |
143 | ) | |
144 | .with("port", "80") | |
145 | .with("group", domain.replace(".", "_")) | |
146 | .with("domain", domain) | |
147 | .with("servers", servers) | |
148 | ); | |
149 | 0 | } |
150 | ||
151 | } |