2018-06-11 13:13:19 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
2021-02-23 20:18:05 +00:00
|
|
|
"fmt"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-09-28 17:56:32 +00:00
|
|
|
"github.com/asaskevich/govalidator"
|
2021-09-07 00:37:26 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
2021-01-11 23:38:49 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2022-10-05 09:33:59 +00:00
|
|
|
"github.com/portainer/portainer/api/stacks/stackbuilders"
|
|
|
|
"github.com/portainer/portainer/api/stacks/stackutils"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type swarmStackFromFileContentPayload struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// Name of the stack
|
|
|
|
Name string `example:"myStack" validate:"required"`
|
|
|
|
// Swarm cluster identifier
|
|
|
|
SwarmID string `example:"jpofkc0i9uo9wtx1zesuk649w" validate:"required"`
|
|
|
|
// Content of the Stack file
|
|
|
|
StackFileContent string `example:"version: 3\n services:\n web:\n image:nginx" validate:"required"`
|
2021-09-20 00:14:22 +00:00
|
|
|
// A list of environment(endpoint) variables used during stack deployment
|
2021-02-23 03:21:39 +00:00
|
|
|
Env []portainer.Pair
|
2021-12-07 11:46:58 +00:00
|
|
|
// Whether the stack is from a app template
|
|
|
|
FromAppTemplate bool `example:"false"`
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *swarmStackFromFileContentPayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Name) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid stack name")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.SwarmID) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid Swarm ID")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.StackFileContent) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid stack file content")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
func createStackPayloadFromSwarmFileContentPayload(name string, swarmID string, fileContent string, env []portainer.Pair, fromAppTemplate bool) stackbuilders.StackPayload {
|
|
|
|
return stackbuilders.StackPayload{
|
|
|
|
Name: name,
|
|
|
|
SwarmID: swarmID,
|
|
|
|
StackFileContent: fileContent,
|
|
|
|
Env: env,
|
|
|
|
FromAppTemplate: fromAppTemplate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
func (handler *Handler) createSwarmStackFromFileContent(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
2018-06-11 13:13:19 +00:00
|
|
|
var payload swarmStackFromFileContentPayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 21:53:42 +00:00
|
|
|
payload.Name = handler.SwarmStackManager.NormalizeStackName(payload.Name)
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
isUnique, err := handler.checkUniqueStackNameInDocker(endpoint, payload.Name, 0, true)
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to check for name collision", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2021-02-23 20:18:05 +00:00
|
|
|
if !isUnique {
|
2021-10-01 03:56:34 +00:00
|
|
|
return stackExistsError(payload.Name)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-10-05 09:33:59 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve info from request context", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
stackPayload := createStackPayloadFromSwarmFileContentPayload(payload.Name, payload.SwarmID, payload.StackFileContent, payload.Env, payload.FromAppTemplate)
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
swarmStackBuilder := stackbuilders.CreateSwarmStackFileContentBuilder(securityContext,
|
|
|
|
handler.DataStore,
|
|
|
|
handler.FileService,
|
|
|
|
handler.StackDeployer)
|
2021-01-11 23:38:49 +00:00
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
stackBuilderDirector := stackbuilders.NewStackBuilderDirector(swarmStackBuilder)
|
|
|
|
stack, httpErr := stackBuilderDirector.Build(&stackPayload, endpoint)
|
|
|
|
if httpErr != nil {
|
|
|
|
return httpErr
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
return handler.decorateStackResponse(w, stack, userID)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type swarmStackFromGitRepositoryPayload struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// Name of the stack
|
|
|
|
Name string `example:"myStack" validate:"required"`
|
|
|
|
// Swarm cluster identifier
|
|
|
|
SwarmID string `example:"jpofkc0i9uo9wtx1zesuk649w" validate:"required"`
|
2021-09-20 00:14:22 +00:00
|
|
|
// A list of environment(endpoint) variables used during stack deployment
|
2021-02-23 03:21:39 +00:00
|
|
|
Env []portainer.Pair
|
|
|
|
|
|
|
|
// URL of a Git repository hosting the Stack file
|
|
|
|
RepositoryURL string `example:"https://github.com/openfaas/faas" validate:"required"`
|
|
|
|
// Reference name of a Git repository hosting the Stack file
|
|
|
|
RepositoryReferenceName string `example:"refs/heads/master"`
|
|
|
|
// Use basic authentication to clone the Git repository
|
|
|
|
RepositoryAuthentication bool `example:"true"`
|
|
|
|
// Username used in basic authentication. Required when RepositoryAuthentication is true.
|
|
|
|
RepositoryUsername string `example:"myGitUsername"`
|
|
|
|
// Password used in basic authentication. Required when RepositoryAuthentication is true.
|
|
|
|
RepositoryPassword string `example:"myGitPassword"`
|
2021-12-07 11:46:58 +00:00
|
|
|
// Whether the stack is from a app template
|
|
|
|
FromAppTemplate bool `example:"false"`
|
2021-02-23 03:21:39 +00:00
|
|
|
// Path to the Stack file inside the Git repository
|
2021-08-17 01:12:07 +00:00
|
|
|
ComposeFile string `example:"docker-compose.yml" default:"docker-compose.yml"`
|
|
|
|
// Applicable when deploying with multiple stack files
|
|
|
|
AdditionalFiles []string `example:"[nz.compose.yml, uat.compose.yml]"`
|
|
|
|
// Optional auto update configuration
|
|
|
|
AutoUpdate *portainer.StackAutoUpdate
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *swarmStackFromGitRepositoryPayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Name) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid stack name")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.SwarmID) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid Swarm ID")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid repository URL. Must correspond to a valid URL format")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2021-08-17 01:12:07 +00:00
|
|
|
if payload.RepositoryAuthentication && govalidator.IsNull(payload.RepositoryPassword) {
|
|
|
|
return errors.New("Invalid repository credentials. Password must be specified when authentication is enabled")
|
|
|
|
}
|
2022-10-05 09:33:59 +00:00
|
|
|
if err := stackutils.ValidateStackAutoUpdate(payload.AutoUpdate); err != nil {
|
2021-08-17 01:12:07 +00:00
|
|
|
return err
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
func createStackPayloadFromSwarmGitPayload(name, swarmID, repoUrl, repoReference, repoUsername, repoPassword string, repoAuthentication bool, composeFile string, additionalFiles []string, autoUpdate *portainer.StackAutoUpdate, env []portainer.Pair, fromAppTemplate bool) stackbuilders.StackPayload {
|
|
|
|
return stackbuilders.StackPayload{
|
|
|
|
Name: name,
|
|
|
|
SwarmID: swarmID,
|
|
|
|
RepositoryConfigPayload: stackbuilders.RepositoryConfigPayload{
|
|
|
|
URL: repoUrl,
|
|
|
|
ReferenceName: repoReference,
|
|
|
|
Authentication: repoAuthentication,
|
|
|
|
Username: repoUsername,
|
|
|
|
Password: repoPassword,
|
|
|
|
},
|
|
|
|
ComposeFile: composeFile,
|
|
|
|
AdditionalFiles: additionalFiles,
|
|
|
|
AutoUpdate: autoUpdate,
|
|
|
|
Env: env,
|
|
|
|
FromAppTemplate: fromAppTemplate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
func (handler *Handler) createSwarmStackFromGitRepository(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
2018-06-11 13:13:19 +00:00
|
|
|
var payload swarmStackFromGitRepositoryPayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 21:53:42 +00:00
|
|
|
payload.Name = handler.SwarmStackManager.NormalizeStackName(payload.Name)
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
isUnique, err := handler.checkUniqueStackNameInDocker(endpoint, payload.Name, 0, true)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to check for name collision", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2021-02-23 20:18:05 +00:00
|
|
|
if !isUnique {
|
2021-10-01 03:56:34 +00:00
|
|
|
return stackExistsError(payload.Name)
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//make sure the webhook ID is unique
|
|
|
|
if payload.AutoUpdate != nil && payload.AutoUpdate.Webhook != "" {
|
|
|
|
isUnique, err := handler.checkUniqueWebhookID(payload.AutoUpdate.Webhook)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to check for webhook ID collision", err)
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
if !isUnique {
|
2022-10-05 09:33:59 +00:00
|
|
|
return &httperror.HandlerError{StatusCode: http.StatusConflict, Message: fmt.Sprintf("Webhook ID: %s already exists", payload.AutoUpdate.Webhook), Err: stackutils.ErrWebhookIDAlreadyExists}
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-10-05 09:33:59 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve info from request context", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stackPayload := createStackPayloadFromSwarmGitPayload(payload.Name,
|
|
|
|
payload.SwarmID,
|
|
|
|
payload.RepositoryURL,
|
|
|
|
payload.RepositoryReferenceName,
|
|
|
|
payload.RepositoryUsername,
|
|
|
|
payload.RepositoryPassword,
|
|
|
|
payload.RepositoryAuthentication,
|
|
|
|
payload.ComposeFile,
|
|
|
|
payload.AdditionalFiles,
|
|
|
|
payload.AutoUpdate,
|
|
|
|
payload.Env,
|
|
|
|
payload.FromAppTemplate)
|
|
|
|
|
|
|
|
swarmStackBuilder := stackbuilders.CreateSwarmStackGitBuilder(securityContext,
|
|
|
|
handler.DataStore,
|
|
|
|
handler.FileService,
|
|
|
|
handler.GitService,
|
|
|
|
handler.Scheduler,
|
|
|
|
handler.StackDeployer)
|
|
|
|
|
|
|
|
stackBuilderDirector := stackbuilders.NewStackBuilderDirector(swarmStackBuilder)
|
|
|
|
stack, httpErr := stackBuilderDirector.Build(&stackPayload, endpoint)
|
|
|
|
if httpErr != nil {
|
|
|
|
return httpErr
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
return handler.decorateStackResponse(w, stack, userID)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type swarmStackFromFileUploadPayload struct {
|
|
|
|
Name string
|
|
|
|
SwarmID string
|
|
|
|
StackFileContent []byte
|
|
|
|
Env []portainer.Pair
|
|
|
|
}
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
func createStackPayloadFromSwarmFileUploadPayload(name, swarmID string, fileContentBytes []byte, env []portainer.Pair) stackbuilders.StackPayload {
|
|
|
|
return stackbuilders.StackPayload{
|
|
|
|
Name: name,
|
|
|
|
SwarmID: swarmID,
|
|
|
|
StackFileContentBytes: fileContentBytes,
|
|
|
|
Env: env,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
func (payload *swarmStackFromFileUploadPayload) Validate(r *http.Request) error {
|
|
|
|
name, err := request.RetrieveMultiPartFormValue(r, "Name", false)
|
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid stack name")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
payload.Name = name
|
|
|
|
|
|
|
|
swarmID, err := request.RetrieveMultiPartFormValue(r, "SwarmID", false)
|
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid Swarm ID")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
payload.SwarmID = swarmID
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
composeFileContent, _, err := request.RetrieveMultiPartFormFile(r, "file")
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid Compose file. Ensure that the Compose file is uploaded correctly")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
payload.StackFileContent = composeFileContent
|
|
|
|
|
|
|
|
var env []portainer.Pair
|
|
|
|
err = request.RetrieveMultiPartFormJSONValue(r, "Env", &env, true)
|
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid Env parameter")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
payload.Env = env
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
func (handler *Handler) createSwarmStackFromFileUpload(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
2018-06-11 13:13:19 +00:00
|
|
|
payload := &swarmStackFromFileUploadPayload{}
|
|
|
|
err := payload.Validate(r)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 21:53:42 +00:00
|
|
|
payload.Name = handler.SwarmStackManager.NormalizeStackName(payload.Name)
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
isUnique, err := handler.checkUniqueStackNameInDocker(endpoint, payload.Name, 0, true)
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to check for name collision", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2021-02-23 20:18:05 +00:00
|
|
|
if !isUnique {
|
2021-10-01 03:56:34 +00:00
|
|
|
return stackExistsError(payload.Name)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
2022-10-05 09:33:59 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve info from request context", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2022-09-28 17:56:32 +00:00
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
stackPayload := createStackPayloadFromSwarmFileUploadPayload(payload.Name, payload.SwarmID, payload.StackFileContent, payload.Env)
|
2020-07-22 18:38:45 +00:00
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
swarmStackBuilder := stackbuilders.CreateSwarmStackFileUploadBuilder(securityContext,
|
|
|
|
handler.DataStore,
|
|
|
|
handler.FileService,
|
|
|
|
handler.StackDeployer)
|
2021-08-17 01:12:07 +00:00
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
stackBuilderDirector := stackbuilders.NewStackBuilderDirector(swarmStackBuilder)
|
|
|
|
stack, httpErr := stackBuilderDirector.Build(&stackPayload, endpoint)
|
|
|
|
if httpErr != nil {
|
|
|
|
return httpErr
|
2019-10-07 03:12:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
return handler.decorateStackResponse(w, stack, userID)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|