Skip to content

Commit

Permalink
cmd/tailscale,ipn: add auto-update flag and pref
Browse files Browse the repository at this point in the history
The flag is hidden and a noop for now. Adding propagation to tailscaled
and persistence only. The prefs field is wrapped in a struct to allow
for future expansion (like update schedule).

Updates #6907

Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
  • Loading branch information
awly committed Aug 14, 2023
1 parent c40d095 commit 96bbf75
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/tailscale/cli/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type setArgsT struct {
acceptedRisks string
profileName string
forceDaemon bool
autoUpdate bool
}

func newSetFlagSet(goos string, setArgs *setArgsT) *flag.FlagSet {
Expand All @@ -60,6 +61,7 @@ func newSetFlagSet(goos string, setArgs *setArgsT) *flag.FlagSet {
setf.StringVar(&setArgs.hostname, "hostname", "", "hostname to use instead of the one provided by the OS")
setf.StringVar(&setArgs.advertiseRoutes, "advertise-routes", "", "routes to advertise to other nodes (comma-separated, e.g. \"10.0.0.0/8,192.168.0.0/24\") or empty string to not advertise routes")
setf.BoolVar(&setArgs.advertiseDefaultRoute, "advertise-exit-node", false, "offer to be an exit node for internet traffic for the tailnet")
setf.BoolVar(&setArgs.autoUpdate, "auto-update", false, "HIDDEN: automatically update to the latest available version in the background")
if safesocket.GOOSUsesPeerCreds(goos) {
setf.StringVar(&setArgs.opUser, "operator", "", "Unix username to allow to operate on tailscaled without sudo")
}
Expand Down Expand Up @@ -98,6 +100,7 @@ func runSet(ctx context.Context, args []string) (retErr error) {
Hostname: setArgs.hostname,
OperatorUser: setArgs.opUser,
ForceDaemon: setArgs.forceDaemon,
AutoUpdate: &ipn.AutoUpdatePrefs{Enable: setArgs.autoUpdate},
},
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/tailscale/cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func newUpFlagSet(goos string, upArgs *upArgsT, cmd string) *flag.FlagSet {
}
upf := newFlagSet(cmd)

// When adding new flags, prefer to put them under "tailscale set" instead
// of here. Setting preferences via "tailscale up" is deprecated.
upf.BoolVar(&upArgs.qr, "qr", false, "show QR code for login URLs")
upf.StringVar(&upArgs.authKeyOrFile, "auth-key", "", `node authorization key; if it begins with "file:", then it's a path to a file containing the authkey`)

Expand Down Expand Up @@ -712,6 +714,7 @@ func init() {
addPrefFlagMapping("operator", "OperatorUser")
addPrefFlagMapping("ssh", "RunSSH")
addPrefFlagMapping("nickname", "ProfileName")
addPrefFlagMapping("auto-update", "AutoUpdate")
}

func addPrefFlagMapping(flagName string, prefNames ...string) {
Expand Down
5 changes: 5 additions & 0 deletions ipn/ipn_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion ipn/ipn_view.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion ipn/prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ type Prefs struct {
// and CLI.
ProfileName string `json:",omitempty"`

// AutoUpdate sets the auto-update preferences for the node agent. See
// AutoUpdatePrefs docs for more details. Nil means auto-updates are
// disabled.
AutoUpdate *AutoUpdatePrefs `json:",omitempty"`

// The Persist field is named 'Config' in the file for backward
// compatibility with earlier versions.
// TODO(apenwarr): We should move this out of here, it's not a pref.
Expand All @@ -203,6 +208,14 @@ type Prefs struct {
Persist *persist.Persist `json:"Config"`
}

// AutoUpdatePrefs are the auto update settings for the node agent.
type AutoUpdatePrefs struct {
// Enable specifies whether background auto-updates are enabled. When
// enabled, tailscaled will periodically check for available updates and
// apply them.
Enable bool
}

// MaskedPrefs is a Prefs with an associated bitmask of which fields are set.
type MaskedPrefs struct {
Prefs
Expand All @@ -228,6 +241,7 @@ type MaskedPrefs struct {
NetfilterModeSet bool `json:",omitempty"`
OperatorUserSet bool `json:",omitempty"`
ProfileNameSet bool `json:",omitempty"`
AutoUpdateSet bool `json:",omitempty"`
}

// ApplyEdits mutates p, assigning fields from m.Prefs for each MaskedPrefs
Expand Down Expand Up @@ -283,6 +297,12 @@ func (m *MaskedPrefs) Pretty() string {
if v.Type().Elem().Kind() == reflect.String {
return "%s=%q"
}
case reflect.Struct:
return "%s=%+v"
case reflect.Pointer:
if v.Type().Elem().Kind() == reflect.Struct {
return "%s=%+v"
}
}
return "%s=%v"
}
Expand Down Expand Up @@ -333,6 +353,9 @@ func (p *Prefs) pretty(goos string) string {
if p.ShieldsUp {
sb.WriteString("shields=true ")
}
if p.AutoUpdate != nil {
fmt.Fprintf(&sb, "autoUpdate={enable=%v} ", p.AutoUpdate.Enable)
}
if p.ExitNodeIP.IsValid() {
fmt.Fprintf(&sb, "exit=%v lan=%t ", p.ExitNodeIP, p.ExitNodeAllowLANAccess)
} else if !p.ExitNodeID.IsZero() {
Expand Down Expand Up @@ -413,7 +436,19 @@ func (p *Prefs) Equals(p2 *Prefs) bool {
compareIPNets(p.AdvertiseRoutes, p2.AdvertiseRoutes) &&
compareStrings(p.AdvertiseTags, p2.AdvertiseTags) &&
p.Persist.Equals(p2.Persist) &&
p.ProfileName == p2.ProfileName
p.ProfileName == p2.ProfileName &&
p.AutoUpdate.Equals(p2.AutoUpdate)
}

func (au *AutoUpdatePrefs) Equals(au2 *AutoUpdatePrefs) bool {
if (au == nil) != (au2 == nil) {
return false
}
if au == nil {
return true
}
return au.Enable == au2.Enable

}

func compareIPNets(a, b []netip.Prefix) bool {
Expand Down
21 changes: 21 additions & 0 deletions ipn/prefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestPrefsEqual(t *testing.T) {
"NetfilterMode",
"OperatorUser",
"ProfileName",
"AutoUpdate",
"Persist",
}
if have := fieldsOf(reflect.TypeOf(Prefs{})); !reflect.DeepEqual(have, prefsHandles) {
Expand Down Expand Up @@ -288,6 +289,26 @@ func TestPrefsEqual(t *testing.T) {
&Prefs{ProfileName: "home"},
false,
},
{
&Prefs{AutoUpdate: &AutoUpdatePrefs{Enable: true}},
&Prefs{AutoUpdate: &AutoUpdatePrefs{Enable: false}},
false,
},
{
&Prefs{AutoUpdate: &AutoUpdatePrefs{Enable: true}},
&Prefs{AutoUpdate: nil},
false,
},
{
&Prefs{AutoUpdate: nil},
&Prefs{AutoUpdate: &AutoUpdatePrefs{Enable: false}},
false,
},
{
&Prefs{AutoUpdate: &AutoUpdatePrefs{Enable: true}},
&Prefs{AutoUpdate: &AutoUpdatePrefs{Enable: true}},
true,
},
}
for i, tt := range tests {
got := tt.a.Equals(tt.b)
Expand Down

0 comments on commit 96bbf75

Please sign in to comment.