woodpecker/cmd/server/setup.go

280 lines
8.9 KiB
Go
Raw Normal View History

2018-02-19 22:24:10 +00:00
// Copyright 2018 Drone.IO Inc.
//
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-02-19 22:24:10 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
//
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 (
2017-06-28 17:21:22 +00:00
"fmt"
2017-09-20 19:29:57 +00:00
"time"
2017-06-28 17:21:22 +00:00
2017-07-31 19:15:05 +00:00
"github.com/dimfeld/httptreemux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/woodpecker-ci/woodpecker/cncd/queue"
"github.com/woodpecker-ci/woodpecker/model"
"github.com/woodpecker-ci/woodpecker/plugins/environments"
"github.com/woodpecker-ci/woodpecker/plugins/registry"
"github.com/woodpecker-ci/woodpecker/plugins/secrets"
"github.com/woodpecker-ci/woodpecker/remote"
"github.com/woodpecker-ci/woodpecker/remote/bitbucket"
"github.com/woodpecker-ci/woodpecker/remote/bitbucketserver"
"github.com/woodpecker-ci/woodpecker/remote/coding"
"github.com/woodpecker-ci/woodpecker/remote/gitea"
"github.com/woodpecker-ci/woodpecker/remote/github"
"github.com/woodpecker-ci/woodpecker/remote/gitlab"
"github.com/woodpecker-ci/woodpecker/remote/gitlab3"
"github.com/woodpecker-ci/woodpecker/remote/gogs"
droneserver "github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/web"
"github.com/woodpecker-ci/woodpecker/store"
"github.com/woodpecker-ci/woodpecker/store/datastore"
2019-05-30 09:11:14 +00:00
"golang.org/x/sync/errgroup"
2017-05-03 21:25:33 +00:00
"github.com/urfave/cli"
)
func setupStore(c *cli.Context) store.Store {
return datastore.New(
c.String("driver"),
c.String("datasource"),
)
}
2017-05-04 00:02:08 +00:00
func setupQueue(c *cli.Context, s store.Store) queue.Queue {
return model.WithTaskStore(queue.New(), s)
}
func setupSecretService(c *cli.Context, s store.Store) model.SecretService {
return secrets.New(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")),
)
} else {
return registry.New(s)
}
}
2017-06-29 22:51:22 +00:00
func setupEnvironService(c *cli.Context, s store.Store) model.EnvironService {
return environments.Filesystem(c.StringSlice("environment"))
2017-06-29 22:51:22 +00:00
}
2017-06-28 17:21:22 +00:00
// helper function to setup the remote from the CLI arguments.
2017-06-29 22:51:22 +00:00
func SetupRemote(c *cli.Context) (remote.Remote, error) {
2017-06-28 17:21:22 +00: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 09:12:09 +00:00
case c.Bool("coding"):
return setupCoding(c)
2017-06-28 17:21:22 +00: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) {
return bitbucket.New(
c.String("bitbucket-client"),
c.String("bitbucket-secret"),
), nil
}
// helper function to setup the Gogs remote from the CLI arguments.
func setupGogs(c *cli.Context) (remote.Remote, error) {
return gogs.New(gogs.Opts{
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"),
})
}
// helper function to setup the Gitea remote from the CLI arguments.
func setupGitea(c *cli.Context) (remote.Remote, error) {
if !c.IsSet("gitea-client") {
return gitea.New(gitea.Opts{
URL: c.String("gitea-server"),
Context: c.String("gitea-context"),
Username: c.String("gitea-git-username"),
Password: c.String("gitea-git-password"),
PrivateMode: c.Bool("gitea-private-mode"),
SkipVerify: c.Bool("gitea-skip-verify"),
})
}
return gitea.NewOauth(gitea.Opts{
2017-06-28 17:21:22 +00:00
URL: c.String("gitea-server"),
Context: c.String("gitea-context"),
2017-06-28 17:21:22 +00:00
Username: c.String("gitea-git-username"),
Password: c.String("gitea-git-password"),
Client: c.String("gitea-client"),
Secret: c.String("gitea-secret"),
2017-06-28 17:21:22 +00:00
PrivateMode: c.Bool("gitea-private-mode"),
SkipVerify: c.Bool("gitea-skip-verify"),
})
}
// helper function to setup the Stash remote from the CLI arguments.
func setupStash(c *cli.Context) (remote.Remote, error) {
return bitbucketserver.New(bitbucketserver.Opts{
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"),
})
}
// helper function to setup the Gitlab remote from the CLI arguments.
func setupGitlab(c *cli.Context) (remote.Remote, error) {
if c.Bool("gitlab-v3-api") {
return gitlab3.New(gitlab3.Opts{
URL: c.String("gitlab-server"),
Client: c.String("gitlab-client"),
Secret: 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 17:21:22 +00:00
return gitlab.New(gitlab.Opts{
URL: c.String("gitlab-server"),
Client: c.String("gitlab-client"),
Secret: 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"),
})
}
// helper function to setup the GitHub remote from the CLI arguments.
func setupGithub(c *cli.Context) (remote.Remote, error) {
return github.New(github.Opts{
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"),
})
}
2017-06-29 22:51:22 +00:00
2017-07-22 09:12:09 +00:00
// helper function to setup the Coding remote from the CLI arguments.
func setupCoding(c *cli.Context) (remote.Remote, error) {
return coding.New(coding.Opts{
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"),
})
}
2017-07-31 19:15:05 +00:00
func setupTree(c *cli.Context) *httptreemux.ContextMux {
tree := httptreemux.NewContextMux()
2017-09-20 19:29:57 +00:00
web.New(
web.WithDir(c.String("www")),
web.WithSync(time.Hour*72),
2021-05-27 05:27:13 +00:00
web.WithDocs(c.String("docs")),
2017-09-20 19:29:57 +00:00
).Register(tree)
2017-07-31 19:15:05 +00:00
return tree
}
2017-06-29 22:51:22 +00:00
func before(c *cli.Context) error { return nil }
2019-05-30 09:11:14 +00:00
2019-05-30 10:15:29 +00:00
func setupMetrics(g *errgroup.Group, store_ store.Store) {
2019-05-30 09:11:14 +00:00
pendingJobs := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "drone",
Name: "pending_jobs",
Help: "Total number of pending build processes.",
})
2019-07-10 08:00:38 +00:00
waitingJobs := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "drone",
Name: "waiting_jobs",
Help: "Total number of builds waiting on deps.",
})
2019-05-30 09:11:14 +00:00
runningJobs := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "drone",
Name: "running_jobs",
Help: "Total number of running build processes.",
})
workers := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "drone",
Name: "worker_count",
Help: "Total number of workers.",
})
builds := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "drone",
2019-06-28 12:23:52 +00:00
Name: "build_total_count",
2019-05-30 09:11:14 +00:00
Help: "Total number of builds.",
})
users := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "drone",
Name: "user_count",
Help: "Total number of users.",
})
repos := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "drone",
Name: "repo_count",
Help: "Total number of repos.",
})
g.Go(func() error {
for {
stats := droneserver.Config.Services.Queue.Info(nil)
pendingJobs.Set(float64(stats.Stats.Pending))
2019-07-10 08:00:38 +00:00
waitingJobs.Set(float64(stats.Stats.WaitingOnDeps))
2019-05-30 09:11:14 +00: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()
buildCount, _ := store_.GetBuildCount()
builds.Set(float64(buildCount))
2019-05-30 09:11:14 +00:00
users.Set(float64(userCount))
repos.Set(float64(repoCount))
time.Sleep(10 * time.Second)
}
})
}