Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/doc/openapi.json

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions api/internal/features/container/controller/get_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package controller

import (
"net/http"
"strconv"

"github.com/go-fuego/fuego"
"github.com/raghavyuva/nixopus-api/internal/features/container/types"
"github.com/raghavyuva/nixopus-api/internal/features/logger"
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
)

func (c *ContainerController) GetContainer(f fuego.ContextNoBody) (*shared_types.Response, error) {
containerID := f.PathParam("container_id")

containerInfo, err := c.dockerService.GetContainerById(containerID)
if err != nil {
c.logger.Log(logger.Error, err.Error(), "")
return nil, fuego.HTTPError{
Err: err,
Status: http.StatusInternalServerError,
}
}

containerData := types.Container{
ID: containerInfo.ID,
Name: containerInfo.Name[1:],
Image: containerInfo.Config.Image,
Status: containerInfo.State.Status,
State: containerInfo.State.Status,
Created: containerInfo.Created,
Labels: containerInfo.Config.Labels,
Command: containerInfo.Config.Cmd[0],
IPAddress: containerInfo.NetworkSettings.IPAddress,
HostConfig: types.HostConfig{
Memory: containerInfo.HostConfig.Memory,
MemorySwap: containerInfo.HostConfig.MemorySwap,
CPUShares: containerInfo.HostConfig.CPUShares,
},
}

for port, bindings := range containerInfo.NetworkSettings.Ports {
for _, binding := range bindings {
containerData.Ports = append(containerData.Ports, types.Port{
PrivatePort: int(port.Int()),
PublicPort: func() int { p, _ := strconv.Atoi(binding.HostPort); return p }(),
Type: port.Proto(),
})
}
}

for _, mount := range containerInfo.Mounts {
containerData.Mounts = append(containerData.Mounts, types.Mount{
Type: string(mount.Type),
Source: mount.Source,
Destination: mount.Destination,
Mode: mount.Mode,
})
}

for name, network := range containerInfo.NetworkSettings.Networks {
containerData.Networks = append(containerData.Networks, types.Network{
Name: name,
IPAddress: network.IPAddress,
Gateway: network.Gateway,
MacAddress: network.MacAddress,
Aliases: network.Aliases,
})
}

return &shared_types.Response{
Status: "success",
Message: "Container fetched successfully",
Data: containerData,
}, nil
}
85 changes: 85 additions & 0 deletions api/internal/features/container/controller/get_container_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package controller

import (
"bytes"
"encoding/binary"
"io"
"net/http"
"strconv"

"github.com/docker/docker/api/types/container"
"github.com/go-fuego/fuego"
"github.com/raghavyuva/nixopus-api/internal/features/container/types"
"github.com/raghavyuva/nixopus-api/internal/features/logger"
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
)

func (c *ContainerController) GetContainerLogs(f fuego.ContextWithBody[types.ContainerLogsRequest]) (*shared_types.Response, error) {
req, err := f.Body()
if err != nil {
return nil, fuego.HTTPError{
Err: err,
Status: http.StatusBadRequest,
}
}

logsReader, err := c.dockerService.GetContainerLogs(req.ID, container.LogsOptions{
Follow: req.Follow,
Tail: strconv.Itoa(req.Tail),
Since: req.Since,
Until: req.Until,
ShowStdout: req.Stdout,
ShowStderr: req.Stderr,
})
if err != nil {
c.logger.Log(logger.Error, err.Error(), "")
return nil, fuego.HTTPError{
Err: err,
Status: http.StatusInternalServerError,
}
}

buf := new(bytes.Buffer)
_, err = io.Copy(buf, logsReader)
if err != nil {
c.logger.Log(logger.Error, err.Error(), "")
return nil, fuego.HTTPError{
Err: err,
Status: http.StatusInternalServerError,
}
}

decodedLogs := decodeDockerLogs(buf.Bytes())

return &shared_types.Response{
Status: "success",
Message: "Container logs fetched successfully",
Data: decodedLogs,
}, nil
}

func decodeDockerLogs(data []byte) string {
var result bytes.Buffer
offset := 0

for offset < len(data) {
if offset+8 > len(data) {
break
}

streamType := data[offset]
length := binary.BigEndian.Uint32(data[offset+4 : offset+8])
offset += 8

if offset+int(length) > len(data) {
break
}

if streamType == 1 || streamType == 2 {
result.Write(data[offset : offset+int(length)])
}
offset += int(length)
}

return result.String()
}
32 changes: 32 additions & 0 deletions api/internal/features/container/controller/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package controller

import (
"context"
"github.com/raghavyuva/nixopus-api/internal/features/deploy/docker"
"github.com/raghavyuva/nixopus-api/internal/features/logger"
"github.com/raghavyuva/nixopus-api/internal/features/notification"
shared_storage "github.com/raghavyuva/nixopus-api/internal/storage"
)

type ContainerController struct {
store *shared_storage.Store
dockerService *docker.DockerService
ctx context.Context
logger logger.Logger
notification *notification.NotificationManager
}

func NewContainerController(
store *shared_storage.Store,
ctx context.Context,
l logger.Logger,
notificationManager *notification.NotificationManager,
) *ContainerController {
return &ContainerController{
store: store,
dockerService: docker.NewDockerService(),
ctx: ctx,
logger: l,
notification: notificationManager,
}
}
82 changes: 82 additions & 0 deletions api/internal/features/container/controller/list_containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package controller

import (
"net/http"

"github.com/go-fuego/fuego"
"github.com/raghavyuva/nixopus-api/internal/features/container/types"
"github.com/raghavyuva/nixopus-api/internal/features/logger"
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
)

func (c *ContainerController) ListContainers(f fuego.ContextNoBody) (*shared_types.Response, error) {
containers, err := c.dockerService.ListAllContainers()
if err != nil {
c.logger.Log(logger.Error, err.Error(), "")
return nil, fuego.HTTPError{
Err: err,
Status: http.StatusInternalServerError,
}
}

var result []types.Container
for _, container := range containers {
containerInfo, err := c.dockerService.GetContainerById(container.ID)
if err != nil {
c.logger.Log(logger.Error, "Error inspecting container", container.ID)
continue
}

containerData := types.Container{
ID: container.ID,
Name: container.Names[0][1:],
Image: container.Image,
Status: container.Status,
State: container.State,
Created: containerInfo.Created,
Labels: container.Labels,
Command: containerInfo.Config.Cmd[0],
IPAddress: containerInfo.NetworkSettings.IPAddress,
HostConfig: types.HostConfig{
Memory: containerInfo.HostConfig.Memory,
MemorySwap: containerInfo.HostConfig.MemorySwap,
CPUShares: containerInfo.HostConfig.CPUShares,
},
}

for _, port := range container.Ports {
containerData.Ports = append(containerData.Ports, types.Port{
PrivatePort: int(port.PrivatePort),
PublicPort: int(port.PublicPort),
Type: port.Type,
})
}

for _, mount := range containerInfo.Mounts {
containerData.Mounts = append(containerData.Mounts, types.Mount{
Type: string(mount.Type),
Source: mount.Source,
Destination: mount.Destination,
Mode: mount.Mode,
})
}

for name, network := range containerInfo.NetworkSettings.Networks {
containerData.Networks = append(containerData.Networks, types.Network{
Name: name,
IPAddress: network.IPAddress,
Gateway: network.Gateway,
MacAddress: network.MacAddress,
Aliases: network.Aliases,
})
}

result = append(result, containerData)
}

return &shared_types.Response{
Status: "success",
Message: "Containers fetched successfully",
Data: result,
}, nil
}
82 changes: 82 additions & 0 deletions api/internal/features/container/controller/list_images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package controller

import (
"net/http"
"strings"

"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/go-fuego/fuego"
container_types "github.com/raghavyuva/nixopus-api/internal/features/container/types"
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
)

type ListImagesRequest struct {
All bool `json:"all,omitempty"`
ContainerID string `json:"container_id,omitempty"`
ImagePrefix string `json:"image_prefix,omitempty"`
}

func (c *ContainerController) ListImages(f fuego.ContextWithBody[ListImagesRequest]) (*shared_types.Response, error) {
req, err := f.Body()
if err != nil {
return nil, fuego.HTTPError{
Err: err,
Status: http.StatusBadRequest,
}
}

filterArgs := filters.NewArgs()
if req.ContainerID != "" {
_, err := c.dockerService.GetContainerById(req.ContainerID)
if err != nil {
return nil, fuego.HTTPError{
Err: err,
Status: http.StatusNotFound,
}
}
}

if req.ImagePrefix != "" {
pattern := req.ImagePrefix
if !strings.HasSuffix(pattern, "*") {
pattern += "*"
}
filterArgs.Add("reference", pattern)
}

images := c.dockerService.ListAllImages(image.ListOptions{
All: req.All,
Filters: filterArgs,
})

if len(images) == 0 {
return &shared_types.Response{
Status: "success",
Message: "No images found",
Data: []container_types.Image{},
}, nil
}

var result []container_types.Image
for _, img := range images {
imageData := container_types.Image{
ID: img.ID,
RepoTags: img.RepoTags,
RepoDigests: img.RepoDigests,
Created: img.Created,
Size: img.Size,
SharedSize: img.SharedSize,
VirtualSize: img.VirtualSize,
Labels: img.Labels,
}

result = append(result, imageData)
}

return &shared_types.Response{
Status: "success",
Message: "Images listed successfully",
Data: result,
}, nil
}
Loading