Merge lp:~natefinch/juju-ci-tools/repository into lp:juju-ci-tools/repository

Proposed by Nate Finch
Status: Merged
Merged at revision: 17
Proposed branch: lp:~natefinch/juju-ci-tools/repository
Merge into: lp:juju-ci-tools/repository
Diff against target: 259 lines (+201/-0)
10 files modified
trusty/fill-logs/README.md (+3/-0)
trusty/fill-logs/actions.yaml (+25/-0)
trusty/fill-logs/actions/actions.go (+149/-0)
trusty/fill-logs/actions/fill-machine (+3/-0)
trusty/fill-logs/actions/fill-unit (+3/-0)
trusty/fill-logs/actions/machine-size (+3/-0)
trusty/fill-logs/actions/unit-size (+3/-0)
trusty/fill-logs/config.yaml (+1/-0)
trusty/fill-logs/hooks/install (+5/-0)
trusty/fill-logs/metadata.yaml (+6/-0)
To merge this branch: bzr merge lp:~natefinch/juju-ci-tools/repository
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+259888@code.launchpad.net

Description of the change

add fill-logs charm for log rotation tests

To post a comment you must log in.
16. By Nate Finch

add fill-log charm

Revision history for this message
Curtis Hovey (sinzui) wrote :

Hi Nate. I have a question about arg checking, but no real code concerns about this charm

review: Needs Information (code)
Revision history for this message
Nate Finch (natefinch) wrote :

See reply inline below.

Revision history for this message
Curtis Hovey (sinzui) wrote :

Hi Nate.

I am not going to block merging your charm. I have a further comment/question. I will merge your code soon.

review: Approve (code)
17. By Nate Finch

code review changes

Revision history for this message
Nate Finch (natefinch) wrote :

made a couple tweaks based on your comments

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'trusty/fill-logs'
2=== added file 'trusty/fill-logs/README.md'
3--- trusty/fill-logs/README.md 1970-01-01 00:00:00 +0000
4+++ trusty/fill-logs/README.md 2015-06-08 19:24:26 +0000
5@@ -0,0 +1,3 @@
6+#fill-logs
7+
8+Fill-logs is a testing charm intended to be used to test log rotation.
9
10=== added directory 'trusty/fill-logs/actions'
11=== added file 'trusty/fill-logs/actions.yaml'
12--- trusty/fill-logs/actions.yaml 1970-01-01 00:00:00 +0000
13+++ trusty/fill-logs/actions.yaml 2015-06-08 19:24:26 +0000
14@@ -0,0 +1,25 @@
15+fill-unit:
16+ description: Fill the unit agent log with data.
17+ params:
18+ megs:
19+ type: integer
20+ description: number of MB of data to add to unit agent log
21+ required: [megs]
22+ additionalProperties: false
23+fill-machine:
24+ description: Fill the machine agent log with data.
25+ params:
26+ megs:
27+ type: integer
28+ description: number of MB of data to add to machine agent log
29+ machine:
30+ type: integer
31+ description: the machine number this unit is on
32+ required: [megs, machine]
33+ additionalProperties: false
34+machine-size:
35+ description: Gets the name and sizes of machine agent logs.
36+ additionalProperties: false
37+unit-size:
38+ description: Gets the name and sizes of unit agent logs.
39+ additionalProperties: false
40\ No newline at end of file
41
42=== added file 'trusty/fill-logs/actions/actions.go'
43--- trusty/fill-logs/actions/actions.go 1970-01-01 00:00:00 +0000
44+++ trusty/fill-logs/actions/actions.go 2015-06-08 19:24:26 +0000
45@@ -0,0 +1,149 @@
46+package main
47+
48+import (
49+ "fmt"
50+ "io"
51+ "os"
52+ "os/exec"
53+ "path/filepath"
54+ "strconv"
55+ "strings"
56+)
57+
58+func main() {
59+ if err := run(os.Args); err != nil {
60+ fmt.Fprintln(os.Stderr, err)
61+ os.Exit(1)
62+ }
63+}
64+
65+func run(args []string) error {
66+ if len(args) != 2 {
67+ return fmt.Errorf("expected exactly one argument, the action name")
68+ }
69+ switch args[1] {
70+ case "fill-unit":
71+ return fillUnit()
72+ case "fill-machine":
73+ return fillMachine()
74+ case "unit-size":
75+ return unitLogSizes()
76+ case "machine-size":
77+ return machineLogSizes()
78+ default:
79+ return fmt.Errorf("unknown action: %q", args[1])
80+ }
81+}
82+
83+func fillUnit() error {
84+ return fillLog(os.Stdout)
85+}
86+
87+func unitLogSizes() error {
88+ return writeSizes("/var/log/juju/unit-fill-logs*.log")
89+}
90+
91+func machineLogSizes() error {
92+ return writeSizes("/var/log/juju/machine-1*.log")
93+}
94+
95+func fillMachine() (err error) {
96+ machine, err := getMachine()
97+ if err != nil {
98+ return err
99+ }
100+ svcname := fmt.Sprintf("jujud-machine-%d", machine)
101+ out, err := exec.Command("service", svcname, "stop").CombinedOutput()
102+ if err != nil {
103+ return fmt.Errorf("error stopping machine agent %q: %s", svcname, out)
104+ }
105+ defer func() {
106+ out, err2 := exec.Command("service", svcname, "start").CombinedOutput()
107+ if err2 == nil {
108+ return
109+ }
110+ if err == nil {
111+ // function error is currently nil, so overwrite with this one.
112+ err = fmt.Errorf("error starting machine agent %q: %s", svcname, out)
113+ return
114+ }
115+ // function error is non-nil, so can't overwrite, just print.
116+ fmt.Printf("error starting machine agent %q: %s", svcname, out)
117+ }()
118+ logname := fmt.Sprintf("/var/log/juju/machine-%d.log", machine)
119+ f, err := os.OpenFile(logname, os.O_APPEND|os.O_WRONLY, 0644)
120+ if err != nil {
121+ return fmt.Errorf("failed to open machine log file: %v", err)
122+ }
123+ defer f.Close()
124+ return fillLog(f)
125+}
126+
127+func fillLog(w io.Writer) error {
128+ megs, err := getMegs()
129+ if err != nil {
130+ return err
131+ }
132+ bytes := megs * 1024 * 1024
133+ total := 0
134+
135+ for total < bytes {
136+ // technically, the log file will be bigger than asked for, since it
137+ // prepends a bunch of stuff to each log call, but this guarantees we've
138+ // put *at least* this much data in the log, which should guarantee a
139+ // rotation.
140+ n, err := fmt.Fprintln(w, lorem)
141+ if err != nil {
142+ return fmt.Errorf("error writing to log: %s", err)
143+ }
144+ total += n
145+ }
146+ return nil
147+}
148+
149+func writeSizes(glob string) error {
150+ paths, err := filepath.Glob(glob)
151+ if err != nil {
152+ return fmt.Errorf("error getting logs for %q: %s", glob, err)
153+ }
154+
155+ // go through the list in reverse, since the primary log file is always last,
156+ // but it's a lot more convenient for parsing if it's first in the output.
157+ for i, j := len(paths)-1, 0; i >= 0; i-- {
158+ path := paths[i]
159+ info, err := os.Stat(path)
160+ if err != nil {
161+ return fmt.Errorf("error stating log %q: %s", path, err)
162+ }
163+ name := fmt.Sprintf("result-map.log%d.name=%s", j, path)
164+ size := fmt.Sprintf("result-map.log%d.size=%d", j, info.Size()/1024/1024)
165+ out, err := exec.Command("action-set", name, size).CombinedOutput()
166+ if err != nil {
167+ return fmt.Errorf("error calling action-set: %s", out)
168+ }
169+ j++
170+ }
171+
172+ return nil
173+}
174+
175+func getMegs() (int, error) {
176+ return getInt("megs")
177+}
178+
179+func getMachine() (int, error) {
180+ return getInt("machine")
181+}
182+
183+func getInt(name string) (int, error) {
184+ out, err := exec.Command("action-get", name).CombinedOutput()
185+ if err != nil {
186+ fmt.Fprintln(os.Stderr, out)
187+ return 0, fmt.Errorf("error calling action-get: %s", err)
188+ }
189+ // for some reason the output always comes with a /n at the end, so just
190+ // trim it.
191+ return strconv.Atoi(strings.TrimSpace(string(out)))
192+}
193+
194+const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`
195
196=== added file 'trusty/fill-logs/actions/fill-machine'
197--- trusty/fill-logs/actions/fill-machine 1970-01-01 00:00:00 +0000
198+++ trusty/fill-logs/actions/fill-machine 2015-06-08 19:24:26 +0000
199@@ -0,0 +1,3 @@
200+#!/bin/bash
201+
202+go run actions/actions.go fill-machine
203\ No newline at end of file
204
205=== added file 'trusty/fill-logs/actions/fill-unit'
206--- trusty/fill-logs/actions/fill-unit 1970-01-01 00:00:00 +0000
207+++ trusty/fill-logs/actions/fill-unit 2015-06-08 19:24:26 +0000
208@@ -0,0 +1,3 @@
209+#!/bin/bash
210+
211+go run actions/actions.go fill-unit
212\ No newline at end of file
213
214=== added file 'trusty/fill-logs/actions/machine-size'
215--- trusty/fill-logs/actions/machine-size 1970-01-01 00:00:00 +0000
216+++ trusty/fill-logs/actions/machine-size 2015-06-08 19:24:26 +0000
217@@ -0,0 +1,3 @@
218+#!/bin/bash
219+
220+go run actions/actions.go machine-size
221\ No newline at end of file
222
223=== added file 'trusty/fill-logs/actions/unit-size'
224--- trusty/fill-logs/actions/unit-size 1970-01-01 00:00:00 +0000
225+++ trusty/fill-logs/actions/unit-size 2015-06-08 19:24:26 +0000
226@@ -0,0 +1,3 @@
227+#!/bin/bash
228+
229+go run actions/actions.go unit-size
230\ No newline at end of file
231
232=== added file 'trusty/fill-logs/config.yaml'
233--- trusty/fill-logs/config.yaml 1970-01-01 00:00:00 +0000
234+++ trusty/fill-logs/config.yaml 2015-06-08 19:24:26 +0000
235@@ -0,0 +1,1 @@
236+options:
237
238=== added directory 'trusty/fill-logs/hooks'
239=== added file 'trusty/fill-logs/hooks/install'
240--- trusty/fill-logs/hooks/install 1970-01-01 00:00:00 +0000
241+++ trusty/fill-logs/hooks/install 2015-06-08 19:24:26 +0000
242@@ -0,0 +1,5 @@
243+#!/bin/bash
244+
245+set -e
246+
247+apt-get install -y golang
248\ No newline at end of file
249
250=== added file 'trusty/fill-logs/metadata.yaml'
251--- trusty/fill-logs/metadata.yaml 1970-01-01 00:00:00 +0000
252+++ trusty/fill-logs/metadata.yaml 2015-06-08 19:24:26 +0000
253@@ -0,0 +1,6 @@
254+name: fill-logs
255+summary: Test charm that fills the logs
256+maintainer: Nate Finch <nate.finch@gmail.com>
257+description: Charm fills the logs.
258+categories:
259+ - application

Subscribers

People subscribed via source and target branches