Merge ~morphis/snappy-hwe-snaps/+git/wifi-ap:improve-wizard into ~snappy-hwe-team/snappy-hwe-snaps/+git/wifi-ap:master

Proposed by Simon Fels
Status: Merged
Approved by: Simon Fels
Approved revision: 492e3263bfee589b41777790e700bf5f653b5077
Merged at revision: 4d85a3cf2dcbc9fcb749320a45bf64549c3c7251
Proposed branch: ~morphis/snappy-hwe-snaps/+git/wifi-ap:improve-wizard
Merge into: ~snappy-hwe-team/snappy-hwe-snaps/+git/wifi-ap:master
Diff against target: 274 lines (+89/-28)
5 files modified
bin/ap.sh (+16/-9)
bin/helper.sh (+6/-0)
cmd/client/cmd_config.go (+16/-9)
cmd/client/cmd_wizard.go (+47/-10)
snapcraft.yaml (+4/-0)
Reviewer Review Type Date Requested Status
System Enablement Bot continuous-integration Approve
Konrad Zapałowicz (community) Approve
Review via email: mp+308686@code.launchpad.net

Description of the change

Improve several things

 * Better wording for the wizard
 * Add support for disabling connection sharing
 * A few script improvements

To post a comment you must log in.
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Konrad Zapałowicz (kzapalowicz) wrote :

Lgtm

review: Approve
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Matteo Croce (teknoraver) :
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Simon Fels (morphis) :
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/bin/ap.sh b/bin/ap.sh
2index 6c26b30..89750fa 100755
3--- a/bin/ap.sh
4+++ b/bin/ap.sh
5@@ -38,9 +38,10 @@ if ! ifconfig $WIFI_INTERFACE ; then
6 exit 1
7 fi
8
9-shutdown() {
10+cleanup_on_exit() {
11 DNSMASQ_PID=$(cat $SNAP_DATA/dnsmasq.pid)
12- kill -TERM $DNSMASQ_PID
13+ # If dnsmasq is already gone don't error out here
14+ kill -TERM $DNSMASQ_PID || true
15 wait $DNSMASQ_PID
16
17 iface=$WIFI_INTERFACE
18@@ -48,7 +49,7 @@ shutdown() {
19 iface=$DEFAULT_ACCESS_POINT_INTERFACE
20 fi
21
22- if [ "$SHARE_NETWORK_INTERFACE" != "none" ] ; then
23+ if [ $SHARE_DISABLED -eq 0 ] ; then
24 # flush forwarding rules out
25 iptables --table nat --delete POSTROUTING --out-interface $SHARE_NETWORK_INTERFACE -j MASQUERADE
26 iptables --delete FORWARD --in-interface $iface -j ACCEPT
27@@ -58,6 +59,13 @@ shutdown() {
28 if [ "$WIFI_INTERFACE_MODE" == "virtual" ] ; then
29 $SNAP/bin/iw dev $iface del
30 fi
31+
32+ if [ is_nm_running ] ; then
33+ # Hand interface back to network-manager. This will also trigger the
34+ # auto connection process inside network-manager to get connected
35+ # with the previous network.
36+ $SNAP/bin/nmcli d set $iface managed yes
37+ fi
38 }
39
40 iface=$WIFI_INTERFACE
41@@ -88,8 +96,7 @@ if [ "$WIFI_INTERFACE_MODE" = "direct" ] ; then
42 fi
43
44
45-nm_status=`$SNAP/bin/nmcli -t -f RUNNING general`
46-if [ "$nm_status" = "running" ] ; then
47+if [ is_nm_running ] ; then
48 # Prevent network-manager from touching the interface we want to use. If
49 # network-manager was configured to use the interface its nothing we want
50 # to prevent here as this is how the user configured the system.
51@@ -106,7 +113,7 @@ if [ $? -ne 0 ] ; then
52 $SNAP/bin/iw dev $iface del
53 fi
54
55- if [ "$nm_status" = "running" ] ; then
56+ if [ is_nm_running ] ; then
57 # Hand interface back to network-manager. This will also trigger the
58 # auto connection process inside network-manager to get connected
59 # with the previous network.
60@@ -120,7 +127,7 @@ fi
61 ifconfig $iface $WIFI_ADDRESS netmask $WIFI_NETMASK
62 sleep 2
63
64-if [ "$SHARE_NETWORK_INTERFACE" != "none" ] ; then
65+if [ $SHARE_DISABLED -eq 0 ] ; then
66 # Enable NAT to forward our network connection
67 iptables --table nat --append POSTROUTING --out-interface $SHARE_NETWORK_INTERFACE -j MASQUERADE
68 iptables --append FORWARD --in-interface $iface -j ACCEPT
69@@ -198,9 +205,9 @@ function exit_handler() {
70 # Wait until hostapd is correctly terminated before we continue
71 # doing anything
72 wait $HOSTAPD_PID
73- shutdown
74+ cleanup_on_exit
75 exit 0
76 }
77
78 wait $HOSTAPD_PID
79-shutdown
80\ No newline at end of file
81+cleanup_on_exit
82diff --git a/bin/helper.sh b/bin/helper.sh
83index df56715..5ad68f5 100644
84--- a/bin/helper.sh
85+++ b/bin/helper.sh
86@@ -50,3 +50,9 @@ generate_dnsmasq_config() {
87 EOF
88 } > $1
89 }
90+
91+is_nm_running() {
92+ nm_status=`$SNAP/bin/nmcli -t -f RUNNING general`
93+ [ "$nm_status" = "running" ] && return 1
94+ return 0
95+}
96diff --git a/cmd/client/cmd_config.go b/cmd/client/cmd_config.go
97index c801b47..8a82f4a 100644
98--- a/cmd/client/cmd_config.go
99+++ b/cmd/client/cmd_config.go
100@@ -20,6 +20,7 @@ import (
101 "encoding/json"
102 "fmt"
103 "os"
104+ "sort"
105 )
106
107 type setCommand struct{}
108@@ -44,21 +45,27 @@ func (cmd *setCommand) Execute(args []string) error {
109 type getCommand struct{}
110
111 func (cmd *getCommand) Execute(args []string) error {
112- if len(args) != 1 {
113- return fmt.Errorf("usage: %s get <key>\n", os.Args[0])
114- }
115-
116 response, err := sendHTTPRequest(getServiceConfigurationURI(), "GET", nil)
117 if err != nil {
118 return err
119 }
120
121- wantedKey := args[0]
122-
123- if val, ok := response.Result[wantedKey]; ok {
124- fmt.Fprintf(os.Stdout, "%s\n", val)
125+ if len(args) == 1 {
126+ wantedKey := args[0]
127+ if val, ok := response.Result[wantedKey]; ok {
128+ fmt.Fprintf(os.Stdout, "%s\n", val)
129+ } else {
130+ return fmt.Errorf("Config item '%s' does not exist", wantedKey)
131+ }
132 } else {
133- return fmt.Errorf("Config item '%s' does not exist", wantedKey)
134+ sortedKeys := make([]string, 0, len(response.Result))
135+ for key, _ := range response.Result {
136+ sortedKeys = append(sortedKeys, key)
137+ }
138+ sort.Strings(sortedKeys)
139+ for n := range sortedKeys {
140+ fmt.Fprintf(os.Stdout, "%s: %s\n", sortedKeys[n], response.Result[sortedKeys[n]])
141+ }
142 }
143
144 return nil
145diff --git a/cmd/client/cmd_wizard.go b/cmd/client/cmd_wizard.go
146index 0837135..5ba1357 100644
147--- a/cmd/client/cmd_wizard.go
148+++ b/cmd/client/cmd_wizard.go
149@@ -63,9 +63,19 @@ type wizardStep func(map[string]string, *bufio.Reader) error
150 var allSteps = [...]wizardStep{
151 // determine the WiFi interface
152 func(configuration map[string]string, reader *bufio.Reader) error {
153- fmt.Println("What is the name of the wireless interface you want to use?")
154 ifaces := findExistingInterfaces(true)
155- fmt.Print("Available interfaces are: " + strings.Join(ifaces, ", ") + ": ")
156+ if len(ifaces) == 0 {
157+ return fmt.Errorf("There are no valid wireless network interfaces available")
158+ } else if len(ifaces) == 1 {
159+ fmt.Println("Automatically selected only available wireless network interface " + ifaces[0])
160+ return nil
161+ }
162+ fmt.Print("Which wireless interface you want to use? ")
163+ ifacesVerb := "are"
164+ if len(ifaces) == 1 {
165+ ifacesVerb = "is"
166+ }
167+ fmt.Printf("Available %s %s:", ifacesVerb, strings.Join(ifaces, ", "))
168 iface := readUserInput(reader)
169 if re := regexp.MustCompile("^[[:alnum:]]+$"); !re.MatchString(iface) {
170 return fmt.Errorf("Invalid interface name '%s' given", iface)
171@@ -77,7 +87,7 @@ var allSteps = [...]wizardStep{
172
173 // Ask for WiFi ESSID
174 func(configuration map[string]string, reader *bufio.Reader) error {
175- fmt.Print("Insert the ESSID of your access point: ")
176+ fmt.Print("Which SSID you want to use for the access point: ")
177 iface := readUserInput(reader)
178 if len(iface) == 0 || len(iface) > 31 {
179 return fmt.Errorf("ESSID length must be between 1 and 31 characters")
180@@ -89,7 +99,7 @@ var allSteps = [...]wizardStep{
181
182 // Select WiFi encryption type
183 func(configuration map[string]string, reader *bufio.Reader) error {
184- fmt.Print("Do you want to protect your network with a WPA2 password? (y/n) ")
185+ fmt.Print("Do you want to protect your network with a WPA2 password instead of staying open for everyone? (y/n) ")
186 switch resp := strings.ToLower(readUserInput(reader)); resp {
187 case "y":
188 configuration["wifi.security"] = "wpa2"
189@@ -107,7 +117,7 @@ var allSteps = [...]wizardStep{
190 if configuration["wifi.security"] == "open" {
191 return nil
192 }
193- fmt.Print("Insert your WPA2 passphrase: ")
194+ fmt.Print("Please enter the WPA2 passphrase: ")
195 key := readUserInput(reader)
196 if len(key) < 8 || len(key) > 63 {
197 return fmt.Errorf("WPA2 passphrase must be between 8 and 63 characters")
198@@ -151,9 +161,10 @@ var allSteps = [...]wizardStep{
199 }
200
201 fmt.Printf("How many host do you want your DHCP pool to hold to? (1-%d) ", maxpoolsize)
202- inputhost, err := strconv.ParseUint(readUserInput(reader), 10, 8)
203+ input := readUserInput(reader)
204+ inputhost, err := strconv.ParseUint(input, 10, 8)
205 if err != nil {
206- return err
207+ return fmt.Errorf("Invalid answer: %s", input)
208 }
209 if byte(inputhost) > maxpoolsize {
210 return fmt.Errorf("%d is bigger than the maximum pool size %d", inputhost, maxpoolsize)
211@@ -172,11 +183,38 @@ var allSteps = [...]wizardStep{
212 return nil
213 },
214
215+ // Enable or disable connection sharing
216+ func(configuration map[string]string, reader *bufio.Reader) error {
217+ fmt.Print("Do you want to enable connection sharing? (y/n) ")
218+ switch resp := strings.ToLower(readUserInput(reader)); resp {
219+ case "y":
220+ configuration["share.disabled"] = "0"
221+ case "n":
222+ configuration["share.disabled"] = "1"
223+ default:
224+ return fmt.Errorf("Invalid answer: %s", resp)
225+ }
226+
227+ return nil
228+ },
229+
230 // Select the wired interface to share
231 func(configuration map[string]string, reader *bufio.Reader) error {
232- fmt.Println("What is the wired interface do you want to share?")
233+ if configuration["share.disabled"] == "1" {
234+ return nil
235+ }
236 ifaces := findExistingInterfaces(false)
237- fmt.Print("Available interfaces are: " + strings.Join(ifaces, ", ") + ": ")
238+ if len(ifaces) == 0 {
239+ fmt.Println("No network interface available which's connection can be shared. Disabling connection sharing.")
240+ configuration["share.disabled"] = "1"
241+ return nil
242+ }
243+ ifacesVerb := "are"
244+ if len(ifaces) == 1 {
245+ ifacesVerb = "is"
246+ }
247+ fmt.Println("Which network interface you want to use for connection sharing?")
248+ fmt.Printf("Available %s %s: ", ifacesVerb, strings.Join(ifaces, ", "))
249 iface := readUserInput(reader)
250 if re := regexp.MustCompile("^[[:alnum:]]+$"); !re.MatchString(iface) {
251 return fmt.Errorf("Invalid interface name '%s' given", iface)
252@@ -236,7 +274,6 @@ func (cmd *wizardCommand) Execute(args []string) error {
253 }
254 }
255
256- // fmt.Println("configuration: ", configuration)
257 return applyConfiguration(configuration)
258 }
259
260diff --git a/snapcraft.yaml b/snapcraft.yaml
261index c742c14..5252576 100644
262--- a/snapcraft.yaml
263+++ b/snapcraft.yaml
264@@ -23,6 +23,10 @@ apps:
265 command: bin/client config
266 plugs:
267 - network
268+ setup-wizard:
269+ command: bin/client wizard
270+ plugs:
271+ - network
272 management-service:
273 command: bin/service
274 daemon: simple

Subscribers

People subscribed via source and target branches

to all changes: