David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "encoding/json" |
| 6 | "github.com/gorilla/mux" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 7 | "net/http" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type RequestInfo struct { |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 12 | Id string `json:"id"` |
| 13 | Name string `json:"name"` |
| 14 | Ip string `json:"ip"` |
| 15 | Mac string `json:"mac"` |
| 16 | RoleSelector string `json:"role_selector"` |
| 17 | Role string `json:"role"` |
| 18 | Script string `json:"script"` |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | func (c *Context) GetRole(info *RequestInfo) (string, error) { |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 22 | if info.Role != "" { |
| 23 | return info.Role, nil |
| 24 | } else if c.config.RoleSelectorURL == "" && info.RoleSelector == "" { |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 25 | return c.config.DefaultRole, nil |
| 26 | } |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 27 | selector := c.config.RoleSelectorURL |
| 28 | if info.RoleSelector != "" { |
| 29 | selector = info.RoleSelector |
| 30 | } |
| 31 | |
| 32 | r, err := http.Get(selector) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 33 | if err != nil { |
| 34 | return "", err |
| 35 | } |
| 36 | |
| 37 | s := bufio.NewScanner(r.Body) |
| 38 | defer r.Body.Close() |
| 39 | role := strings.TrimSpace(s.Text()) |
| 40 | if role == "" { |
| 41 | return c.config.DefaultRole, nil |
| 42 | } |
| 43 | return role, nil |
| 44 | } |
| 45 | |
| 46 | func (c *Context) validateData(info *RequestInfo) bool { |
| 47 | if strings.TrimSpace(info.Id) == "" || |
| 48 | strings.TrimSpace(info.Name) == "" || |
| 49 | strings.TrimSpace(info.Ip) == "" || |
| 50 | strings.TrimSpace(info.Mac) == "" { |
| 51 | return false |
| 52 | } |
| 53 | return true |
| 54 | } |
| 55 | |
| 56 | func (c *Context) ProvisionRequestHandler(w http.ResponseWriter, r *http.Request) { |
| 57 | var info RequestInfo |
| 58 | decoder := json.NewDecoder(r.Body) |
| 59 | defer r.Body.Close() |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 60 | if err := decoder.Decode(&info); err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame^] | 61 | log.Errorf("Unable to decode request to provision : %s", err) |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 62 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 63 | return |
| 64 | } |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 65 | |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 66 | if !c.validateData(&info) { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame^] | 67 | log.Errorf("Provisioning request not valid for '%s'", info.Name) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 68 | w.WriteHeader(http.StatusBadRequest) |
| 69 | return |
| 70 | } |
| 71 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 72 | role, err := c.GetRole(&info) |
| 73 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame^] | 74 | log.Errorf("unable to get provisioning role for node '%s' : %s", info.Name, err) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 75 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 76 | return |
| 77 | } |
David K. Bainbridge | be58a0d | 2016-06-22 15:43:02 -0700 | [diff] [blame] | 78 | |
| 79 | // If the request has a script set, override the default configuration |
| 80 | script := c.config.Script |
| 81 | if info.Script != "" { |
| 82 | script = info.Script |
| 83 | } |
| 84 | err = c.dispatcher.Dispatch(&info, role, script) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 85 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame^] | 86 | log.Errorf("unable to dispatch provisioning request for node '%s' : %s", info.Name, err) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 87 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | w.WriteHeader(http.StatusAccepted) |
| 92 | } |
| 93 | |
| 94 | func (c *Context) ListRequestsHandler(w http.ResponseWriter, r *http.Request) { |
| 95 | list, err := c.storage.List() |
| 96 | bytes, err := json.Marshal(list) |
| 97 | if err != nil { |
| 98 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 99 | return |
| 100 | } |
| 101 | w.Write(bytes) |
| 102 | } |
| 103 | |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 104 | func (c *Context) DeleteStatusHandler(w http.ResponseWriter, r *http.Request) { |
| 105 | vars := mux.Vars(r) |
| 106 | id, ok := vars["nodeid"] |
| 107 | if !ok || strings.TrimSpace(id) == "" { |
| 108 | w.WriteHeader(http.StatusBadRequest) |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | err := c.storage.Delete(id) |
| 113 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame^] | 114 | log.Errorf("Error while deleting status fo '%s' from storage : %s", id, err) |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 115 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 116 | return |
| 117 | } |
| 118 | |
| 119 | w.WriteHeader(http.StatusOK) |
| 120 | } |
| 121 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 122 | func (c *Context) QueryStatusHandler(w http.ResponseWriter, r *http.Request) { |
| 123 | vars := mux.Vars(r) |
| 124 | id, ok := vars["nodeid"] |
| 125 | if !ok || strings.TrimSpace(id) == "" { |
| 126 | w.WriteHeader(http.StatusBadRequest) |
| 127 | return |
| 128 | } |
| 129 | s, err := c.storage.Get(id) |
| 130 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame^] | 131 | log.Errorf("Error while retrieving status for '%s' from strorage : %s", id, err) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 132 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 133 | return |
| 134 | } |
| 135 | if s == nil { |
| 136 | w.WriteHeader(http.StatusNotFound) |
| 137 | return |
| 138 | } |
| 139 | bytes, err := json.Marshal(s) |
| 140 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame^] | 141 | log.Errorf("Error while attempting to marshal status for '%s' from storage : %s", id, err) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 142 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 143 | return |
| 144 | } |
David K. Bainbridge | 8352c59 | 2016-06-02 12:48:37 -0700 | [diff] [blame] | 145 | |
| 146 | switch s.Status { |
| 147 | case Pending, Running: |
| 148 | w.WriteHeader(http.StatusAccepted) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 149 | case Failed, Complete: |
David K. Bainbridge | 8352c59 | 2016-06-02 12:48:37 -0700 | [diff] [blame] | 150 | w.WriteHeader(http.StatusOK) |
| 151 | default: |
| 152 | w.WriteHeader(http.StatusInternalServerError) |
| 153 | } |
| 154 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 155 | w.Write(bytes) |
| 156 | } |