Merge ~alfonsosanchezbeato/snappy-hwe-snaps/+git/wifi-ap:gadget into ~snappy-hwe-team/snappy-hwe-snaps/+git/wifi-ap:master

Proposed by Alfonso Sanchez-Beato on 2017-06-16
Status: Merged
Approved by: Simon Fels on 2017-06-19
Approved revision: 1aa0ddf048b804de9fe0ff75433e3e492dfbd2c1
Merged at revision: b3b46924451aa1b370b28dd5db97428a5c40150c
Proposed branch: ~alfonsosanchezbeato/snappy-hwe-snaps/+git/wifi-ap:gadget
Merge into: ~snappy-hwe-team/snappy-hwe-snaps/+git/wifi-ap:master
Diff against target: 333 lines (+187/-65)
5 files modified
cmd/client/cmd_config.go (+25/-25)
docs/reference/snap-configuration.md (+22/-0)
snap/hooks/configure (+20/-8)
tests/main/conf-from-gadget/task.yaml (+73/-0)
tests/main/conf-wizard-disabled-from-gadget/task.yaml (+47/-32)
Reviewer Review Type Date Requested Status
Simon Fels Approve on 2017-06-19
System Enablement Bot continuous-integration Approve on 2017-06-16
Roberto Mier Escandón  (community) Approve on 2017-06-16
Konrad Zapałowicz (community) code 2017-06-16 Approve on 2017-06-16
Review via email: mp+325800@code.launchpad.net

Description of the change

Support for default SSID option, set from gadget snap

To post a comment you must log in.
Konrad Zapałowicz (kzapalowicz) wrote :

nice one

review: Approve (code)
Simon Fels (morphis) :
review: Needs Fixing

Thanks for the reviews. Comments addressed and branch re-pushed.

Just a small comment

review: Needs Information
review: Approve
Simon Fels (morphis) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cmd/client/cmd_config.go b/cmd/client/cmd_config.go
2index bdef6ea..f027e18 100644
3--- a/cmd/client/cmd_config.go
4+++ b/cmd/client/cmd_config.go
5@@ -23,29 +23,9 @@ import (
6 "strings"
7 )
8
9-type setCommand struct{}
10-
11-func (cmd *setCommand) Execute(args []string) error {
12- if len(args) < 1 {
13- return fmt.Errorf("usage: %s set key1=value1 key2=value2 ...\n", os.Args[0])
14- }
15-
16- request := make(map[string]string, len(args))
17- for _, arg := range args {
18- i := strings.IndexRune(arg, '=')
19- if i <= 0 {
20- return fmt.Errorf("%q is not in the key=val format", arg)
21- }
22- request[arg[:i]] = arg[i+1:]
23- }
24-
25- b, err := json.Marshal(request)
26-
27- _, err = sendHTTPRequest(getServiceConfigurationURI(), "POST", bytes.NewReader(b))
28- if err != nil {
29- return err
30- }
31+type configCommand struct{}
32
33+func (cmd *configCommand) Execute(args []string) error {
34 return nil
35 }
36
37@@ -71,14 +51,34 @@ func (cmd *getCommand) Execute(args []string) error {
38 return nil
39 }
40
41-type configCommand struct{}
42+type setCommand struct{}
43+
44+func (cmd *setCommand) Execute(args []string) error {
45+ if len(args) < 1 {
46+ return fmt.Errorf("usage: %s set key1=value1 key2=value2 ...\n", os.Args[0])
47+ }
48+
49+ request := make(map[string]string, len(args))
50+ for _, arg := range args {
51+ i := strings.IndexRune(arg, '=')
52+ if i <= 0 {
53+ return fmt.Errorf("%q is not in the key=val format", arg)
54+ }
55+ request[arg[:i]] = arg[i+1:]
56+ }
57+
58+ b, err := json.Marshal(request)
59+
60+ _, err = sendHTTPRequest(getServiceConfigurationURI(), "POST", bytes.NewReader(b))
61+ if err != nil {
62+ return err
63+ }
64
65-func (cmd *configCommand) Execute(args []string) error {
66 return nil
67 }
68
69 func init() {
70 cmd, _ := addCommand("config", "Adjust the service configuration", "", &configCommand{})
71- cmd.AddCommand("set", "", "", &setCommand{})
72 cmd.AddCommand("get", "", "", &getCommand{})
73+ cmd.AddCommand("set", "", "", &setCommand{})
74 }
75diff --git a/docs/reference/snap-configuration.md b/docs/reference/snap-configuration.md
76index 03e94fe..9e84a92 100644
77--- a/docs/reference/snap-configuration.md
78+++ b/docs/reference/snap-configuration.md
79@@ -46,3 +46,25 @@ defaults:
80 After this snippet is added to the gadget.yaml and an updated version of the
81 gadget snap is deployed onto the device the automatic setup, of the AP is disabled
82 once the wifi-ap is installed from the Ubuntu Store.
83+
84+## default.wifi.ssid
85+
86+The *default.wifi.ssid* option allows a device to specify the default name of
87+the wireless network (SSID). This name will be used when creating a network
88+unless changed by the user.
89+
90+This option is used only on installation of the wifi-ap snap, and it is ignored
91+on updates. Therefore, doing *snap set* on it produces no effect. It must be
92+set by setting it as a default in the [gadget
93+snap](https://docs.ubuntu.com/core/en/reference/gadget), for instance:
94+
95+```
96+defaults:
97+ # The alpha numeric key below is the id of the wifi-ap snap assigned in the
98+ # Ubuntu Store. Specifying the snap name instead is not possible.
99+ 2rGgvyaY0CCzlWuKAPwFtCWrgwkM8lqS:
100+ default.wifi.ssid: MyDeviceSSID
101+```
102+
103+A gadget snap with this option must be installed before installing wifi-ap for
104+it to have effect.
105diff --git a/snap/hooks/configure b/snap/hooks/configure
106index 6b984cd..0967ade 100755
107--- a/snap/hooks/configure
108+++ b/snap/hooks/configure
109@@ -21,14 +21,14 @@ set -x
110 # being installed. As the configure hook will be called before the
111 # services are being started we can easily create an empty
112 # configuration to fall back to the default one for everything.
113-value=`snapctl get automatic-setup.disable`
114+value=$(snapctl get automatic-setup.disable)
115 if [ -n "$value" ]; then
116 case "$value" in
117 true)
118- touch $SNAP_COMMON/.block_auto_wizard
119+ touch "$SNAP_COMMON"/.block_auto_wizard
120 ;;
121 false)
122- rm $SNAP_COMMON/.block_auto_wizard || true
123+ rm -f "$SNAP_COMMON"/.block_auto_wizard
124 ;;
125 *)
126 echo "ERROR: invalid value '$value' provided for automatic-setup.disable"
127@@ -39,9 +39,21 @@ else
128 snapctl set automatic-setup.disable=false
129 fi
130
131-# If we haven't marked ourself as setup yet after the snap was installed
132-# we do this now so that the already started management service can
133-# continue doing its job.
134-if [ ! -e $SNAP_COMMON/.setup_done ]; then
135- touch $SNAP_COMMON/.setup_done
136+if [ ! -e "$SNAP_COMMON"/.setup_done ]; then
137+ # Get default SSID from gadget only in first installation
138+ config_file="$SNAP_DATA"/config
139+ if ssid=$(snapctl get default.wifi.ssid) && [ -n "$ssid" ]; then
140+ echo "Configure ssid to $ssid"
141+
142+ if grep -q "WIFI_SSID=" "$config_file"; then
143+ sed -i "s/WIFI_SSID=.*/WIFI_SSID=$ssid/" "$config_file"
144+ else
145+ echo "WIFI_SSID=$ssid" >> "$config_file"
146+ fi
147+ fi
148+
149+ # If we haven't marked ourself as setup yet after the snap was installed
150+ # we do this now so that the already started management service can
151+ # continue doing its job.
152+ touch "$SNAP_COMMON"/.setup_done
153 fi
154diff --git a/tests/main/conf-from-gadget/task.yaml b/tests/main/conf-from-gadget/task.yaml
155new file mode 100644
156index 0000000..e26d7bf
157--- /dev/null
158+++ b/tests/main/conf-from-gadget/task.yaml
159@@ -0,0 +1,73 @@
160+summary: Verify that the default SSID can be read from a gadget snap
161+description: |
162+ We check that we get the SSID name from the defaults in the gadget
163+ snap. To do this, we re-package the gadget snap in the store after
164+ changing gadget.yaml, and then we re-install it. A reboot is needed
165+ after this. After it, we install the wifi-ap snap and check that the
166+ SSID name is as expected.
167+
168+prepare: |
169+ . $TESTSLIB/snap-names.sh
170+ # We have a reboot in the test, so we store this in permanent storage
171+ readlink /snap/$gadget_name/current > $HOME/gadget_version
172+
173+restore: |
174+ . $TESTSLIB/snap-names.sh
175+ # Restore the original gadget snap so that any following tests don't suffer
176+ # from our modified gadget.
177+ read original_revision < $HOME/gadget_version
178+ current_revision="$(readlink /snap/$gadget_name/current)"
179+ if [ "$current_revision" != "$original_revision" ]; then
180+ snap revert --revision=$original_revision $gadget_name
181+ fi
182+ rm $HOME/gadget_version
183+
184+execute: |
185+ . $TESTSLIB/snap-names.sh
186+ . $TESTSLIB/utilities.sh
187+
188+ case "$SPREAD_REBOOT" in
189+ 0)
190+ snap remove wifi-ap
191+ snap install --edge --devmode se-test-tools
192+
193+ # We need a custom gadget snap for this so lets fetch one from the store
194+ # and modify it.
195+ snap download --stable $gadget_name
196+ /snap/bin/se-test-tools.unsquashfs -d gadget ${gadget_name}_*.snap
197+ printf "%s\n" \
198+ "defaults:" \
199+ " # Below snap ID matches the one assigned for wifi-ap in the Ubuntu Store" \
200+ " 2rGgvyaY0CCzlWuKAPwFtCWrgwkM8lqS:" \
201+ " default.wifi.ssid: GadgetSnap" \
202+ >> gadget/meta/gadget.yaml
203+
204+ /snap/bin/se-test-tools.mksquashfs gadget $gadget_name.snap -comp xz -no-xattrs
205+ snap install --dangerous $gadget_name.snap
206+
207+ REBOOT
208+ ;;
209+ 1)
210+ # Applying default configuration from a gadget snap only works when the snap
211+ # comes from the store. It is not possible for an unasserted snap to be
212+ # configured with defaults from the gadget. As a workaround, we install from
213+ # edge and then sideload the snap we are testing.
214+ snap install --edge wifi-ap
215+
216+ # So we run the configure hook
217+ rm /var/snap/wifi-ap/common/.setup_done
218+
219+ # Install prebuilt wifi-ap snap
220+ snap install --dangerous /home/wifi-ap/wifi-ap_*.snap
221+
222+ test "$(snap get wifi-ap default.wifi.ssid)" = "GadgetSnap"
223+
224+ # Wait until we can talk to the service
225+ while ! /snap/bin/wifi-ap.status; do sleep .1 ; done
226+
227+ test "$(/snap/bin/wifi-ap.config get wifi.ssid)" = "GadgetSnap"
228+ ;;
229+ *)
230+ echo "Test failed, too many reboots"
231+ exit 1
232+ esac
233diff --git a/tests/main/conf-wizard-disabled-from-gadget/task.yaml b/tests/main/conf-wizard-disabled-from-gadget/task.yaml
234index 66496a6..84cbbd3 100644
235--- a/tests/main/conf-wizard-disabled-from-gadget/task.yaml
236+++ b/tests/main/conf-wizard-disabled-from-gadget/task.yaml
237@@ -2,49 +2,64 @@ summary: Verify the wizard can be disabled by default from a gadget snap
238
239 prepare: |
240 . $TESTSLIB/snap-names.sh
241- echo $(readlink /snap/$gadget_name/current) > /tmp/gadget_version
242+ # We have a reboot in the test, so we store this in permanent storage
243+ readlink /snap/$gadget_name/current > $HOME/gadget_version
244
245 restore: |
246 . $TESTSLIB/snap-names.sh
247 # Restore the original gadget snap so that any following tests don't suffer
248 # from our modified gadget.
249- original_revision="$(cat /tmp/gadget_version)"
250+ read original_revision < $HOME/gadget_version
251 current_revision="$(readlink /snap/$gadget_name/current)"
252 if [ "$current_revision" != "$original_revision" ]; then
253 snap revert --revision=$original_revision $gadget_name
254 fi
255+ rm $HOME/gadget_version
256
257 execute: |
258 . $TESTSLIB/snap-names.sh
259 . $TESTSLIB/utilities.sh
260
261- snap remove wifi-ap
262- snap install --edge --devmode se-test-tools
263-
264- # We need a custom gadget snap for this so lets fetch one from the store
265- # and modify it.
266- snap download --stable $gadget_name
267- /snap/bin/se-test-tools.unsquashfs -d gadget ${gadget_name}_*.snap
268- cat << EOF >> gadget/meta/gadget.yaml
269- defaults:
270- # Below snap ID matches the one assigned for wifi-ap in the Ubuntu Store
271- 2rGgvyaY0CCzlWuKAPwFtCWrgwkM8lqS:
272- automatic-setup.disable: true
273- EOF
274- /snap/bin/se-test-tools.mksquashfs gadget $gadget_name.snap -comp xz -no-xattrs
275- snap install --dangerous $gadget_name.snap
276-
277- # Applying default configuration from a gadget snap only works when the
278- # snap is installed and comes from the store. It's not possible for an
279- # unasserted snap to be configured with defaults from the gadget.
280- snap install --edge wifi-ap
281-
282- test "$(snap get wifi-ap automatic-setup.disable)" = true
283-
284- # Wait until we can talk to the service
285- while ! /snap/bin/wifi-ap.status; do sleep .1 ; done
286-
287- test "$(/snap/bin/wifi-ap.config get disabled)" = true
288- /snap/bin/wifi-ap.status | grep "ap.active: false"
289- ! pgrep dnsmasq
290- ! pgrep hostapd
291+ case "$SPREAD_REBOOT" in
292+ 0)
293+ snap remove wifi-ap
294+ snap install --edge --devmode se-test-tools
295+
296+ # We need a custom gadget snap for this so lets fetch one from the store
297+ # and modify it.
298+ snap download --stable $gadget_name
299+ /snap/bin/se-test-tools.unsquashfs -d gadget ${gadget_name}_*.snap
300+ printf "%s\n" \
301+ "defaults:" \
302+ " # Below snap ID matches the one assigned for wifi-ap in the Ubuntu Store" \
303+ " 2rGgvyaY0CCzlWuKAPwFtCWrgwkM8lqS:" \
304+ " automatic-setup.disable: true" \
305+ >> gadget/meta/gadget.yaml
306+
307+ /snap/bin/se-test-tools.mksquashfs gadget $gadget_name.snap -comp xz -no-xattrs
308+ snap install --dangerous $gadget_name.snap
309+
310+ REBOOT
311+ ;;
312+ 1)
313+ # Applying default configuration from a gadget snap only works when the snap
314+ # comes from the store. It is not possible for an unasserted snap to be
315+ # configured with defaults from the gadget. As a workaround, we install from
316+ # edge and then sideload the snap we are testing.
317+ snap install --edge wifi-ap
318+ snap install --dangerous /home/wifi-ap/wifi-ap_*.snap
319+
320+ test "$(snap get wifi-ap automatic-setup.disable)" = true
321+
322+ # Wait until we can talk to the service
323+ while ! /snap/bin/wifi-ap.status; do sleep .1 ; done
324+
325+ test "$(/snap/bin/wifi-ap.config get disabled)" = true
326+ /snap/bin/wifi-ap.status | grep "ap.active: false"
327+ ! pgrep dnsmasq
328+ ! pgrep hostapd
329+ ;;
330+ *)
331+ echo "Test failed, too many reboots"
332+ exit 1
333+ esac

Subscribers

People subscribed via source and target branches