You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
addon-context-linux/base/usr/sbin/one-contextd

107 lines
2.9 KiB

#!/bin/bash
#
# -------------------------------------------------------------------------- #
# Copyright 2010-2016, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
LOCK_FILE="/var/run/one-context.lock"
CONTEXT_FILE="/tmp/context.sh"
CONTEXT_NEW="${CONTEXT_FILE}.new"
END_CONTEXT="/tmp/context.end"
function export_rc_vars
{
if [ -f $1 ] ; then
ONE_VARS=`cat $1 | egrep -e '^[a-zA-Z\-\_0-9]*=' | sed 's/=.*$//'`
. $1
for v in $ONE_VARS; do
export $v
done
fi
}
function execute_scripts {
SCRIPTS_DIR="/etc/one-context.d"
for script in $SCRIPTS_DIR/*; do
"$script" "$1"
done
}
function get_new_context {
CONTEXT_DEV=`blkid -l -t LABEL="CONTEXT" -o device`
if [ -e "$CONTEXT_DEV" ]; then
mount -t iso9660 -L CONTEXT -o ro /mnt
if [ -f /mnt/context.sh ]; then
cp /mnt/context.sh ${CONTEXT_NEW}
fi
echo "umount /mnt" > ${END_CONTEXT}
elif curl -o ${CONTEXT_NEW} http://169.254.169.254/latest/user-data ; then
echo -n ""
elif type vmtoolsd ; then
vmtoolsd --cmd 'info-get guestinfo.opennebula.context' | \
openssl base64 -d > ${CONTEXT_NEW}
fi
}
function check_context {
if [ -s "${CONTEXT_NEW}" ]; then
diff ${CONTEXT_FILE} ${CONTEXT_NEW} >/dev/null 2>&1 && return 1
return 0
else
return 1
fi
}
function run_context {
cp ${CONTEXT_NEW} ${CONTEXT_FILE}
export_rc_vars ${CONTEXT_FILE}
execute_scripts "$1"
}
function end_context {
if [ -e "${END_CONTEXT}" ]; then
sh "${END_CONTEXT}"
rm "${END_CONTEXT}"
fi
[ -e "${CONTEXT_NEW}" ] && rm "${CONTEXT_NEW}"
}
function adquire_lock {
while [ -e "${LOCK_FILE}" ]; do
sleep 1
done
touch "${LOCK_FILE}"
}
function release_lock {
rm -f "${LOCK_FILE}"
}
COMMAND="$1"
adquire_lock
get_new_context
check_context && run_context "$COMMAND"
end_context
release_lock