I am creating a wrapper for firecracker.
To start a VM with firecracker on command line, you have to pass a socket file to the firecracker
executable. Something like this:
firecracker --api-sock /path/to/file.socket
Then from another terminal, you can make requests to this server/socket something like this:
curl --unix-socket /tmp/firecracker.socket -XPUT 'http://localhost/actions' -d '{"action_type": "SendCtrlAltDel"}'
I am trying to replicate the same thing from within a Gin server.
I have an endpoint which does the first work, which is to start a server. A minimal code looks like this:
cmd := exec.Command("firecracker", "--api-sock", "/path/to/file.socket")
err := cmd.Start()
This endpoint starts the server and listens for any command. The problem is, I don't know how to use the socket file to make a PUT request to this server. I have found this on the web, but it does not makes much sense to me.
Here is a starter code which does not use any socket file.
func BootSource(c *gin.Context) {
var body models.BootSource
c.BindJSON(&body)
bodyJson, _ := json.Marshal(body)
// initialize http client
client := &http.Client{}
// set the HTTP method, url, and request body
req, err := http.NewRequest(http.MethodPut, "http://localhost/boot-source", bytes.NewBuffer(bodyJson))
if err != nil {
panic(err)
}
// set the request header Content-Type for json
_, err = client.Do(req)
if err != nil {
panic(err)
}
}
How do I make this PUT request use the socket file?
Please also note that I'm using Gin framework.