-
Notifications
You must be signed in to change notification settings - Fork 12
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 install/uninstall containers #228
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright (C) 2022 Toitware ApS. All rights reserved. | ||
// Use of this source code is governed by an MIT-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/toitlang/jaguar/cmd/jag/directory" | ||
) | ||
|
||
func ContainerCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "container", | ||
Short: "Manipulate the set of installed containers on a device", | ||
Long: "Manipulate the set of installed containers on a device.\n" + | ||
"Installed containers run on boot and are primarily used to provide\n" + | ||
"services and drivers to applications.", | ||
} | ||
|
||
cmd.AddCommand(ContainerListCmd()) | ||
cmd.AddCommand(ContainerInstallCmd()) | ||
cmd.AddCommand(ContainerUninstallCmd()) | ||
return cmd | ||
} | ||
|
||
func ContainerListCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, err := directory.GetDeviceConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
deviceSelect, err := parseDeviceFlag(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := cmd.Context() | ||
sdk, err := GetSDK(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
device, err := GetDevice(ctx, cfg, sdk, false, deviceSelect) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
containers, err := device.ContainerList(ctx, sdk) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for id, name := range containers { | ||
fmt.Println(id + ": " + name) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringP("device", "d", "", "use device with a given name, id, or address") | ||
return cmd | ||
} | ||
|
||
func ContainerInstallCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "install <name> <file>", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
deviceSelect, err := parseDeviceFlag(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cfg, err := directory.GetDeviceConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entrypoint := args[1] | ||
if stat, err := os.Stat(entrypoint); err != nil { | ||
if os.IsNotExist(err) { | ||
return fmt.Errorf("no such file or directory: '%s'", entrypoint) | ||
} | ||
return fmt.Errorf("can't stat file '%s', reason: %w", entrypoint, err) | ||
} else if stat.IsDir() { | ||
return fmt.Errorf("can't run directory: '%s'", entrypoint) | ||
} | ||
|
||
sdk, err := GetSDK(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name := args[0] | ||
fmt.Printf("Installing container '%s' from '%s' on '%s' ...\n", name, entrypoint, device.Name) | ||
return RunFile(cmd, device, sdk, entrypoint, containerRunOptions(name)) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringP("device", "d", "", "use device with a given name, id, or address") | ||
return cmd | ||
} | ||
|
||
func ContainerUninstallCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "uninstall <name>", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
deviceSelect, err := parseDeviceFlag(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cfg, err := directory.GetDeviceConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sdk, err := GetSDK(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name := args[0] | ||
fmt.Printf("Uninstalling container '%s' on '%s' ...\n", name, device.Name) | ||
return device.ContainerUninstall(ctx, sdk, containerRunOptions(name)) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringP("device", "d", "", "use device with a given name, id, or address") | ||
return cmd | ||
} | ||
|
||
func containerRunOptions(name string) string { | ||
escapedName := strings.ReplaceAll(name, "\"", "\\\"") | ||
runOptions := "{ \"container.name\": \"" + escapedName + "\" }" | ||
return runOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (C) 2022 Toitware ApS. All rights reserved. | ||
// Use of this source code is governed by an MIT-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import device | ||
import uuid | ||
import system.containers | ||
|
||
flash_ ::= device.FlashStore | ||
jaguar_ / uuid.Uuid ::= containers.current | ||
|
||
class ContainerRegistry: | ||
static KEY_ /string ::= "jag.containers" | ||
|
||
loaded_ / bool := false | ||
id_by_name_ / Map ::= {:} // Map<string, uuid.Uuid> | ||
name_by_id_ / Map ::= {:} // Map<uuid.Uuid, string> | ||
|
||
entries -> Map: | ||
ensure_loaded_ | ||
result := {:} | ||
name_by_id_.do: | id/uuid.Uuid name/string | result["$id"] = name | ||
return result | ||
|
||
install name/string? [block] -> uuid.Uuid: | ||
ensure_loaded_ | ||
// Uninstall all unnamed images. This is used to prepare | ||
// for running another unnamed image. | ||
images/List ::= containers.images | ||
images.do: | id/uuid.Uuid | | ||
if not name_by_id_.contains id: | ||
containers.uninstall id | ||
if name: uninstall name | ||
// Now actually create the image by invoking the block. | ||
image := block.call | ||
if not name: return image | ||
// Update the name mapping and make sure we do not have | ||
// an old name for the same image floating around. | ||
old := name_by_id_.get image | ||
if old: id_by_name_.remove old | ||
name_by_id_[image] = name | ||
id_by_name_[name] = image | ||
store_ | ||
return image | ||
|
||
uninstall name/string -> uuid.Uuid?: | ||
ensure_loaded_ | ||
id := id_by_name_.get name --if_absent=: return null | ||
containers.uninstall id | ||
id_by_name_.remove name | ||
name_by_id_.remove id | ||
store_ | ||
return id | ||
|
||
ensure_loaded_ -> none: | ||
if loaded_: return | ||
dirty := true | ||
entries := {:} | ||
catch --trace: | ||
entries = flash_.get KEY_ | ||
dirty = false | ||
// Run through the images actually installed in flash and update the | ||
// registry accordingly. This involves inventing names for unexpected | ||
// containers found in flash and pruning names for containers that | ||
// we cannot find anymore. | ||
index := 0 | ||
images/List ::= containers.images | ||
images.do: | id/uuid.Uuid | | ||
name/string? := null | ||
kasperl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// We are not sure that the entries loaded from flash is a map | ||
// with the correct structure, so we guard the access to the | ||
// individual entries and treat malformed ones as non-existing. | ||
catch: name = entries.get "$id" | ||
if not name: | ||
name = (id == jaguar_) ? "jaguar" : "container-$(index++)" | ||
dirty = true | ||
id_by_name_[name] = id | ||
name_by_id_[id] = name | ||
// We're done loading. If we've changed the name mapping in any way, | ||
// we write the updated entries back into flash. | ||
loaded_ = true | ||
if dirty or entries.size > images.size: store_ | ||
|
||
store_ -> none: | ||
flash_.set KEY_ entries |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.