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
=== added directory 'trusty/fill-logs'
=== added file 'trusty/fill-logs/README.md'
--- trusty/fill-logs/README.md 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/README.md 2015-06-08 19:24:26 +0000
@@ -0,0 +1,3 @@
1#fill-logs
2
3Fill-logs is a testing charm intended to be used to test log rotation.
04
=== added directory 'trusty/fill-logs/actions'
=== added file 'trusty/fill-logs/actions.yaml'
--- trusty/fill-logs/actions.yaml 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/actions.yaml 2015-06-08 19:24:26 +0000
@@ -0,0 +1,25 @@
1fill-unit:
2 description: Fill the unit agent log with data.
3 params:
4 megs:
5 type: integer
6 description: number of MB of data to add to unit agent log
7 required: [megs]
8 additionalProperties: false
9fill-machine:
10 description: Fill the machine agent log with data.
11 params:
12 megs:
13 type: integer
14 description: number of MB of data to add to machine agent log
15 machine:
16 type: integer
17 description: the machine number this unit is on
18 required: [megs, machine]
19 additionalProperties: false
20machine-size:
21 description: Gets the name and sizes of machine agent logs.
22 additionalProperties: false
23unit-size:
24 description: Gets the name and sizes of unit agent logs.
25 additionalProperties: false
0\ No newline at end of file26\ No newline at end of file
127
=== added file 'trusty/fill-logs/actions/actions.go'
--- trusty/fill-logs/actions/actions.go 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/actions/actions.go 2015-06-08 19:24:26 +0000
@@ -0,0 +1,149 @@
1package main
2
3import (
4 "fmt"
5 "io"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strconv"
10 "strings"
11)
12
13func main() {
14 if err := run(os.Args); err != nil {
15 fmt.Fprintln(os.Stderr, err)
16 os.Exit(1)
17 }
18}
19
20func run(args []string) error {
21 if len(args) != 2 {
22 return fmt.Errorf("expected exactly one argument, the action name")
23 }
24 switch args[1] {
25 case "fill-unit":
26 return fillUnit()
27 case "fill-machine":
28 return fillMachine()
29 case "unit-size":
30 return unitLogSizes()
31 case "machine-size":
32 return machineLogSizes()
33 default:
34 return fmt.Errorf("unknown action: %q", args[1])
35 }
36}
37
38func fillUnit() error {
39 return fillLog(os.Stdout)
40}
41
42func unitLogSizes() error {
43 return writeSizes("/var/log/juju/unit-fill-logs*.log")
44}
45
46func machineLogSizes() error {
47 return writeSizes("/var/log/juju/machine-1*.log")
48}
49
50func fillMachine() (err error) {
51 machine, err := getMachine()
52 if err != nil {
53 return err
54 }
55 svcname := fmt.Sprintf("jujud-machine-%d", machine)
56 out, err := exec.Command("service", svcname, "stop").CombinedOutput()
57 if err != nil {
58 return fmt.Errorf("error stopping machine agent %q: %s", svcname, out)
59 }
60 defer func() {
61 out, err2 := exec.Command("service", svcname, "start").CombinedOutput()
62 if err2 == nil {
63 return
64 }
65 if err == nil {
66 // function error is currently nil, so overwrite with this one.
67 err = fmt.Errorf("error starting machine agent %q: %s", svcname, out)
68 return
69 }
70 // function error is non-nil, so can't overwrite, just print.
71 fmt.Printf("error starting machine agent %q: %s", svcname, out)
72 }()
73 logname := fmt.Sprintf("/var/log/juju/machine-%d.log", machine)
74 f, err := os.OpenFile(logname, os.O_APPEND|os.O_WRONLY, 0644)
75 if err != nil {
76 return fmt.Errorf("failed to open machine log file: %v", err)
77 }
78 defer f.Close()
79 return fillLog(f)
80}
81
82func fillLog(w io.Writer) error {
83 megs, err := getMegs()
84 if err != nil {
85 return err
86 }
87 bytes := megs * 1024 * 1024
88 total := 0
89
90 for total < bytes {
91 // technically, the log file will be bigger than asked for, since it
92 // prepends a bunch of stuff to each log call, but this guarantees we've
93 // put *at least* this much data in the log, which should guarantee a
94 // rotation.
95 n, err := fmt.Fprintln(w, lorem)
96 if err != nil {
97 return fmt.Errorf("error writing to log: %s", err)
98 }
99 total += n
100 }
101 return nil
102}
103
104func writeSizes(glob string) error {
105 paths, err := filepath.Glob(glob)
106 if err != nil {
107 return fmt.Errorf("error getting logs for %q: %s", glob, err)
108 }
109
110 // go through the list in reverse, since the primary log file is always last,
111 // but it's a lot more convenient for parsing if it's first in the output.
112 for i, j := len(paths)-1, 0; i >= 0; i-- {
113 path := paths[i]
114 info, err := os.Stat(path)
115 if err != nil {
116 return fmt.Errorf("error stating log %q: %s", path, err)
117 }
118 name := fmt.Sprintf("result-map.log%d.name=%s", j, path)
119 size := fmt.Sprintf("result-map.log%d.size=%d", j, info.Size()/1024/1024)
120 out, err := exec.Command("action-set", name, size).CombinedOutput()
121 if err != nil {
122 return fmt.Errorf("error calling action-set: %s", out)
123 }
124 j++
125 }
126
127 return nil
128}
129
130func getMegs() (int, error) {
131 return getInt("megs")
132}
133
134func getMachine() (int, error) {
135 return getInt("machine")
136}
137
138func getInt(name string) (int, error) {
139 out, err := exec.Command("action-get", name).CombinedOutput()
140 if err != nil {
141 fmt.Fprintln(os.Stderr, out)
142 return 0, fmt.Errorf("error calling action-get: %s", err)
143 }
144 // for some reason the output always comes with a /n at the end, so just
145 // trim it.
146 return strconv.Atoi(strings.TrimSpace(string(out)))
147}
148
149const 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.`
0150
=== added file 'trusty/fill-logs/actions/fill-machine'
--- trusty/fill-logs/actions/fill-machine 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/actions/fill-machine 2015-06-08 19:24:26 +0000
@@ -0,0 +1,3 @@
1#!/bin/bash
2
3go run actions/actions.go fill-machine
0\ No newline at end of file4\ No newline at end of file
15
=== added file 'trusty/fill-logs/actions/fill-unit'
--- trusty/fill-logs/actions/fill-unit 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/actions/fill-unit 2015-06-08 19:24:26 +0000
@@ -0,0 +1,3 @@
1#!/bin/bash
2
3go run actions/actions.go fill-unit
0\ No newline at end of file4\ No newline at end of file
15
=== added file 'trusty/fill-logs/actions/machine-size'
--- trusty/fill-logs/actions/machine-size 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/actions/machine-size 2015-06-08 19:24:26 +0000
@@ -0,0 +1,3 @@
1#!/bin/bash
2
3go run actions/actions.go machine-size
0\ No newline at end of file4\ No newline at end of file
15
=== added file 'trusty/fill-logs/actions/unit-size'
--- trusty/fill-logs/actions/unit-size 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/actions/unit-size 2015-06-08 19:24:26 +0000
@@ -0,0 +1,3 @@
1#!/bin/bash
2
3go run actions/actions.go unit-size
0\ No newline at end of file4\ No newline at end of file
15
=== added file 'trusty/fill-logs/config.yaml'
--- trusty/fill-logs/config.yaml 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/config.yaml 2015-06-08 19:24:26 +0000
@@ -0,0 +1,1 @@
1options:
02
=== added directory 'trusty/fill-logs/hooks'
=== added file 'trusty/fill-logs/hooks/install'
--- trusty/fill-logs/hooks/install 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/hooks/install 2015-06-08 19:24:26 +0000
@@ -0,0 +1,5 @@
1#!/bin/bash
2
3set -e
4
5apt-get install -y golang
0\ No newline at end of file6\ No newline at end of file
17
=== added file 'trusty/fill-logs/metadata.yaml'
--- trusty/fill-logs/metadata.yaml 1970-01-01 00:00:00 +0000
+++ trusty/fill-logs/metadata.yaml 2015-06-08 19:24:26 +0000
@@ -0,0 +1,6 @@
1name: fill-logs
2summary: Test charm that fills the logs
3maintainer: Nate Finch <nate.finch@gmail.com>
4description: Charm fills the logs.
5categories:
6 - application

Subscribers

People subscribed via source and target branches