mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: add NFS volume type
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
package volumes
|
||||
|
||||
import (
|
||||
"ironmount/internal/core"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
@@ -19,22 +17,15 @@ func SetupHandlers(router *gin.Engine) {
|
||||
})
|
||||
|
||||
router.POST("/api/volumes", func(c *gin.Context) {
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
var body VolumeCreateRequest
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to bind JSON for volume creation")
|
||||
c.JSON(400, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
clean := core.Slugify(req.Name)
|
||||
if clean == "" || clean != req.Name {
|
||||
c.JSON(400, gin.H{"error": "invalid volume name"})
|
||||
return
|
||||
}
|
||||
|
||||
volume, status, err := volumeService.CreateVolume(clean)
|
||||
volume, status, err := volumeService.CreateVolume(body)
|
||||
if err != nil {
|
||||
c.JSON(status, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -2,6 +2,7 @@ package volumes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"ironmount/internal/db"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -27,9 +28,18 @@ func (q *VolumeQueries) QueryVolumeByName(n string) (*db.Volume, error) {
|
||||
return volume, nil
|
||||
}
|
||||
|
||||
func (q *VolumeQueries) InsertVolume(name, path string) error {
|
||||
func (q *VolumeQueries) InsertVolume(name string, path string, volType VolumeBackendType, config string) error {
|
||||
ctx := context.Background()
|
||||
err := gorm.G[db.Volume](db.DB).Create(ctx, &db.Volume{Name: name, Path: path})
|
||||
|
||||
validate := validator.New(validator.WithRequiredStructEnabled())
|
||||
|
||||
data := &db.Volume{}
|
||||
if err := validate.Struct(data); err != nil {
|
||||
log.Error().Err(err).Str("name", name).Msg("Validation error while inserting volume")
|
||||
return err
|
||||
}
|
||||
|
||||
err := gorm.G[db.Volume](db.DB).Create(ctx, &db.Volume{})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -9,6 +9,9 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"k8s.io/utils/mount"
|
||||
)
|
||||
|
||||
type VolumeService struct{}
|
||||
@@ -16,7 +19,12 @@ type VolumeService struct{}
|
||||
var volumeQueries = VolumeQueries{}
|
||||
|
||||
// CreateVolume handles the creation of a new volume.
|
||||
func (v *VolumeService) CreateVolume(name string) (*db.Volume, int, error) {
|
||||
func (v *VolumeService) CreateVolume(body VolumeCreateRequest) (*db.Volume, int, error) {
|
||||
name := core.Slugify(body.Name)
|
||||
if name == "" || name != body.Name {
|
||||
return nil, http.StatusBadRequest, fmt.Errorf("invalid volume name: %s", body.Name)
|
||||
}
|
||||
|
||||
existingVol, _ := volumeQueries.QueryVolumeByName(name)
|
||||
|
||||
if existingVol != nil {
|
||||
@@ -25,14 +33,48 @@ func (v *VolumeService) CreateVolume(name string) (*db.Volume, int, error) {
|
||||
|
||||
cfg := core.LoadConfig()
|
||||
|
||||
volPathHost := filepath.Join(cfg.VolumeRootHost, name)
|
||||
volPathLocal := filepath.Join(constants.VolumeRootLocal, name)
|
||||
volPathHost := filepath.Join(cfg.VolumeRootHost, name, "_data")
|
||||
volPathLocal := filepath.Join(constants.VolumeRootLocal, name, "_data")
|
||||
|
||||
if err := os.MkdirAll(volPathLocal, 0755); err != nil {
|
||||
return nil, http.StatusInternalServerError, fmt.Errorf("failed to create volume directory: %w", err)
|
||||
}
|
||||
|
||||
if err := volumeQueries.InsertVolume(name, volPathHost); err != nil {
|
||||
switch body.Type {
|
||||
case VolumeBackendTypeNFS:
|
||||
var cfg NFSConfig
|
||||
cfg, err := core.DecodeStrict[NFSConfig](body.Config)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, fmt.Errorf("invalid NFS configuration: %w", err)
|
||||
}
|
||||
|
||||
mounter := mount.New("")
|
||||
source := fmt.Sprintf("%s:%s", cfg.Server, cfg.ExportPath)
|
||||
options := []string{"vers=" + cfg.Version, "port=" + fmt.Sprintf("%d", cfg.Port)}
|
||||
|
||||
if err := UnmountVolume(volPathLocal); err != nil {
|
||||
return nil, http.StatusInternalServerError, fmt.Errorf("failed to unmount existing volume: %w", err)
|
||||
}
|
||||
|
||||
if err := mounter.Mount(source, volPathLocal, "nfs", options); err != nil {
|
||||
return nil, http.StatusInternalServerError, fmt.Errorf("failed to mount NFS volume: %w", err)
|
||||
}
|
||||
|
||||
case VolumeBackendTypeSMB:
|
||||
var _ SMBConfig
|
||||
|
||||
case VolumeBackendTypeLocal:
|
||||
var cfg DirectoryConfig
|
||||
log.Debug().Str("directory_path", cfg.Path).Msg("Using local directory for volume")
|
||||
}
|
||||
|
||||
bytesConfig, err := body.Config.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, fmt.Errorf("failed to marshal volume configuration: %w", err)
|
||||
}
|
||||
stringConfig := string(bytesConfig)
|
||||
|
||||
if err := volumeQueries.InsertVolume(name, volPathHost, stringConfig); err != nil {
|
||||
if strings.Contains(err.Error(), "UNIQUE") {
|
||||
return nil, http.StatusConflict, fmt.Errorf("volume %s already exists", name)
|
||||
}
|
||||
@@ -79,7 +121,13 @@ func (v *VolumeService) DeleteVolume(name string) (int, error) {
|
||||
return http.StatusInternalServerError, fmt.Errorf("failed to remove volume from database: %w", err)
|
||||
}
|
||||
|
||||
// os.RemoveAll(vol.Path) ?? depends on whether we want to delete the actual directory
|
||||
volPathLocal := filepath.Join(constants.VolumeRootLocal, name)
|
||||
log.Debug().Str("volume_path", volPathLocal).Msg("Deleting volume directory")
|
||||
if err := UnmountVolume(volPathLocal); err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("failed to unmount volume: %w", err)
|
||||
}
|
||||
|
||||
os.RemoveAll(volPathLocal)
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package volumes
|
||||
|
||||
var DateFormat = "2006-01-02T15:04:05Z"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CreateVolumeBody struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
var DateFormat = "2006-01-02T15:04:05Z"
|
||||
|
||||
type CreateVolumeResponse struct {
|
||||
Name string `json:"name"`
|
||||
@@ -29,3 +31,57 @@ type ListVolumesResponse struct {
|
||||
Volumes []VolumeInfo `json:"volumes"`
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
type VolumeBackendType string
|
||||
|
||||
const (
|
||||
VolumeBackendTypeSMB VolumeBackendType = "smb"
|
||||
VolumeBackendTypeNFS VolumeBackendType = "nfs"
|
||||
VolumeBackendTypeLocal VolumeBackendType = "local"
|
||||
)
|
||||
|
||||
func (vbt VolumeBackendType) String() string {
|
||||
return string(vbt)
|
||||
}
|
||||
|
||||
func (vbt *VolumeBackendType) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return fmt.Errorf("volume backend type should be a string: %w", err)
|
||||
}
|
||||
|
||||
lower := strings.ToLower(s)
|
||||
|
||||
switch VolumeBackendType(lower) {
|
||||
case VolumeBackendTypeSMB, VolumeBackendTypeNFS, VolumeBackendTypeLocal:
|
||||
*vbt = VolumeBackendType(lower)
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("invalid volume backend type: '%s'. Allowed types are: smb, nfs, local", lower)
|
||||
}
|
||||
}
|
||||
|
||||
type VolumeCreateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type VolumeBackendType `json:"type" binding:"required,oneof=nfs smb directory"`
|
||||
Config json.RawMessage `json:"config" binding:"required"`
|
||||
}
|
||||
|
||||
type NFSConfig struct {
|
||||
Server string `json:"server" binding:"required,hostname|ip"`
|
||||
ExportPath string `json:"exportPath" binding:"required"`
|
||||
Port int `json:"port" binding:"required,min=1,max=65535"`
|
||||
Version string `json:"version" binding:"required,oneof=3 4"`
|
||||
}
|
||||
|
||||
type SMBConfig struct {
|
||||
Server string `json:"server" binding:"required"`
|
||||
Share string `json:"share" binding:"required"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Domain string `json:"domain,omitempty"`
|
||||
}
|
||||
|
||||
type DirectoryConfig struct {
|
||||
Path string `json:"path" binding:"required"`
|
||||
}
|
||||
|
||||
21
internal/modules/volumes/utils.go
Normal file
21
internal/modules/volumes/utils.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package volumes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"k8s.io/utils/mount"
|
||||
)
|
||||
|
||||
func UnmountVolume(path string) error {
|
||||
mounter := mount.New("")
|
||||
if err := mounter.Unmount(path); err != nil {
|
||||
if strings.Contains(err.Error(), "not mounted") || strings.Contains(err.Error(), "No such file or directory") || strings.Contains(err.Error(), "Invalid argument") {
|
||||
// Volume is not mounted
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to unmount volume at %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user