Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TerminateDocker |
|
| 2.0;2 |
1 | 2 | /** |
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.aspects.Tv; | |
34 | import com.jcabi.immutable.ArrayMap; | |
35 | import com.jcabi.log.Logger; | |
36 | import com.jcabi.xml.XML; | |
37 | import com.thindeck.api.Agent; | |
38 | import java.io.IOException; | |
39 | import java.text.ParseException; | |
40 | import java.util.Collection; | |
41 | import java.util.Date; | |
42 | import java.util.concurrent.TimeUnit; | |
43 | import org.apache.commons.lang3.time.DateFormatUtils; | |
44 | import org.xembly.Directive; | |
45 | import org.xembly.Directives; | |
46 | ||
47 | /** | |
48 | * Terminate waste containers that is ALIVE for too long. | |
49 | * | |
50 | * @author Yegor Bugayenko (yegor@teamed.io) | |
51 | * @version $Id$ | |
52 | * @since 0.1 | |
53 | */ | |
54 | @Immutable | |
55 | public final class TerminateDocker implements Agent { | |
56 | ||
57 | /** | |
58 | * Script to use. | |
59 | */ | |
60 | private final transient Script script; | |
61 | ||
62 | /** | |
63 | * Ctor. | |
64 | * @throws IOException If fails | |
65 | */ | |
66 | public TerminateDocker() throws IOException { | |
67 | 0 | this( |
68 | new Script.Default( | |
69 | TerminateDocker.class.getResource("terminate-docker.sh") | |
70 | ) | |
71 | ); | |
72 | 0 | } |
73 | ||
74 | /** | |
75 | * Ctor. | |
76 | * @param spt Script. | |
77 | */ | |
78 | 1 | public TerminateDocker(final Script spt) { |
79 | 1 | this.script = spt; |
80 | 1 | } |
81 | ||
82 | @Override | |
83 | public Iterable<Directive> exec(final XML deck) throws IOException { | |
84 | 0 | final Collection<XML> containers = deck.nodes( |
85 | "/deck/containers/container[@waste and @state='alive']" | |
86 | ); | |
87 | 0 | final Date today = new Date(); |
88 | 0 | for (final XML ctr : containers) { |
89 | final int age; | |
90 | try { | |
91 | 0 | age = (int) ((today.getTime() |
92 | - DateFormatUtils.ISO_DATETIME_FORMAT.parse( | |
93 | ctr.xpath("@waste").get(0) | |
94 | ).getTime()) / TimeUnit.MINUTES.toMillis(1L)); | |
95 | 0 | } catch (final ParseException ex) { |
96 | 0 | throw new IOException(ex); |
97 | 0 | } |
98 | 0 | if (age < Tv.TEN) { |
99 | 0 | continue; |
100 | } | |
101 | 0 | final String name = ctr.xpath("name/text()").get(0); |
102 | 0 | Logger.info( |
103 | this, "Container %s has to die, it's in waste for over %d mins", | |
104 | name, age | |
105 | ); | |
106 | 0 | this.terminate( |
107 | ctr.xpath("host/text()").get(0), | |
108 | name | |
109 | ); | |
110 | 0 | } |
111 | 0 | return new Directives(); |
112 | } | |
113 | ||
114 | /** | |
115 | * Terminate docker container. | |
116 | * @param host Host | |
117 | * @param name Name of container | |
118 | * @throws IOException If fails | |
119 | */ | |
120 | private void terminate(final String host, final String name) | |
121 | throws IOException { | |
122 | 0 | this.script.exec( |
123 | host, | |
124 | new ArrayMap<String, String>().with("container", name) | |
125 | ); | |
126 | 0 | Logger.info( |
127 | StartDocker.class, | |
128 | "Docker container %s terminated at %s", name, host | |
129 | ); | |
130 | 0 | } |
131 | ||
132 | } |