Files
kopia/cli/command_server_source_manager_action.go
Jarek Kowalski 2e9a57f0b4 server: support for server control APIs and tooling (#1644)
This adds new set of APIs `/api/v1/control/*` which can be used to administratively control a running server.

Once the server is started, the administrative user can control it
using CLI commands:

export KOPIA_SERVER_ADDRESS=...
export KOPIA_SERVER_CERT_FINGERPRINT=...
export KOPIA_SERVER_PASSWORD=...

* `kopia server status` - displays status of sources managed by the server
* `kopia server snapshot` - triggers server-side upload of snapshots for managed sources
* `kopia server cancel` - cancels upload of snapshots for managed sources
* `kopia server pause` - pauses scheduled snapshots for managed sources
* `kopia server resume` - resumes scheduled snapshots for managed sources
* `kopia server refresh` - causes server to resynchronize with externally-made changes, such as policies or new sources
* `kopia server flush` - causes server to flush all pending writes
* `kopia server shutdown` - graceful shutdown of the server

Authentication uses new user `server-control` and is disabled
by default. To enable it when starting the server, provide the password
using one of the following methods:

* `--server-control-password`
* `--random-server-control-password`
* `.htpasswd` file
* `KOPIA_SERVER_CONTROL_PASSWORD` environment variable

This change allows us to tighten the API security and remove some
methods that UI user was able to call, but which were not needed.
2022-01-03 18:48:38 -08:00

67 lines
1.6 KiB
Go

package cli
import (
"context"
"net/url"
"path/filepath"
"github.com/alecthomas/kingpin"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/apiclient"
"github.com/kopia/kopia/internal/serverapi"
)
// commandServerSourceManagerAction encapsulates common logic for all commands
// that operate on snapshot sources managed by the server and can be used to
// act upon them one-by-one or all at the same time.
type commandServerSourceManagerAction struct {
sf serverClientFlags
source string
all bool
out textOutput
}
func (c *commandServerSourceManagerAction) setup(svc appServices, cmd *kingpin.CmdClause) {
cmd.Flag("all", "All paths managed by server").BoolVar(&c.all)
cmd.Arg("source", "Source path managed by server").StringVar(&c.source)
c.sf.setup(cmd)
c.out.setup(svc)
}
func (c *commandServerSourceManagerAction) triggerActionOnMatchingSources(ctx context.Context, cli *apiclient.KopiaAPIClient, path string) error {
var resp serverapi.MultipleSourceActionResponse
uv := url.Values{}
if !c.all {
if c.source == "" {
return errors.Errorf("must specify source or --all")
}
absPath, err := filepath.Abs(c.source)
if err != nil {
return errors.Wrap(err, "unable to determine absolute path")
}
uv.Set("path", absPath)
}
if err := cli.Post(ctx, path+"?"+uv.Encode(), &serverapi.Empty{}, &resp); err != nil {
return errors.Wrapf(err, "server returned error")
}
for src, resp := range resp.Sources {
if resp.Success {
log(ctx).Infof("Success %v", src)
} else {
log(ctx).Warnf("Failed %v", src)
}
}
return nil
}