2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-10 21:09:14 +02:00
|
|
|
//
|
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-03-10 21:09:14 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-10 21:09:14 +02:00
|
|
|
//
|
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 (
|
2017-06-28 19:21:22 +02:00
|
|
|
"fmt"
|
2017-09-20 21:29:57 +02:00
|
|
|
"time"
|
2017-06-28 19:21:22 +02:00
|
|
|
|
2021-09-29 17:34:56 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-12 09:25:13 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 16:12:46 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/plugins/environments"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/plugins/registry"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/plugins/secrets"
|
2021-09-23 22:29:09 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/queue"
|
2021-09-23 18:25:51 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/bitbucket"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/bitbucketserver"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/coding"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/gitea"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/github"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/gitlab"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/gogs"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/datastore"
|
2021-05-25 14:08:27 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/web"
|
2017-05-03 23:25:33 +02:00
|
|
|
)
|
|
|
|
|
2021-10-19 11:44:49 +02:00
|
|
|
func setupStore(c *cli.Context) (store.Store, error) {
|
|
|
|
opts := &datastore.Opts{
|
|
|
|
Driver: c.String("driver"),
|
|
|
|
Config: c.String("datasource"),
|
|
|
|
}
|
|
|
|
log.Trace().Msgf("setup datastore: %#v", opts)
|
|
|
|
return datastore.New(opts)
|
2017-05-03 23:25:33 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 02:02:08 +02:00
|
|
|
func setupQueue(c *cli.Context, s store.Store) queue.Queue {
|
|
|
|
return model.WithTaskStore(queue.New(), s)
|
|
|
|
}
|
|
|
|
|
2017-05-07 18:47:06 +02:00
|
|
|
func setupSecretService(c *cli.Context, s store.Store) model.SecretService {
|
|
|
|
return secrets.New(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRegistryService(c *cli.Context, s store.Store) model.RegistryService {
|
2020-05-19 14:44:16 +02:00
|
|
|
if c.String("docker-config") != "" {
|
|
|
|
return registry.Combined(
|
|
|
|
registry.New(s),
|
|
|
|
registry.Filesystem(c.String("docker-config")),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return registry.New(s)
|
|
|
|
}
|
2017-05-07 18:47:06 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 00:51:22 +02:00
|
|
|
func setupEnvironService(c *cli.Context, s store.Store) model.EnvironService {
|
2020-05-18 17:48:31 +02:00
|
|
|
return environments.Filesystem(c.StringSlice("environment"))
|
2017-06-30 00:51:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 08:11:11 +02:00
|
|
|
// SetupRemote helper function to setup the remote from the CLI arguments.
|
2017-06-30 00:51:22 +02:00
|
|
|
func SetupRemote(c *cli.Context) (remote.Remote, error) {
|
2017-06-28 19:21:22 +02:00
|
|
|
switch {
|
|
|
|
case c.Bool("github"):
|
|
|
|
return setupGithub(c)
|
|
|
|
case c.Bool("gitlab"):
|
|
|
|
return setupGitlab(c)
|
|
|
|
case c.Bool("bitbucket"):
|
|
|
|
return setupBitbucket(c)
|
|
|
|
case c.Bool("stash"):
|
|
|
|
return setupStash(c)
|
|
|
|
case c.Bool("gogs"):
|
|
|
|
return setupGogs(c)
|
|
|
|
case c.Bool("gitea"):
|
|
|
|
return setupGitea(c)
|
2017-07-22 11:12:09 +02:00
|
|
|
case c.Bool("coding"):
|
|
|
|
return setupCoding(c)
|
2017-06-28 19:21:22 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("version control system not configured")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to setup the Bitbucket remote from the CLI arguments.
|
|
|
|
func setupBitbucket(c *cli.Context) (remote.Remote, error) {
|
2021-10-19 11:44:49 +02:00
|
|
|
opts := &bitbucket.Opts{
|
|
|
|
Client: c.String("bitbucket-client"),
|
|
|
|
Secret: c.String("bitbucket-secret"),
|
|
|
|
}
|
|
|
|
log.Trace().Msgf("Remote (bitbucket) opts: %#v", opts)
|
|
|
|
return bitbucket.New(opts)
|
2017-06-28 19:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to setup the Gogs remote from the CLI arguments.
|
|
|
|
func setupGogs(c *cli.Context) (remote.Remote, error) {
|
2021-10-19 11:44:49 +02:00
|
|
|
opts := gogs.Opts{
|
2017-06-28 19:21:22 +02:00
|
|
|
URL: c.String("gogs-server"),
|
|
|
|
Username: c.String("gogs-git-username"),
|
|
|
|
Password: c.String("gogs-git-password"),
|
|
|
|
PrivateMode: c.Bool("gogs-private-mode"),
|
|
|
|
SkipVerify: c.Bool("gogs-skip-verify"),
|
2021-10-19 11:44:49 +02:00
|
|
|
}
|
|
|
|
log.Trace().Msgf("Remote (gogs) opts: %#v", opts)
|
|
|
|
return gogs.New(opts)
|
2017-06-28 19:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to setup the Gitea remote from the CLI arguments.
|
|
|
|
func setupGitea(c *cli.Context) (remote.Remote, error) {
|
Clean up config environment variables for server and agent (#218)
The goal here is to make consistent use of configuration environment variables prefixed `WOODPECKER_`. Where several variants existed, this PR aims to remove all but one option, leaving the most explicit.
This PR only changes server and agent code, but not documentation, in order to keep the PR digestible. Once we have consensus that this is correct, I'll change docs accordingly.
User (rather: admin) facing changes in this PR:
- In general, support for all server and agent config environment variables (env vars) starting with `DRONE_` is removed. The according `WOODPECKER_*` variables must be used instead.
- The env var `WOODPECKER_HOST` replaces `DRONE_HOST`, and `DRONE_SERVER_HOST`.
- The env var `WOODPECKER_AGENT_SECRET` is used to configure the shared secret which agents use to authenticate against the server. It replaces `WOODPECKER_SECRET`, `DRONE_SECRET`, `WOODPECKER_PASSWORD`, `DRONE_PASSWORD`, and `DRONE_AGENT_SECRET`.
- The env var `WOODPECKER_DATABASE_DRIVER` replaces `DRONE_DATABASE_DRIVER` and `DATABASE_DRIVER`.
- The env var `WOODPECKER_DATABASE_DATASOURCE` replaces `DRONE_DATABASE_DATASOURCE` and `DATABASE_CONFIG`.
2021-09-28 15:43:44 +02:00
|
|
|
opts := gitea.Opts{
|
2017-06-28 19:21:22 +02:00
|
|
|
URL: c.String("gitea-server"),
|
2018-03-10 21:09:14 +02:00
|
|
|
Context: c.String("gitea-context"),
|
2017-06-28 19:21:22 +02:00
|
|
|
Username: c.String("gitea-git-username"),
|
|
|
|
Password: c.String("gitea-git-password"),
|
2021-06-17 09:02:44 +02:00
|
|
|
Client: c.String("gitea-client"),
|
|
|
|
Secret: c.String("gitea-secret"),
|
2017-06-28 19:21:22 +02:00
|
|
|
PrivateMode: c.Bool("gitea-private-mode"),
|
|
|
|
SkipVerify: c.Bool("gitea-skip-verify"),
|
Clean up config environment variables for server and agent (#218)
The goal here is to make consistent use of configuration environment variables prefixed `WOODPECKER_`. Where several variants existed, this PR aims to remove all but one option, leaving the most explicit.
This PR only changes server and agent code, but not documentation, in order to keep the PR digestible. Once we have consensus that this is correct, I'll change docs accordingly.
User (rather: admin) facing changes in this PR:
- In general, support for all server and agent config environment variables (env vars) starting with `DRONE_` is removed. The according `WOODPECKER_*` variables must be used instead.
- The env var `WOODPECKER_HOST` replaces `DRONE_HOST`, and `DRONE_SERVER_HOST`.
- The env var `WOODPECKER_AGENT_SECRET` is used to configure the shared secret which agents use to authenticate against the server. It replaces `WOODPECKER_SECRET`, `DRONE_SECRET`, `WOODPECKER_PASSWORD`, `DRONE_PASSWORD`, and `DRONE_AGENT_SECRET`.
- The env var `WOODPECKER_DATABASE_DRIVER` replaces `DRONE_DATABASE_DRIVER` and `DATABASE_DRIVER`.
- The env var `WOODPECKER_DATABASE_DATASOURCE` replaces `DRONE_DATABASE_DATASOURCE` and `DATABASE_CONFIG`.
2021-09-28 15:43:44 +02:00
|
|
|
}
|
|
|
|
if len(opts.URL) == 0 {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Fatal().Msg("WOODPECKER_GITEA_URL must be set")
|
Clean up config environment variables for server and agent (#218)
The goal here is to make consistent use of configuration environment variables prefixed `WOODPECKER_`. Where several variants existed, this PR aims to remove all but one option, leaving the most explicit.
This PR only changes server and agent code, but not documentation, in order to keep the PR digestible. Once we have consensus that this is correct, I'll change docs accordingly.
User (rather: admin) facing changes in this PR:
- In general, support for all server and agent config environment variables (env vars) starting with `DRONE_` is removed. The according `WOODPECKER_*` variables must be used instead.
- The env var `WOODPECKER_HOST` replaces `DRONE_HOST`, and `DRONE_SERVER_HOST`.
- The env var `WOODPECKER_AGENT_SECRET` is used to configure the shared secret which agents use to authenticate against the server. It replaces `WOODPECKER_SECRET`, `DRONE_SECRET`, `WOODPECKER_PASSWORD`, `DRONE_PASSWORD`, and `DRONE_AGENT_SECRET`.
- The env var `WOODPECKER_DATABASE_DRIVER` replaces `DRONE_DATABASE_DRIVER` and `DATABASE_DRIVER`.
- The env var `WOODPECKER_DATABASE_DATASOURCE` replaces `DRONE_DATABASE_DATASOURCE` and `DATABASE_CONFIG`.
2021-09-28 15:43:44 +02:00
|
|
|
}
|
2021-10-19 11:44:49 +02:00
|
|
|
log.Trace().Msgf("Remote (gitea) opts: %#v", opts)
|
Clean up config environment variables for server and agent (#218)
The goal here is to make consistent use of configuration environment variables prefixed `WOODPECKER_`. Where several variants existed, this PR aims to remove all but one option, leaving the most explicit.
This PR only changes server and agent code, but not documentation, in order to keep the PR digestible. Once we have consensus that this is correct, I'll change docs accordingly.
User (rather: admin) facing changes in this PR:
- In general, support for all server and agent config environment variables (env vars) starting with `DRONE_` is removed. The according `WOODPECKER_*` variables must be used instead.
- The env var `WOODPECKER_HOST` replaces `DRONE_HOST`, and `DRONE_SERVER_HOST`.
- The env var `WOODPECKER_AGENT_SECRET` is used to configure the shared secret which agents use to authenticate against the server. It replaces `WOODPECKER_SECRET`, `DRONE_SECRET`, `WOODPECKER_PASSWORD`, `DRONE_PASSWORD`, and `DRONE_AGENT_SECRET`.
- The env var `WOODPECKER_DATABASE_DRIVER` replaces `DRONE_DATABASE_DRIVER` and `DATABASE_DRIVER`.
- The env var `WOODPECKER_DATABASE_DATASOURCE` replaces `DRONE_DATABASE_DATASOURCE` and `DATABASE_CONFIG`.
2021-09-28 15:43:44 +02:00
|
|
|
return gitea.New(opts)
|
2017-06-28 19:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to setup the Stash remote from the CLI arguments.
|
|
|
|
func setupStash(c *cli.Context) (remote.Remote, error) {
|
2021-10-19 11:44:49 +02:00
|
|
|
opts := bitbucketserver.Opts{
|
2017-06-28 19:21:22 +02:00
|
|
|
URL: c.String("stash-server"),
|
|
|
|
Username: c.String("stash-git-username"),
|
|
|
|
Password: c.String("stash-git-password"),
|
|
|
|
ConsumerKey: c.String("stash-consumer-key"),
|
|
|
|
ConsumerRSA: c.String("stash-consumer-rsa"),
|
|
|
|
ConsumerRSAString: c.String("stash-consumer-rsa-string"),
|
|
|
|
SkipVerify: c.Bool("stash-skip-verify"),
|
2021-10-19 11:44:49 +02:00
|
|
|
}
|
|
|
|
log.Trace().Msgf("Remote (bitbucketserver) opts: %#v", opts)
|
|
|
|
return bitbucketserver.New(opts)
|
2017-06-28 19:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to setup the Gitlab remote from the CLI arguments.
|
|
|
|
func setupGitlab(c *cli.Context) (remote.Remote, error) {
|
|
|
|
return gitlab.New(gitlab.Opts{
|
2021-10-03 14:42:47 +02:00
|
|
|
URL: c.String("gitlab-server"),
|
|
|
|
ClientID: c.String("gitlab-client"),
|
|
|
|
ClientSecret: c.String("gitlab-secret"),
|
|
|
|
Username: c.String("gitlab-git-username"),
|
|
|
|
Password: c.String("gitlab-git-password"),
|
|
|
|
PrivateMode: c.Bool("gitlab-private-mode"),
|
|
|
|
SkipVerify: c.Bool("gitlab-skip-verify"),
|
2017-06-28 19:21:22 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to setup the GitHub remote from the CLI arguments.
|
|
|
|
func setupGithub(c *cli.Context) (remote.Remote, error) {
|
2021-10-19 11:44:49 +02:00
|
|
|
opts := github.Opts{
|
2017-06-28 19:21:22 +02:00
|
|
|
URL: c.String("github-server"),
|
|
|
|
Context: c.String("github-context"),
|
|
|
|
Client: c.String("github-client"),
|
|
|
|
Secret: c.String("github-secret"),
|
|
|
|
Scopes: c.StringSlice("github-scope"),
|
|
|
|
Username: c.String("github-git-username"),
|
|
|
|
Password: c.String("github-git-password"),
|
|
|
|
PrivateMode: c.Bool("github-private-mode"),
|
|
|
|
SkipVerify: c.Bool("github-skip-verify"),
|
|
|
|
MergeRef: c.BoolT("github-merge-ref"),
|
2021-10-19 11:44:49 +02:00
|
|
|
}
|
|
|
|
log.Trace().Msgf("Remote (github) opts: %#v", opts)
|
|
|
|
return github.New(opts)
|
2017-06-28 19:21:22 +02:00
|
|
|
}
|
2017-06-30 00:51:22 +02:00
|
|
|
|
2017-07-22 11:12:09 +02:00
|
|
|
// helper function to setup the Coding remote from the CLI arguments.
|
|
|
|
func setupCoding(c *cli.Context) (remote.Remote, error) {
|
2021-10-19 11:44:49 +02:00
|
|
|
opts := coding.Opts{
|
2017-07-22 11:12:09 +02:00
|
|
|
URL: c.String("coding-server"),
|
|
|
|
Client: c.String("coding-client"),
|
|
|
|
Secret: c.String("coding-secret"),
|
|
|
|
Scopes: c.StringSlice("coding-scope"),
|
|
|
|
Machine: c.String("coding-git-machine"),
|
|
|
|
Username: c.String("coding-git-username"),
|
|
|
|
Password: c.String("coding-git-password"),
|
|
|
|
SkipVerify: c.Bool("coding-skip-verify"),
|
2021-10-19 11:44:49 +02:00
|
|
|
}
|
|
|
|
log.Trace().Msgf("Remote (coding) opts: %#v", opts)
|
|
|
|
return coding.New(opts)
|
2017-07-22 11:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 17:34:56 +02:00
|
|
|
func setupTree(c *cli.Context) *gin.Engine {
|
|
|
|
tree := gin.New()
|
2017-09-20 21:29:57 +02:00
|
|
|
web.New(
|
|
|
|
web.WithSync(time.Hour*72),
|
2021-05-27 07:27:13 +02:00
|
|
|
web.WithDocs(c.String("docs")),
|
2017-09-20 21:29:57 +02:00
|
|
|
).Register(tree)
|
2017-07-31 21:15:05 +02:00
|
|
|
return tree
|
|
|
|
}
|
|
|
|
|
2017-06-30 00:51:22 +02:00
|
|
|
func before(c *cli.Context) error { return nil }
|
2019-05-30 11:11:14 +02:00
|
|
|
|
2019-05-30 12:15:29 +02:00
|
|
|
func setupMetrics(g *errgroup.Group, store_ store.Store) {
|
2019-05-30 11:11:14 +02:00
|
|
|
pendingJobs := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 11:11:14 +02:00
|
|
|
Name: "pending_jobs",
|
|
|
|
Help: "Total number of pending build processes.",
|
|
|
|
})
|
2019-07-10 10:00:38 +02:00
|
|
|
waitingJobs := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2019-07-10 10:00:38 +02:00
|
|
|
Name: "waiting_jobs",
|
|
|
|
Help: "Total number of builds waiting on deps.",
|
|
|
|
})
|
2019-05-30 11:11:14 +02:00
|
|
|
runningJobs := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 11:11:14 +02:00
|
|
|
Name: "running_jobs",
|
|
|
|
Help: "Total number of running build processes.",
|
|
|
|
})
|
|
|
|
workers := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 11:11:14 +02:00
|
|
|
Name: "worker_count",
|
|
|
|
Help: "Total number of workers.",
|
|
|
|
})
|
|
|
|
builds := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2019-06-28 14:23:52 +02:00
|
|
|
Name: "build_total_count",
|
2019-05-30 11:11:14 +02:00
|
|
|
Help: "Total number of builds.",
|
|
|
|
})
|
|
|
|
users := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 11:11:14 +02:00
|
|
|
Name: "user_count",
|
|
|
|
Help: "Total number of users.",
|
|
|
|
})
|
|
|
|
repos := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2019-05-30 11:11:14 +02:00
|
|
|
Name: "repo_count",
|
|
|
|
Help: "Total number of repos.",
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
for {
|
2021-09-22 20:48:01 +02:00
|
|
|
stats := server.Config.Services.Queue.Info(nil)
|
2019-05-30 11:11:14 +02:00
|
|
|
pendingJobs.Set(float64(stats.Stats.Pending))
|
2019-07-10 10:00:38 +02:00
|
|
|
waitingJobs.Set(float64(stats.Stats.WaitingOnDeps))
|
2019-05-30 11:11:14 +02:00
|
|
|
runningJobs.Set(float64(stats.Stats.Running))
|
|
|
|
workers.Set(float64(stats.Stats.Workers))
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
g.Go(func() error {
|
|
|
|
for {
|
|
|
|
repoCount, _ := store_.GetRepoCount()
|
|
|
|
userCount, _ := store_.GetUserCount()
|
2019-05-30 13:57:52 +02:00
|
|
|
buildCount, _ := store_.GetBuildCount()
|
|
|
|
builds.Set(float64(buildCount))
|
2019-05-30 11:11:14 +02:00
|
|
|
users.Set(float64(userCount))
|
|
|
|
repos.Set(float64(repoCount))
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|