mirror of
https://github.com/kopia/kopia.git
synced 2026-03-27 18:42:26 -04:00
* cli: refactored snapshot list * cli: show range tags in snapshot list For example if N snapshots are coalesced together because they have identical roots we may emit now: ``` 2021-03-31 23:09:27 PDT ked3400debc7dd61baffab070bafd59cd (monthly-10) 2021-04-30 06:12:53 PDT kd0576d212e55a831b7ff1636f90a7233 (monthly-4..9) + 5 identical snapshots until 2021-09-30 23:00:19 PDT 2021-10-31 23:22:25 PDT k846bf22aa2863d27f05e820f840b14f8 (monthly-3) 2021-11-08 21:29:31 PST k5793ddcd61ef27b93c75ab74a5828176 (latest-1..3,hourly-1..13,daily-1..7,weekly-1..4,monthly-1..2,annual-1) + 18 identical snapshots until 2021-12-04 10:09:54 PST ``` * server: server-side coalescing of snapshot * ui: added coalescing of retention tags
152 lines
4.0 KiB
Go
152 lines
4.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/internal/clock"
|
|
"github.com/kopia/kopia/internal/serverapi"
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/snapshot"
|
|
"github.com/kopia/kopia/snapshot/policy"
|
|
)
|
|
|
|
func (s *Server) handlePolicyList(ctx context.Context, r *http.Request, body []byte) (interface{}, *apiError) {
|
|
policies, err := policy.ListPolicies(ctx, s.rep)
|
|
if err != nil {
|
|
return nil, internalServerError(err)
|
|
}
|
|
|
|
resp := &serverapi.PoliciesResponse{
|
|
Policies: []*serverapi.PolicyListEntry{},
|
|
}
|
|
|
|
for _, pol := range policies {
|
|
target := pol.Target()
|
|
if !sourceMatchesURLFilter(target, r.URL.Query()) {
|
|
continue
|
|
}
|
|
|
|
resp.Policies = append(resp.Policies, &serverapi.PolicyListEntry{
|
|
ID: pol.ID(),
|
|
Target: target,
|
|
Policy: pol,
|
|
})
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
func getSnapshotSourceFromURL(u *url.URL) snapshot.SourceInfo {
|
|
host := u.Query().Get("host")
|
|
path := u.Query().Get("path")
|
|
username := u.Query().Get("userName")
|
|
|
|
return snapshot.SourceInfo{
|
|
Host: host,
|
|
Path: path,
|
|
UserName: username,
|
|
}
|
|
}
|
|
|
|
func (s *Server) handlePolicyGet(ctx context.Context, r *http.Request, body []byte) (interface{}, *apiError) {
|
|
pol, err := policy.GetDefinedPolicy(ctx, s.rep, getSnapshotSourceFromURL(r.URL))
|
|
if errors.Is(err, policy.ErrPolicyNotFound) {
|
|
return nil, requestError(serverapi.ErrorNotFound, "policy not found")
|
|
}
|
|
|
|
return pol, nil
|
|
}
|
|
|
|
func (s *Server) handlePolicyResolve(ctx context.Context, r *http.Request, body []byte) (interface{}, *apiError) {
|
|
var req serverapi.ResolvePolicyRequest
|
|
|
|
if err := json.Unmarshal(body, &req); err != nil {
|
|
return nil, requestError(serverapi.ErrorMalformedRequest, "unable to decode request: "+err.Error())
|
|
}
|
|
|
|
target := getSnapshotSourceFromURL(r.URL)
|
|
|
|
// build a list of parents
|
|
policies, err := policy.GetPolicyHierarchy(ctx, s.rep, target, nil)
|
|
if err != nil {
|
|
return nil, internalServerError(err)
|
|
}
|
|
|
|
resp := &serverapi.ResolvePolicyResponse{
|
|
Defined: policies[0],
|
|
}
|
|
|
|
if req.Updates != nil {
|
|
policies[0] = req.Updates
|
|
policies[0].Labels = policy.LabelsForSource(target)
|
|
}
|
|
|
|
resp.Effective, resp.Definition = policy.MergePolicies(policies, target)
|
|
resp.UpcomingSnapshotTimes = []time.Time{}
|
|
|
|
now := clock.Now().Local()
|
|
|
|
for i := 0; i < req.NumUpcomingSnapshotTimes; i++ {
|
|
st, ok := resp.Effective.SchedulingPolicy.NextSnapshotTime(now, now)
|
|
if !ok {
|
|
break
|
|
}
|
|
|
|
resp.UpcomingSnapshotTimes = append(resp.UpcomingSnapshotTimes, st)
|
|
now = st.Add(1 * time.Second)
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *Server) handlePolicyDelete(ctx context.Context, r *http.Request, body []byte) (interface{}, *apiError) {
|
|
if _, ok := s.rep.(repo.RepositoryWriter); !ok {
|
|
return nil, repositoryNotWritableError()
|
|
}
|
|
|
|
sourceInfo := getSnapshotSourceFromURL(r.URL)
|
|
|
|
if err := repo.WriteSession(ctx, s.rep, repo.WriteSessionOptions{
|
|
Purpose: "PolicyDelete",
|
|
}, func(ctx context.Context, w repo.RepositoryWriter) error {
|
|
return errors.Wrap(policy.RemovePolicy(ctx, w, sourceInfo), "unable to delete policy")
|
|
}); err != nil {
|
|
return nil, internalServerError(err)
|
|
}
|
|
|
|
s.triggerRefreshSource(sourceInfo)
|
|
|
|
return &serverapi.Empty{}, nil
|
|
}
|
|
|
|
func (s *Server) handlePolicyPut(ctx context.Context, r *http.Request, body []byte) (interface{}, *apiError) {
|
|
newPolicy := &policy.Policy{}
|
|
if err := json.Unmarshal(body, newPolicy); err != nil {
|
|
return nil, requestError(serverapi.ErrorMalformedRequest, "malformed request body")
|
|
}
|
|
|
|
if _, ok := s.rep.(repo.RepositoryWriter); !ok {
|
|
return nil, repositoryNotWritableError()
|
|
}
|
|
|
|
sourceInfo := getSnapshotSourceFromURL(r.URL)
|
|
|
|
if err := repo.WriteSession(ctx, s.rep, repo.WriteSessionOptions{
|
|
Purpose: "PolicyPut",
|
|
}, func(ctx context.Context, w repo.RepositoryWriter) error {
|
|
return errors.Wrap(policy.SetPolicy(ctx, w, sourceInfo, newPolicy), "unable to set policy")
|
|
}); err != nil {
|
|
return nil, internalServerError(err)
|
|
}
|
|
|
|
s.triggerRefreshSource(sourceInfo)
|
|
|
|
return &serverapi.Empty{}, nil
|
|
}
|