-
Notifications
You must be signed in to change notification settings - Fork 208
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
Allow toolbox containers to be created with a custom hostname #1007
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
| "time" | ||
|
|
||
|
|
@@ -44,6 +45,7 @@ var ( | |
| createFlags struct { | ||
| container string | ||
| distro string | ||
| hostname string | ||
| image string | ||
| release string | ||
| } | ||
|
|
@@ -78,6 +80,12 @@ func init() { | |
| "", | ||
| "Create a toolbox container for a different operating system distribution than the host") | ||
|
|
||
| flags.StringVarP(&createFlags.hostname, | ||
| "hostname", | ||
| "", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: if you use flags.StringVar (ie., without the trailing |
||
| "", | ||
| "Set the container's hostname (defaults to 'toolbox')") | ||
|
|
||
| flags.StringVarP(&createFlags.image, | ||
| "image", | ||
| "i", | ||
|
|
@@ -95,6 +103,10 @@ func init() { | |
| } | ||
|
|
||
| func create(cmd *cobra.Command, args []string) error { | ||
| // This regex filters out strings which are not valid hostnames, according to RFC-1123. | ||
| // Source: https://stackoverflow.com/a/106223 | ||
| var hostnameRegexp = regexp.MustCompile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])$") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to have this regular expression directly in the code? Can't we use some Go package for it? |
||
|
|
||
| if utils.IsInsideContainer() { | ||
| if !utils.IsInsideToolboxContainer() { | ||
| return errors.New("this is not a toolbox container") | ||
|
|
@@ -115,8 +127,13 @@ func create(cmd *cobra.Command, args []string) error { | |
| return errors.New("options --image and --release cannot be used together") | ||
| } | ||
|
|
||
| if cmd.Flag("hostname").Changed && !hostnameRegexp.MatchString(cmd.Flag("hostname").Value.String()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| return errors.New("invalid hostname") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be good to improve this error. See the examples in |
||
| } | ||
|
|
||
| var container string | ||
| var containerArg string | ||
| var hostname = cmd.Flag("hostname").Value.String() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessary ... |
||
|
|
||
| if len(args) != 0 { | ||
| container = args[0] | ||
|
|
@@ -158,14 +175,14 @@ func create(cmd *cobra.Command, args []string) error { | |
| return err | ||
| } | ||
|
|
||
| if err := createContainer(container, image, release, true); err != nil { | ||
| if err := createContainer(container, image, release, hostname, true); err != nil { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... because you can pass |
||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func createContainer(container, image, release string, showCommandToEnter bool) error { | ||
| func createContainer(container, image, release string, hostname string, showCommandToEnter bool) error { | ||
| if container == "" { | ||
| panic("container not specified") | ||
| } | ||
|
|
@@ -178,6 +195,10 @@ func createContainer(container, image, release string, showCommandToEnter bool) | |
| panic("release not specified") | ||
| } | ||
|
|
||
| if hostname == "" { | ||
| hostname = "toolbox" | ||
| } | ||
|
|
||
| enterCommand := getEnterCommand(container) | ||
|
|
||
| logrus.Debugf("Checking if container %s already exists", container) | ||
|
|
@@ -392,7 +413,7 @@ func createContainer(container, image, release string, showCommandToEnter bool) | |
| createArgs = append(createArgs, xdgRuntimeDirEnv...) | ||
|
|
||
| createArgs = append(createArgs, []string{ | ||
| "--hostname", "toolbox", | ||
| "--hostname", hostname, | ||
| "--ipc", "host", | ||
| "--label", "com.github.containers.toolbox=true", | ||
| }...) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,25 @@ teardown() { | |
| assert_output --regexp "Created[[:blank:]]+fedora-toolbox-32" | ||
| } | ||
|
|
||
| @test "create: Create a container with a custom hostname ('test.host-name.local')" { | ||
| run $TOOLBOX -y create --hostname test.host-name.local | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be good to use |
||
|
|
||
| assert_success | ||
| assert_output --partial "Enter with: toolbox enter" | ||
|
|
||
| run $TOOLBOX -y run -- hostname | ||
|
|
||
| assert_success | ||
| assert_output --partial "test.host-name.local" | ||
| } | ||
|
|
||
| @test "create: Create a container with an invalid hostname ('test.host-name.local.')" { | ||
| run $TOOLBOX -y create --hostname test.host-name.local. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto about |
||
|
|
||
| assert_failure | ||
| assert_line --index 0 "Error: invalid hostname" | ||
| } | ||
|
|
||
| @test "create: Try to create a container based on non-existent image" { | ||
| run $TOOLBOX -y create -i foo.org/bar | ||
|
|
||
|
|
||
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.
Typo alert: netowork