Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Remove command option from scripts/vyos-cloudinit
Browse files Browse the repository at this point in the history
  • Loading branch information
higebu committed Dec 20, 2016
1 parent 005dac4 commit d2d2a10
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 83 deletions.
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
vyos-cloudinit (0.2.1) unstable; urgency=low

* Remove command option from scripts/vyos-cloudinit

-- Yuya Kusakabe <[email protected]> Tue, 20 Dec 2016 23:26:16 +0900

vyos-cloudinit (0.2.0) unstable; urgency=low

* Initial support for OpenStack .
Expand Down
38 changes: 2 additions & 36 deletions etc/init.d/vyos-cloudinit
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,6 @@
: ${vyatta_env:=/etc/default/vyatta}
source $vyatta_env

ENVIRONMENT=""
SSH_USER="vyos"
SSH_KEY=""
USER_DATA=""

conf_dir="${vyatta_sysconfdir}/vyos-cloudinit"

. ${conf_dir}/vyos-cloudinit.conf

log_action_begin_msg "Starting vyos-cloudinit"
if [ -n "${ENVIRONMENT}" ]; then
env_conf=${conf_dir}/${ENVIRONMENT}.conf
if [ -f ${env_conf} ]; then
. ${env_conf}
else
echo "${ENVIRONMENT} is not supported"
fi
fi

# override with user specified parameters
. ${conf_dir}/vyos-cloudinit.conf

code=0
if [[ -z "${SSH_KEY}" && -z "${USER_DATA}" ]]; then
log_action_msg "both ssh-key and user-data are not specified"
elif [[ -n "${SSH_KEY}" && -z "${USER_DATA}" ]]; then
${vyatta_sbindir}/vyos-cloudinit -u "${SSH_USER}" -s "${SSH_KEY}"
code=$?
elif [[ -z "${SSH_KEY}" && -n "${USER_DATA}" ]]; then
${vyatta_sbindir}/vyos-cloudinit -u "${SSH_USER}" -d "${USER_DATA}"
code=$?
elif [[ -n "${SSH_KEY}" && -n "${USER_DATA}" ]]; then
${vyatta_sbindir}/vyos-cloudinit -u "${SSH_USER}" -s "${SSH_KEY}" -d "${USER_DATA}"
code=$?
fi

log_action_end_msg $code
${vyatta_sbindir}/vyos-cloudinit
log_action_end_msg $?
88 changes: 41 additions & 47 deletions scripts/vyos-cloudinit
Original file line number Diff line number Diff line change
@@ -1,113 +1,107 @@
#!/bin/vbash

usage() {
cat <<EOF
Usage: $0 [options...]
-s ssh key path
-u ssh user
-d user data path
-h show this message
EOF
exit 1
}
: ${vyatta_env:=/etc/default/vyatta}
source $vyatta_env

ENVIRONMENT=""
SSH_KEY=""
SSH_USER="vyos"
USER_DATA=""
while getopts e:s:u:d:h: OPT
do
case $OPT in
s) SSH_KEY=$OPTARG;;
u) SSH_USER=$OPTARG;;
d) USER_DATA=$OPTARG;;
h) usage;;
\?) usage;;
esac
done

if [[ -z "${SSH_KEY}" && -z "${USER_DATA}" ]]; then
usage
CONF_DIR="${vyatta_sysconfdir}/vyos-cloudinit"
. ${CONF_DIR}/vyos-cloudinit.conf

if [ -n "${ENVIRONMENT}" ]; then
env_conf=${CONF_DIR}/${ENVIRONMENT}.conf
if [ -f ${env_conf} ]; then
. ${env_conf}
else
echo "${ENVIRONMENT} is not supported"
fi
fi

: ${vyatta_env:=/etc/default/vyatta}
source $vyatta_env
# override with user specified parameters
. ${CONF_DIR}/vyos-cloudinit.conf

if [[ -z "${SSH_KEY}" && -z "${USER_DATA}" ]]; then
echo "both ssh-key and user-data are not specified"
exit 0
fi

LOAD_KEY="${vyatta_sbindir}/vyatta-load-user-key.pl"
LOAD_CONFIG="${vyatta_sbindir}/vyatta-load-config.pl"

code=0

_exit=exit
source ${vyatta_sysconfdir}/functions/script-template

function load_key() {
echo "loading ssh key..."
${LOAD_KEY} ${SSH_USER} ${SSH_KEY}
code=$?
}

ssh_key_code=0
if [[ -n "${SSH_KEY}" && "${SSH_KEY}" == "http"* ]]; then
/usr/bin/curl -m 3 -sf "${SSH_KEY}"
if [ $? -ne 0 ]; then
echo "could not retrieve ssh key from ${SSH_KEY}"
code=1
ssh_key_code=1
else
load_key
ssh_key_code=$?
fi
elif [[ -n "${SSH_KEY}" ]]; then
load_key
ssh_key_code=$?
fi

if [[ -z "${USER_DATA}" ]]; then
$_exit $code
$_exit ${ssh_key_code}
fi

tmpfiles=""
user_data_code=0
if [[ "${USER_DATA}" == "http"* ]]; then
tmp=$(mktemp /tmp/XXXXXX-user-data)
tmpfiles=$tmp
/usr/bin/curl -m 3 -sf "${USER_DATA}" -o ${tmp}
tmpdata=$(mktemp /tmp/XXXXXX-user-data)
/usr/bin/curl -m 3 -sf "${USER_DATA}" -o ${tmpdata}
if [ $? -ne 0 ]; then
echo "could not retrieve user-data from ${USER_DATA}"
code=1
user_data_code=1
fi
USER_DATA="${tmp}"
USER_DATA="${tmpdata}"
fi

header=$(head -n1 ${USER_DATA})

if [[ "${header}" == "#vyos-config" ]]; then
echo "merging VyOS config..."
tmp=$(mktemp /tmp/XXXXXX-config)
tmpfiles="${tmpfiles} ${tmp}"
tmpconf=$(mktemp /tmp/XXXXXX-config)
output=$(mktemp /tmp/XXXXXX-output)
tail -n +2 ${USER_DATA} > ${tmp}
echo Y | python -c 'import pty, sys; pty.spawn(sys.argv[1:])' ${LOAD_CONFIG} ${tmpfile} --merge > ${output}
tail -n +2 ${USER_DATA} > ${tmpconf}
echo Y | python -c 'import pty, sys; pty.spawn(sys.argv[1:])' ${LOAD_CONFIG} ${tmpconf} --merge > ${output}
result=$(cat ${output} | tail -n +5 | head -n -1)
grep -q fail ${output}
code=$?
if [ ${code} -eq 0 ]; then
user_data_code=$?
if [ ${user_data_code} -eq 0 ]; then
echo "merge failed"
echo "${result}"
code=1
user_data_code=1
else
commit
save
code=0
user_data_code=0
fi
elif [[ "${header}" == "#!/bin/vbash" ]]; then
echo "running user script..."
chmod +x ${USER_DATA}
result=$(${USER_DATA})
code=$?
if [[ ${code} -ne 0 ]]; then
user_data_code=$?
if [[ ${user_data_code} -ne 0 ]]; then
echo "user script failed"
echo "${result}"
fi
fi

if [[ ${code} -eq 0 ]]; then
rm -f $tmpfiles $output
if [[ ${user_data_code} -eq 0 ]]; then
rm -f ${tmpdata} ${tmpconf} ${output}
fi

$_exit $code

0 comments on commit d2d2a10

Please sign in to comment.