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
|
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"github.com/gorilla/mux"
internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/jmap"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/db/cluster"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/instance"
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"
localUtil "github.com/lxc/incus/v6/internal/server/util"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/osarch"
)
// swagger:operation PATCH /1.0/instances/{name} instances instance_patch
//
// Partially update the instance
//
// Updates a subset of the instance configuration
//
// ---
// 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:
// "200":
// $ref: "#/responses/EmptySyncResponse"
// "400":
// $ref: "#/responses/BadRequest"
// "403":
// $ref: "#/responses/Forbidden"
// "500":
// $ref: "#/responses/InternalServerError"
func instancePatch(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
}
unlock, err := instanceOperationLock(s.ShutdownCtx, projectName, name)
if err != nil {
return response.SmartError(err)
}
defer unlock()
c, err := instance.LoadByProjectAndName(s, projectName, name)
if err != nil {
return response.SmartError(err)
}
// Validate the ETag
err = localUtil.EtagCheck(r, c.ETag())
if err != nil {
return response.PreconditionFailed(err)
}
body, err := io.ReadAll(r.Body)
if err != nil {
return response.InternalError(err)
}
rdr1 := io.NopCloser(bytes.NewBuffer(body))
rdr2 := io.NopCloser(bytes.NewBuffer(body))
reqRaw := jmap.Map{}
err = json.NewDecoder(rdr1).Decode(&reqRaw)
if err != nil {
return response.BadRequest(err)
}
req := api.InstancePut{}
err = json.NewDecoder(rdr2).Decode(&req)
if err != nil {
return response.BadRequest(err)
}
if req.Restore != "" {
return response.BadRequest(errors.New("Can't call PATCH in restore mode"))
}
// Check if architecture was passed
var architecture int
_, err = reqRaw.GetString("architecture")
if err != nil {
architecture = c.Architecture()
} else {
architecture, err = osarch.ArchitectureID(req.Architecture)
if err != nil {
architecture = 0
}
}
// Check if ephemeral was passed
_, err = reqRaw.GetBool("ephemeral")
if err != nil {
req.Ephemeral = c.IsEphemeral()
}
profileNames := make([]string, 0, len(c.Profiles()))
for _, profile := range c.Profiles() {
profileNames = append(profileNames, profile.Name)
}
// Check if profiles was passed
if req.Profiles == nil {
req.Profiles = profileNames
}
// Check if config was passed
if req.Config == nil {
req.Config = c.LocalConfig()
} else {
for k, v := range c.LocalConfig() {
_, ok := req.Config[k]
if !ok {
req.Config[k] = v
}
}
}
// Check if devices was passed
if req.Devices == nil {
req.Devices = c.LocalDevices().CloneNative()
} else {
for k, v := range c.LocalDevices() {
_, ok := req.Devices[k]
if !ok {
req.Devices[k] = v
}
}
}
// Check project limits.
apiProfiles := make([]api.Profile, 0, len(req.Profiles))
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
profiles, err := cluster.GetProfilesIfEnabled(ctx, tx.Tx(), projectName, req.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, req, c.LocalConfig())
})
if err != nil {
return response.SmartError(err)
}
// Update container configuration
args := db.InstanceArgs{
Architecture: architecture,
Config: req.Config,
Description: req.Description,
Devices: deviceConfig.NewDevices(req.Devices),
Ephemeral: req.Ephemeral,
Profiles: apiProfiles,
Project: projectName,
}
err = c.Update(args, true)
if err != nil {
return response.SmartError(err)
}
return response.EmptySyncResponse
}
|