Skip to content

Commit cfaf603

Browse files
committed
Allow to download files related to cloud-init via http(s)
Now URLs can be specified instead of local files for `--bootconf`, `--config`, `--userdata`, `--metadata` and `--file`.
1 parent 01b4571 commit cfaf603

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

flash

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,23 @@ fi
494494

495495
check_requirements
496496

497+
http_download() {
498+
local uri="${1}"
499+
local tmp="/tmp/${2}"
500+
501+
if beginswith http:// "${uri}" || beginswith https:// "${uri}"; then
502+
command -v curl 2>/dev/null || error "Error: curl not found. Aborting" 1
503+
echo "Downloading ${uri} ..."
504+
curl -L --fail -o "${tmp}" "${uri}"
505+
_RET="${tmp}"
506+
else
507+
_RET="${uri}"
508+
fi
509+
}
510+
497511
if [ -n "${USER_DATA}" ]; then
512+
http_download "$USER_DATA" "flash.user_data.yaml"
513+
USER_DATA="$_RET"
498514
if [ ! -f "${USER_DATA}" ]; then
499515
echo "Cloud-init file ${USER_DATA} not found!"
500516
exit 10
@@ -503,27 +519,35 @@ if [ -n "${USER_DATA}" ]; then
503519
fi
504520

505521
if [ -n "${META_DATA}" ]; then
522+
http_download "$META_DATA" "flash.meta_data.yaml"
523+
META_DATA="$_RET"
506524
if [ ! -f "${META_DATA}" ]; then
507525
echo "Cloud-init file ${META_DATA} not found!"
508526
exit 10
509527
fi
510528
fi
511529

512530
if [ -n "${FILE}" ]; then
531+
http_download "$FILE" "flash.file"
532+
FILE="$_RET"
513533
if [ ! -f "${FILE}" ]; then
514534
echo "File ${FILE} not found!"
515535
exit 10
516536
fi
517537
fi
518538

519539
if [ -n "${BOOT_CONF}" ]; then
540+
http_download "$BOOT_CONF" "flash.config.txt"
541+
BOOT_CONF="$_RET"
520542
if [ ! -f "${BOOT_CONF}" ]; then
521543
echo "File ${BOOT_CONF} not found!"
522544
exit 10
523545
fi
524546
fi
525547

526548
if [ -n "${CONFIG_FILE}" ]; then
549+
http_download "$CONFIG_FILE" "flash.device_init.yaml"
550+
CONFIG_FILE="$_RET"
527551
if [ ! -f "${CONFIG_FILE}" ]; then
528552
echo "File ${CONFIG_FILE} not found!"
529553
exit 10

test/cloud-init.bats

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,42 @@ teardown() {
184184
run cat /tmp/boot/config.txt
185185
assert_output_contains "enable_uart=0"
186186
}
187+
188+
@test "cloud-init: flash --userdata can be downloaded" {
189+
run ./flash -f -d $img --userdata https://raw.githubusercontent.com/hypriot/flash/master/test/resources/good.yml cloud-init.img
190+
assert_success
191+
assert_output_contains Downloading
192+
assert_output_contains Finished.
193+
194+
mount_sd_boot $img /tmp/boot
195+
run cat /tmp/boot/user-data
196+
assert_output_contains "hostname: good"
197+
assert_output_contains "name: other"
198+
assert_output_contains "ssh-authorized-keys:"
199+
200+
assert [ -e "/tmp/boot/meta-data" ]
201+
assert [ ! -s "/tmp/boot/meta-data" ]
202+
}
203+
204+
@test "cloud-init: flash --metadata can be downloaded" {
205+
run ./flash -f -d $img --userdata https://raw.githubusercontent.com/hypriot/flash/master/test/resources/meta.yml cloud-init.img
206+
assert_success
207+
assert_output_contains Downloading
208+
assert_output_contains Finished.
209+
210+
mount_sd_boot $img /tmp/boot
211+
run cat /tmp/boot/user-data
212+
assert_output_contains "hostname: good"
213+
assert_output_contains "name: other"
214+
assert_output_contains "ssh-authorized-keys:"
215+
216+
run cat /tmp/boot/meta-data
217+
assert_output_contains "instance-id: iid-local01"
218+
}
219+
220+
@test "cloud-init: flash --userdata aborts on 'not found' (404)" {
221+
run ./flash -f -d $img --userdata https://raw.githubusercontent.com/hypriot/flash/master/test/resources/foo.bar cloud-init.img
222+
assert_failure
223+
224+
assert_output_contains "The requested URL returned error: 404 Not Found"
225+
}

0 commit comments

Comments
 (0)