From 93cb488ffdd66f3360a1fe1235807d17be51af6d Mon Sep 17 00:00:00 2001 From: Vlastimil Holer Date: Tue, 7 Feb 2017 16:05:16 +0100 Subject: [PATCH] Restructure, logging, initial systemd support, pre/post network scripts --- .gitignore | 1 + PKGBUILD | 6 +- README.md | 171 ++++++++----- base/usr/sbin/one-contextd | 120 --------- base_arch/etc/one-context.d/15-hostname | 112 -------- generate-all.sh | 10 +- generate.sh | 110 +++++--- postinstall.ec2 | 2 +- postinstall.one | 26 +- ...ne-context-reconfigure => preuninstall.ec2 | 20 +- preuninstall.one | 24 ++ .../99-opennebula-settings.cfg##deb.one | 0 .../etc/init.d/vmcontext##deb | 0 .../etc/init.d/vmcontext##rpm.sysv.ec2 | 0 .../etc/init.d/vmcontext##rpm.sysv.one | 0 .../etc/one-context.d/loc-05-grow-rootfs##one | 0 .../etc/one-context.d/loc-10-network##arch | 0 .../etc/one-context.d/loc-10-network##deb.one | 2 + .../etc/one-context.d/loc-10-network##rpm.one | 6 +- .../etc/one-context.d/loc-10-network-pci##one | 0 .../etc/one-context.d/loc-11-dns##one | 0 .../etc/one-context.d/loc-14-mount-swap##one | 0 .../etc/one-context.d/loc-16-gen-env | 8 +- .../loc-20-set-username-password | 0 .../etc/one-context.d/loc-22-ssh_public_key | 0 .../etc/one-context.d/loc-23-selinux-ssh##rpm | 0 .../one-context.d/net-11-fix-loopback##one | 0 .../one-context.d/net-12-ssh_host_key##deb | 0 .../etc/one-context.d/net-15-hostname##one | 0 .../etc/one-context.d/net-97-start-script | 7 +- .../etc/one-context.d/net-98-execute-scripts | 5 +- .../etc/one-context.d/net-99-report-ready | 0 .../rules.d/75-cd-aliases-generator.rules | 0 .../rules.d/75-persistent-net-generator.rules | 0 .../rules.d/65-context.rules##one.systemd | 7 + .../udev/rules.d/65-context.rules##one.sysv | 0 {base => src}/usr/bin/onegate | 2 +- {base => src}/usr/bin/onegate.rb | 0 ...one-context-local.service##rpm.systemd.one | 14 + ...configure-delayed.service##rpm.systemd.one | 11 + ...ntext-reconfigure.service##rpm.systemd.one | 13 + .../systemd/system/one-context.service##arch | 2 +- .../one-context.service##rpm.systemd.one | 13 + src/usr/sbin/one-context-reconfigure##one | 31 +++ src/usr/sbin/one-contextd | 242 ++++++++++++++++++ targets.sh | 78 ++++++ 46 files changed, 657 insertions(+), 386 deletions(-) delete mode 100755 base/usr/sbin/one-contextd delete mode 100755 base_arch/etc/one-context.d/15-hostname rename base.one/usr/sbin/one-context-reconfigure => preuninstall.ec2 (79%) create mode 100755 preuninstall.one rename base_deb.one/etc/default/grub.d/99-opennebula-settings.cfg => src/etc/default/grub.d/99-opennebula-settings.cfg##deb.one (100%) rename base_deb/etc/init.d/vmcontext => src/etc/init.d/vmcontext##deb (100%) rename base_rpm.ec2/etc/init.d/vmcontext => src/etc/init.d/vmcontext##rpm.sysv.ec2 (100%) rename base_rpm.one/etc/init.d/vmcontext => src/etc/init.d/vmcontext##rpm.sysv.one (100%) rename base.one/etc/one-context.d/05-grow-rootfs => src/etc/one-context.d/loc-05-grow-rootfs##one (100%) rename base_arch/etc/one-context.d/10-network => src/etc/one-context.d/loc-10-network##arch (100%) rename base_deb.one/etc/one-context.d/10-network => src/etc/one-context.d/loc-10-network##deb.one (99%) rename base_rpm.one/etc/one-context.d/10-network => src/etc/one-context.d/loc-10-network##rpm.one (97%) rename base.one/etc/one-context.d/10-network-pci => src/etc/one-context.d/loc-10-network-pci##one (100%) rename base.one/etc/one-context.d/11-dns => src/etc/one-context.d/loc-11-dns##one (100%) rename base.one/etc/one-context.d/14-mount-swap => src/etc/one-context.d/loc-14-mount-swap##one (100%) rename base/etc/one-context.d/16-gen-env => src/etc/one-context.d/loc-16-gen-env (78%) rename base/etc/one-context.d/20-set-username-password => src/etc/one-context.d/loc-20-set-username-password (100%) rename base/etc/one-context.d/22-ssh_public_key => src/etc/one-context.d/loc-22-ssh_public_key (100%) rename base_rpm/etc/one-context.d/23-selinux-ssh => src/etc/one-context.d/loc-23-selinux-ssh##rpm (100%) rename base.one/etc/one-context.d/11-fix-loopback => src/etc/one-context.d/net-11-fix-loopback##one (100%) rename base_deb/etc/one-context.d/12-ssh_host_key => src/etc/one-context.d/net-12-ssh_host_key##deb (100%) rename base.one/etc/one-context.d/15-hostname => src/etc/one-context.d/net-15-hostname##one (100%) rename base/etc/one-context.d/97-start-script => src/etc/one-context.d/net-97-start-script (75%) rename base/etc/one-context.d/98-execute-scripts => src/etc/one-context.d/net-98-execute-scripts (72%) rename base/etc/one-context.d/99-report-ready => src/etc/one-context.d/net-99-report-ready (100%) rename {base => src}/etc/udev/rules.d/75-cd-aliases-generator.rules (100%) rename {base => src}/etc/udev/rules.d/75-persistent-net-generator.rules (100%) create mode 100644 src/lib/udev/rules.d/65-context.rules##one.systemd rename base.one/lib/udev/rules.d/00-context.rules => src/lib/udev/rules.d/65-context.rules##one.sysv (100%) rename {base => src}/usr/bin/onegate (78%) rename {base => src}/usr/bin/onegate.rb (100%) create mode 100644 src/usr/lib/systemd/system/one-context-local.service##rpm.systemd.one create mode 100644 src/usr/lib/systemd/system/one-context-reconfigure-delayed.service##rpm.systemd.one create mode 100644 src/usr/lib/systemd/system/one-context-reconfigure.service##rpm.systemd.one rename base_arch/usr/lib/systemd/system/one-context.service => src/usr/lib/systemd/system/one-context.service##arch (96%) create mode 100644 src/usr/lib/systemd/system/one-context.service##rpm.systemd.one create mode 100755 src/usr/sbin/one-context-reconfigure##one create mode 100755 src/usr/sbin/one-contextd create mode 100644 targets.sh diff --git a/.gitignore b/.gitignore index f7288a3..2db707d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +out/ *.rpm *.deb diff --git a/PKGBUILD b/PKGBUILD index 2dc3bfb..74e28a8 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -13,8 +13,8 @@ install=one-context.install md5sums=('') package() { - cp -rT ${srcdir}/addon-context-linux-${pkgver}/base_arch ${pkgdir} - cp -rT ${srcdir}/addon-context-linux-${pkgver}/base ${pkgdir} + cd ${srcdir} + TARGET=arch OUT=${pkgdir} ./generate.sh cp -rT ${pkgdir}/usr/sbin ${pkgdir}/usr/bin rm -rf ${pkgdir}/usr/sbin -} \ No newline at end of file +} diff --git a/README.md b/README.md index 05eff9a..52f6b65 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Linux VM Contextualization -## Description - -These are the source of the contextualization packages used by VM to be configured with the information generated by OpenNebula. +These are the source of the contextualization packages used by VM to be +configured with the information generated by OpenNebula. This add-on is +compatible with OpenNebula >= 4.6. ## Get packages @@ -11,7 +11,9 @@ Latest versions can be downloaded from the ## Development -To contribute bug patches or new features, you can use the github Pull Request model. It is assumed that code and documentation are contributed under the Apache License 2.0. +To contribute bug patches or new features, you can use the github Pull Request +model. It is assumed that code and documentation are contributed under +the Apache License 2.0. More info: * [How to Contribute](http://opennebula.org/addons/contribute/) @@ -19,91 +21,126 @@ More info: * Development: [OpenNebula developers forum](https://forum.opennebula.org/c/development) * Issues Tracking: Github issues (https://github.com/OpenNebula/addon-context-linux/issues) -## Authors +### Repository structure -* Leader: Javier Fontan (jfontan@opennebula.org) +All code is located under `src/` and structure follows the installation +directory structure. Files for different environments/targets are picked +by the tag, tags are part of the filename separated from the installation +name by 2 hashes (`##`). Tags are dot-separated. -## Compatibility +Examples: -This add-on is compatible with OpenNebula >= 4.6. +* `script` - non-tagged file for all targets +* `script##systemd` - file tagged with **systemd** +* `script##systemd.rpm` - file tagged with **systemd** and **rpm** -## Requirements +### Contextualization scripts - * Ruby >= 1.8.7 - * gem fpm - * dpkg utils for deb package creation - * rpm utils for rpm package creation +Contextualization scripts, which are executed on every boot and during +the reconfiguration, are located in `src/etc/one-context.d/`. Scripts are +divided into following 2 parts: + +* local - pre-networking, prefixed with `loc-` +* post-networking, prefixed with `net-` + +All other scripts, which are not prefixed with `loc-` or `net-`, are +executed as a first during the post-networking contextualization stage. -On Ubuntu/Debian you can install the package `rpm` and you will be able to generate both rpm and deb packages. +## Build own package -## Use +Package contains following parts: -### Package Description +* main control scripts (`/usr/sbin/one-context*`) +* contextualization scripts (`/etc/one-context.d/*`) +* init scripts to start the contextualization +* OneGate scripts (`/usr/bin/one-gate*`) +* udev rules to trigger reconfiguration on NIC hotplug -Here are located the files needed to generate OpenNebula contextualization packages. The packages generated contain these files: +Other actions include: -* `/etc/udev/rules.d/*` These files disable the udev network an cdrom - generation -* `/etc/init.d/vmcontext` This is the startup script that will try to mount - context cdrom, load contextualizaton variables, - call scripts in the contextualization scripts - directory and call init.sh if it exists in the - context cd. -* `/etc/one-context.d/*` This directory holds the scripts that will be - called by vmcontext script. They should be named - starting with a number so they are called in order. +* delete persistent cd and net rules from /etc/udev/rules.d +* delete network configuration files -By default only the network configuration context script is included in the -packages. These scripts are different for rpm and deb based distributions and -are located in `base_` directories. +### Requirements -The packages also have a post-install script that does these steps: + * Ruby >= 1.8.7 + * gem fpm + * dpkg utils for deb package creation + * rpm utils for rpm package creation - * Delete persistent cd and net rules from /etc/udev/rules.d - * Links vmcontext script to /etc/rc.d - * Deletes network configuration files +On Ubuntu/Debian you can install the package `rpm` and you will be able +to generate both rpm and deb packages. -### Package Generation +### Steps -The script `generator.sh` generates both deb and rpm packages and can be configured to include more files in the package or change some of its parameters. +The script `generate.sh` is able to create both **deb** and **rpm** packages +and can be configured to include more files in the package or change some of +its parameters. Package type and content are configured by the env. variable +`TARGET`, the corresponding target must be defined in `target.sh`. Target +describes the package format, name, dependencies, and files. Files are +selected by the tags. Set of required tags is defined for the target +(in `targets.sh`), each file has a list of corresponding tags right in its +filename (divided by the regular name by 2 hashes `##`, dot-separated). On start it creates a temporary directory and copies there: - * `base` directory - * `base_` directory + * All files tagged with no, some (but only from TARGET set) or all tags. * Any file or directory from the arguments. -Then these files are included in the package. - The default parameters to create a package are as follows: - VERSION=1.0.1 - MAINTAINER=OpenNebula Systems - LICENSE=Apache - PACKAGE_NAME=one-context - VENDOR=OpenNebula Systems - DESCRIPTION=" - This package prepares a VM image for OpenNebula: - * Disables udev net and cd persistent rules - * Deletes udev net and cd persistent rules - * Unconfigures the network - * Adds OpenNebula contextualization scripts to startup - - To get support use the OpenNebula mailing list: - http://opennebula.org/community:mailinglists - " - PACKAGE_TYPE=deb - URL=http://opennebula.org - -You can change any parameter setting an environment variable with the same name. For example, to generate an rpm package with a different package name: - - $ PACKAGE_TYPE=rpm PACKAGE_NAME=my-context ./generate.sh - -You can also include new files. This is handy to, for example, include new scripts executed to contextualize an image. For example, we can have an script that install a user ssh key. We will create the file hierarchy that will go inside the package in a directory: - - $ mkdir -p ssh/etc/one-context.d - $ cp ssh/etc/one-context.d/01-ssh-key - $ ./generate.sh ssh/etc +``` +VERSION=1.0.1 +RELEASE=1 +MAINTAINER="OpenNebula Systems " +LICENSE="Apache 2.0" +VENDOR="OpenNebula Systems" +DESCRIPTION=" +This package prepares a VM image for OpenNebula: + * Disables udev net and cd persistent rules + * Deletes udev net and cd persistent rules + * Unconfigures the network + * Adds OpenNebula contextualization scripts to startup + +To get support use the OpenNebula mailing list: + http://OpenNebula.org +" +URL=http://opennebula.org +``` + +A target contains following parameters, e.g. **el7** target: + +``` +NAME=one-context +RELSUFFIX=.el7 +TYPE=rpm +TAGS="rpm systemd one" +DEPENDS="util-linux bind-utils cloud-utils-growpart ruby rubygem-json" +REPLACES="cloud-init" +POSTIN=postinstall.one +PREUN=preuninstall.one +``` + +You can change any parameter setting an environment variable with the same name. +For example, to generate an **el7 rpm** package with a different package name: + +``` +$ TARGET=el7 NAME=my-one-context ./generate.sh +``` + +You can also include new files. This is handy to include new scripts executed +to contextualize an image. For example, we can have a script that installs +a user ssh key. We will create the file hierarchy that will go inside +the package in a directory: + +``` +$ mkdir -p ssh/etc/one-context.d +$ cp ssh/etc/one-context.d/loc-01-ssh-key +$ TARGET=el7 ./generate.sh ssh/etc +``` NOTE: The generator must be executed from the same directory it resides. +## Authors + +* Leader: Javier Fontan (jfontan@opennebula.org) \ No newline at end of file diff --git a/base/usr/sbin/one-contextd b/base/usr/sbin/one-contextd deleted file mode 100755 index 1798119..0000000 --- a/base/usr/sbin/one-contextd +++ /dev/null @@ -1,120 +0,0 @@ -#!/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 vmware_context { - if type vmtoolsd ; then - if vmtoolsd --cmd 'info-get guestinfo.opennebula.context' >/dev/null 2>&1 ; then - return 0 - fi - fi - - return 1 -} - -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 vmware_context ; then - vmtoolsd --cmd 'info-get guestinfo.opennebula.context' | \ - openssl base64 -d > ${CONTEXT_NEW} - elif curl -o ${CONTEXT_NEW} http://169.254.169.254/latest/user-data ; then - echo -n "" - 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 acquire_lock { - while true; do - if mkdir "${LOCK_FILE}" 2>/dev/null; then - trap 'cleanup' EXIT - break - fi - - sleep 1 - done -} - -function release_lock { - rm -rf "${LOCK_FILE}" -} - -function cleanup { - end_context - release_lock -} - -COMMAND="$1" - -acquire_lock - -get_new_context -check_context && run_context "$COMMAND" diff --git a/base_arch/etc/one-context.d/15-hostname b/base_arch/etc/one-context.d/15-hostname deleted file mode 100755 index 9085e3b..0000000 --- a/base_arch/etc/one-context.d/15-hostname +++ /dev/null @@ -1,112 +0,0 @@ -#!/bin/bash - -function set_hostname() { - hostname=$1 - - if [ -d /run/systemd/system/ ]; then - hostnamectl set-hostname --static "${hostname}" - else - if [ -f /etc/sysconfig/network ]; then - sed -i '/^HOSTNAME=.*$/d' /etc/sysconfig/network - echo "HOSTNAME=${hostname}" >>/etc/sysconfig/network - else - echo "${hostname}" >/etc/hostname - fi - - hostname "${hostname}" - fi -} - -function set_domainname() { - domain=$1 - sed -i '/^domain .*/d' /etc/resolv.conf - echo "domain ${domain}" >>/etc/resolv.conf -} - -function get_first_ip() { - hostname -I | cut -d' ' -f1 -} - -function get_dns_name() { - text=$(LC_ALL=C host "$1" 2>/dev/null) - [ $? = 0 ] || exit 0 - [[ $text == *"has no PTR record" ]] && exit 0 - name=$(echo "$text" | awk '/(has address|name pointer)/ {print $(NF)}' | sed 's/\.$//') - echo $name -} - -function update_hosts() { - ip=$1 - name=$2 - hostname=$3 - - if [ "x${hostname}" = "x${name}" ]; then - hosts="${name}" - else - hosts="${name} ${hostname}" - fi - - note='# one-contextd' - entry="${ip}\t${hosts}\t\t${note}" - - # update our old entry - if grep -qi "${note}" /etc/hosts; then - sed -i -e "s/^.*${note}\$/${entry}/" /etc/hosts - # update entry with same IP (but not localhost) - elif grep -E "^${ip}\s" /etc/hosts | grep -qv localhost; then - sed -i -e "/localhost/! s/^${ip}\s.*\$/${entry}/" /etc/hosts - # update entry with same name - elif grep -qE "\s${name}(\s|#|\$)" /etc/hosts; then - sed -i -re "s/^.*\s${name}([ #\t].*|$)/${entry}/" /etc/hosts - # create new entry - elif [ -f /etc/hosts ]; then - sed -i -e "1s/^/${entry}\n/" /etc/hosts - else - echo -e "${entry}" >>/etc/hosts - fi -} - -##### - -first_ip=$(get_first_ip) - -if [ -n "$SET_HOSTNAME" ]; then - name=$(echo "$SET_HOSTNAME" | \ - sed -e 's/[^-a-zA-Z0-9\.]/-/g' -e 's/^-*//g' -e 's/-*$//g') -elif [ -n "$DNS_HOSTNAME" ]; then - name=$(get_dns_name "${first_ip}") -fi - -if [ -n "${name}" ]; then - # split host and domain names - hostname=${name%%.*} - domain=${name#*.} - if [ "x${domain}" = "x${hostname}" ]; then - domain='' - fi - - set_hostname "${hostname}" - if [ -n "${domain}" ]; then - set_domainname "${domain}" - fi - - if [ -n "${DNS_HOSTNAME}" ]; then - host_ip=$first_ip - else - # If selected hostname resolves on first IP, - # use first IP for local hostname in /etc/hosts. - # Otherwise use loopback IP. - name_ip=$(get_dns_name "${name}") - if [ "x${first_ip}" = "x${name_ip}" ]; then - host_ip=$first_ip - elif [ -f /etc/debian_version ]; then - host_ip='127.0.1.1' - else - host_ip='127.0.0.1' - fi - fi - - if [ -n "${host_ip}" ]; then - update_hosts "${host_ip}" "${name}" "${hostname}" - fi -fi diff --git a/generate-all.sh b/generate-all.sh index 2b15efa..e633d5c 100755 --- a/generate-all.sh +++ b/generate-all.sh @@ -1,7 +1,9 @@ -ENVIRONMENT=one PACKAGE_TYPE=deb ./generate.sh -ENVIRONMENT=one PACKAGE_TYPE=rpm ./generate.sh -ENVIRONMENT=ec2 PACKAGE_TYPE=deb ./generate.sh -ENVIRONMENT=ec2 PACKAGE_TYPE=rpm ./generate.sh +TARGETS='el6 el6_ec2 el7 el7_ec2 deb deb_ec2' + +set -e +for TARGET in $TARGETS; do + TARGET="${TARGET}" ./generate.sh +done echo echo "The packages are here:" diff --git a/generate.sh b/generate.sh index d97dfd4..1163e24 100755 --- a/generate.sh +++ b/generate.sh @@ -1,7 +1,7 @@ #!/bin/bash # -------------------------------------------------------------------------- # -# Copyright 2010-2016, OpenNebula Systems # +# Copyright 2010-2017, 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 # @@ -16,18 +16,19 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -ENVIRONMENT=${ENVIRONMENT:-one} - -if [ $ENVIRONMENT != "one" ]; then - DEFAULT_NAME="one-context-$ENVIRONMENT" -else - DEFAULT_NAME="one-context" +if [ -z "${TARGET}" ]; then + echo 'Error: env. variable TARGET not set' >&2 + exit 1 fi +set -e +source targets.sh +set +e + VERSION=${VERSION:-5.0.3} +RELEASE=${RELEASE:-1} MAINTAINER=${MAINTAINER:-OpenNebula Systems } LICENSE=${LICENSE:-Apache 2.0} -PACKAGE_NAME=${PACKAGE_NAME:-$DEFAULT_NAME} VENDOR=${VENDOR:-OpenNebula Systems} SUMMARY="OpenNebula Contextualization Package" DESC=" @@ -47,45 +48,72 @@ To get support check the OpenNebula web page: http://OpenNebula.org " DESCRIPTION=${DESCRIPTION:-$DESC} -PACKAGE_TYPE=${PACKAGE_TYPE:-deb} URL=${URL:-http://opennebula.org} +RELEASE_FULL="${RELEASE}${RELSUFFIX}" -[ $PACKAGE_TYPE = rpm ] && PKGARGS="--rpm-os linux" - -SCRIPTS_DIR=$PWD -NAME="${PACKAGE_NAME}_${VERSION}.${PACKAGE_TYPE}" - -rm -f $NAME - -rm -rf tmp -mkdir tmp - -cp -r base/* tmp -test -d base.$ENVIRONMENT && cp -r base.$ENVIRONMENT/* tmp - -cp -r base_$PACKAGE_TYPE/* tmp -test -d base_$PACKAGE_TYPE.$ENVIRONMENT && \ - cp -r base_$PACKAGE_TYPE.$ENVIRONMENT/* tmp - -for i in $*; do - cp -r "$i" tmp -done - -if [ -f "postinstall.$ENVIRONMENT" ]; then - POSTINSTALL="postinstall.$ENVIRONMENT" +if [ "${TYPE}" = 'deb' ]; then + FILENAME="${NAME}_${VERSION}-${RELEASE_FULL}.${TYPE}" else - POSTINSTALL="postinstall.one" + FILENAME="${NAME}-${VERSION}-${RELEASE_FULL}.noarch.${TYPE}" fi -cd tmp +### + +set -e + +BUILD_DIR=$(mktemp -d) +trap "rm -rf ${BUILD_DIR}" EXIT + +while IFS= read -r -d $'\0' SRC; do + F_TAGS=${SRC##*##} + if [ "x${SRC}" != "x${F_TAGS}" ]; then + for F_TAG in $(echo ${F_TAGS} | sed -e 's/\./ /g'); do + for TAG in ${TAGS}; do + if [ "${F_TAG}" = "${TAG}" ]; then + continue 2 # tag matches, continue with next tag + fi + done + continue 2 # tags not maching, skip this file + done + fi + + # file matches + DST=${SRC%##*} #strip tags + mkdir -p "${BUILD_DIR}/$(dirname "${DST}")" + cp "src/${SRC}" "${BUILD_DIR}/${DST}" +done < <(cd src/ && find . -type f -print0) + +for F in $@; do + cp -r "$F" "${BUILD_DIR}/" +done -mkdir -p "$SCRIPTS_DIR/out" -rm -f "$SCRIPTS_DIR/out/$NAME" +# fix permissions and set umask for fpm +find "${BUILD_DIR}/" -perm -u+r -exec chmod go+r {} \; +find "${BUILD_DIR}/" -perm -u+x -exec chmod go+x {} \; +umask 0022 -fpm -n "$PACKAGE_NAME" -t "$PACKAGE_TYPE" $PKGARGS -s dir --vendor "$VENDOR" \ - --license "$LICENSE" --description "$DESCRIPTION" --url "$URL" \ - -m "$MAINTAINER" -v "$VERSION" --after-install $SCRIPTS_DIR/$POSTINSTALL \ - -a all -p $SCRIPTS_DIR/out/$NAME --rpm-summary "$SUMMARY" * +# cleanup +if [ -z "${OUT}" ]; then + OUT="out/${FILENAME}" + mkdir -p $(dirname "${OUT}") + rm -rf "${OUT}" +fi -echo $NAME +if [ "${TYPE}" = 'dir' ]; then + cp -rT "${BUILD_DIR}" "${OUT}" +else + fpm --name "${NAME}" --version "${VERSION}" --iteration "${RELEASE_FULL}" \ + --architecture all --license "${LICENSE}" \ + --vendor "${VENDOR}" --maintainer "${MAINTAINER}" \ + --description "${DESCRIPTION}" --url "${URL}" \ + --output-type "${TYPE}" --input-type dir --chdir "${BUILD_DIR}" \ + ${POSTIN:+ --after-install ${POSTIN}} \ + ${PREUN:+ --before-remove ${PREUN}} \ + --rpm-os linux \ + --rpm-summary "${SUMMARY}" \ + ${DEPENDS:+ --depends ${DEPENDS// / --depends }} \ + --replaces "${REPLACES}" \ + --package "${OUT}" +fi +echo $(basename ${OUT}) diff --git a/postinstall.ec2 b/postinstall.ec2 index 9d7d35b..a680e70 100755 --- a/postinstall.ec2 +++ b/postinstall.ec2 @@ -1,7 +1,7 @@ #!/bin/bash # -------------------------------------------------------------------------- # -# Copyright 2010-2016, OpenNebula Systems # +# Copyright 2010-2017, 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 # diff --git a/postinstall.one b/postinstall.one index 9c0107c..a921cf5 100755 --- a/postinstall.one +++ b/postinstall.one @@ -1,7 +1,7 @@ #!/bin/bash # -------------------------------------------------------------------------- # -# Copyright 2010-2016, OpenNebula Systems # +# Copyright 2010-2017, 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 # @@ -20,13 +20,26 @@ rm -f /etc/udev/rules.d/70-persistent-cd.rules rm -f /etc/udev/rules.d/70-persistent-net.rules # Reload udev rules -udevadm control --reload +udevadm control --reload >/dev/null 2>&1 || : + +# Register service +if [ -f /etc/init.d/vmcontext ]; then + if [ -d /etc/sysconfig/network-scripts ]; then + chkconfig --add vmcontext + elif [ -d /etc/network ]; then + update-rc.d vmcontext defaults 9 99 + else + echo 'WARNING: Contextualization service not enabled' >&2 + fi +elif [ -d /run/systemd/system/ ]; then + systemctl enable one-context-local.service + systemctl enable one-context.service +else + echo 'WARNING: Contextualization service not enabled' >&2 +fi # Debian based distros if [ -d /etc/network ]; then - # Register Service - update-rc.d vmcontext defaults 9 99 - # Prepare network files cp /etc/network/interfaces /etc/network/interfaces.$(date "+%s") cat > /etc/network/interfaces <> /tmp/context.log 2>&1 - diff --git a/preuninstall.one b/preuninstall.one new file mode 100755 index 0000000..4bed584 --- /dev/null +++ b/preuninstall.one @@ -0,0 +1,24 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +# Unregister service +if [ -d /run/systemd/system/ ]; then + /bin/systemctl --no-reload disable one-context-local.service one-context.service >/dev/null 2>&1 || : + /bin/systemctl daemon-reload >/dev/null 2>&1 || : +fi + diff --git a/base_deb.one/etc/default/grub.d/99-opennebula-settings.cfg b/src/etc/default/grub.d/99-opennebula-settings.cfg##deb.one similarity index 100% rename from base_deb.one/etc/default/grub.d/99-opennebula-settings.cfg rename to src/etc/default/grub.d/99-opennebula-settings.cfg##deb.one diff --git a/base_deb/etc/init.d/vmcontext b/src/etc/init.d/vmcontext##deb similarity index 100% rename from base_deb/etc/init.d/vmcontext rename to src/etc/init.d/vmcontext##deb diff --git a/base_rpm.ec2/etc/init.d/vmcontext b/src/etc/init.d/vmcontext##rpm.sysv.ec2 similarity index 100% rename from base_rpm.ec2/etc/init.d/vmcontext rename to src/etc/init.d/vmcontext##rpm.sysv.ec2 diff --git a/base_rpm.one/etc/init.d/vmcontext b/src/etc/init.d/vmcontext##rpm.sysv.one similarity index 100% rename from base_rpm.one/etc/init.d/vmcontext rename to src/etc/init.d/vmcontext##rpm.sysv.one diff --git a/base.one/etc/one-context.d/05-grow-rootfs b/src/etc/one-context.d/loc-05-grow-rootfs##one similarity index 100% rename from base.one/etc/one-context.d/05-grow-rootfs rename to src/etc/one-context.d/loc-05-grow-rootfs##one diff --git a/base_arch/etc/one-context.d/10-network b/src/etc/one-context.d/loc-10-network##arch similarity index 100% rename from base_arch/etc/one-context.d/10-network rename to src/etc/one-context.d/loc-10-network##arch diff --git a/base_deb.one/etc/one-context.d/10-network b/src/etc/one-context.d/loc-10-network##deb.one similarity index 99% rename from base_deb.one/etc/one-context.d/10-network rename to src/etc/one-context.d/loc-10-network##deb.one index 811847a..6880a96 100755 --- a/base_deb.one/etc/one-context.d/10-network +++ b/src/etc/one-context.d/loc-10-network##deb.one @@ -16,6 +16,8 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +COMMAND=${1} + # Gets IP address from a given MAC mac2ip() { mac=$1 diff --git a/base_rpm.one/etc/one-context.d/10-network b/src/etc/one-context.d/loc-10-network##rpm.one similarity index 97% rename from base_rpm.one/etc/one-context.d/10-network rename to src/etc/one-context.d/loc-10-network##rpm.one index 251681e..b0743e1 100755 --- a/base_rpm.one/etc/one-context.d/10-network +++ b/src/etc/one-context.d/loc-10-network##rpm.one @@ -16,6 +16,8 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +COMMAND=${1} + # Gets IP address from a given MAC mac2ip() { mac=$1 @@ -195,7 +197,9 @@ configure_network() { gen_network_configuration - service network restart + if ! [ -d /run/systemd/system/ ] || [ "${COMMAND}" = 'reconfigure' ]; then + service network restart + fi sleep 2 } diff --git a/base.one/etc/one-context.d/10-network-pci b/src/etc/one-context.d/loc-10-network-pci##one similarity index 100% rename from base.one/etc/one-context.d/10-network-pci rename to src/etc/one-context.d/loc-10-network-pci##one diff --git a/base.one/etc/one-context.d/11-dns b/src/etc/one-context.d/loc-11-dns##one similarity index 100% rename from base.one/etc/one-context.d/11-dns rename to src/etc/one-context.d/loc-11-dns##one diff --git a/base.one/etc/one-context.d/14-mount-swap b/src/etc/one-context.d/loc-14-mount-swap##one similarity index 100% rename from base.one/etc/one-context.d/14-mount-swap rename to src/etc/one-context.d/loc-14-mount-swap##one diff --git a/base/etc/one-context.d/16-gen-env b/src/etc/one-context.d/loc-16-gen-env similarity index 78% rename from base/etc/one-context.d/16-gen-env rename to src/etc/one-context.d/loc-16-gen-env index fcebd65..372a483 100755 --- a/base/etc/one-context.d/16-gen-env +++ b/src/etc/one-context.d/loc-16-gen-env @@ -1,8 +1,8 @@ #!/bin/bash -ENV_FILE="/tmp/one_env" - -TOKENTXT=$(cat /mnt/token.txt) +ENV_FILE=/var/run/one-context/one_env +MOUNT_DIR=${MOUNT_DIR:-/mnt} +TOKENTXT=$(cat "${MOUNT_DIR}/token.txt") if [ -n "$ONEGATE_TOKEN" ]; then TOKENTXT="$ONEGATE_TOKEN" @@ -25,7 +25,7 @@ function export_rc_vars fi } -export_rc_vars /tmp/context.sh +export_rc_vars ${CONTEXT_FILE} chown root:root $ENV_FILE chmod a+r $ENV_FILE diff --git a/base/etc/one-context.d/20-set-username-password b/src/etc/one-context.d/loc-20-set-username-password similarity index 100% rename from base/etc/one-context.d/20-set-username-password rename to src/etc/one-context.d/loc-20-set-username-password diff --git a/base/etc/one-context.d/22-ssh_public_key b/src/etc/one-context.d/loc-22-ssh_public_key similarity index 100% rename from base/etc/one-context.d/22-ssh_public_key rename to src/etc/one-context.d/loc-22-ssh_public_key diff --git a/base_rpm/etc/one-context.d/23-selinux-ssh b/src/etc/one-context.d/loc-23-selinux-ssh##rpm similarity index 100% rename from base_rpm/etc/one-context.d/23-selinux-ssh rename to src/etc/one-context.d/loc-23-selinux-ssh##rpm diff --git a/base.one/etc/one-context.d/11-fix-loopback b/src/etc/one-context.d/net-11-fix-loopback##one similarity index 100% rename from base.one/etc/one-context.d/11-fix-loopback rename to src/etc/one-context.d/net-11-fix-loopback##one diff --git a/base_deb/etc/one-context.d/12-ssh_host_key b/src/etc/one-context.d/net-12-ssh_host_key##deb similarity index 100% rename from base_deb/etc/one-context.d/12-ssh_host_key rename to src/etc/one-context.d/net-12-ssh_host_key##deb diff --git a/base.one/etc/one-context.d/15-hostname b/src/etc/one-context.d/net-15-hostname##one similarity index 100% rename from base.one/etc/one-context.d/15-hostname rename to src/etc/one-context.d/net-15-hostname##one diff --git a/base/etc/one-context.d/97-start-script b/src/etc/one-context.d/net-97-start-script similarity index 75% rename from base/etc/one-context.d/97-start-script rename to src/etc/one-context.d/net-97-start-script index 8f872d9..3302a90 100755 --- a/base/etc/one-context.d/97-start-script +++ b/src/etc/one-context.d/net-97-start-script @@ -1,8 +1,8 @@ #!/bin/bash -MOUNT_DIR=/mnt -TMP_DIR=/tmp/one-context-tmp -TMP_FILE=$TMP_DIR/one-start-script +MOUNT_DIR=${MOUNT_DIR:-/mnt} +TMP_DIR=$(mktemp -d "${RUNTIME_DIR:-/tmp}.XXXX") +TMP_FILE="${TMP_DIR}/one-start-script" START_SCRIPT_AVAILABLE=no mkdir -p $TMP_DIR @@ -22,3 +22,4 @@ if [ "$START_SCRIPT_AVAILABLE" = "yes" ]; then $TMP_FILE fi +rm -rf ${TMP_DIR} diff --git a/base/etc/one-context.d/98-execute-scripts b/src/etc/one-context.d/net-98-execute-scripts similarity index 72% rename from base/etc/one-context.d/98-execute-scripts rename to src/etc/one-context.d/net-98-execute-scripts index 06e071c..3cf0977 100755 --- a/base/etc/one-context.d/98-execute-scripts +++ b/src/etc/one-context.d/net-98-execute-scripts @@ -1,7 +1,7 @@ #!/bin/bash -MOUNT_DIR=/mnt -TMP_DIR=/tmp/one-context-tmp +MOUNT_DIR=${MOUNT_DIR:-/mnt} +TMP_DIR=$(mktemp -d "${RUNTIME_DIR:-/tmp}.XXXX") if [ -z "$INIT_SCRIPTS" ]; then if [ -f "$MOUNT_DIR/init.sh" ]; then @@ -18,3 +18,4 @@ for f in $INIT_SCRIPTS; do $TMP_DIR/$f done +rm -rf ${TMP_DIR} diff --git a/base/etc/one-context.d/99-report-ready b/src/etc/one-context.d/net-99-report-ready similarity index 100% rename from base/etc/one-context.d/99-report-ready rename to src/etc/one-context.d/net-99-report-ready diff --git a/base/etc/udev/rules.d/75-cd-aliases-generator.rules b/src/etc/udev/rules.d/75-cd-aliases-generator.rules similarity index 100% rename from base/etc/udev/rules.d/75-cd-aliases-generator.rules rename to src/etc/udev/rules.d/75-cd-aliases-generator.rules diff --git a/base/etc/udev/rules.d/75-persistent-net-generator.rules b/src/etc/udev/rules.d/75-persistent-net-generator.rules similarity index 100% rename from base/etc/udev/rules.d/75-persistent-net-generator.rules rename to src/etc/udev/rules.d/75-persistent-net-generator.rules diff --git a/src/lib/udev/rules.d/65-context.rules##one.systemd b/src/lib/udev/rules.d/65-context.rules##one.systemd new file mode 100644 index 0000000..de598fe --- /dev/null +++ b/src/lib/udev/rules.d/65-context.rules##one.systemd @@ -0,0 +1,7 @@ +# On NIC hogplug the delayed reconfiguration is triggered. +SUBSYSTEM=="net", ACTION=="add", TAG+="systemd", ENV{SYSTEMD_WANTS}+="one-context-reconfigure-delayed.service" + +# When CONTEXT CD-ROM is changed, it generates 2 events. This rule takes +# every second event and triggers systemd service one-context-reconfigure-now. +# This service also stops any existing delayed reconfiguration. +SUBSYSTEM=="block", ACTION=="change", ENV{ID_FS_TYPE}=="iso9660" ENV{ID_FS_LABEL_ENC}=="CONTEXT", ENV{SEQNUM}=="*[02468]" RUN+="/bin/systemctl start one-context-reconfigure.service" diff --git a/base.one/lib/udev/rules.d/00-context.rules b/src/lib/udev/rules.d/65-context.rules##one.sysv similarity index 100% rename from base.one/lib/udev/rules.d/00-context.rules rename to src/lib/udev/rules.d/65-context.rules##one.sysv diff --git a/base/usr/bin/onegate b/src/usr/bin/onegate similarity index 78% rename from base/usr/bin/onegate rename to src/usr/bin/onegate index da2ce35..b50d1f8 100755 --- a/base/usr/bin/onegate +++ b/src/usr/bin/onegate @@ -1,6 +1,6 @@ #!/bin/bash -ENV_FILE=/tmp/one_env +ENV_FILE=/var/run/one-context/one_env if [ -f $ENV_FILE ]; then . $ENV_FILE diff --git a/base/usr/bin/onegate.rb b/src/usr/bin/onegate.rb similarity index 100% rename from base/usr/bin/onegate.rb rename to src/usr/bin/onegate.rb diff --git a/src/usr/lib/systemd/system/one-context-local.service##rpm.systemd.one b/src/usr/lib/systemd/system/one-context-local.service##rpm.systemd.one new file mode 100644 index 0000000..042b2ed --- /dev/null +++ b/src/usr/lib/systemd/system/one-context-local.service##rpm.systemd.one @@ -0,0 +1,14 @@ +[Unit] +Description=OpenNebula pre-networking contextualization script +Before=network-pre.target +Wants=network-pre.target dev-disk-by\x2dlabel-CONTEXT.device +After=dev-disk-by\x2dlabel-CONTEXT.device +ConditionPathExists=!/var/run/one-context/context.sh.local + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/sbin/one-contextd local + +[Install] +WantedBy=multi-user.target diff --git a/src/usr/lib/systemd/system/one-context-reconfigure-delayed.service##rpm.systemd.one b/src/usr/lib/systemd/system/one-context-reconfigure-delayed.service##rpm.systemd.one new file mode 100644 index 0000000..4bacd98 --- /dev/null +++ b/src/usr/lib/systemd/system/one-context-reconfigure-delayed.service##rpm.systemd.one @@ -0,0 +1,11 @@ +[Unit] +Description=OpenNebula delayed reconfiguration script +After=one-context.service +Requisite=one-context.service + +[Service] +Type=oneshot +ExecStart=/usr/sbin/one-context-reconfigure + +[Install] +WantedBy=multi-user.target diff --git a/src/usr/lib/systemd/system/one-context-reconfigure.service##rpm.systemd.one b/src/usr/lib/systemd/system/one-context-reconfigure.service##rpm.systemd.one new file mode 100644 index 0000000..e3bd8f7 --- /dev/null +++ b/src/usr/lib/systemd/system/one-context-reconfigure.service##rpm.systemd.one @@ -0,0 +1,13 @@ +[Unit] +Description=OpenNebula reconfiguration script +After=one-context.service +Requisite=one-context.service +Conflicts=one-context-reconfigure-delayed.service + +[Service] +Type=oneshot +Environment=TIMEOUT=0 +ExecStart=/usr/sbin/one-context-reconfigure + +[Install] +WantedBy=multi-user.target diff --git a/base_arch/usr/lib/systemd/system/one-context.service b/src/usr/lib/systemd/system/one-context.service##arch similarity index 96% rename from base_arch/usr/lib/systemd/system/one-context.service rename to src/usr/lib/systemd/system/one-context.service##arch index 95dea34..e55ac74 100644 --- a/base_arch/usr/lib/systemd/system/one-context.service +++ b/src/usr/lib/systemd/system/one-context.service##arch @@ -21,4 +21,4 @@ SyslogIdentifier=one-context RemainAfterExit=yes [Install] -WantedBy=multi-user.target \ No newline at end of file +WantedBy=multi-user.target diff --git a/src/usr/lib/systemd/system/one-context.service##rpm.systemd.one b/src/usr/lib/systemd/system/one-context.service##rpm.systemd.one new file mode 100644 index 0000000..03545be --- /dev/null +++ b/src/usr/lib/systemd/system/one-context.service##rpm.systemd.one @@ -0,0 +1,13 @@ +[Unit] +Description=OpenNebula contextualization script +Requires=one-context-local.service +After=multi-user.target one-context-local.service +ConditionPathExists=!/var/run/one-context/context.sh.network + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/sbin/one-contextd network + +[Install] +WantedBy=multi-user.target diff --git a/src/usr/sbin/one-context-reconfigure##one b/src/usr/sbin/one-context-reconfigure##one new file mode 100755 index 0000000..7ceff2c --- /dev/null +++ b/src/usr/sbin/one-context-reconfigure##one @@ -0,0 +1,31 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +# Set PATH +export PATH=/sbin:/bin:/usr/sbin:/usr/bin + +TIMEOUT=${TIMEOUT:-60} +CONTEXT_BASE=${CONTEXT_BASE:-/var/run/one-context/context.sh} + +if [ -f ${CONTEXT_BASE}.local ] && [ -f ${CONTEXT_BASE}.network ]; then + sleep "${TIMEOUT}" && \ + /usr/sbin/one-contextd all reconfigure +else + echo 'Contextualization must start first as a boot service' >&2 + exit 1 +fi diff --git a/src/usr/sbin/one-contextd b/src/usr/sbin/one-contextd new file mode 100755 index 0000000..f43511e --- /dev/null +++ b/src/usr/sbin/one-contextd @@ -0,0 +1,242 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +set -e + +TYPE="${1:-all}" # local, network, all +COMMAND="${2}" # reconfigure? + +SCRIPTS_DIR=${SCRIPTS_DIR:-/etc/one-context.d} +RUNTIME_DIR=${RUNTIME_DIR:-/var/run/one-context} +LOCK_FILE="${RUNTIME_DIR}/one-context.lock" +CONTEXT_BASE="${RUNTIME_DIR}/context.sh" +SYSLOG_TAG="$(basename $0)" + +[ -d "${RUNTIME_DIR}" ] || mkdir -m 0700 -p "${RUNTIME_DIR}" +CONTEXT_NEW=$(mktemp "${CONTEXT_BASE}.XXXX" 2>/dev/null) +SYSLOG_FACILITY="${SYSLOG_FACILITY:-local3}" + +shopt -s extglob +set +e + + +function log +{ + # display on stdout/err?, force if DEBUG + local _echo_fd=$3 + if [ -n "${DEBUG}" ] && [ "${_echo_fd}" = '' ]; then + _echo_fd=1 + fi + + if [ "${_echo_fd}" = '1' ] || [ "${_echo_fd}" = '2' ]; then + echo "${2}" >&${_echo_fd} + fi + + # try systemd/journald with fallback to logger + systemd-cat -t "${SYSLOG_TAG}" -p "${1}" \ + echo "${2}" 2>/dev/null + + if [ "$?" != "0" ]; then + logger -t "${SYSLOG_TAG}" \ + -p "${SYSLOG_FACILITY}.${1}" \ + "${2}" 2>/dev/null + fi +} + +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 { + local _type=$1 + local _command=$1 + + # choose + if [ "${_type}" = 'local' ]; then + local _scripts=$(ls ${SCRIPTS_DIR}/loc-* 2>/dev/null) + elif [ "${_type}" = 'network' ]; then + local _scripts=$(ls ${SCRIPTS_DIR}/!(net-*|loc-*) 2>/dev/null; \ + ls ${SCRIPTS_DIR}/net-* 2>/dev/null) + fi + + export MOUNT_DIR + export RUNTIME_DIR + + log info "Processing ${_type} scripts" + for _script in ${_scripts}; do + local _name=$(basename "${_script}") + + # run script and catch output and exit code + log debug "Script ${_name}: Starting ..." + _out=$("${_script}" "${_command}" 2>&1) + local _rtn=$? + + # log on any output + if [ -n "${_out}" ]; then + log info "Script ${_name} output: ${_out}" + fi + + # set log level to error if script failed + if [ ${_rtn} -eq 0 ]; then + local _level=debug + else + local _level=err + fi + + log "${_level}" "Script ${_name}: Finished with exit code ${_rtn}" + done + log debug 'Finished scripts processing' +} + +function vmware_context { + if type vmtoolsd >/dev/null 2>&1; then + if vmtoolsd --cmd 'info-get guestinfo.opennebula.context' >/dev/null 2>&1 ; then + return 0 + else + log debug "VMware ONE context not found" + fi + fi + + log debug "Command vmtoolsd not found" + return 1 +} + +function get_new_context { + local dev_context=$(blkid -l -t LABEL="CONTEXT" -o device) + if [ -e "${dev_context}" ]; then + MOUNT_DIR=$(mktemp -d "${RUNTIME_DIR}/mount.XXXX" 2>/dev/null) + if ! [ -d "${MOUNT_DIR}" ]; then + log err 'Error: Failed to create mountpoint' 2 + exit 1 + fi + + log debug "Mounting CD-ROM ${dev_context} on ${MOUNT_DIR}" + mount -t iso9660 -o ro ${dev_context} ${MOUNT_DIR} 2>/dev/null + if [ "$?" != '0' ]; then + log err "Error: Failed to mount ${dev_context}" 2 + exit 1 + fi + + local fn_mnt_context="${MOUNT_DIR}/context.sh" + if [ -f "${fn_mnt_context}" ]; then + log debug "Found context ${fn_mnt_context}" + cp "${fn_mnt_context}" "${CONTEXT_NEW}" + fi + + elif vmware_context ; then + log debug "Reading context via vmtoolsd" + vmtoolsd --cmd 'info-get guestinfo.opennebula.context' | \ + openssl base64 -d > ${CONTEXT_NEW} + + elif curl -s -o ${CONTEXT_NEW} http://169.254.169.254/latest/user-data; then + log debug "Reading EC2 user-data" + echo -n "" + fi +} + +function check_context { + local _f_new=$1 + local _f_old=$2 + local _rtn=1 + + log debug "Comparing ${_f_new} and ${_f_old} for changes" + if [ -s "${_f_new}" ]; then + diff "${_f_old}" "${_f_new}" >/dev/null 2>&1 || _rtn=0 + fi + + if [ ${_rtn} -eq 0 ]; then + log debug "New context with changes" + else + log info "No changes in context, skipping" + fi + + return ${_rtn} +} + +function run_context { + local _type=$1 + local _command=$2 + + export CONTEXT_FILE="${CONTEXT_BASE}.${_type}" + if check_context "${CONTEXT_NEW}" "${CONTEXT_FILE}"; then + cp -f "${CONTEXT_NEW}" "${CONTEXT_FILE}" + export_rc_vars "${CONTEXT_FILE}" + execute_scripts "${_type}" "${_command}" + fi +} + +function acquire_lock { + # acquire for execution lock + log debug "Acquiring lock ${LOCK_FILE}" + while true; do + if mkdir "${LOCK_FILE}" 2>/dev/null; then + trap 'cleanup' EXIT + log debug "Acquired lock ${LOCK_FILE}" + break + fi + + sleep 1 + done +} + +function cleanup { + # unmount context + if [ -d "${MOUNT_DIR}" ]; then + log debug "Unmounting ${MOUNT_DIR}" + umount -l "${MOUNT_DIR}" + rmdir "${MOUNT_DIR}" + fi + + # remove remporary files + if [ -f "${CONTEXT_NEW}" ]; then + unlink "${CONTEXT_NEW}" + fi + + # remove lock + log debug "Releasing lock ${LOCK_FILE}" + rm -rf "${LOCK_FILE}" +} + +##### + +if ! [[ ${TYPE} =~ ^(local|network|all)$ ]]; then + log err "Error: Invalid or missing execution type ${TYPE}" 2 + exit 1 +fi + +log info "Started ${TYPE:+for type $TYPE} ${COMMAND:+to $COMMAND}" +acquire_lock +get_new_context + +if [ "${TYPE}" = 'all' ]; then + run_context 'local' "${COMMAND}" + run_context 'network' "${COMMAND}" +else + run_context "${TYPE}" "${COMMAND}" +fi + +log info "Done" diff --git a/targets.sh b/targets.sh new file mode 100644 index 0000000..770f238 --- /dev/null +++ b/targets.sh @@ -0,0 +1,78 @@ +case "${TARGET}" in + 'el6') + NAME=${NAME:-one-context} + RELSUFFIX=${RELSUFFIX:-.el6} + TYPE=${TYPE:-rpm} + TAGS=${TAGS:-rpm sysv one} + DEPENDS=${DEPENDS:-util-linux-ng bind-utils cloud-utils-growpart ruby rubygem-json} + REPLACES=${REPLACES:-cloud-init} + POSTIN=${POSTINST:-postinstall.one} + PREUN=${PREUN:-preuninstall.one} + ;; + + + 'el6_ec2') + NAME=${NAME:-one-context-ec2} + RELSUFFIX=${RELSUFFIX:-.el6} + TYPE=${TYPE:-rpm} + TAGS=${TAGS:-rpm sysv ec2} + DEPENDS=${DEPENDS:-util-linux-ng bind-utils cloud-utils-growpart ruby rubygem-json} + REPLACES=${REPLACES:-cloud-init} + POSTIN=${POSTINST:-postinstall.ec2} + PREUN=${PREUN:-preuninstall.ec2} + ;; + + 'el7') + NAME=${NAME:-one-context} + RELSUFFIX=${RELSUFFIX:-.el7} + TYPE=${TYPE:-rpm} + TAGS=${TAGS:-rpm systemd one} + DEPENDS=${DEPENDS:-util-linux bind-utils cloud-utils-growpart ruby rubygem-json} + REPLACES=${REPLACES:-cloud-init} + POSTIN=${POSTINST:-postinstall.one} + PREUN=${PREUN:-preuninstall.one} + ;; + + 'el7_ec2') + NAME=${NAME:-one-context-ec2} + RELSUFFIX=${RELSUFFIX:-.el7} + TYPE=${TYPE:-rpm} + TAGS=${TAGS:-rpm sysv ec2} + DEPENDS=${DEPENDS:-util-linux bind-utils cloud-utils-growpart ruby rubygem-json} + REPLACES=${REPLACES:-cloud-init} + POSTIN=${POSTINST:-postinstall.ec2} + PREUN=${PREUN:-preuninstall.ec2} + ;; + + 'deb') + NAME=${NAME:-one-context} + RELSUFFIX=${RELSUFFIX:-} + TYPE=${TYPE:-deb} + TAGS=${TAGS:-deb one} + DEPENDS=${DEPENDS:-util-linux bind9-host cloud-utils ruby python} + REPLACES=${REPLACES:-cloud-init} + POSTIN=${POSTINST:-postinstall.one} + PREUN=${PREUN:-preuninstall.one} + ;; + + 'deb_ec2') + NAME=${NAME:-one-context-ec2} + RELSUFFIX=${RELSUFFIX:-} + TYPE=${TYPE:-deb} + TAGS=${TAGS:-deb ec2} + DEPENDS=${DEPENDS:-util-linux bind9-host cloud-utils ruby python} + REPLACES=${REPLACES:-cloud-init} + POSTIN=${POSTINST:-postinstall.ec2} + PREUN=${PREUN:-preuninstall.ec2} + ;; + + 'arch') + NAME=${NAME:-one-context} + TYPE=${TYPE:-dir} + TAGS=${TAGS:-arch one} + ;; + + *) + echo "Invalid target ${TARGET}" + exit 1 +esac