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

Commit

Permalink
Separate ssh key script from cloudinit script
Browse files Browse the repository at this point in the history
  • Loading branch information
higebu committed Dec 30, 2016
1 parent d2d2a10 commit b65599e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 43 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ sysconfdir = /opt/vyatta/etc/vyos-cloudinit

initd_SCRIPTS =
initd_SCRIPTS += etc/init.d/vyos-cloudinit
initd_SCRIPTS += etc/init.d/vyos-ssh-key

sbin_SCRIPTS =
sbin_SCRIPTS += scripts/vyos-cloudinit
sbin_SCRIPTS += scripts/vyos-ssh-key

sysconf_DATA =
sysconf_DATA += sysconf/vyos-cloudinit/vyos-cloudinit.conf
Expand Down
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.3.0) unstable; urgency=low

* Separate ssh key script from cloudinit script

-- Yuya Kusakabe <[email protected]> Fri, 30 Dec 2016 13:11:05 +0900

vyos-cloudinit (0.2.1) unstable; urgency=low

* Remove command option from scripts/vyos-cloudinit
Expand Down
19 changes: 19 additions & 0 deletions etc/init.d/vyos-ssh-key
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
### BEGIN INIT INFO
# Provides: vyos-ssh-key
# Required-Start: vyos-cloudinit
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Load user SSH key.
# Description: Load user SSH key.
### END INIT INFO

. /lib/lsb/init-functions

: ${vyatta_env:=/etc/default/vyatta}
source $vyatta_env

log_action_begin_msg "Starting vyos-ssh-key"
${vyatta_sbindir}/vyos-ssh-key
log_action_end_msg $?
52 changes: 9 additions & 43 deletions scripts/vyos-cloudinit
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
source $vyatta_env

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

CONF_DIR="${vyatta_sysconfdir}/vyos-cloudinit"
Expand All @@ -23,48 +21,22 @@ fi
# 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"
if [[ -z "${USER_DATA}" ]]; then
echo "user-data is not specified"
exit 0
fi

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

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

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

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}"
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 ${ssh_key_code}
fi

user_data_code=0
if [[ "${USER_DATA}" == "http"* ]]; then
tmpdata=$(mktemp /tmp/XXXXXX-user-data)
/usr/bin/curl -m 3 -sf "${USER_DATA}" -o ${tmpdata}
if [ $? -ne 0 ]; then
if [[ $? != 0 ]]; then
echo "could not retrieve user-data from ${USER_DATA}"
user_data_code=1
$_exit 1
fi
USER_DATA="${tmpdata}"
fi
Expand All @@ -79,29 +51,23 @@ if [[ "${header}" == "#vyos-config" ]]; then
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}
user_data_code=$?
if [ ${user_data_code} -eq 0 ]; then
if [[ $? == 0 ]]; then
echo "merge failed"
echo "${result}"
user_data_code=1
$_exit 1
else
commit
save
user_data_code=0
fi
elif [[ "${header}" == "#!/bin/vbash" ]]; then
echo "running user script..."
chmod +x ${USER_DATA}
result=$(${USER_DATA})
user_data_code=$?
if [[ ${user_data_code} -ne 0 ]]; then
if [[ $? != 0 ]]; then
echo "user script failed"
echo "${result}"
$_exit 1
fi
fi

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

$_exit $code
rm -f ${tmpdata} ${tmpconf} ${output}
52 changes: 52 additions & 0 deletions scripts/vyos-ssh-key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/vbash

: ${vyatta_env:=/etc/default/vyatta}
source $vyatta_env

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

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

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

if [[ -z "${SSH_KEY}" ]]; then
echo "ssh-key not specified"
exit 0
fi

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

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

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

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}"
$_exit 1
else
load_key
$_exit $?
fi
elif [[ -n "${SSH_KEY}" ]]; then
load_key
$_exit $?
fi
3 changes: 3 additions & 0 deletions templates-cfg/service/cloudinit/node.def
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
priority: 400
help: Cloud init
create: sudo /usr/sbin/update-rc.d vyos-cloudinit defaults
sudo /usr/sbin/update-rc.d vyos-ssh-key defaults

delete: sudo /usr/sbin/update-rc.d -f vyos-cloudinit remove
sudo /usr/sbin/update-rc.d -f vyos-ssh-key remove

0 comments on commit b65599e

Please sign in to comment.