2019-02-21 16:58:25 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateProjectRequest struct {
|
|
|
|
Name string
|
2019-03-14 13:36:18 +00:00
|
|
|
ParentID string
|
2019-02-21 16:58:25 +00:00
|
|
|
RemoteSourceName string
|
2019-04-03 09:07:54 +00:00
|
|
|
RepoPath string
|
2019-03-14 13:36:18 +00:00
|
|
|
CurrentUserID string
|
2019-02-21 16:58:25 +00:00
|
|
|
SkipSSHHostKeyCheck bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRequest) (*types.Project, error) {
|
|
|
|
if !util.ValidateName(req.Name) {
|
2019-04-08 10:08:31 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid project name %q", req.Name))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSourceByName(ctx, req.RemoteSourceName)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
c.log.Errorf("err: %+v", err)
|
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("rs: %s", util.Dump(rs))
|
|
|
|
var la *types.LinkedAccount
|
|
|
|
for _, v := range user.LinkedAccounts {
|
|
|
|
if v.RemoteSourceID == rs.ID {
|
|
|
|
la = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.log.Infof("la: %s", util.Dump(la))
|
|
|
|
if la == nil {
|
|
|
|
return nil, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name)
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
gitsource, err := c.GetGitSource(ctx, rs, user.UserName, la)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create gitsource client")
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
repo, err := gitsource.GetRepoInfo(req.RepoPath)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to get repository info from gitsource")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Infof("generating ssh key pairs")
|
|
|
|
privateKey, _, err := util.GenSSHKeyPair(4096)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to generate ssh key pair")
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
parentID := req.ParentID
|
|
|
|
if parentID == "" {
|
|
|
|
// create project in current user namespace
|
|
|
|
parentID = path.Join("user", user.UserName)
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
p := &types.Project{
|
2019-03-14 13:36:18 +00:00
|
|
|
Name: req.Name,
|
|
|
|
Parent: types.Parent{
|
|
|
|
Type: types.ConfigTypeProjectGroup,
|
|
|
|
ID: parentID,
|
|
|
|
},
|
2019-02-21 16:58:25 +00:00
|
|
|
LinkedAccountID: la.ID,
|
2019-04-11 15:11:17 +00:00
|
|
|
RepositoryID: repo.ID,
|
|
|
|
RepositoryPath: req.RepoPath,
|
2019-02-21 16:58:25 +00:00
|
|
|
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
|
|
|
|
SSHPrivateKey: string(privateKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Infof("creating project")
|
2019-04-09 12:53:00 +00:00
|
|
|
p, resp, err = c.configstoreClient.CreateProject(ctx, p)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create project"))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("project %s created, ID: %s", p.Name, p.ID)
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
return p, c.SetupProject(ctx, rs, user, la, p)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, user *types.User, la *types.LinkedAccount, project *types.Project) error {
|
|
|
|
gitsource, err := c.GetGitSource(ctx, rs, user.UserName, la)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create gitsource client")
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-04-11 15:11:17 +00:00
|
|
|
pubKey, err := util.ExtractPublicKey([]byte(project.SSHPrivateKey))
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-03 09:07:54 +00:00
|
|
|
return errors.Wrapf(err, "failed to extract public key")
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 15:11:17 +00:00
|
|
|
webhookURL := fmt.Sprintf("%s/webhooks?projectid=%s", c.apiExposedURL, project.ID)
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-02-28 16:19:53 +00:00
|
|
|
// generate deploy keys and webhooks containing the agola project id so we
|
|
|
|
// can have multiple projects referencing the same remote repository and this
|
|
|
|
// will trigger multiple different runs
|
2019-04-11 15:11:17 +00:00
|
|
|
deployKeyName := fmt.Sprintf("agola deploy key - %s", project.ID)
|
2019-02-21 16:58:25 +00:00
|
|
|
c.log.Infof("creating/updating deploy key: %s", string(pubKey))
|
2019-04-11 15:11:17 +00:00
|
|
|
if err := gitsource.UpdateDeployKey(project.RepositoryPath, deployKeyName, string(pubKey), true); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
return errors.Wrapf(err, "failed to create deploy key")
|
|
|
|
}
|
|
|
|
c.log.Infof("deleting existing webhooks")
|
2019-04-11 15:11:17 +00:00
|
|
|
if err := gitsource.DeleteRepoWebhook(project.RepositoryPath, webhookURL); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
return errors.Wrapf(err, "failed to delete repository webhook")
|
|
|
|
}
|
|
|
|
c.log.Infof("creating webhook to url: %s", webhookURL)
|
2019-04-11 15:11:17 +00:00
|
|
|
if err := gitsource.CreateRepoWebhook(project.RepositoryPath, webhookURL, ""); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
return errors.Wrapf(err, "failed to create repository webhook")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:04:55 +00:00
|
|
|
func (c *CommandHandler) ReconfigProject(ctx context.Context, projectRef string) error {
|
2019-04-09 12:53:00 +00:00
|
|
|
p, resp, err := c.configstoreClient.GetProject(ctx, projectRef)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", projectRef))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
user, resp, err := c.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user with linked account id %q", p.LinkedAccountID))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
la := user.LinkedAccounts[p.LinkedAccountID]
|
|
|
|
c.log.Infof("la: %s", util.Dump(la))
|
|
|
|
if la == nil {
|
|
|
|
return errors.Errorf("linked account %q in user %q doesn't exist", p.LinkedAccountID, user.UserName)
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 15:11:17 +00:00
|
|
|
// TODO(sgotti) update project repo path if the remote let us query by repository id
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
return c.SetupProject(ctx, rs, user, la, p)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|