Files
portainer/api/backup/seed.go
T
zees-dev 6380ffbafc - initial db fixture/seeding implementation
- unit tests
- TODO: migrate to using portainer-cli repo
2021-12-20 17:49:45 +13:00

66 lines
1.7 KiB
Go

package backup
import (
"context"
"encoding/json"
"os"
"path/filepath"
"github.com/pkg/errors"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/bolt"
"github.com/portainer/portainer/api/http/offlinegate"
)
// SeedStore seeds the store with provided JSON/map data, will trigger system shutdown, when finished.
// NOTE: THIS WILL COMPLETELY OVERWRITE THE CURRENT STORE - only use this for testing.
func SeedStore(storeData map[string]interface{}, filestorePath string, gate *offlinegate.OfflineGate, datastore portainer.DataStore, shutdownTrigger context.CancelFunc) error {
// write storeData map to a temporary file
file, err := writeMapToFile(storeData)
if err != nil {
return err
}
defer os.Remove(file.Name())
unlock := gate.Lock()
defer unlock()
if err := datastore.Close(); err != nil {
return errors.Wrap(err, "Failed to stop db")
}
storePath := filepath.Join(filestorePath, bolt.DatabaseFileName)
// TODO: use portainer-cli to import
if err := ImportJsonToDatabase(file.Name(), storePath); err != nil {
return errors.Wrap(err, "Unable to import JSON data to database")
}
shutdownTrigger()
return nil
}
// writeMapToFile writes a map to a temporary file and returns the file.
func writeMapToFile(mapData map[string]interface{}) (*os.File, error) {
// map (json export) -> string
jsonData, err := json.Marshal(mapData)
if err != nil {
return nil, err
}
// write string (json) to temporary file
file, err := os.CreateTemp("", "temp-db-export-json")
if err != nil {
return nil, err
}
_, err = file.Write(jsonData)
if err != nil {
file.Close()
return nil, err
}
if err = file.Close(); err != nil {
return nil, err
}
return file, nil
}