2018-06-19 11:15:10 +00:00
|
|
|
package migrator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/boltdb/bolt"
|
2021-02-23 20:18:05 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2021-07-14 09:15:21 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/dockerhub"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/endpoint"
|
|
|
|
"github.com/portainer/portainer/api/bolt/endpointgroup"
|
2020-05-14 02:14:28 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/endpointrelation"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/extension"
|
2021-07-14 09:15:21 +00:00
|
|
|
plog "github.com/portainer/portainer/api/bolt/log"
|
2019-05-24 06:04:58 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/registry"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/resourcecontrol"
|
2019-09-09 22:58:26 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/role"
|
2019-10-08 03:18:32 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/schedule"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/settings"
|
|
|
|
"github.com/portainer/portainer/api/bolt/stack"
|
2020-03-29 09:54:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/tag"
|
2019-09-09 22:58:26 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/teammembership"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/user"
|
|
|
|
"github.com/portainer/portainer/api/bolt/version"
|
2020-06-16 07:58:16 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
2018-06-19 11:15:10 +00:00
|
|
|
)
|
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
var migrateLog = plog.NewScopedLog("bolt, migrate")
|
|
|
|
|
2018-06-19 11:15:10 +00:00
|
|
|
type (
|
|
|
|
// Migrator defines a service to migrate data after a Portainer version update.
|
|
|
|
Migrator struct {
|
2020-05-14 02:14:28 +00:00
|
|
|
currentDBVersion int
|
|
|
|
db *bolt.DB
|
|
|
|
endpointGroupService *endpointgroup.Service
|
|
|
|
endpointService *endpoint.Service
|
|
|
|
endpointRelationService *endpointrelation.Service
|
|
|
|
extensionService *extension.Service
|
|
|
|
registryService *registry.Service
|
|
|
|
resourceControlService *resourcecontrol.Service
|
|
|
|
roleService *role.Service
|
|
|
|
scheduleService *schedule.Service
|
|
|
|
settingsService *settings.Service
|
|
|
|
stackService *stack.Service
|
|
|
|
tagService *tag.Service
|
|
|
|
teamMembershipService *teammembership.Service
|
|
|
|
userService *user.Service
|
|
|
|
versionService *version.Service
|
|
|
|
fileService portainer.FileService
|
2020-06-16 07:58:16 +00:00
|
|
|
authorizationService *authorization.Service
|
2021-07-14 09:15:21 +00:00
|
|
|
dockerhubService *dockerhub.Service
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parameters represents the required parameters to create a new Migrator instance.
|
|
|
|
Parameters struct {
|
2020-05-14 02:14:28 +00:00
|
|
|
DB *bolt.DB
|
|
|
|
DatabaseVersion int
|
|
|
|
EndpointGroupService *endpointgroup.Service
|
|
|
|
EndpointService *endpoint.Service
|
|
|
|
EndpointRelationService *endpointrelation.Service
|
|
|
|
ExtensionService *extension.Service
|
|
|
|
RegistryService *registry.Service
|
|
|
|
ResourceControlService *resourcecontrol.Service
|
|
|
|
RoleService *role.Service
|
|
|
|
ScheduleService *schedule.Service
|
|
|
|
SettingsService *settings.Service
|
|
|
|
StackService *stack.Service
|
|
|
|
TagService *tag.Service
|
|
|
|
TeamMembershipService *teammembership.Service
|
|
|
|
UserService *user.Service
|
|
|
|
VersionService *version.Service
|
|
|
|
FileService portainer.FileService
|
2020-06-16 07:58:16 +00:00
|
|
|
AuthorizationService *authorization.Service
|
2021-07-14 09:15:21 +00:00
|
|
|
DockerhubService *dockerhub.Service
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewMigrator creates a new Migrator.
|
|
|
|
func NewMigrator(parameters *Parameters) *Migrator {
|
|
|
|
return &Migrator{
|
2020-05-14 02:14:28 +00:00
|
|
|
db: parameters.DB,
|
|
|
|
currentDBVersion: parameters.DatabaseVersion,
|
|
|
|
endpointGroupService: parameters.EndpointGroupService,
|
|
|
|
endpointService: parameters.EndpointService,
|
|
|
|
endpointRelationService: parameters.EndpointRelationService,
|
|
|
|
extensionService: parameters.ExtensionService,
|
|
|
|
registryService: parameters.RegistryService,
|
|
|
|
resourceControlService: parameters.ResourceControlService,
|
|
|
|
roleService: parameters.RoleService,
|
|
|
|
scheduleService: parameters.ScheduleService,
|
|
|
|
settingsService: parameters.SettingsService,
|
|
|
|
tagService: parameters.TagService,
|
|
|
|
teamMembershipService: parameters.TeamMembershipService,
|
|
|
|
stackService: parameters.StackService,
|
|
|
|
userService: parameters.UserService,
|
|
|
|
versionService: parameters.VersionService,
|
|
|
|
fileService: parameters.FileService,
|
2020-05-20 05:23:15 +00:00
|
|
|
authorizationService: parameters.AuthorizationService,
|
2021-07-14 09:15:21 +00:00
|
|
|
dockerhubService: parameters.DockerhubService,
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate checks the database version and migrate the existing data to the most recent data model.
|
|
|
|
func (m *Migrator) Migrate() error {
|
|
|
|
// Portainer < 1.12
|
|
|
|
if m.currentDBVersion < 1 {
|
|
|
|
err := m.updateAdminUserToDBVersion1()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Portainer 1.12.x
|
|
|
|
if m.currentDBVersion < 2 {
|
|
|
|
err := m.updateResourceControlsToDBVersion2()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = m.updateEndpointsToDBVersion2()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Portainer 1.13.x
|
|
|
|
if m.currentDBVersion < 3 {
|
|
|
|
err := m.updateSettingsToDBVersion3()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Portainer 1.14.0
|
|
|
|
if m.currentDBVersion < 4 {
|
|
|
|
err := m.updateEndpointsToDBVersion4()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/portainer/portainer/issues/1235
|
|
|
|
if m.currentDBVersion < 5 {
|
|
|
|
err := m.updateSettingsToVersion5()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/portainer/portainer/issues/1236
|
|
|
|
if m.currentDBVersion < 6 {
|
|
|
|
err := m.updateSettingsToVersion6()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/portainer/portainer/issues/1449
|
|
|
|
if m.currentDBVersion < 7 {
|
|
|
|
err := m.updateSettingsToVersion7()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.currentDBVersion < 8 {
|
|
|
|
err := m.updateEndpointsToVersion8()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https: //github.com/portainer/portainer/issues/1396
|
|
|
|
if m.currentDBVersion < 9 {
|
|
|
|
err := m.updateEndpointsToVersion9()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/portainer/portainer/issues/461
|
|
|
|
if m.currentDBVersion < 10 {
|
|
|
|
err := m.updateEndpointsToVersion10()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/portainer/portainer/issues/1906
|
|
|
|
if m.currentDBVersion < 11 {
|
|
|
|
err := m.updateEndpointsToVersion11()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 11:28:07 +00:00
|
|
|
// Portainer 1.18.0
|
2018-06-19 11:15:10 +00:00
|
|
|
if m.currentDBVersion < 12 {
|
|
|
|
err := m.updateEndpointsToVersion12()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.updateEndpointGroupsToVersion12()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.updateStacksToVersion12()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 14:05:57 +00:00
|
|
|
// Portainer 1.19.0
|
2018-07-23 04:57:38 +00:00
|
|
|
if m.currentDBVersion < 13 {
|
|
|
|
err := m.updateSettingsToVersion13()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 08:40:26 +00:00
|
|
|
// Portainer 1.19.2
|
2018-08-19 05:57:28 +00:00
|
|
|
if m.currentDBVersion < 14 {
|
|
|
|
err := m.updateResourceControlsToDBVersion14()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 04:03:25 +00:00
|
|
|
// Portainer 1.20.0
|
2018-12-05 22:36:25 +00:00
|
|
|
if m.currentDBVersion < 15 {
|
|
|
|
err := m.updateSettingsToDBVersion15()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-11 20:46:05 +00:00
|
|
|
|
|
|
|
err = m.updateTemplatesToVersion15()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-05 22:36:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 08:16:44 +00:00
|
|
|
if m.currentDBVersion < 16 {
|
|
|
|
err := m.updateSettingsToDBVersion16()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:15:18 +00:00
|
|
|
// Portainer 1.20.1
|
2019-01-17 21:00:18 +00:00
|
|
|
if m.currentDBVersion < 17 {
|
|
|
|
err := m.updateExtensionsToDBVersion17()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 03:51:11 +00:00
|
|
|
// Portainer 1.21.0
|
2019-05-24 06:04:58 +00:00
|
|
|
if m.currentDBVersion < 18 {
|
|
|
|
err := m.updateUsersToDBVersion18()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.updateEndpointsToDBVersion18()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.updateEndpointGroupsToDBVersion18()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.updateRegistriesToDBVersion18()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 02:01:49 +00:00
|
|
|
// Portainer 1.22.0
|
2019-07-25 22:38:07 +00:00
|
|
|
if m.currentDBVersion < 19 {
|
|
|
|
err := m.updateSettingsToDBVersion19()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 21:40:41 +00:00
|
|
|
// Portainer 1.22.1
|
2019-09-09 22:58:26 +00:00
|
|
|
if m.currentDBVersion < 20 {
|
|
|
|
err := m.updateUsersToDBVersion20()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-08 00:17:58 +00:00
|
|
|
|
|
|
|
err = m.updateSettingsToDBVersion20()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-08 03:18:32 +00:00
|
|
|
|
|
|
|
err = m.updateSchedulesToDBVersion20()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-09 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 04:05:36 +00:00
|
|
|
// Portainer 1.23.0
|
2019-11-12 23:41:42 +00:00
|
|
|
// DBVersion 21 is missing as it was shipped as via hotfix 1.22.2
|
|
|
|
if m.currentDBVersion < 22 {
|
|
|
|
err := m.updateResourceControlsToDBVersion22()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.updateUsersAndRolesToDBVersion22()
|
2019-10-30 23:12:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 02:34:32 +00:00
|
|
|
// Portainer 1.24.0
|
2020-03-29 09:54:14 +00:00
|
|
|
if m.currentDBVersion < 23 {
|
2020-05-14 02:14:28 +00:00
|
|
|
err := m.updateTagsToDBVersion23()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.updateEndpointsAndEndpointGroupsToDBVersion23()
|
2020-03-29 09:54:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 23:10:46 +00:00
|
|
|
// Portainer 1.24.1
|
2020-05-25 06:10:02 +00:00
|
|
|
if m.currentDBVersion < 24 {
|
|
|
|
err := m.updateSettingsToDB24()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 05:48:02 +00:00
|
|
|
// Portainer 2.0.0
|
2020-07-24 23:10:46 +00:00
|
|
|
if m.currentDBVersion < 25 {
|
|
|
|
err := m.updateSettingsToDB25()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-03 22:18:53 +00:00
|
|
|
|
|
|
|
err = m.updateStacksToDB24()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-24 23:10:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 08:09:06 +00:00
|
|
|
// Portainer 2.1.0
|
|
|
|
if m.currentDBVersion < 26 {
|
|
|
|
err := m.updateEndpointSettingsToDB25()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:18:05 +00:00
|
|
|
// Portainer 2.2.0
|
|
|
|
if m.currentDBVersion < 27 {
|
|
|
|
err := m.updateStackResourceControlToDB27()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 07:58:30 +00:00
|
|
|
// Portainer 2.6.0
|
|
|
|
if m.currentDBVersion < 30 {
|
2021-07-19 07:43:49 +00:00
|
|
|
err := m.migrateDBVersionToDB30()
|
2021-06-10 22:09:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
// Portainer 2.9.0
|
|
|
|
if m.currentDBVersion < 32 {
|
2021-07-19 07:43:49 +00:00
|
|
|
err := m.migrateDBVersionToDB32()
|
2021-07-14 09:15:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
if m.currentDBVersion < 33 {
|
|
|
|
if err := m.migrateDBVersionTo33(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 11:15:10 +00:00
|
|
|
return m.versionService.StoreDBVersion(portainer.DBVersion)
|
|
|
|
}
|