Skip to content

Commit b4ec460

Browse files
committed
Add update-connection to machine start and init
This allows users to set the associated machine's system connection to the system default when running `podman machine init --now` or `podman machine start`. It also changes the default bbehavior of these commands in that the user will be prompted and asked if they would like to switch the system connection. It also introduces a command line switch called `--update-connection`. If the switch is unset, then the user will be prmpted. If the command value is explicitly set to `false`, the user will not be prompted and the system connection will not be altered. If the value is set to `true`, the system connection will be made the default and the user will not be prompted. Fixes: https://issues.redhat.com/browse/RUN-3632 Signed-off-by: Brent Baude <[email protected]>
1 parent 8aea109 commit b4ec460

File tree

12 files changed

+289
-19
lines changed

12 files changed

+289
-19
lines changed

cmd/podman/machine/init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ func init() {
166166
providerFlagName := "provider"
167167
flags.StringVar(&providerOverride, providerFlagName, "", "Override the default machine provider")
168168
_ = initCmd.RegisterFlagCompletionFunc(providerFlagName, autocompleteMachineProvider)
169+
170+
setDefaultConnectionFlagName := "update-connection"
171+
flags.BoolVarP(&setDefaultSystemConn, setDefaultConnectionFlagName, "u", false, "Set default system connection for this machine")
169172
}
170173

171174
func initMachine(cmd *cobra.Command, args []string) error {
@@ -296,6 +299,7 @@ func initMachine(cmd *cobra.Command, args []string) error {
296299
if now {
297300
return start(cmd, args)
298301
}
302+
299303
extra := ""
300304
if initOpts.Name != defaultMachineName {
301305
extra = " " + initOpts.Name

cmd/podman/machine/start.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var (
2323
Example: `podman machine start podman-machine-default`,
2424
ValidArgsFunction: autocompleteMachine,
2525
}
26-
startOpts = machine.StartOptions{}
26+
startOpts = machine.StartOptions{}
27+
setDefaultSystemConn bool
2728
)
2829

2930
func init() {
@@ -38,15 +39,13 @@ func init() {
3839

3940
quietFlagName := "quiet"
4041
flags.BoolVarP(&startOpts.Quiet, quietFlagName, "q", false, "Suppress machine starting status output")
41-
}
4242

43-
func start(_ *cobra.Command, args []string) error {
44-
var (
45-
err error
46-
)
43+
setDefaultConnectionFlagName := "update-connection"
44+
flags.BoolVarP(&setDefaultSystemConn, setDefaultConnectionFlagName, "u", false, "Set default system connection for this machine")
45+
}
4746

47+
func start(cmd *cobra.Command, args []string) error {
4848
startOpts.NoInfo = startOpts.Quiet || startOpts.NoInfo
49-
5049
vmName := defaultMachineName
5150
if len(args) > 0 && len(args[0]) > 0 {
5251
vmName = args[0]
@@ -61,10 +60,18 @@ func start(_ *cobra.Command, args []string) error {
6160
fmt.Printf("Starting machine %q\n", vmName)
6261
}
6362

64-
if err := shim.Start(mc, vmProvider, startOpts); err != nil {
63+
shouldUpdate := processSystemConnUpdate(cmd, setDefaultSystemConn)
64+
if err := shim.Start(mc, vmProvider, startOpts, shouldUpdate); err != nil {
6565
return err
6666
}
6767
fmt.Printf("Machine %q started successfully\n", vmName)
6868
newMachineEvent(events.Start, events.Event{Name: vmName})
6969
return nil
7070
}
71+
72+
func processSystemConnUpdate(cmd *cobra.Command, updateVal bool) *bool {
73+
if !cmd.Flags().Changed("update-connection") {
74+
return nil
75+
}
76+
return &updateVal
77+
}

docs/source/markdown/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ podman-logs.1.md
2828
podman-machine-init.1.md
2929
podman-machine-list.1.md
3030
podman-machine-set.1.md
31+
podman-machine-start.1.md
3132
podman-manifest-add.1.md
3233
podman-manifest-annotate.1.md
3334
podman-manifest-create.1.md
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
####> This option file is used in:
2+
####> podman machine init, machine start
3+
####> If file is edited, make sure the changes
4+
####> are applicable to all of those.
5+
#### **--update-connection**, **-u**
6+
7+
When used in conjunction with `podman machine init --now` or `podman machine start`, this option sets the
8+
associated machine system connection as the default. When using this option, a `-u` or `-update-connection` will
9+
set the value to true. To set this value to false, meaning no change and no prompting,
10+
use `--update-connection=false`.
11+
12+
If the value is set to true, the machine connection will be set as the system default.
13+
If the value is set to false, the system default will be unchanged.
14+
If the option is not set, the user will be prompted and asked if it should be changed.

docs/source/markdown/podman-machine-init.1.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ as the host Windows operating system.
127127

128128
@@option tls-verify
129129

130+
@@option update-connection
131+
130132
#### **--usb**=*bus=number,devnum=number* or *vendor=hexadecimal,product=hexadecimal*
131133

132134
Assign a USB device from the host to the VM via USB passthrough.

docs/source/markdown/podman-machine-start.1.md renamed to docs/source/markdown/podman-machine-start.1.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Suppress informational tips.
3939

4040
Suppress machine starting status output.
4141

42+
@@option update-connection
43+
4244
## EXAMPLES
4345

4446
Start the specified podman machine.

pkg/machine/e2e/config_init_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package e2e_test
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67

@@ -23,6 +24,7 @@ type initMachine struct {
2324
timezone string
2425
rootful bool
2526
volumes []string
27+
updateConnection *bool
2628
userModeNetworking bool
2729
tlsVerify *bool
2830

@@ -78,6 +80,10 @@ func (i *initMachine) buildCmd(m *machineTestBuilder) []string {
7880
if i.tlsVerify != nil {
7981
cmd = append(cmd, "--tls-verify="+strconv.FormatBool(*i.tlsVerify))
8082
}
83+
if i.updateConnection != nil {
84+
cmd = append(cmd, fmt.Sprintf("--update-connection=%s", strconv.FormatBool(*i.updateConnection)))
85+
}
86+
8187
name := m.name
8288
cmd = append(cmd, name)
8389

@@ -175,6 +181,11 @@ func (i *initMachine) withTlsVerify(tlsVerify *bool) *initMachine {
175181
return i
176182
}
177183

184+
func (i *initMachine) withUpdateConnection(value *bool) *initMachine {
185+
i.updateConnection = value
186+
return i
187+
}
188+
178189
func (i *initMachine) withUserModeNetworking(r bool) *initMachine { //nolint:unused,nolintlint
179190
i.userModeNetworking = r
180191
return i

pkg/machine/e2e/config_start_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package e2e_test
22

3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
38
type startMachine struct {
49
/*
510
No command line args other than a machine vm name (also not required)
611
*/
7-
quiet bool
8-
noInfo bool
12+
quiet bool
13+
noInfo bool
14+
updateConnection *bool
915
}
1016

1117
func (s *startMachine) buildCmd(m *machineTestBuilder) []string {
@@ -19,6 +25,9 @@ func (s *startMachine) buildCmd(m *machineTestBuilder) []string {
1925
if s.noInfo {
2026
cmd = append(cmd, "--no-info")
2127
}
28+
if s.updateConnection != nil {
29+
cmd = append(cmd, fmt.Sprintf("--update-connection=%s", strconv.FormatBool(*s.updateConnection)))
30+
}
2231
return cmd
2332
}
2433

@@ -31,3 +40,12 @@ func (s *startMachine) withNoInfo() *startMachine {
3140
s.noInfo = true
3241
return s
3342
}
43+
44+
func (s *startMachine) withUpdateConnection(value *bool) *startMachine {
45+
s.updateConnection = value
46+
return s
47+
}
48+
49+
func ptrBool(v bool) *bool {
50+
return &v
51+
}

pkg/machine/e2e/rm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ var _ = Describe("podman machine rm", func() {
147147

148148
barName := "bar"
149149
bar := new(initMachine)
150-
session, err = mb.setName(barName).setCmd(bar.withImage(mb.imagePath).withNow()).run()
150+
session, err = mb.setName(barName).setCmd(bar.withUpdateConnection(ptrBool(false)).withImage(mb.imagePath).withNow()).run()
151151
Expect(err).ToNot(HaveOccurred())
152152
Expect(session).To(Exit(0))
153153

0 commit comments

Comments
 (0)