Compare commits
3 Commits
a43cae385c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1578ac3679 | ||
|
|
121c2af7b0 | ||
|
|
73afbef415 |
3
Makefile
3
Makefile
@@ -1,11 +1,10 @@
|
|||||||
#GOFLAGS=""
|
|
||||||
LDFLAGS=-ldflags "-s -w"
|
LDFLAGS=-ldflags "-s -w"
|
||||||
BINARY=metrorama
|
BINARY=metrorama
|
||||||
|
|
||||||
.DEFAULT_GOAL: $(BINARY)
|
.DEFAULT_GOAL: $(BINARY)
|
||||||
|
|
||||||
$(BINARY): clean
|
$(BINARY): clean
|
||||||
go build ${GOFLAGS} ${LDFLAGS} -o ${BINARY}
|
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build ${GOFLAGS} ${LDFLAGS} -o ${BINARY}
|
||||||
|
|
||||||
install:
|
install:
|
||||||
go install ${GOFLAGS} ${LDFLAGS} -o ${BINARY}
|
go install ${GOFLAGS} ${LDFLAGS} -o ${BINARY}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
* Install
|
* Install dependencies
|
||||||
|
|
||||||
- =make deps=
|
- =make deps=
|
||||||
|
|
||||||
@@ -10,6 +10,13 @@
|
|||||||
|
|
||||||
- =make run=
|
- =make run=
|
||||||
|
|
||||||
|
* Build container image (must have =buildah= installed)
|
||||||
|
|
||||||
|
- =buildah unshare=
|
||||||
|
- =./build.sh=
|
||||||
|
|
||||||
|
*NOTE*: Not sure about "system metrics" when run in container...
|
||||||
|
|
||||||
* Usage
|
* Usage
|
||||||
|
|
||||||
Use =metrorama -h= to see a list of flags and options.
|
Use =metrorama -h= to see a list of flags and options.
|
||||||
|
|||||||
24
build.sh
Executable file
24
build.sh
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Remember to run buildah unshare before launching this
|
||||||
|
# Build container
|
||||||
|
echo "Start creating first stage "
|
||||||
|
buildctrl=$(buildah from golang:1.15.2)
|
||||||
|
buildmnt=$(buildah mount $buildctrl)
|
||||||
|
|
||||||
|
buildah copy $buildctrl *.go .
|
||||||
|
buildah copy $buildctrl Makefile .
|
||||||
|
buildah run $buildctrl make deps
|
||||||
|
buildah run $buildctrl make
|
||||||
|
|
||||||
|
# Runtime container
|
||||||
|
echo "Start creating second stage"
|
||||||
|
buildctrl2=$(buildah from alpine:latest)
|
||||||
|
buildmnt2=$(buildah mount $builctrl2)
|
||||||
|
|
||||||
|
buildah copy $buildctrl2 $buildmnt/go/metrorama
|
||||||
|
buildah config --cmd ./metrorama $buildctrl2
|
||||||
|
buildah config --port 8080 $buildctrl2
|
||||||
|
buildah unmount $buildctrl
|
||||||
|
buildah unmount $buildctrl2
|
||||||
|
buildah commit $buildctrl2 metrorama:latest
|
||||||
|
buildah rm $buildctrl $buildctrl2
|
||||||
50
main.go
50
main.go
@@ -86,7 +86,7 @@ func (t *Test) collectData() {
|
|||||||
return // really needed?
|
return // really needed?
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Test) startTest(w http.ResponseWriter, req *http.Request) {
|
func (t *Test) Start(w http.ResponseWriter, req *http.Request) {
|
||||||
// Check if another collectData is running (better: check sync.Mutex)
|
// Check if another collectData is running (better: check sync.Mutex)
|
||||||
if t.Status == "ONGOING" {
|
if t.Status == "ONGOING" {
|
||||||
http.Error(w, "Another test is currently ongoing, stop or try again later", 409) //better error code
|
http.Error(w, "Another test is currently ongoing, stop or try again later", 409) //better error code
|
||||||
@@ -144,7 +144,7 @@ func (t *Test) startTest(w http.ResponseWriter, req *http.Request) {
|
|||||||
go t.collectData()
|
go t.collectData()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Test) stopTest(w http.ResponseWriter, req *http.Request) {
|
func (t *Test) Stop(w http.ResponseWriter, req *http.Request) {
|
||||||
if t.Status != "ONGOING" {
|
if t.Status != "ONGOING" {
|
||||||
e := fmt.Sprintf("Cannot stop test in %s status: try start first", t.Status)
|
e := fmt.Sprintf("Cannot stop test in %s status: try start first", t.Status)
|
||||||
http.Error(w, e, 400)
|
http.Error(w, e, 400)
|
||||||
@@ -157,18 +157,54 @@ func (t *Test) stopTest(w http.ResponseWriter, req *http.Request) {
|
|||||||
fmt.Fprintf(w, "%v\n", t.Result)
|
fmt.Fprintf(w, "%v\n", t.Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Test) testStatus(w http.ResponseWriter, req *http.Request) {
|
func (t *Test) getStatus(w http.ResponseWriter, req *http.Request) {
|
||||||
|
qStream := req.URL.Query().Get("stream")
|
||||||
qFormat := req.URL.Query().Get("format")
|
qFormat := req.URL.Query().Get("format")
|
||||||
|
|
||||||
|
flusher, ok := w.(http.Flusher)
|
||||||
|
if !ok {
|
||||||
|
if qStream == "true" {
|
||||||
|
log.Printf("Http server does not support flusher\n")
|
||||||
|
http.Error(w, "Server does not support flusher",
|
||||||
|
http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if qStream == "true" {
|
||||||
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
}
|
||||||
|
|
||||||
if qFormat == "json" {
|
if qFormat == "json" {
|
||||||
|
for {
|
||||||
b, err := json.Marshal(t.Result)
|
b, err := json.Marshal(t.Result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Cannot marshal json: %s\n", err)
|
log.Printf("Cannot marshal json: %s\n", err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "%s\n", b)
|
fmt.Fprintf(w, "%s\n", b)
|
||||||
|
|
||||||
|
if qStream == "true" {
|
||||||
|
time.Sleep(time.Duration(t.Rate) * time.Millisecond)
|
||||||
|
flusher.Flush()
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, "%v\n", t.Result)
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for {
|
||||||
|
fmt.Fprintf(w, "%+ v\n", t.Result)
|
||||||
|
if qStream == "true" {
|
||||||
|
time.Sleep(time.Duration(t.Rate) * time.Millisecond)
|
||||||
|
flusher.Flush()
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
fmt.Fprintf(w, "Test %s, status: %s\n", t.Name, t.Status)
|
fmt.Fprintf(w, "Test %s, status: %s\n", t.Name, t.Status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -193,9 +229,9 @@ func main() {
|
|||||||
t := newTest()
|
t := newTest()
|
||||||
log.Printf("%+v\n", t)
|
log.Printf("%+v\n", t)
|
||||||
|
|
||||||
http.HandleFunc("/start", t.startTest)
|
http.HandleFunc("/start", t.Start)
|
||||||
http.HandleFunc("/status", t.testStatus)
|
http.HandleFunc("/status", t.getStatus)
|
||||||
http.HandleFunc("/stop", t.stopTest)
|
http.HandleFunc("/stop", t.Stop)
|
||||||
|
|
||||||
log.Printf("Listening on %s\n", *listen)
|
log.Printf("Listening on %s\n", *listen)
|
||||||
log.Fatal(http.ListenAndServe(*listen, nil))
|
log.Fatal(http.ListenAndServe(*listen, nil))
|
||||||
|
|||||||
Reference in New Issue
Block a user