Skip to content

Commit

Permalink
Merge pull request #541 from convox/docker-logs-tail
Browse files Browse the repository at this point in the history
Logs Delivery and Tail Reliability
  • Loading branch information
Noah Zoschke committed Apr 19, 2016
2 parents 5893671 + 148a28f commit 7b0286f
Show file tree
Hide file tree
Showing 29 changed files with 273 additions and 329 deletions.
39 changes: 24 additions & 15 deletions api/controllers/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"os"
"sort"
"strings"
"time"

"github.com/convox/rack/api/httperr"
"github.com/convox/rack/api/models"
"github.com/convox/rack/api/provider"
"github.com/convox/rack/api/structs"
"github.com/gorilla/mux"
"golang.org/x/net/websocket"
)
Expand Down Expand Up @@ -107,27 +110,33 @@ func AppDelete(rw http.ResponseWriter, r *http.Request) *httperr.Error {

func AppLogs(ws *websocket.Conn) *httperr.Error {
app := mux.Vars(ws.Request())["app"]
header := ws.Request().Header

a, err := models.GetApp(app)
var err error

if awsError(err) == "ValidationError" {
return httperr.Errorf(404, "no such app: %s", app)
follow := true
if header.Get("Follow") == "false" {
follow = false
}

if err != nil {
return httperr.Server(err)
since := 2 * time.Minute
if s := header.Get("Since"); s != "" {
since, err = time.ParseDuration(s)
if err != nil {
return httperr.Errorf(403, "Invalid duration %s", s)
}
}

logs := make(chan []byte)
done := make(chan bool)

a.SubscribeLogs(logs, done)

go signalWsClose(ws, done)

for data := range logs {
ws.Write(data)
err = provider.LogStream(app, ws, structs.LogStreamOptions{
Filter: header.Get("Filter"),
Follow: follow,
Since: since,
})
if err != nil {
if strings.HasSuffix(err.Error(), "write: broken pipe") {
return nil
}
return httperr.Server(err)
}

return nil
}
1 change: 0 additions & 1 deletion api/controllers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func NewRouter() (router *mux.Router) {
router.Handle("/apps/{app}/processes/{process}/run", ws("process.run.attach", ProcessRunAttached)).Methods("GET")
router.Handle("/instances/{id}/ssh", ws("instance.ssh", InstanceSSH)).Methods("GET")
router.Handle("/proxy/{host}/{port}", ws("proxy", Proxy)).Methods("GET")
router.Handle("/services/{service}/logs", ws("service.logs", ServiceLogs)).Methods("GET")

// utility
router.HandleFunc("/boom", UtilityBoom).Methods("GET")
Expand Down
24 changes: 0 additions & 24 deletions api/controllers/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/convox/rack/api/httperr"
"github.com/convox/rack/api/models"
"github.com/gorilla/mux"
"golang.org/x/net/websocket"
)

func ServiceList(rw http.ResponseWriter, r *http.Request) *httperr.Error {
Expand Down Expand Up @@ -169,29 +168,6 @@ func ServiceUpdate(rw http.ResponseWriter, r *http.Request) *httperr.Error {
return RenderJson(rw, s)
}

func ServiceLogs(ws *websocket.Conn) *httperr.Error {
service := mux.Vars(ws.Request())["service"]

s, err := models.GetService(service)

if err != nil {
return httperr.Server(err)
}

logs := make(chan []byte)
done := make(chan bool)

s.SubscribeLogs(logs, done)

go signalWsClose(ws, done)

for data := range logs {
ws.Write(data)
}

return nil
}

func convoxifyCloudformationError(msg string) string {
newMsg := strings.Replace(msg, "do not exist in the template", "are not supported by this service", 1)
newMsg = strings.Replace(newMsg, "Parameters:", "Options:", 1)
Expand Down
2 changes: 1 addition & 1 deletion api/dist/kernel.json
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@
{ "Fn::Join": [ "", [ "echo \"", { "Ref": "AWS::Region" }, "\" > /etc/convox/region" ] ] },
{ "Fn::Join": [ "", [ "echo \"", { "Ref": "Kinesis" }, "\" > /etc/convox/kinesis" ] ] },
{ "Fn::Join": [ "", [ "echo \"", { "Ref": "LogGroup" }, "\" > /etc/convox/log_group" ] ] },
"curl -s https://convox.s3.amazonaws.com/agent/0.66/convox.conf > /etc/init/convox.conf",
"curl -s https://convox.s3.amazonaws.com/agent/0.67/convox.conf > /etc/init/convox.conf",
"start convox",
{ "Ref": "InstanceBootCommand" },
{ "Fn::Join": [ " ", [ "/opt/aws/bin/cfn-init", "-s", { "Ref": "AWS::StackName" }, "-r", "Instances", "--region", {"Ref":"AWS::Region"} ] ] },
Expand Down
8 changes: 0 additions & 8 deletions api/models/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,6 @@ func (a *App) Formation() (string, error) {
return string(data), nil
}

// During the transition from Kinesis to CloudWatch Logs, apps might not have been
// re-deployed and provisioned a LogGroup.
// Conditionally fall back to reading from Kinesis in this case.
func (a *App) SubscribeLogs(output chan []byte, quit chan bool) error {
go subscribeKinesis(a.Outputs["Kinesis"], output, quit)
return nil
}

func (a *App) ForkRelease() (*Release, error) {
release, err := a.LatestRelease()

Expand Down
5 changes: 4 additions & 1 deletion api/models/fixtures/command_exec_form.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,16 @@
"cmd2"
],
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "main"
"PROCESS": "main",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
5 changes: 4 additions & 1 deletion api/models/fixtures/command_string_form.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,16 @@
{
"Command": "cmd1 cmd2",
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "main"
"PROCESS": "main",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
5 changes: 4 additions & 1 deletion api/models/fixtures/complex_environment.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@
{
"Command": "bin/eat",
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"DRINK": "top \"o\" the mornin",
"FOOD": "pb\u0026j",
"KINESIS": {
Expand All @@ -317,7 +319,8 @@
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "main"
"PROCESS": "main",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
10 changes: 8 additions & 2 deletions api/models/fixtures/multi_balancer.json
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,16 @@
{
"Command": "bash -c 'bundle exec puma -C config/puma.rb'",
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "web"
"PROCESS": "web",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down Expand Up @@ -1056,13 +1059,16 @@
{
"Command": "bash -c \"bundle exec worker\"",
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "worker"
"PROCESS": "worker",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
10 changes: 8 additions & 2 deletions api/models/fixtures/multi_balancer_unbound.json
Original file line number Diff line number Diff line change
Expand Up @@ -933,13 +933,16 @@
{
"Command": "bash -c 'bundle exec puma -C config/puma.rb'",
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "web"
"PROCESS": "web",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down Expand Up @@ -1080,13 +1083,16 @@
{
"Command": "bash -c \"bundle exec worker\"",
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "worker"
"PROCESS": "worker",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
5 changes: 4 additions & 1 deletion api/models/fixtures/web_external_internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,16 @@
"BlankWebService",
{
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "web"
"PROCESS": "web",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
5 changes: 4 additions & 1 deletion api/models/fixtures/web_external_internal_unbound.json
Original file line number Diff line number Diff line change
Expand Up @@ -857,13 +857,16 @@
"BlankWebService",
{
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "web"
"PROCESS": "web",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
10 changes: 8 additions & 2 deletions api/models/fixtures/web_postgis.json
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@
"BlankPostgresService",
{
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
Expand All @@ -735,7 +737,8 @@
},
"POSTGRES_PASSWORD": "password",
"POSTGRES_USERNAME": "postgres",
"PROCESS": "postgres"
"PROCESS": "postgres",
"RACK": "convox-test"
},
"Image": "mdillon/postgis",
"Memory": {
Expand Down Expand Up @@ -955,13 +958,16 @@
"BlankWebService",
{
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "web"
"PROCESS": "web",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
10 changes: 8 additions & 2 deletions api/models/fixtures/web_postgis_internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@
"BlankPostgresService",
{
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
Expand All @@ -587,7 +589,8 @@
},
"POSTGRES_PASSWORD": "password",
"POSTGRES_USERNAME": "postgres",
"PROCESS": "postgres"
"PROCESS": "postgres",
"RACK": "convox-test"
},
"Image": "mdillon/postgis",
"Memory": {
Expand Down Expand Up @@ -795,13 +798,16 @@
"BlankWebService",
{
"Environment": {
"APP": "httpd",
"AWS_REGION": "us-test-2",
"KINESIS": {
"Ref": "Kinesis"
},
"LOG_GROUP": {
"Ref": "LogGroup"
},
"PROCESS": "web"
"PROCESS": "web",
"RACK": "convox-test"
},
"Image": "",
"Memory": {
Expand Down
Loading

0 comments on commit 7b0286f

Please sign in to comment.