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