2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-10 19:09:14 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-10 19:09:14 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-10 19:09:14 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-06-29 22:51:22 +00:00
|
|
|
package main
|
2017-05-03 21:25:33 +00:00
|
|
|
|
|
|
|
import (
|
2021-11-25 16:15:36 +00:00
|
|
|
"context"
|
2024-06-27 14:52:09 +00:00
|
|
|
"encoding/base32"
|
|
|
|
"errors"
|
2017-06-28 17:21:22 +00:00
|
|
|
"fmt"
|
2024-07-13 11:11:07 +00:00
|
|
|
"net/url"
|
2021-10-28 19:02:43 +00:00
|
|
|
"os"
|
2024-07-13 11:11:07 +00:00
|
|
|
"strings"
|
2017-09-20 19:29:57 +00:00
|
|
|
"time"
|
2017-06-28 17:21:22 +00:00
|
|
|
|
2024-06-27 14:52:09 +00:00
|
|
|
"github.com/gorilla/securecookie"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2024-06-04 06:30:54 +00:00
|
|
|
prometheus_auto "github.com/prometheus/client_golang/prometheus/promauto"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 07:25:13 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/cache"
|
2024-07-13 11:11:07 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/setup"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/logging"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/pubsub"
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/queue"
|
2024-07-13 11:11:07 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/services"
|
2024-06-06 12:34:57 +00:00
|
|
|
logService "go.woodpecker-ci.org/woodpecker/v2/server/services/log"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/services/log/file"
|
2024-07-13 11:11:07 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/services/permissions"
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/datastore"
|
2024-06-27 14:52:09 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"
|
2024-07-13 11:11:07 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/constant"
|
2017-05-03 21:25:33 +00:00
|
|
|
)
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
func setupStore(c *cli.Context) (store.Store, error) {
|
2021-10-30 12:53:24 +00:00
|
|
|
datasource := c.String("datasource")
|
|
|
|
driver := c.String("driver")
|
2023-07-14 23:15:13 +00:00
|
|
|
xorm := store.XORM{
|
|
|
|
Log: c.Bool("log-xorm"),
|
|
|
|
ShowSQL: c.Bool("log-xorm-sql"),
|
|
|
|
}
|
2021-10-30 12:53:24 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if driver == "sqlite3" {
|
|
|
|
if datastore.SupportedDriver("sqlite3") {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Debug().Msg("server has sqlite3 support")
|
2021-11-13 19:18:06 +00:00
|
|
|
} else {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Debug().Msg("server was built without sqlite3 support!")
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !datastore.SupportedDriver(driver) {
|
2024-01-10 14:34:44 +00:00
|
|
|
return nil, fmt.Errorf("database driver '%s' not supported", driver)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if driver == "sqlite3" {
|
2023-07-28 11:31:25 +00:00
|
|
|
if err := checkSqliteFileExist(datasource); err != nil {
|
2024-01-10 14:34:44 +00:00
|
|
|
return nil, fmt.Errorf("check sqlite file: %w", err)
|
2021-10-30 12:53:24 +00:00
|
|
|
}
|
2021-10-28 19:02:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
opts := &store.Opts{
|
2021-10-30 12:53:24 +00:00
|
|
|
Driver: driver,
|
|
|
|
Config: datasource,
|
2023-07-14 23:15:13 +00:00
|
|
|
XORM: xorm,
|
2021-10-19 09:44:49 +00:00
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
log.Trace().Msgf("setup datastore: %#v", *opts)
|
|
|
|
store, err := datastore.NewEngine(opts)
|
|
|
|
if err != nil {
|
2024-01-10 14:34:44 +00:00
|
|
|
return nil, fmt.Errorf("could not open datastore: %w", err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
if err := store.Migrate(c.Bool("migrations-allow-long")); err != nil {
|
2024-01-10 14:34:44 +00:00
|
|
|
return nil, fmt.Errorf("could not migrate datastore: %w", err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
return store, nil
|
2017-05-03 21:25:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 11:31:25 +00:00
|
|
|
func checkSqliteFileExist(path string) error {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
log.Warn().Msgf("no sqlite3 file found, will create one at '%s'", path)
|
|
|
|
return nil
|
2021-10-30 12:53:24 +00:00
|
|
|
}
|
2023-07-28 11:31:25 +00:00
|
|
|
return err
|
2021-10-28 19:02:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 00:02:08 +00:00
|
|
|
func setupQueue(c *cli.Context, s store.Store) queue.Queue {
|
2021-12-11 12:15:04 +00:00
|
|
|
return queue.WithTaskStore(queue.New(c.Context), s)
|
2017-05-04 00:02:08 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
func setupMembershipService(_ *cli.Context, _store store.Store) cache.MembershipService {
|
|
|
|
return cache.NewMembershipService(_store)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 22:51:22 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
func setupMetrics(g *errgroup.Group, _store store.Store) {
|
2024-06-04 06:30:54 +00:00
|
|
|
pendingSteps := prometheus_auto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 06:34:57 +00:00
|
|
|
Namespace: "woodpecker",
|
2022-10-28 15:38:53 +00:00
|
|
|
Name: "pending_steps",
|
|
|
|
Help: "Total number of pending pipeline steps.",
|
2019-05-30 09:11:14 +00:00
|
|
|
})
|
2024-06-04 06:30:54 +00:00
|
|
|
waitingSteps := prometheus_auto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 06:34:57 +00:00
|
|
|
Namespace: "woodpecker",
|
2022-10-28 15:38:53 +00:00
|
|
|
Name: "waiting_steps",
|
2022-10-18 01:24:12 +00:00
|
|
|
Help: "Total number of pipeline waiting on deps.",
|
2019-07-10 08:00:38 +00:00
|
|
|
})
|
2024-06-04 06:30:54 +00:00
|
|
|
runningSteps := prometheus_auto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 06:34:57 +00:00
|
|
|
Namespace: "woodpecker",
|
2022-10-28 15:38:53 +00:00
|
|
|
Name: "running_steps",
|
|
|
|
Help: "Total number of running pipeline steps.",
|
2019-05-30 09:11:14 +00:00
|
|
|
})
|
2024-06-04 06:30:54 +00:00
|
|
|
workers := prometheus_auto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 06:34:57 +00:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 09:11:14 +00:00
|
|
|
Name: "worker_count",
|
|
|
|
Help: "Total number of workers.",
|
|
|
|
})
|
2024-06-04 06:30:54 +00:00
|
|
|
pipelines := prometheus_auto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 06:34:57 +00:00
|
|
|
Namespace: "woodpecker",
|
2022-10-18 01:24:12 +00:00
|
|
|
Name: "pipeline_total_count",
|
|
|
|
Help: "Total number of pipelines.",
|
2019-05-30 09:11:14 +00:00
|
|
|
})
|
2024-06-04 06:30:54 +00:00
|
|
|
users := prometheus_auto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 06:34:57 +00:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 09:11:14 +00:00
|
|
|
Name: "user_count",
|
|
|
|
Help: "Total number of users.",
|
|
|
|
})
|
2024-06-04 06:30:54 +00:00
|
|
|
repos := prometheus_auto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 06:34:57 +00:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 09:11:14 +00:00
|
|
|
Name: "repo_count",
|
|
|
|
Help: "Total number of repos.",
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
for {
|
2021-11-25 16:15:36 +00:00
|
|
|
stats := server.Config.Services.Queue.Info(context.TODO())
|
2022-10-28 15:38:53 +00:00
|
|
|
pendingSteps.Set(float64(stats.Stats.Pending))
|
|
|
|
waitingSteps.Set(float64(stats.Stats.WaitingOnDeps))
|
|
|
|
runningSteps.Set(float64(stats.Stats.Running))
|
2019-05-30 09:11:14 +00:00
|
|
|
workers.Set(float64(stats.Stats.Workers))
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
g.Go(func() error {
|
|
|
|
for {
|
2021-12-01 13:22:06 +00:00
|
|
|
repoCount, _ := _store.GetRepoCount()
|
|
|
|
userCount, _ := _store.GetUserCount()
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineCount, _ := _store.GetPipelineCount()
|
|
|
|
pipelines.Set(float64(pipelineCount))
|
2019-05-30 09:11:14 +00:00
|
|
|
users.Set(float64(userCount))
|
|
|
|
repos.Set(float64(repoCount))
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-06-06 12:34:57 +00:00
|
|
|
|
|
|
|
func setupLogStore(c *cli.Context, s store.Store) (logService.Service, error) {
|
|
|
|
switch c.String("log-store") {
|
|
|
|
case "file":
|
|
|
|
return file.NewLogStore(c.String("log-store-file-path"))
|
|
|
|
default:
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 14:52:09 +00:00
|
|
|
|
|
|
|
const jwtSecretID = "jwt-secret"
|
|
|
|
|
|
|
|
func setupJWTSecret(_store store.Store) (string, error) {
|
|
|
|
jwtSecret, err := _store.ServerConfigGet(jwtSecretID)
|
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
jwtSecret := base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
)
|
|
|
|
err = _store.ServerConfigSet(jwtSecretID, jwtSecret)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
log.Debug().Msg("created jwt secret")
|
|
|
|
return jwtSecret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return jwtSecret, nil
|
|
|
|
}
|
2024-07-13 11:11:07 +00:00
|
|
|
|
|
|
|
func setupEvilGlobals(c *cli.Context, s store.Store) error {
|
|
|
|
// services
|
|
|
|
server.Config.Services.Queue = setupQueue(c, s)
|
|
|
|
server.Config.Services.Logs = logging.New()
|
|
|
|
server.Config.Services.Pubsub = pubsub.New()
|
|
|
|
server.Config.Services.Membership = setupMembershipService(c, s)
|
|
|
|
serviceManager, err := services.NewManager(c, s, setup.Forge)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not setup service manager: %w", err)
|
|
|
|
}
|
|
|
|
server.Config.Services.Manager = serviceManager
|
|
|
|
|
|
|
|
server.Config.Services.LogStore, err = setupLogStore(c, s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not setup log store: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// authentication
|
|
|
|
server.Config.Pipeline.AuthenticatePublicRepos = c.Bool("authenticate-public-repos")
|
|
|
|
|
|
|
|
// Cloning
|
|
|
|
server.Config.Pipeline.DefaultCloneImage = c.String("default-clone-image")
|
|
|
|
constant.TrustedCloneImages = append(constant.TrustedCloneImages, server.Config.Pipeline.DefaultCloneImage)
|
|
|
|
|
|
|
|
// Execution
|
|
|
|
_events := c.StringSlice("default-cancel-previous-pipeline-events")
|
|
|
|
events := make([]model.WebhookEvent, 0, len(_events))
|
|
|
|
for _, v := range _events {
|
|
|
|
events = append(events, model.WebhookEvent(v))
|
|
|
|
}
|
|
|
|
server.Config.Pipeline.DefaultCancelPreviousPipelineEvents = events
|
|
|
|
server.Config.Pipeline.DefaultTimeout = c.Int64("default-pipeline-timeout")
|
|
|
|
server.Config.Pipeline.MaxTimeout = c.Int64("max-pipeline-timeout")
|
|
|
|
|
|
|
|
// limits
|
|
|
|
server.Config.Pipeline.Limits.MemSwapLimit = c.Int64("limit-mem-swap")
|
|
|
|
server.Config.Pipeline.Limits.MemLimit = c.Int64("limit-mem")
|
|
|
|
server.Config.Pipeline.Limits.ShmSize = c.Int64("limit-shm-size")
|
|
|
|
server.Config.Pipeline.Limits.CPUQuota = c.Int64("limit-cpu-quota")
|
|
|
|
server.Config.Pipeline.Limits.CPUShares = c.Int64("limit-cpu-shares")
|
|
|
|
server.Config.Pipeline.Limits.CPUSet = c.String("limit-cpu-set")
|
|
|
|
|
|
|
|
// backend options for pipeline compiler
|
|
|
|
server.Config.Pipeline.Proxy.No = c.String("backend-no-proxy")
|
|
|
|
server.Config.Pipeline.Proxy.HTTP = c.String("backend-http-proxy")
|
|
|
|
server.Config.Pipeline.Proxy.HTTPS = c.String("backend-https-proxy")
|
|
|
|
|
|
|
|
// server configuration
|
|
|
|
server.Config.Server.JWTSecret, err = setupJWTSecret(s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not setup jwt secret: %w", err)
|
|
|
|
}
|
|
|
|
server.Config.Server.Cert = c.String("server-cert")
|
|
|
|
server.Config.Server.Key = c.String("server-key")
|
|
|
|
server.Config.Server.AgentToken = c.String("agent-secret")
|
|
|
|
serverHost := strings.TrimSuffix(c.String("server-host"), "/")
|
|
|
|
server.Config.Server.Host = serverHost
|
|
|
|
if c.IsSet("server-webhook-host") {
|
|
|
|
server.Config.Server.WebhookHost = c.String("server-webhook-host")
|
|
|
|
} else {
|
|
|
|
server.Config.Server.WebhookHost = serverHost
|
|
|
|
}
|
|
|
|
if c.IsSet("server-dev-oauth-host-deprecated") {
|
|
|
|
server.Config.Server.OAuthHost = c.String("server-dev-oauth-host-deprecated")
|
|
|
|
} else {
|
|
|
|
server.Config.Server.OAuthHost = serverHost
|
|
|
|
}
|
|
|
|
server.Config.Server.Port = c.String("server-addr")
|
|
|
|
server.Config.Server.PortTLS = c.String("server-addr-tls")
|
|
|
|
server.Config.Server.StatusContext = c.String("status-context")
|
|
|
|
server.Config.Server.StatusContextFormat = c.String("status-context-format")
|
|
|
|
server.Config.Server.SessionExpires = c.Duration("session-expires")
|
|
|
|
u, _ := url.Parse(server.Config.Server.Host)
|
|
|
|
rootPath := strings.TrimSuffix(u.Path, "/")
|
|
|
|
if rootPath != "" && !strings.HasPrefix(rootPath, "/") {
|
|
|
|
rootPath = "/" + rootPath
|
|
|
|
}
|
|
|
|
server.Config.Server.RootPath = rootPath
|
|
|
|
server.Config.Server.CustomCSSFile = strings.TrimSpace(c.String("custom-css-file"))
|
|
|
|
server.Config.Server.CustomJsFile = strings.TrimSpace(c.String("custom-js-file"))
|
|
|
|
server.Config.Pipeline.Networks = c.StringSlice("network")
|
|
|
|
server.Config.Pipeline.Volumes = c.StringSlice("volume")
|
|
|
|
server.Config.Pipeline.Privileged = c.StringSlice("escalate")
|
|
|
|
server.Config.WebUI.EnableSwagger = c.Bool("enable-swagger")
|
|
|
|
server.Config.WebUI.SkipVersionCheck = c.Bool("skip-version-check")
|
|
|
|
|
|
|
|
// prometheus
|
|
|
|
server.Config.Prometheus.AuthToken = c.String("prometheus-auth-token")
|
|
|
|
|
|
|
|
// permissions
|
|
|
|
server.Config.Permissions.Open = c.Bool("open")
|
|
|
|
server.Config.Permissions.Admins = permissions.NewAdmins(c.StringSlice("admin"))
|
|
|
|
server.Config.Permissions.Orgs = permissions.NewOrgs(c.StringSlice("orgs"))
|
|
|
|
server.Config.Permissions.OwnersAllowlist = permissions.NewOwnersAllowlist(c.StringSlice("repo-owners"))
|
|
|
|
return nil
|
|
|
|
}
|