mirror of https://github.com/portainer/portainer
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
6.4 KiB
207 lines
6.4 KiB
8 years ago
|
package cron
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
8 years ago
|
"strings"
|
||
8 years ago
|
|
||
|
"github.com/portainer/portainer"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
endpointSyncJob struct {
|
||
|
endpointService portainer.EndpointService
|
||
|
endpointFilePath string
|
||
|
}
|
||
|
|
||
|
synchronization struct {
|
||
|
endpointsToCreate []*portainer.Endpoint
|
||
|
endpointsToUpdate []*portainer.Endpoint
|
||
|
endpointsToDelete []*portainer.Endpoint
|
||
|
}
|
||
7 years ago
|
|
||
|
fileEndpoint struct {
|
||
|
Name string `json:"Name"`
|
||
|
URL string `json:"URL"`
|
||
|
TLS bool `json:"TLS,omitempty"`
|
||
|
TLSSkipVerify bool `json:"TLSSkipVerify,omitempty"`
|
||
|
TLSCACert string `json:"TLSCACert,omitempty"`
|
||
|
TLSCert string `json:"TLSCert,omitempty"`
|
||
|
TLSKey string `json:"TLSKey,omitempty"`
|
||
|
}
|
||
8 years ago
|
)
|
||
|
|
||
|
const (
|
||
|
// ErrEmptyEndpointArray is an error raised when the external endpoint source array is empty.
|
||
|
ErrEmptyEndpointArray = portainer.Error("External endpoint source is empty")
|
||
|
)
|
||
|
|
||
|
func newEndpointSyncJob(endpointFilePath string, endpointService portainer.EndpointService) endpointSyncJob {
|
||
|
return endpointSyncJob{
|
||
|
endpointService: endpointService,
|
||
|
endpointFilePath: endpointFilePath,
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
func endpointSyncError(err error) bool {
|
||
8 years ago
|
if err != nil {
|
||
6 years ago
|
log.Printf("cron error: synchronization job error (err=%s)\n", err)
|
||
8 years ago
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func isValidEndpoint(endpoint *portainer.Endpoint) bool {
|
||
|
if endpoint.Name != "" && endpoint.URL != "" {
|
||
8 years ago
|
if !strings.HasPrefix(endpoint.URL, "unix://") && !strings.HasPrefix(endpoint.URL, "tcp://") {
|
||
|
return false
|
||
|
}
|
||
8 years ago
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
7 years ago
|
func convertFileEndpoints(fileEndpoints []fileEndpoint) []portainer.Endpoint {
|
||
|
convertedEndpoints := make([]portainer.Endpoint, 0)
|
||
|
|
||
|
for _, e := range fileEndpoints {
|
||
|
endpoint := portainer.Endpoint{
|
||
|
Name: e.Name,
|
||
|
URL: e.URL,
|
||
|
TLSConfig: portainer.TLSConfiguration{},
|
||
|
}
|
||
|
if e.TLS {
|
||
|
endpoint.TLSConfig.TLS = true
|
||
|
endpoint.TLSConfig.TLSSkipVerify = e.TLSSkipVerify
|
||
|
endpoint.TLSConfig.TLSCACertPath = e.TLSCACert
|
||
|
endpoint.TLSConfig.TLSCertPath = e.TLSCert
|
||
|
endpoint.TLSConfig.TLSKeyPath = e.TLSKey
|
||
|
}
|
||
|
convertedEndpoints = append(convertedEndpoints, endpoint)
|
||
|
}
|
||
|
|
||
|
return convertedEndpoints
|
||
|
}
|
||
|
|
||
8 years ago
|
func endpointExists(endpoint *portainer.Endpoint, endpoints []portainer.Endpoint) int {
|
||
|
for idx, v := range endpoints {
|
||
|
if endpoint.Name == v.Name && isValidEndpoint(&v) {
|
||
|
return idx
|
||
|
}
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
func mergeEndpointIfRequired(original, updated *portainer.Endpoint) *portainer.Endpoint {
|
||
|
var endpoint *portainer.Endpoint
|
||
7 years ago
|
if original.URL != updated.URL || original.TLSConfig.TLS != updated.TLSConfig.TLS ||
|
||
|
(updated.TLSConfig.TLS && original.TLSConfig.TLSSkipVerify != updated.TLSConfig.TLSSkipVerify) ||
|
||
|
(updated.TLSConfig.TLS && original.TLSConfig.TLSCACertPath != updated.TLSConfig.TLSCACertPath) ||
|
||
|
(updated.TLSConfig.TLS && original.TLSConfig.TLSCertPath != updated.TLSConfig.TLSCertPath) ||
|
||
|
(updated.TLSConfig.TLS && original.TLSConfig.TLSKeyPath != updated.TLSConfig.TLSKeyPath) {
|
||
8 years ago
|
endpoint = original
|
||
|
endpoint.URL = updated.URL
|
||
7 years ago
|
if updated.TLSConfig.TLS {
|
||
|
endpoint.TLSConfig.TLS = true
|
||
|
endpoint.TLSConfig.TLSSkipVerify = updated.TLSConfig.TLSSkipVerify
|
||
|
endpoint.TLSConfig.TLSCACertPath = updated.TLSConfig.TLSCACertPath
|
||
|
endpoint.TLSConfig.TLSCertPath = updated.TLSConfig.TLSCertPath
|
||
|
endpoint.TLSConfig.TLSKeyPath = updated.TLSConfig.TLSKeyPath
|
||
8 years ago
|
} else {
|
||
7 years ago
|
endpoint.TLSConfig.TLS = false
|
||
|
endpoint.TLSConfig.TLSSkipVerify = false
|
||
|
endpoint.TLSConfig.TLSCACertPath = ""
|
||
|
endpoint.TLSConfig.TLSCertPath = ""
|
||
|
endpoint.TLSConfig.TLSKeyPath = ""
|
||
8 years ago
|
}
|
||
|
}
|
||
|
return endpoint
|
||
|
}
|
||
|
|
||
|
func (sync synchronization) requireSync() bool {
|
||
|
if len(sync.endpointsToCreate) != 0 || len(sync.endpointsToUpdate) != 0 || len(sync.endpointsToDelete) != 0 {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// TMP: endpointSyncJob method to access logger, should be generic
|
||
|
func (job endpointSyncJob) prepareSyncData(storedEndpoints, fileEndpoints []portainer.Endpoint) *synchronization {
|
||
|
endpointsToCreate := make([]*portainer.Endpoint, 0)
|
||
|
endpointsToUpdate := make([]*portainer.Endpoint, 0)
|
||
|
endpointsToDelete := make([]*portainer.Endpoint, 0)
|
||
|
|
||
|
for idx := range storedEndpoints {
|
||
|
fidx := endpointExists(&storedEndpoints[idx], fileEndpoints)
|
||
|
if fidx != -1 {
|
||
|
endpoint := mergeEndpointIfRequired(&storedEndpoints[idx], &fileEndpoints[fidx])
|
||
|
if endpoint != nil {
|
||
6 years ago
|
log.Printf("New definition for a stored endpoint found in file, updating database. [name: %v] [url: %v]\n", endpoint.Name, endpoint.URL)
|
||
8 years ago
|
endpointsToUpdate = append(endpointsToUpdate, endpoint)
|
||
|
}
|
||
|
} else {
|
||
6 years ago
|
log.Printf("Stored endpoint not found in file (definition might be invalid), removing from database. [name: %v] [url: %v]", storedEndpoints[idx].Name, storedEndpoints[idx].URL)
|
||
8 years ago
|
endpointsToDelete = append(endpointsToDelete, &storedEndpoints[idx])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for idx, endpoint := range fileEndpoints {
|
||
7 years ago
|
if !isValidEndpoint(&endpoint) {
|
||
6 years ago
|
log.Printf("Invalid file endpoint definition, skipping. [name: %v] [url: %v]", endpoint.Name, endpoint.URL)
|
||
8 years ago
|
continue
|
||
|
}
|
||
|
sidx := endpointExists(&fileEndpoints[idx], storedEndpoints)
|
||
|
if sidx == -1 {
|
||
6 years ago
|
log.Printf("File endpoint not found in database, adding to database. [name: %v] [url: %v]", fileEndpoints[idx].Name, fileEndpoints[idx].URL)
|
||
8 years ago
|
endpointsToCreate = append(endpointsToCreate, &fileEndpoints[idx])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return &synchronization{
|
||
|
endpointsToCreate: endpointsToCreate,
|
||
|
endpointsToUpdate: endpointsToUpdate,
|
||
|
endpointsToDelete: endpointsToDelete,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (job endpointSyncJob) Sync() error {
|
||
|
data, err := ioutil.ReadFile(job.endpointFilePath)
|
||
6 years ago
|
if endpointSyncError(err) {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
var fileEndpoints []fileEndpoint
|
||
8 years ago
|
err = json.Unmarshal(data, &fileEndpoints)
|
||
6 years ago
|
if endpointSyncError(err) {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
if len(fileEndpoints) == 0 {
|
||
|
return ErrEmptyEndpointArray
|
||
|
}
|
||
|
|
||
|
storedEndpoints, err := job.endpointService.Endpoints()
|
||
6 years ago
|
if endpointSyncError(err) {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
convertedFileEndpoints := convertFileEndpoints(fileEndpoints)
|
||
|
|
||
|
sync := job.prepareSyncData(storedEndpoints, convertedFileEndpoints)
|
||
8 years ago
|
if sync.requireSync() {
|
||
|
err = job.endpointService.Synchronize(sync.endpointsToCreate, sync.endpointsToUpdate, sync.endpointsToDelete)
|
||
6 years ago
|
if endpointSyncError(err) {
|
||
8 years ago
|
return err
|
||
|
}
|
||
6 years ago
|
log.Printf("Endpoint synchronization ended. [created: %v] [updated: %v] [deleted: %v]", len(sync.endpointsToCreate), len(sync.endpointsToUpdate), len(sync.endpointsToDelete))
|
||
8 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (job endpointSyncJob) Run() {
|
||
6 years ago
|
log.Println("cron: synchronization job started")
|
||
8 years ago
|
err := job.Sync()
|
||
6 years ago
|
endpointSyncError(err)
|
||
8 years ago
|
}
|