mirror of
https://github.com/portainer/portainer.git
synced 2025-07-21 14:29:40 +02:00
* wip * wip: missing repository & tags removal * feat(registry): private registry management * style(plugin-details): update view * wip * wip * wip * feat(plugins): add license info * feat(plugins): browse feature preview * feat(registry-configure): add the ability to configure registry management * style(app): update text in app * feat(plugins): add plugin version number * feat(plugins): wip plugin upgrade process * feat(plugins): wip plugin upgrade * feat(plugins): add the ability to update a plugin * feat(plugins): init plugins at startup time * feat(plugins): add the ability to remove a plugin * feat(plugins): update to latest plugin definitions * feat(plugins): introduce plugin-tooltip component * refactor(app): relocate plugin files to app/plugins * feat(plugins): introduce PluginDefinitionsURL constant * feat(plugins): update the flags used by the plugins * feat(plugins): wip * feat(plugins): display a label when a plugin has expired * wip * feat(registry-creation): update registry creation logic * refactor(registry-creation): change name/ids for inputs * feat(api): pass registry type to management configuration * feat(api): unstrip /v2 in regsitry proxy * docs(api): add TODO * feat(store): mockup-1 * feat(store): mockup 2 * feat(store): mockup 2 * feat(store): update mockup-2 * feat(app): add unauthenticated event check * update gruntfile * style(support): update support views * style(support): update product views * refactor(extensions): refactor plugins to extensions * feat(extensions): add a deal property * feat(extensions): introduce ExtensionManager * style(extensions): update extension details style * feat(extensions): display license/company when enabling extension * feat(extensions): update extensions views * feat(extensions): use ProductId defined in extension schema * style(app): remove padding left for form section title elements * style(support): use per host model * refactor(extensions): multiple refactors related to extensions mecanism * feat(extensions): update tls file path for registry extension * feat(extensions): update registry management configuration * feat(extensions): send license in header to extension proxy * fix(proxy): fix invalid default loopback address * feat(extensions): add header X-RegistryManagement-ForceNew for specific operations * feat(extensions): add the ability to display screenshots * feat(extensions): center screenshots * style(extensions): tune style * feat(extensions-details): open full screen image on click (#2517) * feat(extension-details): show magnifying glass on images * feat(extensions): support extension logo * feat(extensions): update support logos * refactor(lint): fix lint issues
205 lines
6.1 KiB
Go
205 lines
6.1 KiB
Go
package exec
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"os/exec"
|
|
"path"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/orcaman/concurrent-map"
|
|
"github.com/portainer/portainer"
|
|
"github.com/portainer/portainer/http/client"
|
|
)
|
|
|
|
var extensionDownloadBaseURL = "https://portainer-io-assets.sfo2.digitaloceanspaces.com/extensions/"
|
|
|
|
var extensionBinaryMap = map[portainer.ExtensionID]string{
|
|
portainer.RegistryManagementExtension: "extension-registry-management",
|
|
}
|
|
|
|
// ExtensionManager represents a service used to
|
|
// manage extension processes.
|
|
type ExtensionManager struct {
|
|
processes cmap.ConcurrentMap
|
|
fileService portainer.FileService
|
|
extensionService portainer.ExtensionService
|
|
}
|
|
|
|
// NewExtensionManager returns a pointer to an ExtensionManager
|
|
func NewExtensionManager(fileService portainer.FileService, extensionService portainer.ExtensionService) *ExtensionManager {
|
|
return &ExtensionManager{
|
|
processes: cmap.New(),
|
|
fileService: fileService,
|
|
extensionService: extensionService,
|
|
}
|
|
}
|
|
|
|
func processKey(ID portainer.ExtensionID) string {
|
|
return strconv.Itoa(int(ID))
|
|
}
|
|
|
|
func buildExtensionURL(extension *portainer.Extension) string {
|
|
extensionURL := extensionDownloadBaseURL
|
|
extensionURL += extensionBinaryMap[extension.ID]
|
|
extensionURL += "-" + runtime.GOOS + "-" + runtime.GOARCH
|
|
extensionURL += "-" + extension.Version
|
|
extensionURL += ".zip"
|
|
return extensionURL
|
|
}
|
|
|
|
func buildExtensionPath(binaryPath string, extension *portainer.Extension) string {
|
|
|
|
extensionFilename := extensionBinaryMap[extension.ID]
|
|
extensionFilename += "-" + runtime.GOOS + "-" + runtime.GOARCH
|
|
extensionFilename += "-" + extension.Version
|
|
|
|
extensionPath := path.Join(
|
|
binaryPath,
|
|
extensionFilename)
|
|
|
|
return extensionPath
|
|
}
|
|
|
|
// FetchExtensionDefinitions will fetch the list of available
|
|
// extension definitions from the official Portainer assets server
|
|
func (manager *ExtensionManager) FetchExtensionDefinitions() ([]portainer.Extension, error) {
|
|
extensionData, err := client.Get(portainer.ExtensionDefinitionsURL, 30)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var extensions []portainer.Extension
|
|
err = json.Unmarshal(extensionData, &extensions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return extensions, nil
|
|
}
|
|
|
|
// EnableExtension will check for the existence of the extension binary on the filesystem
|
|
// first. If it does not exist, it will download it from the official Portainer assets server.
|
|
// After installing the binary on the filesystem, it will execute the binary in license check
|
|
// mode to validate the extension license. If the license is valid, it will then start
|
|
// the extension process and register it in the processes map.
|
|
func (manager *ExtensionManager) EnableExtension(extension *portainer.Extension, licenseKey string) error {
|
|
extensionBinaryPath := buildExtensionPath(manager.fileService.GetBinaryFolder(), extension)
|
|
extensionBinaryExists, err := manager.fileService.FileExists(extensionBinaryPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !extensionBinaryExists {
|
|
err := manager.downloadExtension(extension)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
licenseDetails, err := validateLicense(extensionBinaryPath, licenseKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
extension.License = portainer.LicenseInformation{
|
|
LicenseKey: licenseKey,
|
|
Company: licenseDetails[0],
|
|
Expiration: licenseDetails[1],
|
|
}
|
|
extension.Version = licenseDetails[2]
|
|
|
|
return manager.startExtensionProcess(extension, extensionBinaryPath)
|
|
}
|
|
|
|
// DisableExtension will retrieve the process associated to the extension
|
|
// from the processes map and kill the process. It will then remove the process
|
|
// from the processes map and remove the binary associated to the extension
|
|
// from the filesystem
|
|
func (manager *ExtensionManager) DisableExtension(extension *portainer.Extension) error {
|
|
process, ok := manager.processes.Get(processKey(extension.ID))
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
err := process.(*exec.Cmd).Process.Kill()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
manager.processes.Remove(processKey(extension.ID))
|
|
|
|
extensionBinaryPath := buildExtensionPath(manager.fileService.GetBinaryFolder(), extension)
|
|
return manager.fileService.RemoveDirectory(extensionBinaryPath)
|
|
}
|
|
|
|
// UpdateExtension will download the new extension binary from the official Portainer assets
|
|
// server, disable the previous extension via DisableExtension, trigger a license check
|
|
// and then start the extension process and add it to the processes map
|
|
func (manager *ExtensionManager) UpdateExtension(extension *portainer.Extension, version string) error {
|
|
oldVersion := extension.Version
|
|
|
|
extension.Version = version
|
|
err := manager.downloadExtension(extension)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
extension.Version = oldVersion
|
|
err = manager.DisableExtension(extension)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
extension.Version = version
|
|
extensionBinaryPath := buildExtensionPath(manager.fileService.GetBinaryFolder(), extension)
|
|
|
|
licenseDetails, err := validateLicense(extensionBinaryPath, extension.License.LicenseKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
extension.Version = licenseDetails[2]
|
|
|
|
return manager.startExtensionProcess(extension, extensionBinaryPath)
|
|
}
|
|
|
|
func (manager *ExtensionManager) downloadExtension(extension *portainer.Extension) error {
|
|
extensionURL := buildExtensionURL(extension)
|
|
|
|
data, err := client.Get(extensionURL, 30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return manager.fileService.ExtractExtensionArchive(data)
|
|
}
|
|
|
|
func validateLicense(binaryPath, licenseKey string) ([]string, error) {
|
|
licenseCheckProcess := exec.Command(binaryPath, "-license", licenseKey, "-check")
|
|
cmdOutput := &bytes.Buffer{}
|
|
licenseCheckProcess.Stdout = cmdOutput
|
|
|
|
err := licenseCheckProcess.Run()
|
|
if err != nil {
|
|
return nil, errors.New("Invalid extension license key")
|
|
}
|
|
|
|
output := string(cmdOutput.Bytes())
|
|
|
|
return strings.Split(output, "|"), nil
|
|
}
|
|
|
|
func (manager *ExtensionManager) startExtensionProcess(extension *portainer.Extension, binaryPath string) error {
|
|
extensionProcess := exec.Command(binaryPath, "-license", extension.License.LicenseKey)
|
|
err := extensionProcess.Start()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
manager.processes.Set(processKey(extension.ID), extensionProcess)
|
|
return nil
|
|
}
|