1
by Sergio Schvezov
Initial code migration |
1 |
//
|
2 |
// Helpers to work with an Ubuntu image based Upgrade implementation
|
|
3 |
//
|
|
4 |
// Copyright (c) 2013 Canonical Ltd.
|
|
5 |
//
|
|
6 |
// Written by Sergio Schvezov <sergio.schvezov@canonical.com>
|
|
7 |
//
|
|
8 |
package ubuntuimage |
|
9 |
||
10 |
// This program is free software: you can redistribute it and/or modify it
|
|
11 |
// under the terms of the GNU General Public License version 3, as published
|
|
12 |
// by the Free Software Foundation.
|
|
13 |
//
|
|
14 |
// This program is distributed in the hope that it will be useful, but
|
|
15 |
// WITHOUT ANY WARRANTY; without even the implied warranties of
|
|
16 |
// MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
|
17 |
// PURPOSE. See the GNU General Public License for more details.
|
|
18 |
//
|
|
19 |
// You should have received a copy of the GNU General Public License along
|
|
20 |
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
21 |
||
22 |
import ( |
|
29.4.1
by Jani Monoses
Link against crypto/sha512 to work with more SSL certs. |
23 |
_ "crypto/sha512" |
33.2.1
by Jani Monoses
Add TLSSkipVerify command line flag. |
24 |
"crypto/tls"
|
1
by Sergio Schvezov
Initial code migration |
25 |
"encoding/json"
|
26 |
"errors"
|
|
27 |
"fmt"
|
|
154.4.6
by Sergio Schvezov
Prettier code |
28 |
"io/ioutil"
|
1
by Sergio Schvezov
Initial code migration |
29 |
"net/http"
|
30 |
)
|
|
31 |
||
25.1.4
by Sergio Schvezov
Making "full" a constant |
32 |
const ( |
33 |
channelsPath = "/channels.json" |
|
72.2.1
by James Hunt
Add '--list-images' option to display brief image details, one per |
34 |
indexName = "index.json" |
25.1.4
by Sergio Schvezov
Making "full" a constant |
35 |
FULL_IMAGE = "full" |
36 |
)
|
|
1
by Sergio Schvezov
Initial code migration |
37 |
|
33.2.1
by Jani Monoses
Add TLSSkipVerify command line flag. |
38 |
var client = &http.Client{} |
39 |
||
33.3.1
by Jani Monoses
Add docstring |
40 |
// TLSSkipVerify turns off validation of server TLS certificates. It allows connecting
|
41 |
// to HTTPS servers that use self-signed certificates.
|
|
33.2.1
by Jani Monoses
Add TLSSkipVerify command line flag. |
42 |
func TLSSkipVerify() { |
43 |
tr := &http.Transport{ |
|
44 |
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
|
45 |
}
|
|
46 |
client = &http.Client{Transport: tr} |
|
47 |
}
|
|
48 |
||
1
by Sergio Schvezov
Initial code migration |
49 |
func NewChannels(server string) (channels Channels, err error) { |
33.2.1
by Jani Monoses
Add TLSSkipVerify command line flag. |
50 |
resp, err := client.Get(server + channelsPath) |
1
by Sergio Schvezov
Initial code migration |
51 |
if err != nil { |
52 |
return channels, err |
|
53 |
}
|
|
54 |
defer resp.Body.Close() |
|
55 |
dec := json.NewDecoder(resp.Body) |
|
18.1.1
by Sergio Schvezov
Adding tests for ubuntu image server channel operation |
56 |
if err := dec.Decode(&channels); err != nil { |
18.1.3
by Sergio Schvezov
Using fmt.Errorf when error message formatting is needed in the channels*.go |
57 |
return channels, fmt.Errorf("Unable to parse channel information from %s", server) |
18.1.1
by Sergio Schvezov
Adding tests for ubuntu image server channel operation |
58 |
}
|
59 |
return channels, nil |
|
1
by Sergio Schvezov
Initial code migration |
60 |
}
|
61 |
||
62 |
func (channels Channels) GetDeviceChannel(server, channel, device string) (deviceChannel DeviceChannel, err error) { |
|
63 |
if _, found := channels[channel]; !found { |
|
18.1.3
by Sergio Schvezov
Using fmt.Errorf when error message formatting is needed in the channels*.go |
64 |
return deviceChannel, fmt.Errorf("Channel %s not found on server %s", channel, server) |
1
by Sergio Schvezov
Initial code migration |
65 |
} else if _, found := channels[channel].Devices[device]; !found { |
18.1.3
by Sergio Schvezov
Using fmt.Errorf when error message formatting is needed in the channels*.go |
66 |
return deviceChannel, fmt.Errorf("Device %s not found on server %s channel %s", |
25.1.1
by Sergio Schvezov
Sorting images by version and adding relative version support for full images |
67 |
device, server, channel) |
1
by Sergio Schvezov
Initial code migration |
68 |
}
|
69 |
channelUri := server + channels[channel].Devices[device].Index |
|
33.2.1
by Jani Monoses
Add TLSSkipVerify command line flag. |
70 |
resp, err := client.Get(channelUri) |
1
by Sergio Schvezov
Initial code migration |
71 |
if err != nil { |
72 |
return deviceChannel, err |
|
73 |
}
|
|
74 |
defer resp.Body.Close() |
|
75 |
dec := json.NewDecoder(resp.Body) |
|
76 |
err = dec.Decode(&deviceChannel) |
|
18.1.1
by Sergio Schvezov
Adding tests for ubuntu image server channel operation |
77 |
if err != nil { |
18.1.3
by Sergio Schvezov
Using fmt.Errorf when error message formatting is needed in the channels*.go |
78 |
return deviceChannel, fmt.Errorf("Cannot parse channel information for device on %s", channelUri) |
18.1.1
by Sergio Schvezov
Adding tests for ubuntu image server channel operation |
79 |
}
|
1
by Sergio Schvezov
Initial code migration |
80 |
deviceChannel.Alias = channels[channel].Alias |
25.1.1
by Sergio Schvezov
Sorting images by version and adding relative version support for full images |
81 |
order := func(i1, i2 *Image) bool { |
82 |
return i1.Version > i2.Version |
|
83 |
}
|
|
84 |
ImageBy(order).ImageSort(deviceChannel.Images) |
|
72.2.1
by James Hunt
Add '--list-images' option to display brief image details, one per |
85 |
|
86 |
deviceChannel.Url = channelUri |
|
1
by Sergio Schvezov
Initial code migration |
87 |
return deviceChannel, err |
88 |
}
|
|
89 |
||
90 |
func (deviceChannel *DeviceChannel) GetImage(revision int) (image Image, err error) { |
|
91 |
for _, image := range deviceChannel.Images { |
|
25.1.4
by Sergio Schvezov
Making "full" a constant |
92 |
if image.Type == FULL_IMAGE && image.Version == revision { |
1
by Sergio Schvezov
Initial code migration |
93 |
return image, nil |
94 |
}
|
|
95 |
}
|
|
96 |
//If we reached this point, that means we haven't found the image we were looking for.
|
|
18.1.3
by Sergio Schvezov
Using fmt.Errorf when error message formatting is needed in the channels*.go |
97 |
return image, fmt.Errorf("Failed to locate image %d", revision) |
1
by Sergio Schvezov
Initial code migration |
98 |
}
|
25.1.1
by Sergio Schvezov
Sorting images by version and adding relative version support for full images |
99 |
|
72.2.1
by James Hunt
Add '--list-images' option to display brief image details, one per |
100 |
func (deviceChannel *DeviceChannel) ListImageVersions() (err error) { |
101 |
||
102 |
jsonData := map[string]interface{}{} |
|
103 |
||
104 |
resp, err := client.Get(deviceChannel.Url) |
|
105 |
if err != nil { |
|
106 |
return err |
|
107 |
}
|
|
108 |
||
154.4.6
by Sergio Schvezov
Prettier code |
109 |
if resp.StatusCode != 200 { |
72.2.1
by James Hunt
Add '--list-images' option to display brief image details, one per |
110 |
statusErr := errors.New(fmt.Sprintf("Invalid HTTP response: %d", resp.StatusCode)) |
111 |
return statusErr |
|
112 |
}
|
|
113 |
||
114 |
defer resp.Body.Close() |
|
115 |
||
116 |
body, err := ioutil.ReadAll(resp.Body) |
|
117 |
if err != nil { |
|
118 |
return err |
|
119 |
}
|
|
120 |
||
121 |
err = json.Unmarshal(body, &jsonData) |
|
122 |
if err != nil { |
|
123 |
return err |
|
124 |
}
|
|
125 |
||
126 |
images := jsonData["images"].([]interface{}) |
|
127 |
||
128 |
for i := range images { |
|
129 |
entry := images[i].(map[string]interface{}) |
|
130 |
||
131 |
imageType := entry["type"] |
|
132 |
||
133 |
if imageType != FULL_IMAGE { |
|
134 |
// ignore delta images as they cannot be used to
|
|
135 |
// perform an initial device flash
|
|
136 |
continue
|
|
137 |
}
|
|
138 |
||
139 |
fmt.Printf("%d: description='%s'\n", |
|
140 |
int(entry["version"].(float64)), |
|
141 |
entry["description"]) |
|
142 |
}
|
|
143 |
||
144 |
return nil |
|
145 |
}
|
|
146 |
||
25.1.1
by Sergio Schvezov
Sorting images by version and adding relative version support for full images |
147 |
func (deviceChannel *DeviceChannel) GetRelativeImage(revision int) (image Image, err error) { |
148 |
var steps int |
|
149 |
if revision < 0 { |
|
150 |
revision = -revision |
|
151 |
}
|
|
152 |
for _, image := range deviceChannel.Images { |
|
25.1.4
by Sergio Schvezov
Making "full" a constant |
153 |
if image.Type != FULL_IMAGE { |
25.1.1
by Sergio Schvezov
Sorting images by version and adding relative version support for full images |
154 |
continue
|
155 |
}
|
|
156 |
if steps == revision { |
|
157 |
return image, nil |
|
158 |
}
|
|
159 |
steps++ |
|
160 |
}
|
|
161 |
//If we reached this point, that means we haven't found the image we were looking for.
|
|
162 |
if revision == 0 { |
|
163 |
err = errors.New("Failed to locate latest image information") |
|
164 |
} else { |
|
25.1.3
by Sergio Schvezov
Adding error test |
165 |
err = fmt.Errorf("Failed to locate relative image to latest - %d", revision) |
25.1.1
by Sergio Schvezov
Sorting images by version and adding relative version support for full images |
166 |
}
|
167 |
return Image{}, err |
|
168 |
}
|