Skip to content

Commit

Permalink
Merge pull request #165 from BestWillInTheWorld/wifi-params-to-user-data
Browse files Browse the repository at this point in the history
#109: wifi params to user-data
  • Loading branch information
StefanScherer committed Apr 1, 2020
2 parents dc138cb + 0e21d07 commit 923a004
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
8 changes: 8 additions & 0 deletions flash
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,14 @@ fi
echo "Set hostname=${SD_HOSTNAME}"
sed_i -e "s/.*hostname:.*\$/hostname: ${SD_HOSTNAME}/" "${boot}/user-data"
fi
if [ -n "${WIFI_SSID}" ]; then
echo " Set ssid=${WIFI_SSID}"
sed_i -e "/^#/!s/.*ssid=.*\$/ ssid=\"${WIFI_SSID}\"/" "${boot}/user-data"
fi
if [ -n "${WIFI_PASSWORD}" ]; then
echo " Set psk=${WIFI_PASSWORD}"
sed_i -e "/^#/!s/.*psk=.*\$/ psk=\"${WIFI_PASSWORD}\"/" "${boot}/user-data"
fi

if [ ! -f "${boot}/meta-data" ]; then
echo "Creating empty meta-data"
Expand Down
60 changes: 60 additions & 0 deletions test/cloud-init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,66 @@ teardown() {
assert [ ! -s /tmp/boot/meta-data ]
}

@test "cloud-init: flash --ssid does NOT set ssid as it's COMMENTED in default image" {
run ./flash -f -d $img --ssid NEWSSID cloud-init.img
assert_success
assert_output_contains Finished.

mount_sd_boot $img /tmp/boot
run cat /tmp/boot/user-data
refute_output_contains 'ssid="NEWSSID"'
}

@test "cloud-init: flash --password does NOT set psk as it's COMMENTED in default image" {
run ./flash -f -d $img --password NEWPSK cloud-init.img
assert_success
assert_output_contains Finished.

mount_sd_boot $img /tmp/boot
run cat /tmp/boot/user-data
refute_output_contains 'psk="NEWPSK"'
}

@test "cloud-init: flash --ssid still sets ssid when user-data also specified" {
run ./flash -f -d $img -u test/resources/wifi-user-data.yml --ssid NEWSSID cloud-init.img
assert_success
assert_output_contains Finished.

mount_sd_boot $img /tmp/boot
run cat /tmp/boot/user-data
assert_output_contains 'ssid="NEWSSID"'
}

@test "cloud-init: flash --password still sets psk when user-data also specified" {
run ./flash -f -d $img -u test/resources/wifi-user-data.yml --password NEWPSK cloud-init.img
assert_success
assert_output_contains Finished.

mount_sd_boot $img /tmp/boot
run cat /tmp/boot/user-data
assert_output_contains 'psk="NEWPSK"'
}

@test "cloud-init: flash --ssid does NOT set ssid if COMMENTED when user-data also specified" {
run ./flash -f -d $img -u test/resources/wifi-commented-user-data.yml --ssid NEWSSID cloud-init.img
assert_success
assert_output_contains Finished.

mount_sd_boot $img /tmp/boot
run cat /tmp/boot/user-data
refute_output_contains 'ssid="NEWSSID"'
}

@test "cloud-init: flash --password does NOT set psk if COMMENTED when user-data also specified" {
run ./flash -f -d $img -u test/resources/wifi-commented-user-data.yml --password NEWPSK cloud-init.img
assert_success
assert_output_contains Finished.

mount_sd_boot $img /tmp/boot
run cat /tmp/boot/user-data
refute_output_contains 'psk="NEWPSK"'
}

@test "cloud-init: flash --config does not replace user-data" {
run ./flash -f -d $img --config test/resources/good.yml cloud-init.img
assert_success
Expand Down
76 changes: 76 additions & 0 deletions test/resources/wifi-commented-user-data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#cloud-config
# vim: syntax=yaml
#

# The current version of cloud-init in the Hypriot rpi-64 is 0.7.9
# When dealing with cloud-init, it is SUPER important to know the version
# I have wasted many hours creating servers to find out the module I was trying to use wasn't in the cloud-init version I had
# Documentation: http://cloudinit.readthedocs.io/en/0.7.9/index.html

# Set your hostname here, the manage_etc_hosts will update the hosts file entries as well
hostname: pi3
manage_etc_hosts: true

# You could modify this for your own user information
users:
- name: pirate
gecos: "Hypriot Pirate"
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
groups: users,docker,video
plain_text_passwd: hypriot
lock_passwd: false
ssh_pwauth: true
chpasswd: { expire: false }

# # Set the locale of the system
# locale: "en_US.UTF-8"

# # Set the timezone
# # Value of 'timezone' must exist in /usr/share/zoneinfo
# timezone: "America/Los_Angeles"

# # Update apt packages on first boot
# package_update: true
# package_upgrade: true
# package_reboot_if_required: true
package_upgrade: false

# # Install any additional apt packages you need here
# packages:
# - ntp

# # WiFi connect to HotSpot
# To make wifi work with RPi3 and RPi0
# you also have to set "enable_uart=0" in config.txt
# See no-uart-config.txt for an example.
#
# # - use `wpa_passphrase SSID PASSWORD` to encrypt the psk
# write_files:
# - content: |
# allow-hotplug wlan0
# iface wlan0 inet dhcp
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# iface default inet dhcp
# path: /etc/network/interfaces.d/wlan0
# - content: |
# country=YourContryCode
# ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
# update_config=1
# network={
# ssid="YourSSID"
# psk="YourSecretPreSharedKey"
# proto=RSN
# key_mgmt=WPA-PSK
# pairwise=CCMP
# auth_alg=OPEN
# }
# path: /etc/wpa_supplicant/wpa_supplicant.conf

# These commands will be ran once on first boot only
runcmd:
# Pickup the hostname changes
- 'systemctl restart avahi-daemon'

# Activate WiFi interface
- 'ifup wlan0'
76 changes: 76 additions & 0 deletions test/resources/wifi-user-data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#cloud-config
# vim: syntax=yaml
#

# The current version of cloud-init in the Hypriot rpi-64 is 0.7.9
# When dealing with cloud-init, it is SUPER important to know the version
# I have wasted many hours creating servers to find out the module I was trying to use wasn't in the cloud-init version I had
# Documentation: http://cloudinit.readthedocs.io/en/0.7.9/index.html

# Set your hostname here, the manage_etc_hosts will update the hosts file entries as well
hostname: pi3
manage_etc_hosts: true

# You could modify this for your own user information
users:
- name: pirate
gecos: "Hypriot Pirate"
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
groups: users,docker,video
plain_text_passwd: hypriot
lock_passwd: false
ssh_pwauth: true
chpasswd: { expire: false }

# # Set the locale of the system
# locale: "en_US.UTF-8"

# # Set the timezone
# # Value of 'timezone' must exist in /usr/share/zoneinfo
# timezone: "America/Los_Angeles"

# # Update apt packages on first boot
# package_update: true
# package_upgrade: true
# package_reboot_if_required: true
package_upgrade: false

# # Install any additional apt packages you need here
# packages:
# - ntp

# # WiFi connect to HotSpot
# To make wifi work with RPi3 and RPi0
# you also have to set "enable_uart=0" in config.txt
# See no-uart-config.txt for an example.
#
# # - use `wpa_passphrase SSID PASSWORD` to encrypt the psk
write_files:
- content: |
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
path: /etc/network/interfaces.d/wlan0
- content: |
country=YourContryCode
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YourSSID"
psk="YourSecretPreSharedKey"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}
path: /etc/wpa_supplicant/wpa_supplicant.conf
# These commands will be ran once on first boot only
runcmd:
# Pickup the hostname changes
- 'systemctl restart avahi-daemon'

# Activate WiFi interface
- 'ifup wlan0'

0 comments on commit 923a004

Please sign in to comment.