Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add project support to profiles in preseed init #1608

Merged
merged 5 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 45 additions & 54 deletions client/incus_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,81 +470,72 @@ func (r *ProtocolIncus) ApplyServerPreseed(config api.InitPreseed) error {

// Apply profile configuration.
if config.Server.Profiles != nil && len(config.Server.Profiles) > 0 {
// Get the list of profiles.
profileNames, err := r.GetProfileNames()
if err != nil {
return fmt.Errorf("Failed to retrieve list of profiles: %w", err)
}

// Profile creator.
createProfile := func(profile api.ProfilesPost) error {
// Create the profile if doesn't exist.
err := r.CreateProfile(profile)
if err != nil {
return fmt.Errorf("Failed to create profile %q: %w", profile.Name, err)
}

return nil
}

// Profile updater.
updateProfile := func(target api.ProfilesPost) error {
// Apply profile configuration.
applyProfile := func(profile api.InitProfileProjectPost) error {
// Get the current profile.
profile, etag, err := r.GetProfile(target.Name)
currentProfile, etag, err := r.UseProject(profile.Project).GetProfile(profile.Name)

if err != nil {
return fmt.Errorf("Failed to retrieve current profile %q: %w", target.Name, err)
}
// // 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{}

// Description override.
if target.Description != "" {
profile.Description = target.Description
}
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)
}

// Config overrides.
for k, v := range target.Config {
profile.Config[k] = fmt.Sprintf("%v", v)
}
// Description override.
if profile.Description != "" {
updatedProfile.Description = profile.Description
}

// Device overrides.
for k, v := range target.Devices {
// New device.
_, ok := profile.Devices[k]
if !ok {
profile.Devices[k] = v
continue
// Config overrides.
for k, v := range profile.Config {
updatedProfile.Config[k] = fmt.Sprintf("%v", v)
}

// Existing device.
for configKey, configValue := range v {
profile.Devices[k][configKey] = fmt.Sprintf("%v", configValue)
// 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.UpdateProfile(target.Name, profile.Writable(), etag)
if err != nil {
return fmt.Errorf("Failed to update profile %q: %w", target.Name, err)
// 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 {
// New profile.
if !slices.Contains(profileNames, profile.Name) {
err := createProfile(profile)
if err != nil {
return err
}

continue
if profile.Project == "" {
profile.Project = api.ProjectDefaultName
}

// Existing profile.
err := updateProfile(profile)
err := applyProfile(profile)
if err != nil {
return err
}

}
}

Expand Down
38 changes: 22 additions & 16 deletions cmd/incus/admin_init_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,20 @@ func (c *cmdAdminInit) RunAuto(cmd *cobra.Command, args []string, d incus.Instan
config.StoragePools = []api.StoragePoolsPost{pool}

// Profile entry
config.Profiles = []api.ProfilesPost{{
Name: "default",
ProfilePut: api.ProfilePut{
Devices: map[string]map[string]string{
"root": {
"type": "disk",
"path": "/",
"pool": pool.Name,
config.Profiles = []api.InitProfileProjectPost{{
ProfilesPost: api.ProfilesPost{
Name: "default",
ProfilePut: api.ProfilePut{
Devices: map[string]map[string]string{
"root": {
"type": "disk",
"path": "/",
"pool": pool.Name,
},
},
},
},
Project: api.ProjectDefaultName,
}}
}

Expand Down Expand Up @@ -170,17 +173,20 @@ func (c *cmdAdminInit) RunAuto(cmd *cobra.Command, args []string, d incus.Instan

// Add it to the profile
if config.Profiles == nil {
config.Profiles = []api.ProfilesPost{{
Name: "default",
ProfilePut: api.ProfilePut{
Devices: map[string]map[string]string{
"eth0": {
"type": "nic",
"network": network.Name,
"name": "eth0",
config.Profiles = []api.InitProfileProjectPost{{
ProfilesPost: api.ProfilesPost{
Name: "default",
ProfilePut: api.ProfilePut{
Devices: map[string]map[string]string{
"eth0": {
"type": "nic",
"network": network.Name,
"name": "eth0",
},
},
},
},
Project: api.ProjectDefaultName,
}}
} else {
config.Profiles[0].Devices["eth0"] = map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion cmd/incus/admin_init_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *cmdAdminInit) RunDump(d incus.InstanceServer) error {
}

for _, profile := range profiles {
profilesPost := api.ProfilesPost{}
profilesPost := api.InitProfileProjectPost{}
profilesPost.Config = profile.Config
profilesPost.Description = profile.Description
profilesPost.Devices = profile.Devices
Expand Down
13 changes: 8 additions & 5 deletions cmd/incus/admin_init_interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ func (c *cmdAdminInit) RunInteractive(cmd *cobra.Command, args []string, d incus
config.Server.Config = map[string]string{}
config.Server.Networks = []api.InitNetworksProjectPost{}
config.Server.StoragePools = []api.StoragePoolsPost{}
config.Server.Profiles = []api.ProfilesPost{
config.Server.Profiles = []api.InitProfileProjectPost{
{
Name: "default",
ProfilePut: api.ProfilePut{
Config: map[string]string{},
Devices: map[string]map[string]string{},
ProfilesPost: api.ProfilesPost{
Name: "default",
ProfilePut: api.ProfilePut{
Config: map[string]string{},
Devices: map[string]map[string]string{},
},
},
Project: api.ProjectDefaultName,
},
}

Expand Down
3 changes: 3 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2696,3 +2696,6 @@ Add new memory dump API at `/1.0/instances/NAME/debug/memory`.

## `init_preseed_storage_volumes`
This API extension provides the ability to configure storage volumes in preseed init.

## `init_preseed_profile_project`
This API extension provides the ability to specify the project as part of profile definitions in preseed init.
47 changes: 46 additions & 1 deletion doc/rest-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ definitions:
description: Profiles to add
example: '"default" profile with a root disk device'
items:
$ref: '#/definitions/ProfilesPost'
$ref: '#/definitions/InitProfileProjectPost'
type: array
x-go-name: Profiles
projects:
Expand Down Expand Up @@ -1324,6 +1324,51 @@ definitions:
title: InitPreseed represents initialization configuration that can be supplied to `init`.
type: object
x-go-package: github.com/lxc/incus/v6/shared/api
InitProfileProjectPost:
properties:
Project:
description: Project in which the profile will reside
example: '"default"'
type: string
config:
additionalProperties:
type: string
description: Instance configuration map (refer to doc/instances.md)
example:
limits.cpu: "4"
limits.memory: 4GiB
type: object
x-go-name: Config
description:
description: Description of the profile
example: Medium size instances
type: string
x-go-name: Description
devices:
additionalProperties:
additionalProperties:
type: string
type: object
description: List of devices
example:
eth0:
name: eth0
network: mybr0
type: nic
root:
path: /
pool: default
type: disk
type: object
x-go-name: Devices
name:
description: The name of the new profile
example: foo
type: string
x-go-name: Name
title: InitProfileProjectPost represents the fields of a new profile along with its associated project.
type: object
x-go-package: github.com/lxc/incus/v6/shared/api
InitStorageVolumesProjectPost:
properties:
Pool:
Expand Down
1 change: 1 addition & 0 deletions internal/version/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ var APIExtensions = []string{
"network_bridge_acl_devices",
"instance_debug_memory",
"init_preseed_storage_volumes",
"init_preseed_profile_project",
}

// APIExtensionsCount returns the number of available API extensions.
Expand Down
15 changes: 14 additions & 1 deletion shared/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type InitLocalPreseed struct {

// Profiles to add
// Example: "default" profile with a root disk device
Profiles []ProfilesPost `json:"profiles" yaml:"profiles"`
Profiles []InitProfileProjectPost `json:"profiles" yaml:"profiles"`

// Projects to add
// Example: "default" project
Expand All @@ -54,6 +54,19 @@ type InitNetworksProjectPost struct {
Project string
}

// InitProfileProjectPost represents the fields of a new profile along with its associated project.
//
// swagger:model
//
// API extension: init_preseed_profile_project.
type InitProfileProjectPost struct {
ProfilesPost `yaml:",inline"`

// Project in which the profile will reside
// Example: "default"
Project string
}

// InitStorageVolumesProjectPost represents the fields of a new storage volume along with its associated pool.
//
// swagger:model
Expand Down
Loading