Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
feat: print hostname and running container count after deploy
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Tiare Le Bigot <jean-tiare.le-bigot@ovh.net>
  • Loading branch information
Jean-Tiare Le Bigot committed Sep 29, 2015
1 parent 351f739 commit cdb04a9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 65 deletions.
20 changes: 12 additions & 8 deletions compose/up.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compose

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -51,15 +52,18 @@ func cmdUp(cmd *cobra.Command, args []string) {

// Execute request
path := fmt.Sprintf("/applications/%s/fig/up?stream", ns)
reader, _, err := internal.Stream("POST", path, payload,
internal.SetHeader("Content-Type", "application/x-yaml"))
if err != nil {
internal.Exit("Error: %s\n", err)
}
buffer, _, err := internal.Stream("POST", path, payload, internal.SetHeader("Content-Type", "application/x-yaml"))
internal.Check(err)

// Display api stream
err = internal.DisplayStream(reader)
if err != nil {
internal.Exit("Error: %s\n", err)
line, err := internal.DisplayStream(buffer)
internal.Check(err)
if line != nil {
var data map[string]interface{}
err = json.Unmarshal(line, &data)
internal.Check(err)

fmt.Printf("Hostname: %v\n", data["hostname"])
fmt.Printf("Running containers: %v/%v\n", data["container_number"], data["container_target"])
}
}
27 changes: 15 additions & 12 deletions internal/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
)
Expand Down Expand Up @@ -187,32 +186,33 @@ func doRequest(method string, path string, args []byte, mods ...RequestModifier)
return resp.Body, resp.StatusCode, nil
}

// DisplayStream decode each line from http buffer and print either message or error
func DisplayStream(buffer io.ReadCloser) error {
// DisplayStream decode each line from http buffer and print either message or error. Return last read line
func DisplayStream(buffer io.ReadCloser) ([]byte, error) {
reader := bufio.NewReader(buffer)

for {
line, err := reader.ReadBytes('\n')
if Verbose {
fmt.Fprintf(os.Stderr, "Received message: %v\n", string(line))
}

// Progress message
m := DecodeMessage(line)
if m != nil {
fmt.Fprintln(os.Stderr, m.Message)
continue
}

// Error message (will be last message)
e := DecodeError(line)
if e != nil {
return e
}
if err != nil && err == io.EOF {
return nil
}
if err != nil {
return err
fmt.Fprintf(os.Stderr, "Error: %s\n", e.Message)
continue
}
if string(line) != "" {
log.Print(string(line))

// Final message
if err == io.EOF {
return line, nil
}
}
}
Expand All @@ -231,6 +231,9 @@ func GetListApplications(args []string) []string {
// Check checks e and panic if not nil
func Check(err error) {
if err != nil {
if Verbose {
panic(err)
}
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
Expand Down
41 changes: 8 additions & 33 deletions service/add.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package service

import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -250,39 +248,16 @@ func serviceAdd(args Add) {
return
}

reader := bufio.NewReader(buffer)

for {
line, err := reader.ReadBytes('\n')
m := internal.DecodeMessage(line)
if m != nil {
fmt.Fprintln(os.Stderr, m.Message)
}
e := internal.DecodeError(line)
if e != nil {
fmt.Fprintln(os.Stderr, e)
if e.Code == 409 && cmdAddRedeploy {
if internal.Format == "pretty" {
fmt.Fprintf(os.Stderr, "Starting redeploy...\n")
}
ensureMode(args)
return
}
os.Exit(1)
}
if err != nil && err == io.EOF {
break
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
line, err := internal.DisplayStream(buffer)
internal.Check(err)
if line != nil {
var data map[string]interface{}
err = json.Unmarshal(line, &data)
internal.Check(err)

if internal.Format == "pretty" {
fmt.Fprintf(os.Stderr, "Starting service %s/%s...\n", args.Application, args.Service)
fmt.Printf("Hostname: %v\n", data["hostname"])
fmt.Printf("Running containers: %v/%v\n", data["container_number"], data["container_target"])
}
serviceStart(args.Application, args.Service, true)
}

func ensureMode(args Add) {
Expand Down
14 changes: 9 additions & 5 deletions service/redeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func serviceRedeploy(args Redeploy) {
}
// Load Pool
args.Pool = redeployPool
fmt.Fprintf(os.Stderr, "Pool: %s\n", args.Pool)

// Parse ContainerPorts
args.ContainerPorts = parsePublishedPort(redeployPublished)
Expand All @@ -173,10 +172,15 @@ func serviceRedeploy(args Redeploy) {
os.Exit(1)
}

err = internal.DisplayStream(buffer)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
line, err := internal.DisplayStream(buffer)
internal.Check(err)
if line != nil {
var data map[string]interface{}
err = json.Unmarshal(line, &data)
internal.Check(err)

fmt.Printf("Hostname: %v\n", data["hostname"])
fmt.Printf("Running containers: %v/%v\n", data["container_number"], data["container_target"])
}

if !redeployBatch {
Expand Down
10 changes: 9 additions & 1 deletion service/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ func serviceScale(app string, service string, number int, destroy bool, batch bo
buffer, _, err := internal.Stream("POST", path, data)
internal.Check(err)

err = internal.DisplayStream(buffer)
line, err := internal.DisplayStream(buffer)
internal.Check(err)
if line != nil {
var data map[string]interface{}
err = json.Unmarshal(line, &data)
internal.Check(err)

fmt.Printf("Hostname: %v\n", data["hostname"])
fmt.Printf("Running containers: %v/%v\n", data["container_number"], data["container_target"])
}

if !batch {
internal.ExitAfterCtrlC()
Expand Down
16 changes: 11 additions & 5 deletions service/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"encoding/json"
"fmt"
"os"

Expand Down Expand Up @@ -53,13 +54,18 @@ func serviceStart(app string, service string, batch bool) {

path := fmt.Sprintf("/applications/%s/services/%s/start?stream", app, service)
buffer, _, err := internal.Stream("POST", path, []byte("{}"))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
internal.Check(err)

err = internal.DisplayStream(buffer)
line, err := internal.DisplayStream(buffer)
internal.Check(err)
if line != nil {
var data map[string]interface{}
err = json.Unmarshal(line, &data)
internal.Check(err)

fmt.Printf("Hostname: %v\n", data["hostname"])
fmt.Printf("Running containers: %v/%v\n", data["container_number"], data["container_target"])
}

if !batch {
internal.ExitAfterCtrlC()
Expand Down
2 changes: 1 addition & 1 deletion service/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ func serviceStop(app string, service string, batch bool) {
os.Exit(1)
}

err = internal.DisplayStream(buffer)
_, err = internal.DisplayStream(buffer)
internal.Check(err)
}

0 comments on commit cdb04a9

Please sign in to comment.