Compare commits

...

2 commits

Author SHA1 Message Date
08097035e4 tener endpoint y cositas 2023-05-22 12:17:20 -03:00
9a457d3258 correr openrc paralelo 2023-05-22 12:17:04 -03:00
3 changed files with 53 additions and 28 deletions

3
.gitignore vendored
View file

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

View file

@ -10,6 +10,9 @@ 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,29 +22,50 @@ 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.GET("/", hello) e.Logger.Fatal(e.Start(":8080"))
runVM()
// e.Logger.Fatal(e.Start(":8080"))
} }
func hello(c echo.Context) error { type runResp struct {
return c.String(http.StatusOK, "Hello, World!") VmId string
} }
func runVM() { func run(c echo.Context) error {
const socketPath = "/tmp/firecracker.sock" script, err := ioutil.ReadAll(c.Request().Body)
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,
@ -91,8 +112,6 @@ func runVM() {
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))
} }
@ -100,7 +119,7 @@ func runVM() {
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}
@ -108,17 +127,18 @@ func runVM() {
log.Panic(err) log.Panic(err)
} }
if err := agent.run("#!/bin/sh\necho hola mundo"); err != nil { go func() {
log.Println(err) ctx := context.Background()
} if err := m.Wait(ctx); err != nil {
panic(err)
}
os.Remove(cfg.SocketPath)
}()
// if err := m.Wait(ctx); err != nil {
// panic(err)
// }
if err := agent.run("#!/bin/sh\nreboot"); err != nil { return vmid, agent, m
log.Println(err)
}
if err := m.Wait(ctx); err != nil {
panic(err)
}
} }
type agentConfig struct { type agentConfig struct {
@ -135,8 +155,8 @@ func (a agentConfig) request() *http.Request {
return req return req
} }
func (a agentConfig) run(script string) error { func (a agentConfig) run(script []byte) error {
req, err := http.NewRequest("POST", "http://"+a.ip+":8080/run", bytes.NewBuffer([]byte(script))) req, err := http.NewRequest("POST", "http://"+a.ip+":8080/run", bytes.NewBuffer(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)
@ -145,7 +165,6 @@ func (a agentConfig) run(script string) 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
@ -155,6 +174,10 @@ func (a agentConfig) run(script string) 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,