2016-12-18 05:21:29 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2021-04-06 10:08:43 +00:00
|
|
|
"io"
|
2017-03-12 16:24:15 +00:00
|
|
|
"log"
|
2018-06-19 11:15:10 +00:00
|
|
|
"path"
|
2016-12-18 05:21:29 +00:00
|
|
|
"time"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2021-02-22 22:14:52 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-06 23:18:39 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/customtemplate"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/dockerhub"
|
2020-04-16 00:22:08 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/edgegroup"
|
2020-06-25 03:25:51 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/edgejob"
|
2020-04-16 00:22:08 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/edgestack"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/endpoint"
|
|
|
|
"github.com/portainer/portainer/api/bolt/endpointgroup"
|
2020-04-16 00:22:08 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/endpointrelation"
|
2020-07-07 21:57:52 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/errors"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/extension"
|
2021-04-06 10:08:43 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/internal"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/migrator"
|
|
|
|
"github.com/portainer/portainer/api/bolt/registry"
|
|
|
|
"github.com/portainer/portainer/api/bolt/resourcecontrol"
|
2019-05-24 06:04:58 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/role"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/schedule"
|
|
|
|
"github.com/portainer/portainer/api/bolt/settings"
|
|
|
|
"github.com/portainer/portainer/api/bolt/stack"
|
|
|
|
"github.com/portainer/portainer/api/bolt/tag"
|
|
|
|
"github.com/portainer/portainer/api/bolt/team"
|
|
|
|
"github.com/portainer/portainer/api/bolt/teammembership"
|
2020-04-16 00:22:08 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/tunnelserver"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/user"
|
|
|
|
"github.com/portainer/portainer/api/bolt/version"
|
|
|
|
"github.com/portainer/portainer/api/bolt/webhook"
|
2020-06-16 07:58:16 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
2018-06-19 11:15:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
databaseFileName = "portainer.db"
|
2016-12-18 05:21:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Store defines the implementation of portainer.DataStore using
|
|
|
|
// BoltDB as the storage system.
|
|
|
|
type Store struct {
|
2020-05-14 02:14:28 +00:00
|
|
|
path string
|
2021-04-06 10:08:43 +00:00
|
|
|
connection *internal.DbConnection
|
2020-04-16 00:22:08 +00:00
|
|
|
isNew bool
|
2020-05-14 02:14:28 +00:00
|
|
|
fileService portainer.FileService
|
2020-07-06 23:18:39 +00:00
|
|
|
CustomTemplateService *customtemplate.Service
|
2020-05-14 02:14:28 +00:00
|
|
|
DockerHubService *dockerhub.Service
|
|
|
|
EdgeGroupService *edgegroup.Service
|
2020-06-25 03:25:51 +00:00
|
|
|
EdgeJobService *edgejob.Service
|
2020-05-14 02:14:28 +00:00
|
|
|
EdgeStackService *edgestack.Service
|
|
|
|
EndpointGroupService *endpointgroup.Service
|
|
|
|
EndpointService *endpoint.Service
|
|
|
|
EndpointRelationService *endpointrelation.Service
|
|
|
|
ExtensionService *extension.Service
|
|
|
|
RegistryService *registry.Service
|
|
|
|
ResourceControlService *resourcecontrol.Service
|
2020-05-20 05:23:15 +00:00
|
|
|
RoleService *role.Service
|
|
|
|
ScheduleService *schedule.Service
|
2020-05-14 02:14:28 +00:00
|
|
|
SettingsService *settings.Service
|
|
|
|
StackService *stack.Service
|
|
|
|
TagService *tag.Service
|
|
|
|
TeamMembershipService *teammembership.Service
|
|
|
|
TeamService *team.Service
|
|
|
|
TunnelServerService *tunnelserver.Service
|
|
|
|
UserService *user.Service
|
|
|
|
VersionService *version.Service
|
|
|
|
WebhookService *webhook.Service
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 22:14:52 +00:00
|
|
|
func (store *Store) edition() portainer.SoftwareEdition {
|
|
|
|
edition, err := store.VersionService.Edition()
|
|
|
|
if err == errors.ErrObjectNotFound {
|
|
|
|
edition = portainer.PortainerCE
|
|
|
|
}
|
|
|
|
return edition
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
// NewStore initializes a new Store and the associated services
|
2018-06-18 10:07:56 +00:00
|
|
|
func NewStore(storePath string, fileService portainer.FileService) (*Store, error) {
|
2016-12-18 05:21:29 +00:00
|
|
|
store := &Store{
|
2018-06-19 11:15:10 +00:00
|
|
|
path: storePath,
|
|
|
|
fileService: fileService,
|
2020-04-16 00:22:08 +00:00
|
|
|
isNew: true,
|
2021-04-06 10:08:43 +00:00
|
|
|
connection: &internal.DbConnection{},
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
databasePath := path.Join(storePath, databaseFileName)
|
|
|
|
databaseFileExists, err := fileService.FileExists(databasePath)
|
|
|
|
if err != nil {
|
2017-03-12 16:24:15 +00:00
|
|
|
return nil, err
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 00:22:08 +00:00
|
|
|
if databaseFileExists {
|
|
|
|
store.isNew = false
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return store, nil
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens and initializes the BoltDB database.
|
|
|
|
func (store *Store) Open() error {
|
2018-06-19 11:15:10 +00:00
|
|
|
databasePath := path.Join(store.path, databaseFileName)
|
|
|
|
db, err := bolt.Open(databasePath, 0600, &bolt.Options{Timeout: 1 * time.Second})
|
2016-12-18 05:21:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 10:08:43 +00:00
|
|
|
store.connection.DB = db
|
2017-06-20 11:00:32 +00:00
|
|
|
|
2018-06-19 11:15:10 +00:00
|
|
|
return store.initServices()
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the BoltDB database.
|
|
|
|
func (store *Store) Close() error {
|
2021-04-06 10:08:43 +00:00
|
|
|
if store.connection.DB != nil {
|
|
|
|
return store.connection.Close()
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
|
2020-04-16 00:22:08 +00:00
|
|
|
// IsNew returns true if the database was just created and false if it is re-using
|
|
|
|
// existing data.
|
|
|
|
func (store *Store) IsNew() bool {
|
|
|
|
return store.isNew
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:14:52 +00:00
|
|
|
// CheckCurrentEdition checks if current edition is community edition
|
|
|
|
func (store *Store) CheckCurrentEdition() error {
|
|
|
|
if store.edition() != portainer.PortainerCE {
|
|
|
|
return errors.ErrWrongDBEdition
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
// MigrateData automatically migrate the data based on the DBVersion.
|
2020-04-16 00:22:08 +00:00
|
|
|
// This process is only triggered on an existing database, not if the database was just created.
|
2021-04-06 10:08:43 +00:00
|
|
|
// if force is true, then migrate regardless.
|
|
|
|
func (store *Store) MigrateData(force bool) error {
|
|
|
|
if store.isNew && !force {
|
2018-06-19 11:15:10 +00:00
|
|
|
return store.VersionService.StoreDBVersion(portainer.DBVersion)
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
version, err := store.VersionService.DBVersion()
|
2020-07-07 21:57:52 +00:00
|
|
|
if err == errors.ErrObjectNotFound {
|
2017-03-12 16:24:15 +00:00
|
|
|
version = 0
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if version < portainer.DBVersion {
|
2018-06-19 11:15:10 +00:00
|
|
|
migratorParams := &migrator.Parameters{
|
2021-04-06 10:08:43 +00:00
|
|
|
DB: store.connection.DB,
|
2020-05-14 02:14:28 +00:00
|
|
|
DatabaseVersion: version,
|
|
|
|
EndpointGroupService: store.EndpointGroupService,
|
|
|
|
EndpointService: store.EndpointService,
|
|
|
|
EndpointRelationService: store.EndpointRelationService,
|
|
|
|
ExtensionService: store.ExtensionService,
|
|
|
|
RegistryService: store.RegistryService,
|
|
|
|
ResourceControlService: store.ResourceControlService,
|
|
|
|
RoleService: store.RoleService,
|
|
|
|
ScheduleService: store.ScheduleService,
|
|
|
|
SettingsService: store.SettingsService,
|
|
|
|
StackService: store.StackService,
|
|
|
|
TagService: store.TagService,
|
|
|
|
TeamMembershipService: store.TeamMembershipService,
|
|
|
|
UserService: store.UserService,
|
|
|
|
VersionService: store.VersionService,
|
|
|
|
FileService: store.fileService,
|
2021-07-14 09:15:21 +00:00
|
|
|
DockerhubService: store.DockerHubService,
|
2020-06-16 07:58:16 +00:00
|
|
|
AuthorizationService: authorization.NewService(store),
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
migrator := migrator.NewMigrator(migratorParams)
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
log.Printf("Migrating database from version %v to %v.\n", version, portainer.DBVersion)
|
|
|
|
err = migrator.Migrate()
|
|
|
|
if err != nil {
|
2018-06-19 11:15:10 +00:00
|
|
|
log.Printf("An error occurred during database migration: %s\n", err)
|
2017-03-12 16:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-19 11:15:10 +00:00
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
// BackupTo backs up db to a provided writer.
|
|
|
|
// It does hot backup and doesn't block other database reads and writes
|
|
|
|
func (store *Store) BackupTo(w io.Writer) error {
|
|
|
|
return store.connection.View(func(tx *bolt.Tx) error {
|
|
|
|
_, err := tx.WriteTo(w)
|
2018-11-05 20:58:15 +00:00
|
|
|
return err
|
2021-04-06 10:08:43 +00:00
|
|
|
})
|
2020-05-20 05:23:15 +00:00
|
|
|
}
|