errtracker: try multiple paths to read machine-id #3337

Merged
merged 3 commits into from May 18, 2017

Conversation

Projects
None yet
4 participants
Contributor

morphis commented May 17, 2017

The machine-id file is at different locations depending on how the system
is setup. On Fedora for example /var/lib/dbus/machine-id doesn't exist
but we have /etc/machine-id. See
https://www.freedesktop.org/software/systemd/man/machine-id.html for a
few more details.

errtracker/errtracker.go
+ // but we have /etc/machine-id. See
+ // https://www.freedesktop.org/software/systemd/man/machine-id.html for a
+ // few more details.
+ machineIDs = []string{"/var/lib/dbus/machine-id", "/etc/machine-id"}
@morphis

morphis May 17, 2017

Contributor

We could alternatively default to read /etc/machine-id on all systems but I am not sure why /var/lib/dbus/machine-id was preferred over /etc/machine-id. Maybe because of 14.04 support?

A few trivials

errtracker/errtracker.go
+ // but we have /etc/machine-id. See
+ // https://www.freedesktop.org/software/systemd/man/machine-id.html for a
+ // few more details.
+ machineIDs = []string{"/var/lib/dbus/machine-id", "/etc/machine-id"}
@zyga

zyga May 17, 2017

Contributor

I think the order should be the other way around. We should prefer /etc/machine-id

@morphis

morphis May 17, 2017

Contributor

We can do that but I wanted to preserve what we did so far and keep reporting these ids. If everyone agrees I am happy to change this.

@zyga

zyga May 17, 2017

Contributor

According to the manual page /etc/machine-id should take priority so I'd go with that.

+ var err error
+ for _, id := range machineIDs {
+ machineID, err = ioutil.ReadFile(id)
+ if err == nil {
@zyga

zyga May 17, 2017

Contributor

I think we should only silently ignore the error when the file does not exist and log all other errors.

@mvo5

mvo5 May 18, 2017

Collaborator

One more nitpick/simplification suggestion:

--- a/errtracker/errtracker.go
+++ b/errtracker/errtracker.go
@@ -67,10 +67,8 @@ func distroRelease() string {
 }
 
 func readMachineID() ([]byte, error) {
-       var machineID []byte
-       var err error
        for _, id := range machineIDs {
-               machineID, err = ioutil.ReadFile(id)
+               machineID, err := ioutil.ReadFile(id)
                if err == nil {
                        return bytes.TrimSpace(machineID), nil
                } else if !os.IsNotExist(err) {
errtracker/errtracker.go
}
+
+ if len(machineID) == 0 {
+ return "", fmt.Errorf("Can not report as no machine id found")
@zyga

zyga May 17, 2017

Contributor

We always use "cannot" in error messages.

cannot report error: no suitable machine-id file was found

mvo5 approved these changes May 17, 2017

Looks very nice, one small suggestion about how to make the code slightly nicer.

errtracker/errtracker.go
- return "", err
+ var machineID []byte
+ var err error
+ for _, id := range machineIDs {
@mvo5

mvo5 May 17, 2017

Collaborator

nitpick: I think this would be slightly nicer if extracted into a helper like func readMachineID() ([]byte, error).

Simon Fels added some commits May 17, 2017

errtracker: try multiple paths to read machine-id
The machine-id file is at different locations depending on how the system
is setup. On Fedora for example /var/lib/dbus/machine-id doesn't exist
but we have /etc/machine-id. See
https://www.freedesktop.org/software/systemd/man/machine-id.html for a
few more details.

codecov-io commented May 18, 2017

Codecov Report

Merging #3337 into master will decrease coverage by <.01%.
The diff coverage is 72.72%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3337      +/-   ##
==========================================
- Coverage   77.58%   77.58%   -0.01%     
==========================================
  Files         364      364              
  Lines       24973    24982       +9     
==========================================
+ Hits        19376    19382       +6     
- Misses       3870     3872       +2     
- Partials     1727     1728       +1
Impacted Files Coverage Δ
errtracker/errtracker.go 71.23% <72.72%> (-0.65%) ⬇️
interfaces/sorting.go 93.33% <0%> (-3.34%) ⬇️
cmd/snap/cmd_aliases.go 96% <0%> (+2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d0f3985...75eca28. Read the comment docs.

errtracker/errtracker.go
+ for _, id := range machineIDs {
+ machineID, err = ioutil.ReadFile(id)
+ if err == nil {
+ break
@mvo5

mvo5 May 18, 2017

Collaborator

You can simplify this code by returning here: return bytes.TrimSpace(machineID), nil

Then you can write after the loop: return nil, fmt.Errorf("cannot report: no suitable machine id file found") and avoid the len(machineID) check altogether.

@morphis

morphis May 18, 2017

Contributor

Fixed. Thanks!

zyga approved these changes May 18, 2017

LGTM

@mvo5 mvo5 merged commit 1536a16 into snapcore:master May 18, 2017

5 of 7 checks passed

artful-amd64 autopkgtest running
Details
xenial-i386 autopkgtest running
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
xenial-amd64 autopkgtest finished (success)
Details
xenial-ppc64el autopkgtest finished (success)
Details
yakkety-amd64 autopkgtest finished (success)
Details
zesty-amd64 autopkgtest finished (success)
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment