|
| 1 | +package firecracker |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | +) |
| 12 | + |
| 13 | +// Client is a Firecracker HTTP API client. |
| 14 | +type Client struct { |
| 15 | + httpClient *http.Client |
| 16 | + baseURL string |
| 17 | +} |
| 18 | + |
| 19 | +// NewClient creates a new Firecracker API client connected via Unix socket. |
| 20 | +func NewClient(socketPath string) *Client { |
| 21 | + httpClient := &http.Client{ |
| 22 | + Transport: &http.Transport{ |
| 23 | + DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { |
| 24 | + return net.Dial("unix", socketPath) |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + return &Client{ |
| 30 | + httpClient: httpClient, |
| 31 | + baseURL: "http://localhost", |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// doRequest performs an HTTP request to the Firecracker API. |
| 36 | +func (c *Client) doRequest(ctx context.Context, method, path string, body interface{}) (*http.Response, error) { |
| 37 | + var bodyReader io.Reader |
| 38 | + if body != nil { |
| 39 | + jsonBody, err := json.Marshal(body) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("failed to marshal request body: %w", err) |
| 42 | + } |
| 43 | + bodyReader = bytes.NewReader(jsonBody) |
| 44 | + } |
| 45 | + |
| 46 | + req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + if body != nil { |
| 52 | + req.Header.Set("Content-Type", "application/json") |
| 53 | + } |
| 54 | + req.Header.Set("Accept", "application/json") |
| 55 | + |
| 56 | + return c.httpClient.Do(req) |
| 57 | +} |
| 58 | + |
| 59 | +// checkResponse checks if the response indicates success. |
| 60 | +func checkResponse(resp *http.Response) error { |
| 61 | + if resp.StatusCode >= 200 && resp.StatusCode < 300 { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + body, _ := io.ReadAll(resp.Body) |
| 66 | + var apiErr Error |
| 67 | + if err := json.Unmarshal(body, &apiErr); err == nil && apiErr.FaultMessage != "" { |
| 68 | + return fmt.Errorf("firecracker API error: %s", apiErr.FaultMessage) |
| 69 | + } |
| 70 | + |
| 71 | + return fmt.Errorf("firecracker API error: status %d, body: %s", resp.StatusCode, string(body)) |
| 72 | +} |
| 73 | + |
| 74 | +// GetInstanceInfo retrieves information about the instance. |
| 75 | +func (c *Client) GetInstanceInfo(ctx context.Context) (*InstanceInfo, error) { |
| 76 | + resp, err := c.doRequest(ctx, http.MethodGet, "/", nil) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + defer resp.Body.Close() |
| 81 | + |
| 82 | + if err := checkResponse(resp); err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + var info InstanceInfo |
| 87 | + if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { |
| 88 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + return &info, nil |
| 92 | +} |
| 93 | + |
| 94 | +// PutMachineConfig sets the machine configuration. |
| 95 | +func (c *Client) PutMachineConfig(ctx context.Context, config MachineConfig) error { |
| 96 | + resp, err := c.doRequest(ctx, http.MethodPut, "/machine-config", config) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + defer resp.Body.Close() |
| 101 | + |
| 102 | + return checkResponse(resp) |
| 103 | +} |
| 104 | + |
| 105 | +// PutBootSource sets the boot source configuration. |
| 106 | +func (c *Client) PutBootSource(ctx context.Context, bootSource BootSource) error { |
| 107 | + resp, err := c.doRequest(ctx, http.MethodPut, "/boot-source", bootSource) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + defer resp.Body.Close() |
| 112 | + |
| 113 | + return checkResponse(resp) |
| 114 | +} |
| 115 | + |
| 116 | +// PutDrive adds or updates a block device. |
| 117 | +func (c *Client) PutDrive(ctx context.Context, drive Drive) error { |
| 118 | + resp, err := c.doRequest(ctx, http.MethodPut, "/drives/"+drive.DriveID, drive) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + defer resp.Body.Close() |
| 123 | + |
| 124 | + return checkResponse(resp) |
| 125 | +} |
| 126 | + |
| 127 | +// PutNetworkInterface adds or updates a network interface. |
| 128 | +func (c *Client) PutNetworkInterface(ctx context.Context, iface NetworkInterface) error { |
| 129 | + resp, err := c.doRequest(ctx, http.MethodPut, "/network-interfaces/"+iface.IfaceID, iface) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + defer resp.Body.Close() |
| 134 | + |
| 135 | + return checkResponse(resp) |
| 136 | +} |
| 137 | + |
| 138 | +// PutVsock sets the vsock device configuration. |
| 139 | +func (c *Client) PutVsock(ctx context.Context, vsock VsockDevice) error { |
| 140 | + resp, err := c.doRequest(ctx, http.MethodPut, "/vsock", vsock) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + defer resp.Body.Close() |
| 145 | + |
| 146 | + return checkResponse(resp) |
| 147 | +} |
| 148 | + |
| 149 | +// CreateAction performs an instance action (e.g., InstanceStart). |
| 150 | +func (c *Client) CreateAction(ctx context.Context, action InstanceActionInfo) error { |
| 151 | + resp, err := c.doRequest(ctx, http.MethodPut, "/actions", action) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + defer resp.Body.Close() |
| 156 | + |
| 157 | + return checkResponse(resp) |
| 158 | +} |
| 159 | + |
| 160 | +// PatchVMState changes the VM state (Paused or Resumed). |
| 161 | +func (c *Client) PatchVMState(ctx context.Context, state VMState) error { |
| 162 | + resp, err := c.doRequest(ctx, http.MethodPatch, "/vm", state) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + defer resp.Body.Close() |
| 167 | + |
| 168 | + return checkResponse(resp) |
| 169 | +} |
| 170 | + |
| 171 | +// CreateSnapshot creates a snapshot of the microVM. |
| 172 | +func (c *Client) CreateSnapshot(ctx context.Context, params SnapshotCreateParams) error { |
| 173 | + resp, err := c.doRequest(ctx, http.MethodPut, "/snapshot/create", params) |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + defer resp.Body.Close() |
| 178 | + |
| 179 | + return checkResponse(resp) |
| 180 | +} |
| 181 | + |
| 182 | +// LoadSnapshot loads a snapshot into the microVM. |
| 183 | +func (c *Client) LoadSnapshot(ctx context.Context, params SnapshotLoadParams) error { |
| 184 | + resp, err := c.doRequest(ctx, http.MethodPut, "/snapshot/load", params) |
| 185 | + if err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + defer resp.Body.Close() |
| 189 | + |
| 190 | + return checkResponse(resp) |
| 191 | +} |
0 commit comments