2016-12-18 05:21:29 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
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"
|
2017-03-12 16:24:15 +00:00
|
|
|
"github.com/portainer/portainer"
|
2018-06-19 11:15:10 +00:00
|
|
|
"github.com/portainer/portainer/bolt/dockerhub"
|
|
|
|
"github.com/portainer/portainer/bolt/endpoint"
|
|
|
|
"github.com/portainer/portainer/bolt/endpointgroup"
|
|
|
|
"github.com/portainer/portainer/bolt/migrator"
|
|
|
|
"github.com/portainer/portainer/bolt/registry"
|
|
|
|
"github.com/portainer/portainer/bolt/resourcecontrol"
|
|
|
|
"github.com/portainer/portainer/bolt/settings"
|
|
|
|
"github.com/portainer/portainer/bolt/stack"
|
|
|
|
"github.com/portainer/portainer/bolt/tag"
|
|
|
|
"github.com/portainer/portainer/bolt/team"
|
|
|
|
"github.com/portainer/portainer/bolt/teammembership"
|
2018-07-03 18:31:02 +00:00
|
|
|
"github.com/portainer/portainer/bolt/template"
|
2018-06-19 11:15:10 +00:00
|
|
|
"github.com/portainer/portainer/bolt/user"
|
|
|
|
"github.com/portainer/portainer/bolt/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2018-06-19 11:15:10 +00:00
|
|
|
path string
|
|
|
|
db *bolt.DB
|
|
|
|
checkForDataMigration bool
|
|
|
|
fileService portainer.FileService
|
|
|
|
DockerHubService *dockerhub.Service
|
|
|
|
EndpointGroupService *endpointgroup.Service
|
|
|
|
EndpointService *endpoint.Service
|
|
|
|
RegistryService *registry.Service
|
|
|
|
ResourceControlService *resourcecontrol.Service
|
|
|
|
SettingsService *settings.Service
|
|
|
|
StackService *stack.Service
|
|
|
|
TagService *tag.Service
|
|
|
|
TeamMembershipService *teammembership.Service
|
|
|
|
TeamService *team.Service
|
2018-07-03 18:31:02 +00:00
|
|
|
TemplateService *template.Service
|
2018-06-19 11:15:10 +00:00
|
|
|
UserService *user.Service
|
|
|
|
VersionService *version.Service
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if !databaseFileExists {
|
|
|
|
store.checkForDataMigration = false
|
2017-03-12 16:24:15 +00:00
|
|
|
} else {
|
|
|
|
store.checkForDataMigration = true
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
store.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
|
|
|
}
|
|
|
|
|
2018-04-26 16:08:46 +00:00
|
|
|
// Init creates the default data set.
|
|
|
|
func (store *Store) Init() error {
|
|
|
|
groups, err := store.EndpointGroupService.EndpointGroups()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(groups) == 0 {
|
|
|
|
unassignedGroup := &portainer.EndpointGroup{
|
|
|
|
Name: "Unassigned",
|
|
|
|
Description: "Unassigned endpoints",
|
|
|
|
Labels: []portainer.Pair{},
|
|
|
|
AuthorizedUsers: []portainer.UserID{},
|
|
|
|
AuthorizedTeams: []portainer.TeamID{},
|
2018-06-15 07:18:25 +00:00
|
|
|
Tags: []string{},
|
2018-04-26 16:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return store.EndpointGroupService.CreateEndpointGroup(unassignedGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
// Close closes the BoltDB database.
|
|
|
|
func (store *Store) Close() error {
|
|
|
|
if store.db != nil {
|
|
|
|
return store.db.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
|
|
|
|
// MigrateData automatically migrate the data based on the DBVersion.
|
|
|
|
func (store *Store) MigrateData() error {
|
|
|
|
if !store.checkForDataMigration {
|
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()
|
2018-06-19 11:15:10 +00:00
|
|
|
if err == portainer.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{
|
|
|
|
DB: store.db,
|
|
|
|
DatabaseVersion: version,
|
|
|
|
EndpointGroupService: store.EndpointGroupService,
|
|
|
|
EndpointService: store.EndpointService,
|
|
|
|
ResourceControlService: store.ResourceControlService,
|
|
|
|
SettingsService: store.SettingsService,
|
|
|
|
StackService: store.StackService,
|
|
|
|
UserService: store.UserService,
|
|
|
|
VersionService: store.VersionService,
|
|
|
|
FileService: store.fileService,
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
func (store *Store) initServices() error {
|
|
|
|
dockerhubService, err := dockerhub.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.DockerHubService = dockerhubService
|
|
|
|
|
|
|
|
endpointgroupService, err := endpointgroup.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.EndpointGroupService = endpointgroupService
|
|
|
|
|
|
|
|
endpointService, err := endpoint.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.EndpointService = endpointService
|
|
|
|
|
|
|
|
registryService, err := registry.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.RegistryService = registryService
|
|
|
|
|
|
|
|
resourcecontrolService, err := resourcecontrol.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.ResourceControlService = resourcecontrolService
|
|
|
|
|
|
|
|
settingsService, err := settings.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.SettingsService = settingsService
|
|
|
|
|
|
|
|
stackService, err := stack.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.StackService = stackService
|
|
|
|
|
|
|
|
tagService, err := tag.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.TagService = tagService
|
|
|
|
|
|
|
|
teammembershipService, err := teammembership.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.TeamMembershipService = teammembershipService
|
|
|
|
|
|
|
|
teamService, err := team.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.TeamService = teamService
|
|
|
|
|
2018-07-03 18:31:02 +00:00
|
|
|
templateService, err := template.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.TemplateService = templateService
|
|
|
|
|
2018-06-19 11:15:10 +00:00
|
|
|
userService, err := user.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.UserService = userService
|
|
|
|
|
|
|
|
versionService, err := version.NewService(store.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store.VersionService = versionService
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|