1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/cmd/server/setup.go

350 lines
11 KiB
Go
Raw Normal View History

// Copyright 2022 Woodpecker Authors
2018-02-20 00:24:10 +02:00
// Copyright 2018 Drone.IO Inc.
//
2018-02-20 00:24:10 +02: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-02-20 00:24:10 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
//
2018-02-20 00:24:10 +02: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-30 00:51:22 +02:00
package main
2017-05-03 23:25:33 +02:00
import (
"context"
"crypto"
"crypto/ed25519"
"crypto/rand"
"encoding/hex"
"errors"
2017-06-28 19:21:22 +02:00
"fmt"
"net/url"
"os"
"strings"
2017-09-20 21:29:57 +02:00
"time"
2017-06-28 19:21:22 +02:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/cache"
"github.com/woodpecker-ci/woodpecker/server/forge"
"github.com/woodpecker-ci/woodpecker/server/forge/bitbucket"
"github.com/woodpecker-ci/woodpecker/server/forge/gitea"
"github.com/woodpecker-ci/woodpecker/server/forge/github"
"github.com/woodpecker-ci/woodpecker/server/forge/gitlab"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/plugins/environments"
"github.com/woodpecker-ci/woodpecker/server/plugins/registry"
"github.com/woodpecker-ci/woodpecker/server/plugins/secrets"
"github.com/woodpecker-ci/woodpecker/server/queue"
"github.com/woodpecker-ci/woodpecker/server/store"
"github.com/woodpecker-ci/woodpecker/server/store/datastore"
"github.com/woodpecker-ci/woodpecker/server/store/types"
2017-05-03 23:25:33 +02:00
)
func setupStore(c *cli.Context) (store.Store, error) {
datasource := c.String("datasource")
driver := c.String("driver")
xorm := store.XORM{
Log: c.Bool("log-xorm"),
ShowSQL: c.Bool("log-xorm-sql"),
}
if driver == "sqlite3" {
if datastore.SupportedDriver("sqlite3") {
log.Debug().Msgf("server has sqlite3 support")
} else {
log.Debug().Msgf("server was built without sqlite3 support!")
}
}
if !datastore.SupportedDriver(driver) {
log.Fatal().Msgf("database driver '%s' not supported", driver)
}
if driver == "sqlite3" {
2023-03-18 21:35:27 +02:00
if newDatasource, err := fallbackSqlite3File(datasource); err != nil {
log.Fatal().Err(err).Msg("fallback to old sqlite3 file failed")
} else {
2023-03-18 21:35:27 +02:00
datasource = newDatasource
}
}
opts := &store.Opts{
Driver: driver,
Config: datasource,
XORM: xorm,
}
log.Trace().Msgf("setup datastore: %#v", *opts)
store, err := datastore.NewEngine(opts)
if err != nil {
log.Fatal().Err(err).Msg("could not open datastore")
}
if err := store.Migrate(); err != nil {
log.Fatal().Err(err).Msg("could not migrate datastore")
}
return store, nil
2017-05-03 23:25:33 +02:00
}
// TODO: remove it in v1.1.0
// TODO: add it to the "how to migrate from drone docs"
func fallbackSqlite3File(path string) (string, error) {
const dockerDefaultPath = "/var/lib/woodpecker/woodpecker.sqlite"
const dockerDefaultDir = "/var/lib/woodpecker/drone.sqlite"
const dockerOldPath = "/var/lib/drone/drone.sqlite"
const standaloneDefault = "woodpecker.sqlite"
const standaloneOld = "drone.sqlite"
// custom location was set, use that one
if path != dockerDefaultPath && path != standaloneDefault {
return path, nil
}
// file is at new default("/var/lib/woodpecker/woodpecker.sqlite")
_, err := os.Stat(dockerDefaultPath)
if err != nil && !os.IsNotExist(err) {
return "", err
}
if err == nil {
return dockerDefaultPath, nil
}
// file is at new default("woodpecker.sqlite")
_, err = os.Stat(standaloneDefault)
if err != nil && !os.IsNotExist(err) {
return "", err
}
if err == nil {
return standaloneDefault, nil
}
// woodpecker run in standalone mode, file is in same folder but not renamed
_, err = os.Stat(standaloneOld)
if err != nil && !os.IsNotExist(err) {
return "", err
}
if err == nil {
// rename in same folder should be fine as it should be same docker volume
log.Warn().Msgf("found sqlite3 file at '%s' and moved to '%s'", standaloneOld, standaloneDefault)
return standaloneDefault, os.Rename(standaloneOld, standaloneDefault)
}
// file is in new folder but not renamed
_, err = os.Stat(dockerDefaultDir)
if err != nil && !os.IsNotExist(err) {
return "", err
}
if err == nil {
// rename in same folder should be fine as it should be same docker volume
log.Warn().Msgf("found sqlite3 file at '%s' and moved to '%s'", dockerDefaultDir, dockerDefaultPath)
return dockerDefaultPath, os.Rename(dockerDefaultDir, dockerDefaultPath)
}
// file is still at old location
_, err = os.Stat(dockerOldPath)
if err == nil {
log.Fatal().Msgf("found sqlite3 file at old path '%s', please move it to '%s' and update your volume path if necessary", dockerOldPath, dockerDefaultPath)
}
// file does not exist at all
log.Warn().Msgf("no sqlite3 file found, will create one at '%s'", path)
return path, nil
}
2017-05-04 02:02:08 +02:00
func setupQueue(c *cli.Context, s store.Store) queue.Queue {
return queue.WithTaskStore(queue.New(c.Context), s)
2017-05-04 02:02:08 +02:00
}
func setupSecretService(c *cli.Context, s model.SecretStore) model.SecretService {
return secrets.New(c.Context, s)
}
func setupRegistryService(c *cli.Context, s store.Store) model.RegistryService {
if c.String("docker-config") != "" {
return registry.Combined(
registry.New(s),
registry.Filesystem(c.String("docker-config")),
)
}
return registry.New(s)
}
2023-03-18 21:35:27 +02:00
func setupEnvironService(c *cli.Context, _ store.Store) model.EnvironService {
return environments.Parse(c.StringSlice("environment"))
2017-06-30 00:51:22 +02:00
}
func setupMembershipService(_ *cli.Context, r forge.Forge) cache.MembershipService {
return cache.NewMembershipService(r)
}
// setupForge helper function to setup the forge from the CLI arguments.
func setupForge(c *cli.Context) (forge.Forge, error) {
2017-06-28 19:21:22 +02:00
switch {
case c.Bool("github"):
return setupGitHub(c)
2017-06-28 19:21:22 +02:00
case c.Bool("gitlab"):
return setupGitLab(c)
2017-06-28 19:21:22 +02:00
case c.Bool("bitbucket"):
return setupBitbucket(c)
case c.Bool("gitea"):
return setupGitea(c)
default:
return nil, fmt.Errorf("version control system not configured")
}
}
// setupBitbucket helper function to setup the Bitbucket forge from the CLI arguments.
func setupBitbucket(c *cli.Context) (forge.Forge, error) {
opts := &bitbucket.Opts{
Client: c.String("bitbucket-client"),
Secret: c.String("bitbucket-secret"),
}
log.Trace().Msgf("Forge (bitbucket) opts: %#v", opts)
return bitbucket.New(opts)
2017-06-28 19:21:22 +02:00
}
// setupGitea helper function to setup the Gitea forge from the CLI arguments.
func setupGitea(c *cli.Context) (forge.Forge, error) {
server, err := url.Parse(c.String("gitea-server"))
if err != nil {
return nil, err
}
opts := gitea.Opts{
URL: strings.TrimRight(server.String(), "/"),
Client: c.String("gitea-client"),
Secret: c.String("gitea-secret"),
SkipVerify: c.Bool("gitea-skip-verify"),
}
if len(opts.URL) == 0 {
log.Fatal().Msg("WOODPECKER_GITEA_URL must be set")
}
log.Trace().Msgf("Forge (gitea) opts: %#v", opts)
return gitea.New(opts)
2017-06-28 19:21:22 +02:00
}
// setupGitLab helper function to setup the GitLab forge from the CLI arguments.
func setupGitLab(c *cli.Context) (forge.Forge, error) {
2017-06-28 19:21:22 +02:00
return gitlab.New(gitlab.Opts{
URL: c.String("gitlab-server"),
ClientID: c.String("gitlab-client"),
ClientSecret: c.String("gitlab-secret"),
SkipVerify: c.Bool("gitlab-skip-verify"),
2017-06-28 19:21:22 +02:00
})
}
// setupGitHub helper function to setup the GitHub forge from the CLI arguments.
func setupGitHub(c *cli.Context) (forge.Forge, error) {
opts := github.Opts{
URL: c.String("github-server"),
Client: c.String("github-client"),
Secret: c.String("github-secret"),
SkipVerify: c.Bool("github-skip-verify"),
MergeRef: c.Bool("github-merge-ref"),
}
log.Trace().Msgf("Forge (github) opts: %#v", opts)
return github.New(opts)
2017-06-28 19:21:22 +02:00
}
2017-06-30 00:51:22 +02:00
func setupMetrics(g *errgroup.Group, _store store.Store) {
pendingSteps := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "pending_steps",
Help: "Total number of pending pipeline steps.",
2019-05-30 11:11:14 +02:00
})
waitingSteps := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "waiting_steps",
Help: "Total number of pipeline waiting on deps.",
2019-07-10 10:00:38 +02:00
})
runningSteps := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "running_steps",
Help: "Total number of running pipeline steps.",
2019-05-30 11:11:14 +02:00
})
workers := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
2019-05-30 11:11:14 +02:00
Name: "worker_count",
Help: "Total number of workers.",
})
pipelines := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "pipeline_total_count",
Help: "Total number of pipelines.",
2019-05-30 11:11:14 +02:00
})
users := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
2019-05-30 11:11:14 +02:00
Name: "user_count",
Help: "Total number of users.",
})
repos := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
2019-05-30 11:11:14 +02:00
Name: "repo_count",
Help: "Total number of repos.",
})
g.Go(func() error {
for {
stats := server.Config.Services.Queue.Info(context.TODO())
pendingSteps.Set(float64(stats.Stats.Pending))
waitingSteps.Set(float64(stats.Stats.WaitingOnDeps))
runningSteps.Set(float64(stats.Stats.Running))
2019-05-30 11:11:14 +02:00
workers.Set(float64(stats.Stats.Workers))
time.Sleep(500 * time.Millisecond)
}
})
g.Go(func() error {
for {
repoCount, _ := _store.GetRepoCount()
userCount, _ := _store.GetUserCount()
pipelineCount, _ := _store.GetPipelineCount()
pipelines.Set(float64(pipelineCount))
2019-05-30 11:11:14 +02:00
users.Set(float64(userCount))
repos.Set(float64(repoCount))
time.Sleep(10 * time.Second)
}
})
}
// setupSignatureKeys generate or load key pair to sign webhooks requests (i.e. used for extensions)
func setupSignatureKeys(_store store.Store) (crypto.PrivateKey, crypto.PublicKey) {
privKeyID := "signature-private-key"
privKey, err := _store.ServerConfigGet(privKeyID)
if errors.Is(err, types.RecordNotExist) {
_, privKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to generate private key")
return nil, nil
}
err = _store.ServerConfigSet(privKeyID, hex.EncodeToString(privKey))
if err != nil {
log.Fatal().Err(err).Msgf("Failed to generate private key")
return nil, nil
}
log.Debug().Msg("Created private key")
return privKey, privKey.Public()
} else if err != nil {
log.Fatal().Err(err).Msgf("Failed to load private key")
return nil, nil
} else {
privKeyStr, err := hex.DecodeString(privKey)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to decode private key")
return nil, nil
}
privKey := ed25519.PrivateKey(privKeyStr)
return privKey, privKey.Public()
}
}