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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
package incus
import (
"errors"
"fmt"
"io"
"net/http"
"slices"
"github.com/gorilla/websocket"
"github.com/lxc/incus/v6/shared/api"
localtls "github.com/lxc/incus/v6/shared/tls"
"github.com/lxc/incus/v6/shared/util"
)
// Server handling functions
// GetServer returns the server status as a Server struct.
func (r *ProtocolIncus) GetServer() (*api.Server, string, error) {
server := api.Server{}
// Fetch the raw value
etag, err := r.queryStruct("GET", "", nil, "", &server)
if err != nil {
return nil, "", err
}
// Fill in certificate fingerprint if not provided
if server.Environment.CertificateFingerprint == "" && server.Environment.Certificate != "" {
var err error
server.Environment.CertificateFingerprint, err = localtls.CertFingerprintStr(server.Environment.Certificate)
if err != nil {
return nil, "", err
}
}
if !server.Public && len(server.AuthMethods) == 0 {
// TLS is always available for Incus servers
server.AuthMethods = []string{api.AuthenticationMethodTLS}
}
// Add the value to the cache
r.server = &server
return &server, etag, nil
}
// UpdateServer updates the server status to match the provided Server struct.
func (r *ProtocolIncus) UpdateServer(server api.ServerPut, ETag string) error {
// Send the request
_, _, err := r.query("PUT", "", server, ETag)
if err != nil {
return err
}
return nil
}
// HasExtension returns true if the server supports a given API extension.
// Deprecated: Use CheckExtension instead.
func (r *ProtocolIncus) HasExtension(extension string) bool {
// If no cached API information, just assume we're good
// This is needed for those rare cases where we must avoid a GetServer call
if r.server == nil {
return true
}
return slices.Contains(r.server.APIExtensions, extension)
}
// CheckExtension checks if the server has the specified extension.
func (r *ProtocolIncus) CheckExtension(extensionName string) error {
if !r.HasExtension(extensionName) {
return fmt.Errorf("The server is missing the required %q API extension", extensionName)
}
return nil
}
// IsClustered returns true if the server is part of an Incus cluster.
func (r *ProtocolIncus) IsClustered() bool {
return r.server.Environment.ServerClustered
}
// GetServerResources returns the resources available to a given Incus server.
func (r *ProtocolIncus) GetServerResources() (*api.Resources, error) {
if !r.HasExtension("resources") {
return nil, errors.New("The server is missing the required \"resources\" API extension")
}
resources := api.Resources{}
// Fetch the raw value
_, err := r.queryStruct("GET", "/resources", nil, "", &resources)
if err != nil {
return nil, err
}
return &resources, nil
}
// UseProject returns a client that will use a specific project.
func (r *ProtocolIncus) UseProject(name string) InstanceServer {
return &ProtocolIncus{
ctx: r.ctx,
ctxConnected: r.ctxConnected,
ctxConnectedCancel: r.ctxConnectedCancel,
server: r.server,
http: r.http,
httpCertificate: r.httpCertificate,
httpBaseURL: r.httpBaseURL,
httpProtocol: r.httpProtocol,
httpUserAgent: r.httpUserAgent,
httpUnixPath: r.httpUnixPath,
requireAuthenticated: r.requireAuthenticated,
clusterTarget: r.clusterTarget,
project: name,
eventConns: make(map[string]*websocket.Conn), // New project specific listener conns.
eventListeners: make(map[string][]*EventListener), // New project specific listeners.
skipEvents: r.skipEvents,
oidcClient: r.oidcClient,
}
}
// UseTarget returns a client that will target a specific cluster member.
// Use this member-specific operations such as specific container
// placement, preparing a new storage pool or network, ...
func (r *ProtocolIncus) UseTarget(name string) InstanceServer {
return &ProtocolIncus{
ctx: r.ctx,
ctxConnected: r.ctxConnected,
ctxConnectedCancel: r.ctxConnectedCancel,
server: r.server,
http: r.http,
httpCertificate: r.httpCertificate,
httpBaseURL: r.httpBaseURL,
httpProtocol: r.httpProtocol,
httpUserAgent: r.httpUserAgent,
httpUnixPath: r.httpUnixPath,
requireAuthenticated: r.requireAuthenticated,
project: r.project,
eventConns: make(map[string]*websocket.Conn), // New target specific listener conns.
eventListeners: make(map[string][]*EventListener), // New target specific listeners.
skipEvents: r.skipEvents,
oidcClient: r.oidcClient,
clusterTarget: name,
}
}
// IsAgent returns true if the server is an Incus agent.
func (r *ProtocolIncus) IsAgent() bool {
return r.server != nil && r.server.Environment.Server == "incus-agent"
}
// GetMetrics returns the text OpenMetrics data.
func (r *ProtocolIncus) GetMetrics() (string, error) {
// Check that the server supports it.
if !r.HasExtension("metrics") {
return "", errors.New("The server is missing the required \"metrics\" API extension")
}
// Prepare the request.
requestURL, err := r.setQueryAttributes(fmt.Sprintf("%s/1.0/metrics", r.httpBaseURL.String()))
if err != nil {
return "", err
}
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return "", err
}
// Send the request.
resp, err := r.DoHTTP(req)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Bad HTTP status: %d", resp.StatusCode)
}
// Get the content.
content, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(content), nil
}
// ApplyServerPreseed configures a target Incus server with the provided server and cluster configuration.
func (r *ProtocolIncus) ApplyServerPreseed(config api.InitPreseed) error {
// Apply server configuration.
if len(config.Server.Config) > 0 {
// Get current config.
server, etag, err := r.GetServer()
if err != nil {
return fmt.Errorf("Failed to retrieve current server configuration: %w", err)
}
for k, v := range config.Server.Config {
server.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = r.UpdateServer(server.Writable(), etag)
if err != nil {
return fmt.Errorf("Failed to update server configuration: %w", err)
}
}
// Apply storage configuration.
if len(config.Server.StoragePools) > 0 {
// Get the list of storagePools.
storagePoolNames, err := r.GetStoragePoolNames()
if err != nil {
return fmt.Errorf("Failed to retrieve list of storage pools: %w", err)
}
// StoragePool creator
createStoragePool := func(storagePool api.StoragePoolsPost) error {
// Create the storagePool if doesn't exist.
err := r.CreateStoragePool(storagePool)
if err != nil {
return fmt.Errorf("Failed to create storage pool %q: %w", storagePool.Name, err)
}
return nil
}
// StoragePool updater.
updateStoragePool := func(target api.StoragePoolsPost) error {
// Get the current storagePool.
storagePool, etag, err := r.GetStoragePool(target.Name)
if err != nil {
return fmt.Errorf("Failed to retrieve current storage pool %q: %w", target.Name, err)
}
// Quick check.
if storagePool.Driver != target.Driver {
return fmt.Errorf("Storage pool %q is of type %q instead of %q", storagePool.Name, storagePool.Driver, target.Driver)
}
// Description override.
if target.Description != "" {
storagePool.Description = target.Description
}
// Config overrides.
for k, v := range target.Config {
storagePool.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = r.UpdateStoragePool(target.Name, storagePool.Writable(), etag)
if err != nil {
return fmt.Errorf("Failed to update storage pool %q: %w", target.Name, err)
}
return nil
}
for _, storagePool := range config.Server.StoragePools {
// New storagePool.
if !slices.Contains(storagePoolNames, storagePool.Name) {
err := createStoragePool(storagePool)
if err != nil {
return err
}
continue
}
// Existing storagePool.
err := updateStoragePool(storagePool)
if err != nil {
return err
}
}
}
// Apply network configuration function.
applyNetwork := func(target api.InitNetworksProjectPost) error {
network, etag, err := r.UseProject(target.Project).GetNetwork(target.Name)
if err != nil {
// Create the network if doesn't exist.
err := r.UseProject(target.Project).CreateNetwork(target.NetworksPost)
if err != nil {
return fmt.Errorf("Failed to create local member network %q in project %q: %w", target.Name, target.Project, err)
}
} else {
// Description override.
if target.Description != "" {
network.Description = target.Description
}
// Config overrides.
for k, v := range target.Config {
network.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = r.UseProject(target.Project).UpdateNetwork(target.Name, network.Writable(), etag)
if err != nil {
return fmt.Errorf("Failed to update local member network %q in project %q: %w", target.Name, target.Project, err)
}
}
return nil
}
// Apply networks in the default project before other projects config applied (so that if the projects
// depend on a network in the default project they can have their config applied successfully).
for i := range config.Server.Networks {
// Populate default project if not specified for backwards compatibility with earlier
// preseed dump files.
if config.Server.Networks[i].Project == "" {
config.Server.Networks[i].Project = api.ProjectDefaultName
}
if config.Server.Networks[i].Project != api.ProjectDefaultName {
continue
}
err := applyNetwork(config.Server.Networks[i])
if err != nil {
return err
}
}
// Apply project configuration.
if len(config.Server.Projects) > 0 {
// Get the list of projects.
projectNames, err := r.GetProjectNames()
if err != nil {
return fmt.Errorf("Failed to retrieve list of projects: %w", err)
}
// Project creator.
createProject := func(project api.ProjectsPost) error {
// Create the project if doesn't exist.
err := r.CreateProject(project)
if err != nil {
return fmt.Errorf("Failed to create local member project %q: %w", project.Name, err)
}
return nil
}
// Project updater.
updateProject := func(target api.ProjectsPost) error {
// Get the current project.
project, etag, err := r.GetProject(target.Name)
if err != nil {
return fmt.Errorf("Failed to retrieve current project %q: %w", target.Name, err)
}
// Description override.
if target.Description != "" {
project.Description = target.Description
}
// Config overrides.
for k, v := range target.Config {
project.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = r.UpdateProject(target.Name, project.Writable(), etag)
if err != nil {
return fmt.Errorf("Failed to update local member project %q: %w", target.Name, err)
}
return nil
}
for _, project := range config.Server.Projects {
// New project.
if !slices.Contains(projectNames, project.Name) {
err := createProject(project)
if err != nil {
return err
}
continue
}
// Existing project.
err := updateProject(project)
if err != nil {
return err
}
}
}
// Apply networks in non-default projects after project config applied (so that their projects exist).
for i := range config.Server.Networks {
if config.Server.Networks[i].Project == api.ProjectDefaultName {
continue
}
err := applyNetwork(config.Server.Networks[i])
if err != nil {
return err
}
}
// Apply storage volumes configuration.
applyStorageVolume := func(storageVolume api.InitStorageVolumesProjectPost) error {
// Get the current storageVolume.
currentStorageVolume, etag, err := r.UseProject(storageVolume.Project).GetStoragePoolVolume(storageVolume.Pool, storageVolume.Type, storageVolume.Name)
if err != nil {
// Create the storage volume if it doesn't exist.
err := r.UseProject(storageVolume.Project).CreateStoragePoolVolume(storageVolume.Pool, storageVolume.StorageVolumesPost)
if err != nil {
return fmt.Errorf("Failed to create storage volume %q in project %q on pool %q: %w", storageVolume.Name, storageVolume.Project, storageVolume.Pool, err)
}
} else {
// Quick check.
if currentStorageVolume.Type != storageVolume.Type {
return fmt.Errorf("Storage volume %q in project %q is of type %q instead of %q", currentStorageVolume.Name, storageVolume.Project, currentStorageVolume.Type, storageVolume.Type)
}
// Prepare the update.
newStorageVolume := api.StorageVolumePut{}
err = util.DeepCopy(currentStorageVolume.Writable(), &newStorageVolume)
if err != nil {
return fmt.Errorf("Failed to copy configuration of storage volume %q in project %q: %w", storageVolume.Name, storageVolume.Project, err)
}
// Description override.
if storageVolume.Description != "" {
newStorageVolume.Description = storageVolume.Description
}
// Config overrides.
for k, v := range storageVolume.Config {
newStorageVolume.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = r.UseProject(storageVolume.Project).UpdateStoragePoolVolume(storageVolume.Pool, storageVolume.Type, currentStorageVolume.Name, newStorageVolume, etag)
if err != nil {
return fmt.Errorf("Failed to update storage volume %q in project %q: %w", storageVolume.Name, storageVolume.Project, err)
}
}
return nil
}
// Apply storage volumes in the default project before other projects config.
for i := range config.Server.StorageVolumes {
// Populate default project if not specified.
if config.Server.StorageVolumes[i].Project == "" {
config.Server.StorageVolumes[i].Project = api.ProjectDefaultName
}
// Populate default type if not specified.
if config.Server.StorageVolumes[i].Type == "" {
config.Server.StorageVolumes[i].Type = "custom"
}
err := applyStorageVolume(config.Server.StorageVolumes[i])
if err != nil {
return err
}
}
// Apply profile configuration.
if len(config.Server.Profiles) > 0 {
// Apply profile configuration.
applyProfile := func(profile api.InitProfileProjectPost) error {
// Get the current profile.
currentProfile, etag, err := r.UseProject(profile.Project).GetProfile(profile.Name)
if err != nil {
// // Create the profile if it doesn't exist.
err := r.UseProject(profile.Project).CreateProfile(profile.ProfilesPost)
if err != nil {
return fmt.Errorf("Failed to create profile %q in project %q: %w", profile.Name, profile.Project, err)
}
} else {
// Prepare the update.
updatedProfile := api.ProfilePut{}
err = util.DeepCopy(currentProfile.Writable(), &updatedProfile)
if err != nil {
return fmt.Errorf("Failed to copy configuration of profile %q in project %q: %w", profile.Name, profile.Project, err)
}
// Description override.
if profile.Description != "" {
updatedProfile.Description = profile.Description
}
// Config overrides.
for k, v := range profile.Config {
updatedProfile.Config[k] = fmt.Sprintf("%v", v)
}
// Device overrides.
for k, v := range profile.Devices {
// New device.
_, ok := updatedProfile.Devices[k]
if !ok {
updatedProfile.Devices[k] = v
continue
}
// Existing device.
for configKey, configValue := range v {
updatedProfile.Devices[k][configKey] = fmt.Sprintf("%v", configValue)
}
}
// Apply it.
err = r.UseProject(profile.Project).UpdateProfile(profile.Name, updatedProfile, etag)
if err != nil {
return fmt.Errorf("Failed to update profile %q in project %q: %w", profile.Name, profile.Project, err)
}
}
return nil
}
for _, profile := range config.Server.Profiles {
if profile.Project == "" {
profile.Project = api.ProjectDefaultName
}
err := applyProfile(profile)
if err != nil {
return err
}
}
}
// Apply certificate configuration.
if len(config.Server.Certificates) > 0 {
for _, certificate := range config.Server.Certificates {
err := r.CreateCertificate(certificate)
if err != nil {
return fmt.Errorf("Failed to create certificate %q: %w", certificate.Name, err)
}
}
}
// Cluster configuration.
if config.Cluster != nil && config.Cluster.Enabled {
// Get the current cluster configuration
currentCluster, etag, err := r.GetCluster()
if err != nil {
return fmt.Errorf("Failed to retrieve current cluster config: %w", err)
}
// Check if already enabled
if !currentCluster.Enabled {
// Configure the cluster
op, err := r.UpdateCluster(config.Cluster.ClusterPut, etag)
if err != nil {
return fmt.Errorf("Failed to configure cluster: %w", err)
}
err = op.Wait()
if err != nil {
return fmt.Errorf("Failed to configure cluster: %w", err)
}
}
}
return nil
}
|