e15b908983
* EE-319: backup endpoint (#193) * feat(backup): * add an orbiter to block writes while backup * add backup handler * add an ability to tar.gz a dir * add aes encryption support * EE-320: restore endpoint (#196) * feat(backup): * add restore handler * re-init system state after restore * feat(backup): Update server to respect readonly lock (#199) * feat(backup): EE-322 Add backup and restore screen (#198) Co-authored-by: Simon Meng <simon.meng@portainer.io> * name archive as portainer-backup_yyyy-mm-dd_hh-mm-ss * backup custom templates and edge jobs * restart http and proxy servers after restore to re-init internal state * feat(backup): EE-322 hide password field if password protect toggle is off * feat(backup): EE-322 add tooltip for password field of restore backup * feat(backup): EE-322 wait for backend restart after restoring * Shutdown background go-routines * changed restore err message when cannot extract * fix: symlinks are ignored from backups * replace single admin check with a restartable monitor (#238) * clean log Co-authored-by: Maxime Bajeux <max.bajeux@gmail.com> Co-authored-by: cong meng <mcpacino@gmail.com> Co-authored-by: Simon Meng <simon.meng@portainer.io>
146 lines
3.7 KiB
Go
146 lines
3.7 KiB
Go
package endpoint
|
|
|
|
import (
|
|
"github.com/boltdb/bolt"
|
|
portainer "github.com/portainer/portainer/api"
|
|
"github.com/portainer/portainer/api/bolt/internal"
|
|
)
|
|
|
|
const (
|
|
// BucketName represents the name of the bucket where this service stores data.
|
|
BucketName = "endpoints"
|
|
)
|
|
|
|
// Service represents a service for managing endpoint data.
|
|
type Service struct {
|
|
connection *internal.DbConnection
|
|
}
|
|
|
|
// NewService creates a new instance of a service.
|
|
func NewService(connection *internal.DbConnection) (*Service, error) {
|
|
err := internal.CreateBucket(connection, BucketName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Service{
|
|
connection: connection,
|
|
}, nil
|
|
}
|
|
|
|
// Endpoint returns an endpoint by ID.
|
|
func (service *Service) Endpoint(ID portainer.EndpointID) (*portainer.Endpoint, error) {
|
|
var endpoint portainer.Endpoint
|
|
identifier := internal.Itob(int(ID))
|
|
|
|
err := internal.GetObject(service.connection, BucketName, identifier, &endpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &endpoint, nil
|
|
}
|
|
|
|
// UpdateEndpoint updates an endpoint.
|
|
func (service *Service) UpdateEndpoint(ID portainer.EndpointID, endpoint *portainer.Endpoint) error {
|
|
identifier := internal.Itob(int(ID))
|
|
return internal.UpdateObject(service.connection, BucketName, identifier, endpoint)
|
|
}
|
|
|
|
// DeleteEndpoint deletes an endpoint.
|
|
func (service *Service) DeleteEndpoint(ID portainer.EndpointID) error {
|
|
identifier := internal.Itob(int(ID))
|
|
return internal.DeleteObject(service.connection, BucketName, identifier)
|
|
}
|
|
|
|
// Endpoints return an array containing all the endpoints.
|
|
func (service *Service) Endpoints() ([]portainer.Endpoint, error) {
|
|
var endpoints = make([]portainer.Endpoint, 0)
|
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
cursor := bucket.Cursor()
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
var endpoint portainer.Endpoint
|
|
err := internal.UnmarshalObjectWithJsoniter(v, &endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
endpoints = append(endpoints, endpoint)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return endpoints, err
|
|
}
|
|
|
|
// CreateEndpoint assign an ID to a new endpoint and saves it.
|
|
func (service *Service) CreateEndpoint(endpoint *portainer.Endpoint) error {
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
// We manually manage sequences for endpoints
|
|
err := bucket.SetSequence(uint64(endpoint.ID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
data, err := internal.MarshalObject(endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return bucket.Put(internal.Itob(int(endpoint.ID)), data)
|
|
})
|
|
}
|
|
|
|
// GetNextIdentifier returns the next identifier for an endpoint.
|
|
func (service *Service) GetNextIdentifier() int {
|
|
return internal.GetNextIdentifier(service.connection, BucketName)
|
|
}
|
|
|
|
// Synchronize creates, updates and deletes endpoints inside a single transaction.
|
|
func (service *Service) Synchronize(toCreate, toUpdate, toDelete []*portainer.Endpoint) error {
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
for _, endpoint := range toCreate {
|
|
id, _ := bucket.NextSequence()
|
|
endpoint.ID = portainer.EndpointID(id)
|
|
|
|
data, err := internal.MarshalObject(endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = bucket.Put(internal.Itob(int(endpoint.ID)), data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for _, endpoint := range toUpdate {
|
|
data, err := internal.MarshalObject(endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = bucket.Put(internal.Itob(int(endpoint.ID)), data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for _, endpoint := range toDelete {
|
|
err := bucket.Delete(internal.Itob(int(endpoint.ID)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|