~timo-jyrinki/ubuntu-sdk-tools/test_automerge

« back to all changes in this revision

Viewing changes to fixables/devices.go

  • Committer: Benjamin Zeller
  • Date: 2016-07-06 09:06:40 UTC
  • mfrom: (18.1.12 ubuntu-sdk-tools)
  • Revision ID: benjamin.zeller@canonical.com-20160706090640-6nwco3u5b27hfki9
- Deprecate -a and -f switches when creating containers, we can get the information from the image
- Automatically fix broken devices in containers
- Automatically add DRI devices if they are not in the containers
- Automatically fix NVidia driver issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Author: Benjamin Zeller <benjamin.zeller@canonical.com>
 
17
 */
 
18
package fixables
 
19
 
 
20
import (
 
21
        "github.com/lxc/lxd"
 
22
        "launchpad.net/ubuntu-sdk-tools"
 
23
        "fmt"
 
24
        "os"
 
25
        "github.com/lxc/lxd/shared"
 
26
)
 
27
 
 
28
type DevicesFixable struct { }
 
29
 
 
30
func (*DevicesFixable) run(client *lxd.Client, container *shared.ContainerInfo, doFix bool) error {
 
31
 
 
32
        for devName, dev := range container.Devices {
 
33
                var toCheck string = ""
 
34
                switch dev["type"] {
 
35
                case "disk":
 
36
                        toCheck,_ = dev["source"]
 
37
                case "unix-char":
 
38
                        _, hasMaj := dev["major"]
 
39
                        _, hasMin := dev["minor"]
 
40
                        optional, hasOptional := dev["optional"]
 
41
 
 
42
                        //do not care about optional devices
 
43
                        if (hasOptional && optional == "true") {
 
44
                                continue
 
45
                        }
 
46
 
 
47
                        if !(hasMaj && hasMin) {
 
48
                                toCheck = fmt.Sprintf("/%s", dev["path"])
 
49
                        }
 
50
                }
 
51
 
 
52
                if len(toCheck) > 0 {
 
53
                        if _, err := os.Stat(toCheck); os.IsNotExist(err) {
 
54
                                if doFix {
 
55
                                        err = ubuntu_sdk_tools.RemoveDeviceSync(client, container.Name, devName)
 
56
                                        if err != nil {
 
57
                                                return err
 
58
                                        }
 
59
                                } else {
 
60
                                        return fmt.Errorf("Device %s does not exist on the host.", toCheck)
 
61
                                }
 
62
                        }
 
63
                }
 
64
        }
 
65
        return nil
 
66
}
 
67
 
 
68
func (c *DevicesFixable) CheckContainer(client *lxd.Client, container string) error {
 
69
        info, err := client.ContainerInfo(container)
 
70
        if err != nil {
 
71
                return err
 
72
        }
 
73
 
 
74
        return c.run(client, info, false)
 
75
}
 
76
 
 
77
func (c *DevicesFixable) FixContainer(client *lxd.Client, container string) error {
 
78
        info, err := client.ContainerInfo(container)
 
79
        if err != nil {
 
80
                return err
 
81
        }
 
82
 
 
83
        return c.run(client, info, true)
 
84
}
 
85
 
 
86
func (c *DevicesFixable) Check(client *lxd.Client) error {
 
87
        fmt.Printf("Checking for broken devices...\n")
 
88
 
 
89
        targets, err := ubuntu_sdk_tools.FindClickTargets(client)
 
90
        if err != nil {
 
91
                return err
 
92
        }
 
93
 
 
94
        for _, target := range targets {
 
95
                err = c.run(client, &target.Container, false)
 
96
                if err != nil {
 
97
                        return err
 
98
                }
 
99
        }
 
100
        return nil
 
101
}
 
102
func (c *DevicesFixable) Fix(client *lxd.Client) error {
 
103
        fmt.Println("Checking for and removing broken devices....")
 
104
        targets, err := ubuntu_sdk_tools.FindClickTargets(client)
 
105
        if err != nil {
 
106
                return err
 
107
        }
 
108
 
 
109
        for _, target := range targets {
 
110
                err = c.run(client, &target.Container, true)
 
111
                if err != nil {
 
112
                        return err
 
113
                }
 
114
        }
 
115
        return nil
 
116
}
 
117
 
 
118
func (*DevicesFixable) NeedsRoot () bool {
 
119
        return false
 
120
}
 
 
b'\\ No newline at end of file'