| 1 | // Copyright 2018-2026 ThousandEyes Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package handlersscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/handlers`/ |
| 16 | |
| 17 | import ( |
| 18 | "bytesscip-go gomod github.com/golang/go/src go1.22 bytes/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.22 fmt/" |
| 20 | "net/httpscip-go gomod github.com/golang/go/src go1.22 `net/http`/" |
| 21 | |
| 22 | "github.com/thousandeyes/shoelaces/internal/ipxescip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/" |
| 23 | ) |
| 24 | |
| 25 | const menuHeaderscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/handlers`/menuHeader. = "#!ipxe\n" + |
| 26 | "menu Choose target to boot\n" |
| 27 | |
| 28 | const menuFooterscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/handlers`/menuFooter. = "\n" + |
| 29 | "choose target\n" + |
| 30 | "echo -n Enter hostname or none:\n" + |
| 31 | "read hostname\n" + |
| 32 | "set baseurl %s\n" + |
| 33 | "# Boot it as intended.\n" + |
| 34 | "chain ${target}\n" |
| 35 | |
| 36 | // IPXEMenu serves the ipxe menu with list of all available scripts |
| 37 | func IPXEMenuscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/handlers`/IPXEMenu().(w httpscip-go gomod github.com/golang/go/src go1.22 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.22 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.22 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.22 `net/http`/Request#) { |
| 38 | env := envFromRequestscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/handlers`/envFromRequest().(r) |
| 39 | |
| 40 | scripts := ipxescip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/.ScriptListscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/ScriptList().(env) |
| 41 | if len(scripts) == 0 { |
| 42 | httpscip-go gomod github.com/golang/go/src go1.22 `net/http`/.Errorscip-go gomod github.com/golang/go/src go1.22 `net/http`/Error().(w, "No Scripts Found", httpscip-go gomod github.com/golang/go/src go1.22 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.22 `net/http`/StatusInternalServerError.) |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | var bootItemsBuffer bytesscip-go gomod github.com/golang/go/src go1.22 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.22 bytes/Buffer# |
| 47 | //Creates the top portion of the iPXE menu |
| 48 | bootItemsBuffer.WriteStringscip-go gomod github.com/golang/go/src go1.22 bytes/Buffer#WriteString().(menuHeaderscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/handlers`/menuHeader.) |
| 49 | for _, s := range scripts { |
| 50 | //Formats the bootable scripts separated by newlines into a single string |
| 51 | var desc string |
| 52 | if len(s.Envscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/Script#Env.) > 0 { |
| 53 | desc = fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Sprintf().("%s [%s]", s.Namescip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/Script#Name., s.Envscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/Script#Env.) |
| 54 | } else { |
| 55 | desc = string(s.Namescip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/Script#Name.) |
| 56 | } |
| 57 | bootItem := fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Sprintf().("item %s%s %s\n", s.Pathscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/Script#Path., s.Namescip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/ipxe`/Script#Name., desc) |
| 58 | bootItemsBuffer.WriteStringscip-go gomod github.com/golang/go/src go1.22 bytes/Buffer#WriteString().(bootItem) |
| 59 | } |
| 60 | //Creates the bottom portion of the iPXE menu |
| 61 | bootItemsBuffer.WriteStringscip-go gomod github.com/golang/go/src go1.22 bytes/Buffer#WriteString().(fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Sprintf().(menuFooterscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/handlers`/menuFooter., env.BaseURLscip-go gomod github.com/thousandeyes/shoelaces . `github.com/thousandeyes/shoelaces/internal/environment`/Environment#BaseURL.)) |
| 62 | w.Writescip-go gomod github.com/golang/go/src go1.22 `net/http`/ResponseWriter#Write().(bootItemsBuffer.Bytesscip-go gomod github.com/golang/go/src go1.22 bytes/Buffer#Bytes().()) |
| 63 | } |