Skip to content

Commit

Permalink
Merge pull request #140 from hypriot/add-yaml-validate-for-mac
Browse files Browse the repository at this point in the history
Add yaml validate for mac
  • Loading branch information
StefanScherer committed Sep 23, 2018
2 parents a5ab4e5 + c37eaf7 commit a1aa0ee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
41 changes: 29 additions & 12 deletions flash
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ case "${OSTYPE}" in
true
}

validate_yaml() {
set +e
if _RET=$(ruby -e "require 'yaml';YAML.load_file('$1')" 2>&1); then
set -e
return 0
fi
echo "File $1 is not a valid YAML file!"
echo "$_RET" | grep -v "from "
set -e
false
}
# Try to identify the most likely device that the user will use to
# write an image to.
#
Expand Down Expand Up @@ -316,6 +327,11 @@ case "${OSTYPE}" in
fi
}

validate_yaml() {
## NO-OP in Linux
true
}

# Try to identify the most likely device that the user will use to
# write an image to.
#
Expand Down Expand Up @@ -470,28 +486,29 @@ fi

check_requirements

if [ ! -z "${USER_DATA}" ]; then
if [ -n "${USER_DATA}" ]; then
if [ ! -f "${USER_DATA}" ]; then
echo "Cloud-init file ${USER_DATA} not found!"
exit 10
fi
validate_yaml "${USER_DATA}"
fi

if [ ! -z "${META_DATA}" ]; then
if [ -n "${META_DATA}" ]; then
if [ ! -f "${META_DATA}" ]; then
echo "Cloud-init file ${META_DATA} not found!"
exit 10
fi
fi

if [ ! -z "${BOOT_CONF}" ]; then
if [ -n "${BOOT_CONF}" ]; then
if [ ! -f "${BOOT_CONF}" ]; then
echo "File ${BOOT_CONF} not found!"
exit 10
fi
fi

if [ ! -z "${CONFIG_FILE}" ]; then
if [ -n "${CONFIG_FILE}" ]; then
if [ ! -f "${CONFIG_FILE}" ]; then
echo "File ${CONFIG_FILE} not found!"
exit 10
Expand Down Expand Up @@ -647,29 +664,29 @@ mount_boot_disk "${disk}" "${boot}"

if [ -f "${boot}/device-init.yaml" ]; then
echo "Setting device-init"
if [ ! -z "${SD_HOSTNAME}" ]; then
if [ -n "${SD_HOSTNAME}" ]; then
echo " Set hostname=${SD_HOSTNAME}"
sed_i -e "s/.*hostname:.*\$/hostname: ${SD_HOSTNAME}/" "${boot}/device-init.yaml"
fi
if [ ! -z "${WIFI_SSID}" ]; then
if [ -n "${WIFI_SSID}" ]; then
echo " Set wlan0/ssid=${WIFI_SSID}"
sed_i -e "s/.*wlan0:.*\$/ wlan0:/" "${boot}/device-init.yaml"
sed_i -e "s/.*ssid:.*\$/ ssid: \"${WIFI_SSID}\"/" "${boot}/device-init.yaml"
fi
if [ ! -z "${WIFI_PASSWORD}" ]; then
if [ -n "${WIFI_PASSWORD}" ]; then
echo " Set wlan0/password=${WIFI_PASSWORD}"
sed_i -e "s/.*wlan0:.*\$/ wlan0:/" "${boot}/device-init.yaml"
sed_i -e "s/.*password:.*\$/ password: \"${WIFI_PASSWORD}\"/" "${boot}/device-init.yaml"
fi
if [ ! -z "${CLUSTERLAB}" ]; then
if [ -n "${CLUSTERLAB}" ]; then
echo " Set Cluster-Lab/run_on_boot=${CLUSTERLAB}"
sed_i -e "s/.*run_on_boot.*\$/ run_on_boot: \"${CLUSTERLAB}\"/" "${boot}/device-init.yaml"
fi
fi

# cloud-init
if [ -f "${boot}/user-data" ]; then
if [ ! -z "${SD_HOSTNAME}" ]; then
if [ -n "${SD_HOSTNAME}" ]; then
echo "Set hostname=${SD_HOSTNAME}"
sed_i -e "s/.*hostname:.*\$/hostname: ${SD_HOSTNAME}/" "${boot}/user-data"
fi
Expand All @@ -682,15 +699,15 @@ fi
# legacy: /boot/occidentalis.txt of old Hector release
if [ -f "${boot}/occidentalis.txt" ]; then
echo "Setting Occidentalis"
if [ ! -z "${SD_HOSTNAME}" ]; then
if [ -n "${SD_HOSTNAME}" ]; then
echo " Set hostname=${SD_HOSTNAME}"
sed_i -e "s/.*hostname.*=.*\$/hostname=${SD_HOSTNAME}/" "${boot}/occidentalis.txt"
fi
if [ ! -z "${WIFI_SSID}" ]; then
if [ -n "${WIFI_SSID}" ]; then
echo "Set wifi_ssid=${WIFI_SSID}"
sed_i -e "s/.*wifi_ssid.*=.*\$/wifi_ssid=${WIFI_SSID}/" "${boot}/occidentalis.txt"
fi
if [ ! -z "${WIFI_PASSWORD}" ]; then
if [ -n "${WIFI_PASSWORD}" ]; then
echo "Set wifi_password=${WIFI_PASSWORD}"
sed_i -e "s/.*wifi_password.*=.*\$/wifi_password=${WIFI_PASSWORD}/" "${boot}/occidentalis.txt"
fi
Expand Down
10 changes: 10 additions & 0 deletions test/cloud-init.bats
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load test_helper
export OS=$(uname -s)

setup() {
if [ ! -f cloud-init.img ]; then
Expand All @@ -17,6 +18,15 @@ teardown() {
unstub_diskutil
}

@test "cloud-init: flash aborts if YAML is not valid" {
if [ "${OS}" == "Darwin" ]; then
run ./flash -f -d $img -u test/resources/bad.yml cloud-init.img
assert_failure

assert_output_contains "is not a valid YAML file"
fi
}

@test "cloud-init: flash works" {
run ./flash -f -d $img cloud-init.img
assert_success
Expand Down
21 changes: 21 additions & 0 deletions test/resources/bad.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#cloud-config
hostname: good
manage_etc_hosts: true
users:
- name: other
primary-group: users
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users,docker,adm,dialout,audio,plugdev,netdev,video
ssh-import-id: None
lock_passwd: true
ssh-authorized-keys:
- ssh-rsa AAAAxxx testkey
write_files:
- content: |
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
path: /etc/docker/daemon.json
runcmd:
- [ systemctl, restart, avahi-daemon ]

0 comments on commit a1aa0ee

Please sign in to comment.