2018-06-19 11:15:10 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2021-02-22 22:14:52 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
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/internal"
|
2018-06-19 11:15:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BucketName represents the name of the bucket where this service stores data.
|
2020-08-12 05:10:28 +00:00
|
|
|
BucketName = "version"
|
|
|
|
versionKey = "DB_VERSION"
|
|
|
|
instanceKey = "INSTANCE_ID"
|
2021-02-22 22:14:52 +00:00
|
|
|
editionKey = "EDITION"
|
2018-06-19 11:15:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service represents a service to manage stored versions.
|
|
|
|
type Service struct {
|
2021-04-06 10:08:43 +00:00
|
|
|
connection *internal.DbConnection
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new instance of a service.
|
2021-04-06 10:08:43 +00:00
|
|
|
func NewService(connection *internal.DbConnection) (*Service, error) {
|
|
|
|
err := internal.CreateBucket(connection, BucketName)
|
2018-06-19 11:15:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
2021-04-06 10:08:43 +00:00
|
|
|
connection: connection,
|
2018-06-19 11:15:10 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBVersion retrieves the stored database version.
|
|
|
|
func (service *Service) DBVersion() (int, error) {
|
|
|
|
var data []byte
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
value := bucket.Get([]byte(versionKey))
|
|
|
|
if value == nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.ErrObjectNotFound
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, len(value))
|
|
|
|
copy(data, value)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.Atoi(string(data))
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:14:52 +00:00
|
|
|
// Edition retrieves the stored portainer edition.
|
|
|
|
func (service *Service) Edition() (portainer.SoftwareEdition, error) {
|
|
|
|
editionData, err := service.getKey(editionKey)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
edition, err := strconv.Atoi(string(editionData))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return portainer.SoftwareEdition(edition), nil
|
|
|
|
}
|
|
|
|
|
2018-06-19 11:15:10 +00:00
|
|
|
// StoreDBVersion store the database version.
|
|
|
|
func (service *Service) StoreDBVersion(version int) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
data := []byte(strconv.Itoa(version))
|
|
|
|
return bucket.Put([]byte(versionKey), data)
|
|
|
|
})
|
|
|
|
}
|
2020-08-12 05:10:28 +00:00
|
|
|
|
|
|
|
// InstanceID retrieves the stored instance ID.
|
|
|
|
func (service *Service) InstanceID() (string, error) {
|
|
|
|
var data []byte
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2020-08-12 05:10:28 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
value := bucket.Get([]byte(instanceKey))
|
|
|
|
if value == nil {
|
|
|
|
return errors.ErrObjectNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, len(value))
|
|
|
|
copy(data, value)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreInstanceID store the instance ID.
|
|
|
|
func (service *Service) StoreInstanceID(ID string) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2020-08-12 05:10:28 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
data := []byte(ID)
|
|
|
|
return bucket.Put([]byte(instanceKey), data)
|
|
|
|
})
|
|
|
|
}
|
2021-02-22 22:14:52 +00:00
|
|
|
|
|
|
|
func (service *Service) getKey(key string) ([]byte, error) {
|
|
|
|
var data []byte
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2021-02-22 22:14:52 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
value := bucket.Get([]byte(key))
|
|
|
|
if value == nil {
|
|
|
|
return errors.ErrObjectNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, len(value))
|
|
|
|
copy(data, value)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) setKey(key string, value string) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2021-02-22 22:14:52 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
data := []byte(value)
|
|
|
|
return bucket.Put([]byte(key), data)
|
|
|
|
})
|
|
|
|
}
|