-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel-update.go
executable file
·198 lines (158 loc) · 4.38 KB
/
kernel-update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//usr/bin/go run $0 $@ ; exit
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"golang.org/x/net/html"
)
const kernelURLBase = "http://kernel.ubuntu.com/~kernel-ppa/mainline/"
var logger *log.Logger
type fileURLs struct {
allHeaders string
currentArchHeaders string
currentArchImage string
currentArchModules string
}
func main() {
logger = log.New(os.Stdout, "", log.Lshortfile)
var imageFlavor string
flag.StringVar(&imageFlavor, "flavor", "generic", "Kernel flavor: lowlatency|generic.")
flag.Parse()
htmlBodyReader := httpGet(kernelURLBase)
defer htmlBodyReader.Close()
latestVersion := parseLatestKernelVersion(htmlBodyReader)
logger.Println(latestVersion)
dir, err := ioutil.TempDir("", "kernel-update")
if err != nil {
logger.Fatalln(err.Error())
}
logger.Println("Using temporary directory", dir)
os.Chdir(dir)
downloadPackages(kernelURLBase, latestVersion, imageFlavor)
if err := installPackages(dir); err != nil {
logger.Println(err.Error())
}
fmt.Println("Cleaning up...")
err = os.RemoveAll(dir)
if err != nil {
fmt.Println(err.Error())
}
}
func installPackages(dir string) error {
globPath := filepath.Join(dir, "*.deb")
_, err := filepath.Glob(globPath)
if err != nil {
logger.Println(err.Error())
return err
}
sudoCmd := fmt.Sprintf("sudo dpkg -i %s", globPath)
fmt.Printf("Executing '%s'\n", sudoCmd)
cmd := exec.Command("/bin/sh", "-c", sudoCmd)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
return err
}
func httpGet(kernelURL string) io.ReadCloser {
resp, err := http.Get(kernelURL)
if err != nil {
log.Fatalln("Failed to perform request")
}
return resp.Body
}
func parseLatestKernelVersion(reader io.Reader) string {
var latestVersion string
doc, err := html.Parse(reader)
if err != nil {
log.Fatal(err)
}
walkKernelVersionsTree(doc, &latestVersion)
return latestVersion
}
// always sets the latestVersion global variable to the last a href
func walkKernelVersionsTree(n *html.Node, latestVersion *string) {
if n.Type == html.ElementNode && n.Data == "a" {
*latestVersion = n.Attr[0].Val
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walkKernelVersionsTree(c, latestVersion)
}
}
func downloadPackages(kernelURLBase, latestVersion, flavor string) {
files := getPackageFiles(kernelURLBase, latestVersion, flavor)
downloadFromURL(kernelURLBase + latestVersion + files.allHeaders)
downloadFromURL(kernelURLBase + latestVersion + files.currentArchHeaders)
downloadFromURL(kernelURLBase + latestVersion + files.currentArchImage)
downloadFromURL(kernelURLBase + latestVersion + files.currentArchModules)
}
func downloadFromURL(url string) {
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
fmt.Println("Downloading", url, "to", fileName)
// TODO: check file existence first with io.IsExist
output, err := os.Create(fileName)
if err != nil {
fmt.Println("Error while creating", fileName, "-", err)
return
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
defer response.Body.Close()
n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
fmt.Println(n, "bytes downloaded.")
}
func getPackageFiles(kernelURLBase, latestVersion, flavor string) fileURLs {
var fileList fileURLs
htmlBodyReader := httpGet(kernelURLBase + latestVersion)
doc, err := html.Parse(htmlBodyReader)
if err != nil {
log.Fatal(err)
}
walkBuildsTree(doc, &fileList, flavor)
return fileList
}
// always sets the latestVersion global variable to the last a href
func walkBuildsTree(n *html.Node, urls *fileURLs, flavor string) {
if n.Type == html.ElementNode && n.Data == "a" {
file := n.Attr[0].Val
if strings.Contains(file, "headers") &&
strings.Contains(file, "all") {
urls.allHeaders = file
}
if strings.Contains(file, runtime.GOARCH) &&
strings.Contains(file, flavor) {
if strings.Contains(file, "headers") {
urls.currentArchHeaders = file
}
if strings.Contains(file, "image") {
urls.currentArchImage = file
}
if strings.Contains(file, "modules") {
urls.currentArchModules = file
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walkBuildsTree(c, urls, flavor)
}
}