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 support for starting instances on creation #410

Merged
merged 6 commits into from
Jan 20, 2024
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
19 changes: 14 additions & 5 deletions cmd/incus/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (c *cmdCreate) Run(cmd *cobra.Command, args []string) error {
return nil
}

_, _, err = c.create(c.global.conf, args)
_, _, err = c.create(c.global.conf, args, false)
return err
}

func (c *cmdCreate) create(conf *config.Config, args []string) (incus.InstanceServer, string, error) {
func (c *cmdCreate) create(conf *config.Config, args []string, launch bool) (incus.InstanceServer, string, error) {
var name string
var image string
var remote string
Expand Down Expand Up @@ -153,10 +153,18 @@ func (c *cmdCreate) create(conf *config.Config, args []string) (incus.InstanceSe
}

if !c.global.flagQuiet {
if name == "" {
fmt.Printf(i18n.G("Creating the instance") + "\n")
if d.HasExtension("instance_create_start") && launch {
if name == "" {
fmt.Printf(i18n.G("Launching the instance") + "\n")
} else {
fmt.Printf(i18n.G("Launching %s")+"\n", name)
}
} else {
fmt.Printf(i18n.G("Creating %s")+"\n", name)
if name == "" {
fmt.Printf(i18n.G("Creating the instance") + "\n")
} else {
fmt.Printf(i18n.G("Creating %s")+"\n", name)
}
}
}

Expand Down Expand Up @@ -246,6 +254,7 @@ func (c *cmdCreate) create(conf *config.Config, args []string) (incus.InstanceSe
Name: name,
InstanceType: c.flagType,
Type: instanceDBType,
Start: launch,
}

req.Config = configMap
Expand Down
7 changes: 6 additions & 1 deletion cmd/incus/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ func (c *cmdLaunch) Run(cmd *cobra.Command, args []string) error {
}

// Call the matching code from init
d, name, err := c.init.create(conf, args)
d, name, err := c.init.create(conf, args, true)
if err != nil {
return err
}

// Check if the instance was started by the server.
if d.HasExtension("instance_create_start") {
return nil
}

// Get the remote
var remote string
if len(args) == 2 {
Expand Down
41 changes: 35 additions & 6 deletions cmd/incusd/instances_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ func createFromImage(s *state.State, r *http.Request, p api.Project, profiles []
return err
}

return instanceCreateFromImage(s, r, img, args, op)
// Actually create the instance.
err = instanceCreateFromImage(s, r, img, args, op)
if err != nil {
return err
}

return instanceCreateFinish(s, req, args)
}

resources := map[string][]api.URL{}
Expand Down Expand Up @@ -173,8 +179,13 @@ func createFromNone(s *state.State, r *http.Request, projectName string, profile
}

run := func(op *operations.Operation) error {
// Actually create the instance.
_, err := instanceCreateAsEmpty(s, args)
return err
if err != nil {
return err
}

return instanceCreateFinish(s, req, args)
}

resources := map[string][]api.URL{}
Expand Down Expand Up @@ -377,7 +388,8 @@ func createFromMigration(s *state.State, r *http.Request, projectName string, pr

instOp.Done(nil) // Complete operation that was created earlier, to release lock.
runRevert.Success()
return nil

return instanceCreateFinish(s, req, args)
}

resources := map[string][]api.URL{}
Expand Down Expand Up @@ -535,6 +547,7 @@ func createFromCopy(s *state.State, r *http.Request, projectName string, profile
}

run := func(op *operations.Operation) error {
// Actually create the instance.
_, err := instanceCreateAsCopy(s, instanceCreateAsCopyOpts{
sourceInstance: source,
targetInstance: args,
Expand All @@ -547,7 +560,7 @@ func createFromCopy(s *state.State, r *http.Request, projectName string, profile
return err
}

return nil
return instanceCreateFinish(s, req, args)
}

resources := map[string][]api.URL{}
Expand Down Expand Up @@ -634,8 +647,9 @@ func createFromBackup(s *state.State, r *http.Request, projectName string, data
}

// Check project permissions.
var req api.InstancesPost
err = s.DB.Cluster.Transaction(s.ShutdownCtx, func(ctx context.Context, tx *db.ClusterTx) error {
req := api.InstancesPost{
req = api.InstancesPost{
InstancePut: bInfo.Config.Container.InstancePut,
Name: bInfo.Name,
Source: api.InstanceSource{}, // Only relevant for "copy" or "migration", but may not be nil.
Expand Down Expand Up @@ -759,7 +773,8 @@ func createFromBackup(s *state.State, r *http.Request, projectName string, data
}

runRevert.Success()
return nil

return instanceCreateFinish(s, &req, db.InstanceArgs{Name: bInfo.Name, Project: bInfo.Project})
}

resources := map[string][]api.URL{}
Expand Down Expand Up @@ -1315,3 +1330,17 @@ func clusterCopyContainerInternal(s *state.State, r *http.Request, source instan
// Run the migration
return createFromMigration(s, nil, projectName, profiles, req)
}

func instanceCreateFinish(s *state.State, req *api.InstancesPost, args db.InstanceArgs) error {
if req == nil || !req.Start {
return nil
}

// Start the instance.
inst, err := instance.LoadByProjectAndName(s, args.Project, args.Name)
if err != nil {
return fmt.Errorf("Failed to load the instance: %w", err)
}

return inst.Start(false)
}
7 changes: 7 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2311,3 +2311,10 @@ for the attached disk to be connected to the `virtio-blk` bus.
Adds a new `loki.instance` server configuration key to customize the `instance` field in Loki events.
This can be used to expose the name of the cluster rather than the individual system name sending
the event as that's usually already covered by the `location` field.

## `instance_create_start`

Adds a new `start` field to the `POST /1.0/instances` API which when set
to `true` will have the instance automatically start upon creation.

In this scenario, the creation and startup is part of a single background operation.
5 changes: 5 additions & 0 deletions doc/rest-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2390,6 +2390,11 @@ definitions:
x-go-name: Restore
source:
$ref: '#/definitions/InstanceSource'
start:
description: Whether to start the instance after creation
example: true
type: boolean
x-go-name: Start
stateful:
description: Whether the instance currently has saved state on disk
example: false
Expand Down
1 change: 1 addition & 0 deletions internal/version/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ var APIExtensions = []string{
"certificate_description",
"disk_io_bus_virtio_blk",
"loki_config_instance",
"instance_create_start",
}

// APIExtensionsCount returns the number of available API extensions.
Expand Down
44 changes: 27 additions & 17 deletions po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: LXD\n"
"Report-Msgid-Bugs-To: lxc-devel@lists.linuxcontainers.org\n"
"POT-Creation-Date: 2024-01-15 22:31+0100\n"
"POT-Creation-Date: 2024-01-19 22:59-0500\n"
"PO-Revision-Date: 2022-03-10 15:06+0000\n"
"Last-Translator: Krombel <krombel@krombel.de>\n"
"Language-Team: German <https://hosted.weblate.org/projects/linux-containers/"
Expand Down Expand Up @@ -1007,7 +1007,7 @@ msgstr ""
msgid "As neither could be found, the raw SPICE socket can be found at:"
msgstr ""

#: cmd/incus/create.go:348 cmd/incus/rebuild.go:131
#: cmd/incus/create.go:357 cmd/incus/rebuild.go:131
msgid "Asked for a VM but image is of type container"
msgstr ""

Expand Down Expand Up @@ -1121,7 +1121,7 @@ msgstr ""
msgid "Bad key/value pair: %s"
msgstr ""

#: cmd/incus/copy.go:138 cmd/incus/create.go:213 cmd/incus/move.go:358
#: cmd/incus/copy.go:138 cmd/incus/create.go:221 cmd/incus/move.go:358
#: cmd/incus/project.go:129
#, fuzzy, c-format
msgid "Bad key=value pair: %q"
Expand Down Expand Up @@ -1307,7 +1307,7 @@ msgstr ""
msgid "Can't use an image with --empty"
msgstr ""

#: cmd/incus/create.go:317
#: cmd/incus/create.go:326
#, c-format
msgid ""
"Cannot override config for device %q: Device not found in profile devices"
Expand Down Expand Up @@ -1849,12 +1849,12 @@ msgstr "Anhalten des Containers fehlgeschlagen!"
msgid "Created: %s"
msgstr "Erstellt: %s"

#: cmd/incus/create.go:159
#: cmd/incus/create.go:166
#, c-format
msgid "Creating %s"
msgstr "Erstelle %s"

#: cmd/incus/create.go:157
#: cmd/incus/create.go:164
#, fuzzy
msgid "Creating the instance"
msgstr "kann nicht zum selben Container Namen kopieren"
Expand Down Expand Up @@ -2244,7 +2244,7 @@ msgstr ""
"Optionen:\n"
"\n"

#: cmd/incus/create.go:408
#: cmd/incus/create.go:417
#, fuzzy
msgid "Didn't get any affected image, instance or snapshot from server"
msgstr ""
Expand Down Expand Up @@ -2788,17 +2788,17 @@ msgstr ""
msgid "Failed import request: %w"
msgstr "Akzeptiere Zertifikat"

#: cmd/incus/create.go:172
#: cmd/incus/create.go:180
#, fuzzy, c-format
msgid "Failed loading network %q: %w"
msgstr "Akzeptiere Zertifikat"

#: cmd/incus/create.go:296
#: cmd/incus/create.go:305
#, c-format
msgid "Failed loading profile %q for device override: %w"
msgstr ""

#: cmd/incus/create.go:223
#: cmd/incus/create.go:231
#, fuzzy, c-format
msgid "Failed loading storage pool %q: %w"
msgstr "Akzeptiere Zertifikat"
Expand Down Expand Up @@ -3573,7 +3573,7 @@ msgstr ""
msgid "Instance name is mandatory"
msgstr ""

#: cmd/incus/create.go:419
#: cmd/incus/create.go:428
#, c-format
msgid "Instance name is: %s"
msgstr ""
Expand Down Expand Up @@ -3788,6 +3788,16 @@ msgstr ""
msgid "Last used: never"
msgstr ""

#: cmd/incus/create.go:160
#, fuzzy, c-format
msgid "Launching %s"
msgstr "Erstelle %s"

#: cmd/incus/create.go:158
#, fuzzy
msgid "Launching the instance"
msgstr "kann nicht zum selben Container Namen kopieren"

#: cmd/incus/info.go:221
#, fuzzy, c-format
msgid "Link detected: %v"
Expand Down Expand Up @@ -6014,7 +6024,7 @@ msgstr "kann nicht zum selben Container Namen kopieren"
msgid "Retrieve the instance's console log"
msgstr "Herunterfahren des Containers erzwingen."

#: cmd/incus/create.go:362
#: cmd/incus/create.go:371
#, c-format
msgid "Retrieving image: %s"
msgstr ""
Expand Down Expand Up @@ -6721,7 +6731,7 @@ msgstr ""
msgid "Start instances"
msgstr "kann nicht zum selben Container Namen kopieren"

#: cmd/incus/launch.go:78
#: cmd/incus/launch.go:83
#, c-format
msgid "Starting %s"
msgstr ""
Expand Down Expand Up @@ -7034,7 +7044,7 @@ msgid ""
"restarted"
msgstr ""

#: cmd/incus/create.go:440
#: cmd/incus/create.go:449
msgid "The instance you are starting doesn't have any network attached to it."
msgstr ""

Expand Down Expand Up @@ -7240,11 +7250,11 @@ msgstr "Wartezeit bevor der Container gestoppt wird."
msgid "Timestamps:"
msgstr "Zeitstempel:\n"

#: cmd/incus/create.go:442
#: cmd/incus/create.go:451
msgid "To attach a network to an instance, use: incus network attach"
msgstr ""

#: cmd/incus/create.go:441
#: cmd/incus/create.go:450
msgid "To create a new network, use: incus network create"
msgstr ""

Expand Down Expand Up @@ -7323,7 +7333,7 @@ msgstr ""
msgid "Trust token for %s: "
msgstr "Administrator Passwort für %s: "

#: cmd/incus/action.go:311 cmd/incus/launch.go:110
#: cmd/incus/action.go:311 cmd/incus/launch.go:115
#, c-format
msgid "Try `incus info --show-log %s` for more info"
msgstr ""
Expand Down
Loading
Loading