| 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 "errors" | 8 "errors" |
| 9 "fmt" | 9 "fmt" |
| 10 "net/http" | 10 "net/http" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 return server, nil | 42 return server, nil |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 return "", errUnrecognizedHost | 45 return "", errUnrecognizedHost |
| 46 } | 46 } |
| 47 | 47 |
| 48 // Build is for deciphering recipe builds from swarming based off of logs. | 48 // Build is for deciphering recipe builds from swarming based off of logs. |
| 49 type Build struct { | 49 type Build struct { |
| 50 // bl is the buildLoader to use. A zero value is suitable for production
, but | 50 // bl is the buildLoader to use. A zero value is suitable for production
, but |
| 51 // this can be overridden for testing. | 51 // this can be overridden for testing. |
| 52 » bl buildLoader | 52 » bl BuildLoader |
| 53 } | 53 } |
| 54 | 54 |
| 55 // LogHandler writes the build log to the given response writer. | 55 // LogHandler writes the build log to the given response writer. |
| 56 func LogHandler(c *router.Context) { | 56 func LogHandler(c *router.Context) { |
| 57 id := c.Params.ByName("id") | 57 id := c.Params.ByName("id") |
| 58 if id == "" { | 58 if id == "" { |
| 59 common.ErrorPage(c, http.StatusBadRequest, "no id") | 59 common.ErrorPage(c, http.StatusBadRequest, "no id") |
| 60 return | 60 return |
| 61 } | 61 } |
| 62 logname := c.Params.ByName("logname") | 62 logname := c.Params.ByName("logname") |
| 63 if logname == "" { | 63 if logname == "" { |
| 64 common.ErrorPage(c, http.StatusBadRequest, "no log name") | 64 common.ErrorPage(c, http.StatusBadRequest, "no log name") |
| 65 return | 65 return |
| 66 } | 66 } |
| 67 | 67 |
| 68 hostname, err := getSwarmingHost(c.Context, c.Request) | 68 hostname, err := getSwarmingHost(c.Context, c.Request) |
| 69 if err != nil { | 69 if err != nil { |
| 70 common.ErrorPage(c, http.StatusBadRequest, | 70 common.ErrorPage(c, http.StatusBadRequest, |
| 71 fmt.Sprintf("no swarming host: %s", err.Error())) | 71 fmt.Sprintf("no swarming host: %s", err.Error())) |
| 72 return | 72 return |
| 73 } | 73 } |
| 74 » sf, err := newProdService(c.Context, hostname) | 74 » sf, err := NewProdService(c.Context, hostname) |
| 75 if err != nil { | 75 if err != nil { |
| 76 common.ErrorPage(c, errCode(err), err.Error()) | 76 common.ErrorPage(c, errCode(err), err.Error()) |
| 77 return | 77 return |
| 78 } | 78 } |
| 79 | 79 |
| 80 log, closed, err := swarmingBuildLogImpl(c.Context, sf, id, logname) | 80 log, closed, err := swarmingBuildLogImpl(c.Context, sf, id, logname) |
| 81 if err != nil { | 81 if err != nil { |
| 82 common.ErrorPage(c, errCode(err), err.Error()) | 82 common.ErrorPage(c, errCode(err), err.Error()) |
| 83 return | 83 return |
| 84 } | 84 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 common.ErrorPage(c, http.StatusBadRequest, "no id") | 101 common.ErrorPage(c, http.StatusBadRequest, "no id") |
| 102 return | 102 return |
| 103 } | 103 } |
| 104 | 104 |
| 105 hostname, err := getSwarmingHost(c.Context, c.Request) | 105 hostname, err := getSwarmingHost(c.Context, c.Request) |
| 106 if err != nil { | 106 if err != nil { |
| 107 common.ErrorPage(c, http.StatusBadRequest, | 107 common.ErrorPage(c, http.StatusBadRequest, |
| 108 fmt.Sprintf("no swarming host: %s", err.Error())) | 108 fmt.Sprintf("no swarming host: %s", err.Error())) |
| 109 return | 109 return |
| 110 } | 110 } |
| 111 » sf, err := newProdService(c.Context, hostname) | 111 » sf, err := NewProdService(c.Context, hostname) |
| 112 if err != nil { | 112 if err != nil { |
| 113 common.ErrorPage(c, errCode(err), err.Error()) | 113 common.ErrorPage(c, errCode(err), err.Error()) |
| 114 return | 114 return |
| 115 } | 115 } |
| 116 | 116 |
| 117 » result, err := b.bl.swarmingBuildImpl(c.Context, sf, c.Request.URL.Strin
g(), id) | 117 » result, err := b.bl.SwarmingBuildImpl(c.Context, sf, c.Request.URL.Strin
g(), id) |
| 118 if err != nil { | 118 if err != nil { |
| 119 common.ErrorPage(c, errCode(err), err.Error()) | 119 common.ErrorPage(c, errCode(err), err.Error()) |
| 120 return | 120 return |
| 121 } | 121 } |
| 122 | 122 |
| 123 templates.MustRender(c.Context, c.Writer, "pages/build.html", templates.
Args{ | 123 templates.MustRender(c.Context, c.Writer, "pages/build.html", templates.
Args{ |
| 124 "Build": result, | 124 "Build": result, |
| 125 }) | 125 }) |
| 126 } | 126 } |
| 127 | 127 |
| 128 // errCode resolves recognized errors into proper http response codes. | 128 // errCode resolves recognized errors into proper http response codes. |
| 129 func errCode(err error) int { | 129 func errCode(err error) int { |
| 130 if err == errUnrecognizedHost { | 130 if err == errUnrecognizedHost { |
| 131 return http.StatusBadRequest | 131 return http.StatusBadRequest |
| 132 } | 132 } |
| 133 if isAPINotFound(err) || os.IsNotExist(err) { | 133 if isAPINotFound(err) || os.IsNotExist(err) { |
| 134 return http.StatusNotFound | 134 return http.StatusNotFound |
| 135 } | 135 } |
| 136 return http.StatusInternalServerError | 136 return http.StatusInternalServerError |
| 137 } | 137 } |
| 138 | 138 |
| 139 // isAPINotFound returns true if err is a HTTP 404 API response. | 139 // isAPINotFound returns true if err is a HTTP 404 API response. |
| 140 func isAPINotFound(err error) bool { | 140 func isAPINotFound(err error) bool { |
| 141 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code == http.Statu
sNotFound { | 141 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code == http.Statu
sNotFound { |
| 142 return true | 142 return true |
| 143 } | 143 } |
| 144 return false | 144 return false |
| 145 } | 145 } |
| OLD | NEW |