Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package swarming | 5 package swarming |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 "os" | 9 "os" |
| 10 | 10 |
| 11 "google.golang.org/api/googleapi" | 11 "google.golang.org/api/googleapi" |
| 12 | 12 |
| 13 "github.com/julienschmidt/httprouter" | 13 "github.com/julienschmidt/httprouter" |
| 14 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
| 15 | 15 |
| 16 "github.com/luci/luci-go/milo/appengine/settings" | 16 "github.com/luci/luci-go/milo/appengine/settings" |
| 17 "github.com/luci/luci-go/milo/common/miloerror" | 17 "github.com/luci/luci-go/milo/common/miloerror" |
| 18 "github.com/luci/luci-go/server/templates" | 18 "github.com/luci/luci-go/server/templates" |
| 19 ) | 19 ) |
| 20 | 20 |
| 21 func getServer(r *http.Request) string { | 21 const ( |
| 22 » defaultSwarmingServer = "chromium-swarm.appspot.com" | |
| 23 » defaultSwarmingDevServer = "chromium-swarm-dev.appspot.com" | |
| 24 ) | |
| 25 | |
| 26 func getSwarmingService(c context.Context, r *http.Request) (swarmingService, er ror) { | |
| 22 server := r.FormValue("server") | 27 server := r.FormValue("server") |
| 23 // TODO(hinoka): configure this mapping in luci-config | 28 // TODO(hinoka): configure this mapping in luci-config |
| 24 switch server { | 29 switch server { |
| 25 case "": | 30 case "": |
| 26 » » return "chromium-swarm.appspot.com" | 31 » » server = defaultSwarmingServer |
|
hinoka
2017/02/03 01:02:23
This is only used once, lets not do this. It's in
dnj
2017/02/03 01:04:56
The final CL in the chain uses it, too.
| |
| 27 case "dev": | 32 case "dev": |
| 28 » » return "chromium-swarm-dev.appspot.com" | 33 » » server = defaultSwarmingDevServer |
| 29 » default: | |
| 30 » » return server | |
| 31 } | 34 } |
| 35 return newProdService(c, server) | |
| 32 } | 36 } |
| 33 | 37 |
| 34 // Log is for fetching logs from swarming. | 38 // Log is for fetching logs from swarming. |
| 35 type Log struct{} | 39 type Log struct{} |
| 36 | 40 |
| 37 // Build is for deciphering recipe builds from swarming based off of logs. | 41 // Build is for deciphering recipe builds from swarming based off of logs. |
| 38 type Build struct{} | 42 type Build struct{} |
| 39 | 43 |
| 40 // GetTemplateName for Log returns the template name for log pages. | 44 // GetTemplateName for Log returns the template name for log pages. |
| 41 func (l Log) GetTemplateName(t settings.Theme) string { | 45 func (l Log) GetTemplateName(t settings.Theme) string { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 52 } | 56 } |
| 53 } | 57 } |
| 54 logname := p.ByName("logname") | 58 logname := p.ByName("logname") |
| 55 if logname == "" { | 59 if logname == "" { |
| 56 return nil, &miloerror.Error{ | 60 return nil, &miloerror.Error{ |
| 57 Message: "No log name", | 61 Message: "No log name", |
| 58 Code: http.StatusBadRequest, | 62 Code: http.StatusBadRequest, |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 » log, closed, err := swarmingBuildLogImpl(c, getServer(r), id, logname) | 66 » sf, err := getSwarmingService(c, r) |
| 63 if err != nil { | 67 if err != nil { |
| 64 return nil, convertErr(err) | 68 return nil, convertErr(err) |
| 65 } | 69 } |
| 70 | |
| 71 log, closed, err := swarmingBuildLogImpl(c, sf, id, logname) | |
| 72 if err != nil { | |
| 73 return nil, convertErr(err) | |
| 74 } | |
| 66 | 75 |
| 67 args := &templates.Args{ | 76 args := &templates.Args{ |
| 68 "Log": log, | 77 "Log": log, |
| 69 "Closed": closed, | 78 "Closed": closed, |
| 70 } | 79 } |
| 71 return args, nil | 80 return args, nil |
| 72 } | 81 } |
| 73 | 82 |
| 74 // GetTemplateName for Build returns the template name for build pages. | 83 // GetTemplateName for Build returns the template name for build pages. |
| 75 func (b Build) GetTemplateName(t settings.Theme) string { | 84 func (b Build) GetTemplateName(t settings.Theme) string { |
| 76 return "build.html" | 85 return "build.html" |
| 77 } | 86 } |
| 78 | 87 |
| 79 // Render renders both the build page and the log. | 88 // Render renders both the build page and the log. |
| 80 func (b Build) Render(c context.Context, r *http.Request, p httprouter.Params) ( *templates.Args, error) { | 89 func (b Build) Render(c context.Context, r *http.Request, p httprouter.Params) ( *templates.Args, error) { |
| 81 // Get the swarming ID | 90 // Get the swarming ID |
| 82 id := p.ByName("id") | 91 id := p.ByName("id") |
| 83 if id == "" { | 92 if id == "" { |
| 84 return nil, &miloerror.Error{ | 93 return nil, &miloerror.Error{ |
| 85 Message: "No id", | 94 Message: "No id", |
| 86 Code: http.StatusBadRequest, | 95 Code: http.StatusBadRequest, |
| 87 } | 96 } |
| 88 } | 97 } |
| 89 | 98 |
| 90 » result, err := swarmingBuildImpl(c, r.URL.String(), getServer(r), id) | 99 » sf, err := getSwarmingService(c, r) |
| 91 if err != nil { | 100 if err != nil { |
| 92 return nil, convertErr(err) | 101 return nil, convertErr(err) |
| 93 } | 102 } |
| 103 | |
| 104 result, err := swarmingBuildImpl(c, sf, r.URL.String(), id) | |
| 105 if err != nil { | |
| 106 return nil, convertErr(err) | |
| 107 } | |
| 94 | 108 |
| 95 // Render into the template | 109 // Render into the template |
| 96 args := &templates.Args{ | 110 args := &templates.Args{ |
| 97 "Build": result, | 111 "Build": result, |
| 98 } | 112 } |
| 99 return args, nil | 113 return args, nil |
| 100 } | 114 } |
| 101 | 115 |
| 102 func convertErr(err error) error { | 116 func convertErr(err error) error { |
| 103 if isAPINotFound(err) || os.IsNotExist(err) { | 117 if isAPINotFound(err) || os.IsNotExist(err) { |
| 104 return &miloerror.Error{ | 118 return &miloerror.Error{ |
| 105 Message: err.Error(), | 119 Message: err.Error(), |
| 106 Code: http.StatusNotFound, | 120 Code: http.StatusNotFound, |
| 107 } | 121 } |
| 108 } | 122 } |
| 109 return err | 123 return err |
| 110 } | 124 } |
| 111 | 125 |
| 112 // isAPINotFound returns true if err is a HTTP 404 API response. | 126 // isAPINotFound returns true if err is a HTTP 404 API response. |
| 113 func isAPINotFound(err error) bool { | 127 func isAPINotFound(err error) bool { |
| 114 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code == http.Statu sNotFound { | 128 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code == http.Statu sNotFound { |
| 115 return true | 129 return true |
| 116 } | 130 } |
| 117 | 131 |
| 118 return false | 132 return false |
| 119 } | 133 } |
| OLD | NEW |