1#!/bin/sh
2
3# KEYWORD: firstboot
4# PROVIDE: ec2_configinit
5# REQUIRE: NETWORKING
6# BEFORE: SERVERS ec2_fetchkey firstboot_freebsd_update firstboot_pkgs
7
8# Define ec2_configinit_enable=YES in /etc/rc.conf to enable automatic
9# system configuration from EC2 user-data when the system first boots.
10: ${ec2_configinit_enable=NO}
11
12. /etc/rc.subr
13
14name="ec2_configinit"
15rcvar=ec2_configinit_enable
16start_cmd="ec2_configinit_run"
17stop_cmd=":"
18
19CONFIGURL="http://169.254.169.254/latest/user-data"
20
21ec2_configinit_run()
22{
23	# Download to a temporary location.
24	echo -n "Fetching EC2 user-data"
25	CONFFILE=`mktemp "${TMPDIR:-/tmp}/configinit.XXXXXX"`
26	fetch -o ${CONFFILE} ${CONFIGURL} 2>/dev/null
27
28	# If we succeeded, process it; otherwise report failure.
29	if [ $? = 0 ]; then
30		# Process the user-data.
31		echo .
32		echo -n "Processing EC2 user-data"
33		/usr/local/sbin/configinit $CONFFILE
34		echo .
35	else
36		echo " failed."
37	fi
38
39	# Whether we suceeded or not, delete the temporary file.
40	rm $CONFFILE
41
42	# Signal /etc/rc to reload rc.conf in case it changed.
43	kill -ALRM $$
44}
45
46load_rc_config $name
47run_rc_command "$1"
48