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.qcow2
fireactions
*.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
"
# gotta go fast
echo 'rc_parallel="YES"' >> "$dir"/etc/rc.conf
mkdir -p "$dir"/usr/local/sbin
go build -tags=netgo -o "$dir"/usr/local/sbin/fireactions-agent ./agent
# https://github.com/OpenRC/openrc/blob/master/service-script-guide.md

75
main.go
View file

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