2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
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 (
|
2021-11-25 18:15:36 +02:00
|
|
|
"context"
|
2017-06-28 19:21:22 +02:00
|
|
|
"fmt"
|
2021-10-28 21:02:43 +02:00
|
|
|
"os"
|
2017-09-20 21:29:57 +02:00
|
|
|
"time"
|
2017-06-28 19:21:22 +02:00
|
|
|
|
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"
|
2021-10-27 21:03:14 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 09:25:13 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2023-12-08 09:15:08 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/cache"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/queue"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/datastore"
|
2017-05-03 23:25:33 +02:00
|
|
|
)
|
|
|
|
|
2024-01-10 16:34:44 +02:00
|
|
|
func setupStore(c *cli.Context) (store.Store, error) {
|
2021-10-30 14:53:24 +02:00
|
|
|
datasource := c.String("datasource")
|
|
|
|
driver := c.String("driver")
|
2023-07-15 01:15:13 +02:00
|
|
|
xorm := store.XORM{
|
|
|
|
Log: c.Bool("log-xorm"),
|
|
|
|
ShowSQL: c.Bool("log-xorm-sql"),
|
|
|
|
}
|
2021-10-30 14:53:24 +02:00
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
if driver == "sqlite3" {
|
|
|
|
if datastore.SupportedDriver("sqlite3") {
|
2024-01-10 21:57:12 +02:00
|
|
|
log.Debug().Msg("server has sqlite3 support")
|
2021-11-13 21:18:06 +02:00
|
|
|
} else {
|
2024-01-10 21:57:12 +02:00
|
|
|
log.Debug().Msg("server was built without sqlite3 support!")
|
2021-11-13 21:18:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !datastore.SupportedDriver(driver) {
|
2024-01-10 16:34:44 +02:00
|
|
|
return nil, fmt.Errorf("database driver '%s' not supported", driver)
|
2021-11-13 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if driver == "sqlite3" {
|
2023-07-28 13:31:25 +02:00
|
|
|
if err := checkSqliteFileExist(datasource); err != nil {
|
2024-01-10 16:34:44 +02:00
|
|
|
return nil, fmt.Errorf("check sqlite file: %w", err)
|
2021-10-30 14:53:24 +02:00
|
|
|
}
|
2021-10-28 21:02:43 +02:00
|
|
|
}
|
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
opts := &store.Opts{
|
2021-10-30 14:53:24 +02:00
|
|
|
Driver: driver,
|
|
|
|
Config: datasource,
|
2023-07-15 01:15:13 +02:00
|
|
|
XORM: xorm,
|
2021-10-19 11:44:49 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
log.Trace().Msgf("setup datastore: %#v", *opts)
|
|
|
|
store, err := datastore.NewEngine(opts)
|
|
|
|
if err != nil {
|
2024-01-10 16:34:44 +02:00
|
|
|
return nil, fmt.Errorf("could not open datastore: %w", err)
|
2021-11-13 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2023-11-28 11:31:54 +02:00
|
|
|
if err := store.Migrate(c.Bool("migrations-allow-long")); err != nil {
|
2024-01-10 16:34:44 +02:00
|
|
|
return nil, fmt.Errorf("could not migrate datastore: %w", err)
|
2021-11-13 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2024-01-10 16:34:44 +02:00
|
|
|
return store, nil
|
2017-05-03 23:25:33 +02:00
|
|
|
}
|
|
|
|
|
2023-07-28 13:31:25 +02: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 14:53:24 +02:00
|
|
|
}
|
2023-07-28 13:31:25 +02:00
|
|
|
return err
|
2021-10-28 21:02:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 02:02:08 +02:00
|
|
|
func setupQueue(c *cli.Context, s store.Store) queue.Queue {
|
2021-12-11 14:15:04 +02:00
|
|
|
return queue.WithTaskStore(queue.New(c.Context), s)
|
2017-05-04 02:02:08 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 08:04:55 +02:00
|
|
|
func setupMembershipService(_ *cli.Context, _store store.Store) cache.MembershipService {
|
|
|
|
return cache.NewMembershipService(_store)
|
2017-06-28 19:21:22 +02:00
|
|
|
}
|
2017-06-30 00:51:22 +02:00
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
func setupMetrics(g *errgroup.Group, _store store.Store) {
|
2022-10-28 17:38:53 +02:00
|
|
|
pendingSteps := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2022-10-28 17:38:53 +02:00
|
|
|
Name: "pending_steps",
|
|
|
|
Help: "Total number of pending pipeline steps.",
|
2019-05-30 11:11:14 +02:00
|
|
|
})
|
2022-10-28 17:38:53 +02:00
|
|
|
waitingSteps := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2022-10-28 17:38:53 +02:00
|
|
|
Name: "waiting_steps",
|
2022-10-18 03:24:12 +02:00
|
|
|
Help: "Total number of pipeline waiting on deps.",
|
2019-07-10 10:00:38 +02:00
|
|
|
})
|
2022-10-28 17:38:53 +02:00
|
|
|
runningSteps := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2022-10-28 17:38:53 +02:00
|
|
|
Name: "running_steps",
|
|
|
|
Help: "Total number of running pipeline steps.",
|
2019-05-30 11:11:14 +02:00
|
|
|
})
|
|
|
|
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.",
|
|
|
|
})
|
2022-10-18 03:24:12 +02:00
|
|
|
pipelines := promauto.NewGauge(prometheus.GaugeOpts{
|
2021-10-13 08:34:57 +02:00
|
|
|
Namespace: "woodpecker",
|
2022-10-18 03:24:12 +02:00
|
|
|
Name: "pipeline_total_count",
|
|
|
|
Help: "Total number of pipelines.",
|
2019-05-30 11:11:14 +02:00
|
|
|
})
|
|
|
|
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-11-25 18:15:36 +02:00
|
|
|
stats := server.Config.Services.Queue.Info(context.TODO())
|
2022-10-28 17:38:53 +02:00
|
|
|
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 {
|
2021-12-01 15:22:06 +02:00
|
|
|
repoCount, _ := _store.GetRepoCount()
|
|
|
|
userCount, _ := _store.GetUserCount()
|
2022-10-18 03:24:12 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|