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
|
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/google/uuid"
"github.com/gorilla/mux"
internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/db/operationtype"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/operations"
projecthelpers "github.com/lxc/incus/v6/internal/server/project"
"github.com/lxc/incus/v6/internal/server/request"
"github.com/lxc/incus/v6/internal/server/response"
"github.com/lxc/incus/v6/internal/server/state"
localUtil "github.com/lxc/incus/v6/internal/server/util"
"github.com/lxc/incus/v6/internal/version"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/osarch"
"github.com/lxc/incus/v6/shared/revert"
)
// swagger:operation PUT /1.0/instances/{name} instances instance_put
//
// Update the instance
//
// Updates the instance configuration or trigger a snapshot restore.
//
// ---
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - in: query
// name: project
// description: Project name
// type: string
// example: default
// - in: body
// name: instance
// description: Update request
// schema:
// $ref: "#/definitions/InstancePut"
// responses:
// "202":
// $ref: "#/responses/Operation"
// "400":
// $ref: "#/responses/BadRequest"
// "403":
// $ref: "#/responses/Forbidden"
// "500":
// $ref: "#/responses/InternalServerError"
func instancePut(d *Daemon, r *http.Request) response.Response {
// Don't mess with instance while in setup mode.
<-d.waitReady.Done()
s := d.State()
projectName := request.ProjectParam(r)
// Get the container
name, err := url.PathUnescape(mux.Vars(r)["name"])
if err != nil {
return response.SmartError(err)
}
if internalInstance.IsSnapshot(name) {
return response.BadRequest(errors.New("Invalid instance name"))
}
// Handle requests targeted to a container on a different node
resp, err := forwardedResponseIfInstanceIsRemote(s, r, projectName, name)
if err != nil {
return response.SmartError(err)
}
if resp != nil {
return resp
}
reverter := revert.New()
defer reverter.Fail()
unlock, err := instanceOperationLock(s.ShutdownCtx, projectName, name)
if err != nil {
return response.SmartError(err)
}
reverter.Add(func() {
unlock()
})
inst, err := instance.LoadByProjectAndName(s, projectName, name)
if err != nil {
return response.SmartError(err)
}
// Validate the ETag
err = localUtil.EtagCheck(r, inst.ETag())
if err != nil {
return response.PreconditionFailed(err)
}
configRaw := api.InstancePut{}
err = json.NewDecoder(r.Body).Decode(&configRaw)
if err != nil {
return response.BadRequest(err)
}
architecture, err := osarch.ArchitectureID(configRaw.Architecture)
if err != nil {
architecture = 0
}
var do func(*operations.Operation) error
var opType operationtype.Type
if configRaw.Restore == "" {
// Check project limits.
apiProfiles := make([]api.Profile, 0, len(configRaw.Profiles))
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
profiles, err := cluster.GetProfilesIfEnabled(ctx, tx.Tx(), projectName, configRaw.Profiles)
if err != nil {
return err
}
profileConfigs, err := cluster.GetAllProfileConfigs(ctx, tx.Tx())
if err != nil {
return err
}
profileDevices, err := cluster.GetAllProfileDevices(ctx, tx.Tx())
if err != nil {
return err
}
for _, profile := range profiles {
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
if err != nil {
return err
}
apiProfiles = append(apiProfiles, *apiProfile)
}
return projecthelpers.AllowInstanceUpdate(tx, projectName, name, configRaw, inst.LocalConfig())
})
if err != nil {
return response.SmartError(err)
}
// Update container configuration
do = func(op *operations.Operation) error {
inst.SetOperation(op)
defer unlock()
args := db.InstanceArgs{
Architecture: architecture,
Config: configRaw.Config,
Description: configRaw.Description,
Devices: deviceConfig.NewDevices(configRaw.Devices),
Ephemeral: configRaw.Ephemeral,
Profiles: apiProfiles,
Project: projectName,
}
err = inst.Update(args, true)
if err != nil {
return err
}
return nil
}
opType = operationtype.InstanceUpdate
} else {
// Snapshot Restore
do = func(op *operations.Operation) error {
defer unlock()
return instanceSnapRestore(s, projectName, name, configRaw.Restore, configRaw.Stateful, op)
}
opType = operationtype.SnapshotRestore
}
resources := map[string][]api.URL{}
resources["instances"] = []api.URL{*api.NewURL().Path(version.APIVersion, "instances", name)}
op, err := operations.OperationCreate(s, projectName, operations.OperationClassTask, opType, resources, nil, do, nil, nil, r)
if err != nil {
return response.InternalError(err)
}
reverter.Success()
return operations.OperationResponse(op)
}
func instanceSnapRestore(s *state.State, projectName string, name string, snap string, stateful bool, op *operations.Operation) error {
// normalize snapshot name
if !internalInstance.IsSnapshot(snap) {
snap = name + internalInstance.SnapshotDelimiter + snap
}
inst, err := instance.LoadByProjectAndName(s, projectName, name)
if err != nil {
return err
}
inst.SetOperation(op)
source, err := instance.LoadByProjectAndName(s, projectName, snap)
if err != nil {
switch {
case response.IsNotFoundError(err):
return fmt.Errorf("Snapshot %s does not exist", snap)
default:
return err
}
}
source.SetOperation(op)
// Generate a new `volatile.uuid.generation` to differentiate this instance restored from a snapshot from the original instance.
source.LocalConfig()["volatile.uuid.generation"] = uuid.New().String()
err = inst.Restore(source, stateful)
if err != nil {
return err
}
return nil
}
|