cf054a0461
(cherry picked from commit2555e315f7
) (cherry picked from commit51b9c9092e
) [CLI] implement forgejo-cli (squash) support initDB (cherry picked from commit5c31ae602a
) (cherry picked from commitbbf76489a7
) Conflicts: because ofd0dbe52e76
upgrade to https://pkg.go.dev/github.com/urfave/cli/v2 (cherry picked from commitb6c1bcc008
) [CLI] implement forgejo-cli actions (cherry picked from commit08be2b226e
) (cherry picked from commitb6cfa88c6e
) (cherry picked from commit59704200de
) [CLI] implement forgejo-cli actions generate-secret (cherry picked from commit6f7905c8ec
) (cherry picked from commite085d6d273
) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2
) [CLI] implement forgejo-cli actions register (cherry picked from commit2f95143000
) (cherry picked from commit42f2f8731e
) [CLI] implement forgejo-cli actions register (squash) no private Do not go through the private API, directly modify the database (cherry picked from commit1ba7c0d39d
) [CLI] implement forgejo-cli actions (cherry picked from commit6f7905c8ec
) (cherry picked from commite085d6d273
) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2
) (cherry picked from commit4c121ef022
) Conflicts: cmd/forgejo/actions.go tests/integration/cmd_forgejo_actions_test.go (cherry picked from commit36997a48e3
) [CLI] implement forgejo-cli actions (squash) restore --version Refs: https://codeberg.org/forgejo/forgejo/issues/1134 (cherry picked from commit9739eb52d8
) [CI] implement forgejo-cli (squash) the actions subcommand needs config (cherry picked from commit def638475122a26082ab3835842c84cd03839154) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1209 (cherry picked from commita1758a3910
) (cherry picked from commit935fa650c7
) (cherry picked from commitcd21026bc9
) (cherry picked from commit1700b8973a
) (cherry picked from commit1def42a379
) (cherry picked from commit839d97521d
) (cherry picked from commitfd8c13be6b
) (cherry picked from commit588e5d552f
) (cherry picked from commit151a726620
) [v1.22] [CLI] implement forgejo-cli https://codeberg.org/forgejo/forgejo/pulls/1541 (cherry picked from commit46708de7b9
) (cherry picked from commita8e5c1369e
) (cherry picked from commitc8a32aaf24
) Conflicts: models/actions/main_test.go https://codeberg.org/forgejo/forgejo/pulls/1656 (cherry picked from commit79f4553063
) (cherry picked from commit0379da0cf5
) (cherry picked from commit331d58c085
) (cherry picked from commit89705502c4
) (cherry picked from commit4723d5febf
) (cherry picked from commite71b260130
) (cherry picked from commit6a376a5b48
) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1969 (cherry picked from commit6ba97cf4b5
) (cherry picked from commite0a6ebfeca
) (cherry picked from commit5702aeab2d
) (cherry picked from commitf919c4d6c1
) (cherry picked from commita26799a88a
) (cherry picked from commitb6ab473395
)
243 lines
6.8 KiB
Go
243 lines
6.8 KiB
Go
// Copyright The Forgejo Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgejo
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
|
"code.gitea.io/gitea/modules/private"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
private_routers "code.gitea.io/gitea/routers/private"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func CmdActions(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "actions",
|
|
Usage: "Commands for managing Forgejo Actions",
|
|
Subcommands: []*cli.Command{
|
|
SubcmdActionsGenerateRunnerToken(ctx),
|
|
SubcmdActionsGenerateRunnerSecret(ctx),
|
|
SubcmdActionsRegister(ctx),
|
|
},
|
|
}
|
|
}
|
|
|
|
func SubcmdActionsGenerateRunnerToken(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "generate-runner-token",
|
|
Usage: "Generate a new token for a runner to use to register with the server",
|
|
Action: prepareWorkPathAndCustomConf(ctx, func(cliCtx *cli.Context) error { return RunGenerateActionsRunnerToken(ctx, cliCtx) }),
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "scope",
|
|
Aliases: []string{"s"},
|
|
Value: "",
|
|
Usage: "{owner}[/{repo}] - leave empty for a global runner",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func SubcmdActionsGenerateRunnerSecret(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "generate-secret",
|
|
Usage: "Generate a secret suitable for input to the register subcommand",
|
|
Action: func(cliCtx *cli.Context) error { return RunGenerateSecret(ctx, cliCtx) },
|
|
}
|
|
}
|
|
|
|
func SubcmdActionsRegister(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "register",
|
|
Usage: "Idempotent registration of a runner using a shared secret",
|
|
Action: prepareWorkPathAndCustomConf(ctx, func(cliCtx *cli.Context) error { return RunRegister(ctx, cliCtx) }),
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "secret",
|
|
Usage: "the secret the runner will use to connect as a 40 character hexadecimal string",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "secret-stdin",
|
|
Usage: "the secret the runner will use to connect as a 40 character hexadecimal string, read from stdin",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "secret-file",
|
|
Usage: "path to the file containing the secret the runner will use to connect as a 40 character hexadecimal string",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "scope",
|
|
Aliases: []string{"s"},
|
|
Value: "",
|
|
Usage: "{owner}[/{repo}] - leave empty for a global runner",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "labels",
|
|
Value: "",
|
|
Usage: "comma separated list of labels supported by the runner (e.g. docker,ubuntu-latest,self-hosted) (not required since v1.21)",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "name",
|
|
Value: "runner",
|
|
Usage: "name of the runner (default runner)",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "version",
|
|
Value: "",
|
|
Usage: "version of the runner (not required since v1.21)",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func readSecret(ctx context.Context, cliCtx *cli.Context) (string, error) {
|
|
if cliCtx.IsSet("secret") {
|
|
return cliCtx.String("secret"), nil
|
|
}
|
|
if cliCtx.IsSet("secret-stdin") {
|
|
buf, err := io.ReadAll(ContextGetStdin(ctx))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(buf), nil
|
|
}
|
|
if cliCtx.IsSet("secret-file") {
|
|
path := cliCtx.String("secret-file")
|
|
buf, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(buf), nil
|
|
}
|
|
return "", fmt.Errorf("at least one of the --secret, --secret-stdin, --secret-file options is required")
|
|
}
|
|
|
|
func validateSecret(secret string) error {
|
|
secretLen := len(secret)
|
|
if secretLen != 40 {
|
|
return fmt.Errorf("the secret must be exactly 40 characters long, not %d: generate-secret can provide a secret matching the requirements", secretLen)
|
|
}
|
|
if _, err := hex.DecodeString(secret); err != nil {
|
|
return fmt.Errorf("the secret must be an hexadecimal string: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func RunRegister(ctx context.Context, cliCtx *cli.Context) error {
|
|
if !ContextGetNoInit(ctx) {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = installSignals(ctx)
|
|
defer cancel()
|
|
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
setting.MustInstalled()
|
|
|
|
secret, err := readSecret(ctx, cliCtx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := validateSecret(secret); err != nil {
|
|
return err
|
|
}
|
|
scope := cliCtx.String("scope")
|
|
labels := cliCtx.String("labels")
|
|
name := cliCtx.String("name")
|
|
version := cliCtx.String("version")
|
|
|
|
//
|
|
// There are two kinds of tokens
|
|
//
|
|
// - "registration token" only used when a runner interacts to
|
|
// register
|
|
//
|
|
// - "token" obtained after a successful registration and stored by
|
|
// the runner to authenticate
|
|
//
|
|
// The register subcommand does not need a "registration token", it
|
|
// needs a "token". Using the same name is confusing and secret is
|
|
// preferred for this reason in the cli.
|
|
//
|
|
// The ActionsRunnerRegister argument is token to be consistent with
|
|
// the internal naming. It is still confusing to the developer but
|
|
// not to the user.
|
|
//
|
|
owner, repo, err := private_routers.ParseScope(ctx, scope)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
runner, err := actions_model.RegisterRunner(ctx, owner, repo, secret, strings.Split(labels, ","), name, version)
|
|
if err != nil {
|
|
return fmt.Errorf("error while registering runner: %v", err)
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", runner.UUID); err != nil {
|
|
panic(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func RunGenerateSecret(ctx context.Context, cliCtx *cli.Context) error {
|
|
runner := actions_model.ActionRunner{}
|
|
if err := runner.GenerateToken(); err != nil {
|
|
return err
|
|
}
|
|
if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", runner.Token); err != nil {
|
|
panic(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func RunGenerateActionsRunnerToken(ctx context.Context, cliCtx *cli.Context) error {
|
|
if !ContextGetNoInit(ctx) {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = installSignals(ctx)
|
|
defer cancel()
|
|
}
|
|
|
|
setting.MustInstalled()
|
|
|
|
scope := cliCtx.String("scope")
|
|
|
|
respText, extra := private.GenerateActionsRunnerToken(ctx, scope)
|
|
if extra.HasError() {
|
|
return handleCliResponseExtra(ctx, extra)
|
|
}
|
|
if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", respText.Text); err != nil {
|
|
panic(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func prepareWorkPathAndCustomConf(ctx context.Context, action cli.ActionFunc) func(cliCtx *cli.Context) error {
|
|
return func(cliCtx *cli.Context) error {
|
|
if !ContextGetNoInit(ctx) {
|
|
var args setting.ArgWorkPathAndCustomConf
|
|
// from children to parent, check the global flags
|
|
for _, curCtx := range cliCtx.Lineage() {
|
|
if curCtx.IsSet("work-path") && args.WorkPath == "" {
|
|
args.WorkPath = curCtx.String("work-path")
|
|
}
|
|
if curCtx.IsSet("custom-path") && args.CustomPath == "" {
|
|
args.CustomPath = curCtx.String("custom-path")
|
|
}
|
|
if curCtx.IsSet("config") && args.CustomConf == "" {
|
|
args.CustomConf = curCtx.String("config")
|
|
}
|
|
}
|
|
setting.InitWorkPathAndCommonConfig(os.Getenv, args)
|
|
}
|
|
return action(cliCtx)
|
|
}
|
|
}
|