-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef544db
commit 00ff399
Showing
10 changed files
with
234 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
load("@rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "ipxe", | ||
srcs = [ | ||
"handlers.go", | ||
"options.go", | ||
"routes.go", | ||
"server.go", | ||
], | ||
importpath = "toni.systems/goisoboot/pkg/ipxe", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_github_gorilla_mux//:mux", | ||
"@com_github_sirupsen_logrus//:logrus", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ipxe | ||
|
||
import ( | ||
"net/http" | ||
"text/template" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
var ( | ||
ipxeTemplate = `#!ipxe | ||
kernel http://{{ .serverIP }}/linux/{{ .name }}/vmlinuz ip=dhcp initrd=initrd | ||
initrd http://{{ .serverIP }}/linux/{{ .name }}/initrd | ||
boot` | ||
) | ||
|
||
func (s *server) IPXE(w http.ResponseWriter, r *http.Request) { | ||
tmpl, err := template.New("ipxe").Parse(ipxeTemplate) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
|
||
vars := s.getIPXEVars(r) | ||
|
||
err = tmpl.Execute(w, vars) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func (s *server) Kernel(w http.ResponseWriter, r *http.Request) { | ||
http.ServeFile(w, r, "./linux/"+mux.Vars(r)["name"]+"/vmlinuz") | ||
} | ||
|
||
func (s *server) Initrd(w http.ResponseWriter, r *http.Request) { | ||
http.ServeFile(w, r, "./linux/"+mux.Vars(r)["name"]+"/initrd") | ||
} | ||
|
||
func (s *server) Squashfs(w http.ResponseWriter, r *http.Request) { | ||
http.ServeFile(w, r, "./linux/"+mux.Vars(r)["name"]+"/squashfs") | ||
} | ||
|
||
func (s *server) getIPXEVars(r *http.Request) map[string]string { | ||
name := "example" | ||
|
||
vars := make(map[string]string) | ||
|
||
vars["ip"] = r.RemoteAddr | ||
vars["serverIP"] = s.ip | ||
vars["name"] = name | ||
|
||
return vars | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ipxe | ||
|
||
type Option func(*server) | ||
|
||
func WithPort(port int) Option { | ||
return func(s *server) { | ||
s.port = port | ||
} | ||
} | ||
|
||
func WithIP(ip string) Option { | ||
return func(s *server) { | ||
s.ip = ip | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ipxe | ||
|
||
import ( | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func router(server Server) *mux.Router { | ||
r := mux.NewRouter() | ||
r.HandleFunc("/ipxe", server.IPXE).Methods("GET") | ||
r.HandleFunc("/linux/{name}/vmlinuz", server.Kernel).Methods("GET") | ||
r.HandleFunc("/linux/{name}/initrd", server.Initrd).Methods("GET") | ||
r.HandleFunc("/linux/{name}/squashfs", server.Squashfs).Methods("GET") | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package ipxe | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
ErrMissingIP = errors.New("missing IP") | ||
ErrMissingPort = errors.New("missing port") | ||
|
||
dhcpTemplate = ` | ||
if exists user-class and option user-class = "iPXE" { | ||
filename "http://{{ .serverIP }}/ipxe"; | ||
} else { | ||
filename "ipxe.efi"; | ||
}` | ||
) | ||
|
||
type Server interface { | ||
Run() error | ||
IPXE(w http.ResponseWriter, r *http.Request) | ||
Kernel(w http.ResponseWriter, r *http.Request) | ||
Initrd(w http.ResponseWriter, r *http.Request) | ||
Squashfs(w http.ResponseWriter, r *http.Request) | ||
} | ||
|
||
type server struct { | ||
ip string | ||
port int | ||
router *mux.Router | ||
} | ||
|
||
func New(options ...Option) (Server, error) { | ||
s := &server{} | ||
|
||
s.router = router(s) | ||
|
||
for _, o := range options { | ||
o(s) | ||
} | ||
|
||
if s.ip == "" { | ||
return nil, ErrMissingIP | ||
} | ||
if s.port == 0 { | ||
return nil, ErrMissingPort | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *server) Run() error { | ||
addr := fmt.Sprintf(":%d", s.port) | ||
|
||
srv := &http.Server{ | ||
Addr: addr, | ||
WriteTimeout: time.Second * 15, | ||
ReadTimeout: time.Second * 15, | ||
IdleTimeout: time.Second * 60, | ||
Handler: s.router, | ||
} | ||
|
||
log.Infof("Starting HTTP server on %s", addr) | ||
s.printInfo() | ||
|
||
return srv.ListenAndServe() | ||
} | ||
|
||
func (s *server) printInfo() { | ||
log.Infof("Setup ISC DHCP Server with the following configuration:") | ||
tmpl, _ := template.New("dhcp").Parse(dhcpTemplate) | ||
var buff bytes.Buffer | ||
_ = tmpl.Execute(&buff, map[string]string{"serverIP": s.ip}) | ||
|
||
log.Infof(buff.String()) | ||
} |