1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/snapshotstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/overlord/swfeats"
"github.com/snapcore/snapd/strutil"
)
var snapshotCmd = &Command{
// TODO: also support /v2/snapshots/<id>
Path: "/v2/snapshots",
GET: listSnapshots,
POST: changeSnapshots,
Actions: []string{"check", "restore", "forget"},
ReadAccess: openAccess{},
WriteAccess: authenticatedAccess{Polkit: polkitActionManage},
}
var snapshotExportCmd = &Command{
Path: "/v2/snapshots/{id}/export",
GET: getSnapshotExport,
ReadAccess: authenticatedAccess{},
}
var (
snapshotList = snapshotstate.List
snapshotCheck = snapshotstate.Check
snapshotForget = snapshotstate.Forget
snapshotRestore = snapshotstate.Restore
snapshotSave = snapshotstate.Save
snapshotExport = snapshotstate.Export
snapshotImport = snapshotstate.Import
)
var (
checkSnapshotChangeKind = swfeats.RegisterChangeKind("check-snapshot")
restoreSnapshotChangeKind = swfeats.RegisterChangeKind("restore-snapshot")
forgetSnapshotChangeKind = swfeats.RegisterChangeKind("forget-snapshot")
)
func listSnapshots(c *Command, r *http.Request, user *auth.UserState) Response {
query := r.URL.Query()
var setID uint64
if sid := query.Get("set"); sid != "" {
var err error
setID, err = strconv.ParseUint(sid, 10, 64)
if err != nil {
return BadRequest("'set', if given, must be a positive base 10 number; got %q", sid)
}
}
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
sets, err := snapshotList(r.Context(), st, setID, strutil.CommaSeparatedList(r.URL.Query().Get("snaps")))
if err != nil {
return InternalError("%v", err)
}
return SyncResponse(sets)
}
// A snapshotAction is used to request an operation on a snapshot
// keep this in sync with client/snapshotAction...
type snapshotAction struct {
SetID uint64 `json:"set"`
Action string `json:"action"`
Snaps []string `json:"snaps,omitempty"`
Users []string `json:"users,omitempty"`
}
func (action snapshotAction) String() string {
// verb of snapshot #N [for snaps %q] [for users %q]
var snaps string
var users string
if len(action.Snaps) > 0 {
snaps = " for snaps " + strutil.Quoted(action.Snaps)
}
if len(action.Users) > 0 {
users = " for users " + strutil.Quoted(action.Users)
}
return fmt.Sprintf("%s of snapshot set #%d%s%s", strings.Title(action.Action), action.SetID, snaps, users)
}
func changeSnapshots(c *Command, r *http.Request, user *auth.UserState) Response {
contentType := r.Header.Get("Content-Type")
if contentType == client.SnapshotExportMediaType {
return doSnapshotImport(c, r, user)
}
var action snapshotAction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&action); err != nil {
return BadRequest("cannot decode request body into snapshot operation: %v", err)
}
if decoder.More() {
return BadRequest("extra content found after snapshot operation")
}
if action.SetID == 0 {
return BadRequest("snapshot operation requires snapshot set ID")
}
if action.Action == "" {
return BadRequest("snapshot operation requires action")
}
var affected []string
var ts *state.TaskSet
var err error
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
var changeKind string
switch action.Action {
case "check":
affected, ts, err = snapshotCheck(st, action.SetID, action.Snaps, action.Users)
changeKind = checkSnapshotChangeKind
case "restore":
affected, ts, err = snapshotRestore(st, action.SetID, action.Snaps, action.Users)
changeKind = restoreSnapshotChangeKind
case "forget":
if len(action.Users) != 0 {
return BadRequest(`snapshot "forget" operation cannot specify users`)
}
affected, ts, err = snapshotForget(st, action.SetID, action.Snaps)
changeKind = forgetSnapshotChangeKind
default:
return BadRequest("unknown snapshot operation %q", action.Action)
}
switch err {
case nil:
// woo
case client.ErrSnapshotSetNotFound, client.ErrSnapshotSnapsNotFound:
return NotFound("%v", err)
default:
return InternalError("%v", err)
}
chg := newChange(st, changeKind, action.String(), []*state.TaskSet{ts}, affected)
chg.Set("api-data", map[string]any{"snap-names": affected})
ensureStateSoon(st)
return AsyncResponse(nil, chg.ID())
}
// getSnapshotExport streams an archive containing an export of existing snapshots.
//
// The snapshots are re-packaged into a single uncompressed tar archive and
// internally contain multiple zip files.
func getSnapshotExport(c *Command, r *http.Request, user *auth.UserState) Response {
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
vars := muxVars(r)
sid := vars["id"]
setID, err := strconv.ParseUint(sid, 10, 64)
if err != nil {
return BadRequest("'id' must be a positive base 10 number; got %q", sid)
}
export, err := snapshotExport(r.Context(), st, setID)
if err != nil {
return BadRequest("cannot export %v: %v", setID, err)
}
// init (size calculation) can be slow so drop the lock
st.Unlock()
err = export.Init()
st.Lock()
if err != nil {
return BadRequest("cannot calculate size of exported snapshot %v: %v", setID, err)
}
return &snapshotExportResponse{SnapshotExport: export, setID: setID, st: st}
}
func doSnapshotImport(c *Command, r *http.Request, user *auth.UserState) Response {
defer r.Body.Close()
expectedSize, err := strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64)
if err != nil {
return BadRequest("cannot parse Content-Length: %v", err)
}
// ensure we don't read more than we expect
limitedBodyReader := io.LimitReader(r.Body, expectedSize)
// XXX: check that we have enough space to import the compressed snapshots
st := c.d.overlord.State()
setID, snapNames, err := snapshotImport(r.Context(), st, limitedBodyReader)
if err != nil {
return BadRequest(err.Error())
}
result := map[string]any{"set-id": setID, "snaps": snapNames}
return SyncResponse(result)
}
func snapshotMany(_ context.Context, inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
setID, snapshotted, ts, err := snapshotSave(st, inst.Snaps, inst.Users, inst.SnapshotOptions)
if err != nil {
return nil, err
}
var msg string
if len(inst.Snaps) == 0 {
msg = i18n.G("Snapshot all snaps")
} else {
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Snapshot snaps %s"), strutil.Quoted(inst.Snaps))
}
return &snapInstructionResult{
Summary: msg,
Affected: snapshotted,
Tasksets: []*state.TaskSet{ts},
Result: map[string]any{"set-id": setID},
}, nil
}
|