Skip to content

Commit 3a63cf4

Browse files
committed
Squashed 'tools/' content from commit 4fe078a
git-subtree-dir: tools git-subtree-split: 4fe078a730d70c7936f1c43a1eca881f934bffce
0 parents  commit 3a63cf4

37 files changed

+2338
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cover/cover
2+
socks/proxy
3+
socks/image.tar
4+
runner/runner
5+
cmd/wcloud/wcloud
6+
*.pyc
7+
*~

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Weaveworks Build Tools
2+
3+
Included in this repo are tools shared by weave.git and scope.git. They include
4+
5+
- ```cover```: a tool which merges overlapping coverage reports generated by go
6+
test
7+
- ```files-with-type```: a tool to search directories for files of a given
8+
MIME type
9+
- ```lint```: a script to lint Go project; runs various tools like golint, go
10+
vet, errcheck etc
11+
- ```rebuild-image```: a script to rebuild docker images when their input files
12+
change; useful when you using docker images to build your software, but you
13+
don't want to build the image every time.
14+
- ```shell-lint```: a script to lint multiple shell files with
15+
[shellcheck](http://www.shellcheck.net/)
16+
- ```socks```: a simple, dockerised SOCKS proxy for getting your laptop onto
17+
the Weave network
18+
- ```test```: a script to run all go unit tests in subdirectories, gather the
19+
coverage results, and merge them into a single report.
20+
- ```runner```: a tool for running tests in parallel; given each test is
21+
suffixed with the number of hosts it requires, and the hosts available are
22+
contained in the environment variable HOSTS, the tool will run tests in
23+
parallel, on different hosts.
24+
- ```scheduler```: an appengine application that can be used to distribute
25+
tests across different shards in CircleCI.
26+
27+
## Using build-tools.git
28+
29+
To allow you to tie your code to a specific version of build-tools.git, such
30+
that future changes don't break you, we recommendation that you [`git subtree`]()
31+
this repository into your own repository:
32+
33+
[`git subtree`]: http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/
34+
35+
```
36+
git subtree add --prefix tools https://github.com/weaveworks/build-tools.git master --squash
37+
````
38+
39+
To update the code in build-tools.git, the process is therefore:
40+
- PR into build-tools.git, go through normal review process etc.
41+
- Do `git subtree pull --prefix tools https://github.com/weaveworks/build-tools.git master --squash`
42+
in your repo, and PR that.

circle.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
machine:
2+
services:
3+
- docker
4+
environment:
5+
GOPATH: /home/ubuntu
6+
SRCDIR: /home/ubuntu/src/github.com/weaveworks/tools
7+
PATH: $PATH:$HOME/bin
8+
9+
dependencies:
10+
post:
11+
- go clean -i net
12+
- go install -tags netgo std
13+
- mkdir -p $(dirname $SRCDIR)
14+
- cp -r $(pwd)/ $SRCDIR
15+
- go get github.com/golang/lint/golint github.com/fzipp/gocyclo github.com/kisielk/errcheck
16+
17+
test:
18+
override:
19+
- cd $SRCDIR; ./lint .
20+
- cd $SRCDIR/cover; make
21+
- cd $SRCDIR/socks; make
22+
- cd $SRCDIR/runner; make
23+
- cd $SRCDIR/cmd/wcloud; make
24+

cmd/wcloud/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.PHONY: all clean
2+
3+
all: wcloud
4+
5+
wcloud: *.go
6+
go get ./$(@D)
7+
go build -o $@ ./$(@D)
8+
9+
clean:
10+
rm -rf wcloud
11+
go clean ./...

cmd/wcloud/cli.go

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"flag"
7+
"fmt"
8+
"io/ioutil"
9+
"os"
10+
"os/user"
11+
"path/filepath"
12+
"strings"
13+
"time"
14+
15+
"github.com/olekukonko/tablewriter"
16+
"gopkg.in/yaml.v2"
17+
)
18+
19+
// ArrayFlags allows you to collect repeated flags
20+
type ArrayFlags []string
21+
22+
func (a *ArrayFlags) String() string {
23+
return strings.Join(*a, ",")
24+
}
25+
26+
// Set implements flags.Value
27+
func (a *ArrayFlags) Set(value string) error {
28+
*a = append(*a, value)
29+
return nil
30+
}
31+
32+
func env(key, def string) string {
33+
if val, ok := os.LookupEnv(key); ok {
34+
return val
35+
}
36+
return def
37+
}
38+
39+
var (
40+
token = env("SERVICE_TOKEN", "")
41+
baseURL = env("BASE_URL", "https://cloud.weave.works")
42+
)
43+
44+
func usage() {
45+
fmt.Println(`Usage:
46+
deploy <image>:<version> Deploy image to your configured env
47+
list List recent deployments
48+
config (<filename>) Get (or set) the configured env
49+
logs <deploy> Show lots for the given deployment`)
50+
}
51+
52+
func main() {
53+
if len(os.Args) <= 1 {
54+
usage()
55+
os.Exit(1)
56+
}
57+
58+
c := NewClient(token, baseURL)
59+
60+
switch os.Args[1] {
61+
case "deploy":
62+
deploy(c, os.Args[2:])
63+
case "list":
64+
list(c, os.Args[2:])
65+
case "config":
66+
config(c, os.Args[2:])
67+
case "logs":
68+
logs(c, os.Args[2:])
69+
case "events":
70+
events(c, os.Args[2:])
71+
case "help":
72+
usage()
73+
default:
74+
usage()
75+
}
76+
}
77+
78+
func deploy(c Client, args []string) {
79+
var (
80+
flags = flag.NewFlagSet("", flag.ContinueOnError)
81+
username = flags.String("u", "", "Username to report to deploy service (default with be current user)")
82+
services ArrayFlags
83+
)
84+
flag.Var(&services, "service", "Service to update (can be repeated)")
85+
if err := flags.Parse(args); err != nil {
86+
usage()
87+
return
88+
}
89+
args = flags.Args()
90+
if len(args) != 1 {
91+
usage()
92+
return
93+
}
94+
parts := strings.SplitN(args[0], ":", 2)
95+
if len(parts) < 2 {
96+
usage()
97+
return
98+
}
99+
if *username == "" {
100+
user, err := user.Current()
101+
if err != nil {
102+
fmt.Println(err.Error())
103+
os.Exit(1)
104+
}
105+
*username = user.Username
106+
}
107+
deployment := Deployment{
108+
ImageName: parts[0],
109+
Version: parts[1],
110+
TriggeringUser: *username,
111+
IntendedServices: services,
112+
}
113+
if err := c.Deploy(deployment); err != nil {
114+
fmt.Println(err.Error())
115+
os.Exit(1)
116+
}
117+
}
118+
119+
func list(c Client, args []string) {
120+
var (
121+
flags = flag.NewFlagSet("", flag.ContinueOnError)
122+
since = flags.Duration("since", 7*24*time.Hour, "How far back to fetch results")
123+
)
124+
if err := flags.Parse(args); err != nil {
125+
usage()
126+
return
127+
}
128+
through := time.Now()
129+
from := through.Add(-*since)
130+
deployments, err := c.GetDeployments(from.Unix(), through.Unix())
131+
if err != nil {
132+
fmt.Println(err.Error())
133+
os.Exit(1)
134+
}
135+
136+
table := tablewriter.NewWriter(os.Stdout)
137+
table.SetHeader([]string{"Created", "ID", "Image", "Version", "State"})
138+
table.SetBorder(false)
139+
table.SetColumnSeparator(" ")
140+
for _, deployment := range deployments {
141+
table.Append([]string{
142+
deployment.CreatedAt.Format(time.RFC822),
143+
deployment.ID,
144+
deployment.ImageName,
145+
deployment.Version,
146+
deployment.State,
147+
})
148+
}
149+
table.Render()
150+
}
151+
152+
func events(c Client, args []string) {
153+
var (
154+
flags = flag.NewFlagSet("", flag.ContinueOnError)
155+
since = flags.Duration("since", 7*24*time.Hour, "How far back to fetch results")
156+
)
157+
if err := flags.Parse(args); err != nil {
158+
usage()
159+
return
160+
}
161+
through := time.Now()
162+
from := through.Add(-*since)
163+
events, err := c.GetEvents(from.Unix(), through.Unix())
164+
if err != nil {
165+
fmt.Println(err.Error())
166+
os.Exit(1)
167+
}
168+
169+
fmt.Println("events: ", string(events))
170+
}
171+
172+
func loadConfig(filename string) (*Config, error) {
173+
extension := filepath.Ext(filename)
174+
var config Config
175+
buf, err := ioutil.ReadFile(filename)
176+
if err != nil {
177+
return nil, err
178+
}
179+
if extension == ".yaml" || extension == ".yml" {
180+
if err := yaml.Unmarshal(buf, &config); err != nil {
181+
return nil, err
182+
}
183+
} else {
184+
if err := json.NewDecoder(bytes.NewReader(buf)).Decode(&config); err != nil {
185+
return nil, err
186+
}
187+
}
188+
return &config, nil
189+
}
190+
191+
func config(c Client, args []string) {
192+
if len(args) > 1 {
193+
usage()
194+
return
195+
}
196+
197+
if len(args) == 1 {
198+
config, err := loadConfig(args[0])
199+
if err != nil {
200+
fmt.Println("Error reading config:", err)
201+
os.Exit(1)
202+
}
203+
204+
if err := c.SetConfig(config); err != nil {
205+
fmt.Println(err.Error())
206+
os.Exit(1)
207+
}
208+
} else {
209+
config, err := c.GetConfig()
210+
if err != nil {
211+
fmt.Println(err.Error())
212+
os.Exit(1)
213+
}
214+
215+
buf, err := yaml.Marshal(config)
216+
if err != nil {
217+
fmt.Println(err.Error())
218+
os.Exit(1)
219+
}
220+
221+
fmt.Println(string(buf))
222+
}
223+
}
224+
225+
func logs(c Client, args []string) {
226+
if len(args) != 1 {
227+
usage()
228+
return
229+
}
230+
231+
output, err := c.GetLogs(args[0])
232+
if err != nil {
233+
fmt.Println(err.Error())
234+
os.Exit(1)
235+
}
236+
237+
fmt.Println(string(output))
238+
}

0 commit comments

Comments
 (0)