feat: import existing smb.conf and /etc/exports on first boot
Adds auto-detection of pre-existing Samba shares and NFS exports when nasctl is installed on a host that already has these configs. New package internal/importer parses smb.conf (INI-style) and /etc/exports (line-based) and imports them into SQLite. Imported shares/exports are marked dirty so the user must review and apply manually before any file is overwritten. Backup: before the first Apply, each module backs up the original config to <path>.nasctl.bak.<timestamp> (one time only). New CLI flag --import-on-boot / NASCTL_IMPORT_ON_BOOT env var (default false, opt-in). New API endpoints: GET /api/import/status POST /api/import/samba POST /api/import/nfs New DB methods ReplaceSambaShares/ReplaceNFSExports (transactional replace-all), guarded by import.samba.done / import.nfs.done settings flags.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (d *DB) ReplaceSambaShares(shares []SambaShare) error {
|
||||
tx, err := d.conn.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if _, err := tx.Exec(`DELETE FROM samba_shares`); err != nil {
|
||||
return fmt.Errorf("clear samba_shares: %w", err)
|
||||
}
|
||||
|
||||
for _, share := range shares {
|
||||
validUsers, err := encodeJSONStrings(share.ValidUsers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
validGroups, err := encodeJSONStrings(share.ValidGroups)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
readOnly := 0
|
||||
if share.ReadOnly {
|
||||
readOnly = 1
|
||||
}
|
||||
guestOK := 0
|
||||
if share.GuestOK {
|
||||
guestOK = 1
|
||||
}
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO samba_shares (name, path, comment, read_only, guest_ok, valid_users, valid_groups)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
share.Name, share.Path, share.Comment, readOnly, guestOK, validUsers, validGroups,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert samba share %s: %w", share.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (d *DB) ReplaceNFSExports(exports []NFSExport) error {
|
||||
tx, err := d.conn.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if _, err := tx.Exec(`DELETE FROM nfs_exports`); err != nil {
|
||||
return fmt.Errorf("clear nfs_exports: %w", err)
|
||||
}
|
||||
|
||||
for _, exp := range exports {
|
||||
clients, err := encodeJSONStrings(exp.Clients)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO nfs_exports (path, clients, options)
|
||||
VALUES (?, ?, ?)`,
|
||||
exp.Path, clients, exp.Options,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert nfs export %s: %w", exp.Path, err)
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user