Compare commits

..

No commits in common. "08097035e4a430106b15f6cec7cc87b253d28cac" and "1a9b64d503a4c698e0f74c5d30b9ec258b708de3" have entirely different histories.

3 changed files with 28 additions and 53 deletions

3
.gitignore vendored
View file

@ -1,5 +1,6 @@
rootfs.ext4 rootfs.ext4
rootfs.qcow2 rootfs.qcow2
fireactions fireactions
*.ext4 *.ext4
vmlinux.bin vmlinux.bin

View file

@ -10,9 +10,6 @@ echo https://dl-cdn.alpinelinux.org/alpine/v3.18/community >> /rootfs/etc/apk/re
apk add --initdb --root /rootfs alpine-base dropbear util-linux dropbear-dbclient dhcpcd apk add --initdb --root /rootfs alpine-base dropbear util-linux dropbear-dbclient dhcpcd
" "
# gotta go fast
echo 'rc_parallel="YES"' >> "$dir"/etc/rc.conf
mkdir -p "$dir"/usr/local/sbin mkdir -p "$dir"/usr/local/sbin
go build -tags=netgo -o "$dir"/usr/local/sbin/fireactions-agent ./agent go build -tags=netgo -o "$dir"/usr/local/sbin/fireactions-agent ./agent
# https://github.com/OpenRC/openrc/blob/master/service-script-guide.md # https://github.com/OpenRC/openrc/blob/master/service-script-guide.md

75
main.go
View file

@ -22,50 +22,29 @@ import (
func main() { func main() {
e := echo.New() e := echo.New()
e.Use(middleware.Logger()) e.Use(middleware.Logger())
e.Use(middleware.Recover()) e.Use(middleware.Recover())
e.POST("/run", run)
e.Logger.Fatal(e.Start(":8080")) e.GET("/", hello)
runVM()
// e.Logger.Fatal(e.Start(":8080"))
} }
type runResp struct { func hello(c echo.Context) error {
VmId string return c.String(http.StatusOK, "Hello, World!")
} }
func run(c echo.Context) error { func runVM() {
script, err := ioutil.ReadAll(c.Request().Body) const socketPath = "/tmp/firecracker.sock"
if err != nil {
panic(err)
}
vmid, agent, m := startVM()
err = agent.run(script)
if err != nil {
return err
}
go func() {
ctx := context.Background()
if err := m.Wait(ctx); err != nil {
panic(err)
}
}()
defer agent.off()
return c.JSON(http.StatusOK, runResp{
VmId: vmid,
})
}
func startVM() (string, agentConfig, *firecracker.Machine) {
nanid, err := nanoid.Standard(21) nanid, err := nanoid.Standard(21)
if err != nil { if err != nil {
panic(err) panic(err)
} }
vmid := nanid()
secret := nanid() secret := nanid()
socketPath := "/tmp/firecracker-" + vmid + ".sock"
cfg := firecracker.Config{ cfg := firecracker.Config{
SocketPath: socketPath, SocketPath: socketPath,
@ -112,6 +91,8 @@ func startVM() (string, agentConfig, *firecracker.Machine) {
panic(fmt.Errorf("failed to create new machine: %v", err)) panic(fmt.Errorf("failed to create new machine: %v", err))
} }
defer os.Remove(cfg.SocketPath)
if err := m.Start(ctx); err != nil { if err := m.Start(ctx); err != nil {
panic(fmt.Errorf("failed to initialize machine: %v", err)) panic(fmt.Errorf("failed to initialize machine: %v", err))
} }
@ -119,7 +100,7 @@ func startVM() (string, agentConfig, *firecracker.Machine) {
ip := m.Cfg.NetworkInterfaces[0].StaticConfiguration.IPConfiguration.IPAddr.IP ip := m.Cfg.NetworkInterfaces[0].StaticConfiguration.IPConfiguration.IPAddr.IP
log.Printf("IP: %s", ip.String()) log.Printf("IP: %s", ip.String())
// defer m.StopVMM() defer m.StopVMM()
agent := agentConfig{ip: ip.String(), secret: secret} agent := agentConfig{ip: ip.String(), secret: secret}
@ -127,18 +108,17 @@ func startVM() (string, agentConfig, *firecracker.Machine) {
log.Panic(err) log.Panic(err)
} }
go func() { if err := agent.run("#!/bin/sh\necho hola mundo"); err != nil {
ctx := context.Background() log.Println(err)
if err := m.Wait(ctx); err != nil { }
panic(err)
}
os.Remove(cfg.SocketPath)
}()
// if err := m.Wait(ctx); err != nil {
// panic(err)
// }
return vmid, agent, m if err := agent.run("#!/bin/sh\nreboot"); err != nil {
log.Println(err)
}
if err := m.Wait(ctx); err != nil {
panic(err)
}
} }
type agentConfig struct { type agentConfig struct {
@ -155,8 +135,8 @@ func (a agentConfig) request() *http.Request {
return req return req
} }
func (a agentConfig) run(script []byte) error { func (a agentConfig) run(script string) error {
req, err := http.NewRequest("POST", "http://"+a.ip+":8080/run", bytes.NewBuffer(script)) req, err := http.NewRequest("POST", "http://"+a.ip+":8080/run", bytes.NewBuffer([]byte(script)))
req.Header.Set("Authorization", "Bearer "+a.secret) req.Header.Set("Authorization", "Bearer "+a.secret)
if err != nil { if err != nil {
panic(err) panic(err)
@ -165,6 +145,7 @@ func (a agentConfig) run(script []byte) error {
if err != nil { if err != nil {
return err return err
} }
log.Println(res.Body)
byt, err := ioutil.ReadAll(res.Body) byt, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return err return err
@ -174,10 +155,6 @@ func (a agentConfig) run(script []byte) error {
return nil return nil
} }
func (a agentConfig) off() error {
return a.run([]byte("#!/bin/sh\nreboot"))
}
func (a agentConfig) waitForAgent() error { func (a agentConfig) waitForAgent() error {
client := http.Client{ client := http.Client{
Timeout: time.Millisecond * 50, Timeout: time.Millisecond * 50,