diff --git a/generate-all.sh b/generate-all.sh index 3460951..afd0cd1 100755 --- a/generate-all.sh +++ b/generate-all.sh @@ -1,4 +1,4 @@ -TARGETS='el6 el6_ec2 el7 el7_ec2 suse deb deb_ec2 alpine' +TARGETS='el6 el7 el7_ec2 suse deb deb_ec2 alpine' set -e for TARGET in $TARGETS; do diff --git a/generate.sh b/generate.sh index ac09623..64d09ae 100755 --- a/generate.sh +++ b/generate.sh @@ -65,7 +65,10 @@ fi set -e BUILD_DIR=$(mktemp -d) -trap "rm -rf ${BUILD_DIR}" EXIT +_POSTIN=$(mktemp) +_PREUN=$(mktemp) + +trap "rm -rf ${BUILD_DIR} ${_POSTIN} ${_PREUN}" EXIT while IFS= read -r -d $'\0' SRC; do F_TAGS=${SRC##*##} @@ -111,22 +114,31 @@ else ! -path 'etc/init*' \ -type f -printf '--config-files %p ') + # concatenate pre/postinstall scripts + if [ -n "${POSTIN}" ]; then + cat ${POSTIN} >"${_POSTIN}" + fi + + if [ -n "${PREUN}" ]; then + cat ${PREUN} >"${_PREUN}" + fi + 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}} \ + ${POSTIN:+ --after-install ${_POSTIN}} \ + ${PREUN:+ --before-remove ${_PREUN}} \ --rpm-os linux \ --rpm-summary "${SUMMARY}" \ ${DEPENDS:+ --depends ${DEPENDS// / --depends }} \ - --replaces "${REPLACES}" \ - --conflicts "${REPLACES}" \ + ${REPLACES:+ --replaces ${REPLACES// / --replaces }} \ + ${CONFLICTS:+ --conflicts ${CONFLICTS// / --conflicts }} \ + ${PROVIDES:+ --provides ${PROVIDES// / --provides }} \ --deb-no-default-config-files \ ${CONFIG_FILES} \ --package "${OUT}" -# --provides "${REPLACES}" \ fi echo $(basename ${OUT}) diff --git a/postinstall.one b/postinstall similarity index 71% rename from postinstall.one rename to postinstall index 24fb1bc..d31285e 100755 --- a/postinstall.one +++ b/postinstall @@ -16,7 +16,9 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -SERVICES='one-context-local one-context' +NETWORK=${NETWORK:-yes} +NETWORK_EC2=${NETWORK_EC2:-no} +SERVICES=${SERVICES:-one-context-local one-context} rm -f /etc/udev/rules.d/70-persistent-cd.rules rm -f /etc/udev/rules.d/70-persistent-net.rules @@ -62,16 +64,33 @@ else echo 'WARNING: Contextualization service not enabled automatically' >&2 fi +# Cleanup network configuration +if [ "${NETWORK}" != 'yes' ]; then + exit +fi + # Debian based distros if [ -d /etc/network ]; then # Prepare network files cp /etc/network/interfaces /etc/network/interfaces.$(date "+%s") - cat > /etc/network/interfaces < /etc/network/interfaces < /etc/network/interfaces <> /etc/default/networking # fi # fi - - # Remove ttyS* from the grub - if [ -f /etc/default/grub ]; then - sed -i 's/console=ttyS[a-zA-Z0-9,]*//g' /etc/default/grub - fi - - if [ -f /boot/grub/grub.cfg ]; then - sed -i 's/console=ttyS[a-zA-Z0-9,]*//g' /boot/grub/grub.cfg - fi fi # RedHat based distros if [ -d /etc/sysconfig/network-scripts ]; then # Prepare network files - rm -f /etc/sysconfig/network-scripts/ifcfg-eth* + find /etc/sysconfig/network-scripts -type f \ + -name 'ifcfg-*' ! -name 'ifcfg-lo' \ + -exec rm -f {} \; + + if [ "${NETWORK_EC2}" = 'yes' ]; then + cat >/etc/sysconfig/network-scripts/ifcfg-eth0 </etc/sysconfig/network/ifcfg-eth0 </dev/null 2>&1 && [ -d /etc/systemd ]; then diff --git a/preuninstall.ec2 b/preuninstall.ec2 index 4414b4b..ef61834 100755 --- a/preuninstall.ec2 +++ b/preuninstall.ec2 @@ -15,3 +15,5 @@ # See the License for the specific language governing permissions and # # limitations under the License. # #--------------------------------------------------------------------------- # + +SERVICES='one-context' diff --git a/src/etc/init.d/one-context##apk b/src/etc/init.d/one-context##apk.one similarity index 100% rename from src/etc/init.d/one-context##apk rename to src/etc/init.d/one-context##apk.one diff --git a/src/etc/init.d/one-context-local##apk b/src/etc/init.d/one-context-local##apk.one similarity index 100% rename from src/etc/init.d/one-context-local##apk rename to src/etc/init.d/one-context-local##apk.one diff --git a/src/etc/init.d/one-context-reconfigure##apk b/src/etc/init.d/one-context-reconfigure##apk.one similarity index 100% rename from src/etc/init.d/one-context-reconfigure##apk rename to src/etc/init.d/one-context-reconfigure##apk.one diff --git a/src/etc/init.d/one-context-reconfigure-delayed##apk b/src/etc/init.d/one-context-reconfigure-delayed##apk.one similarity index 100% rename from src/etc/init.d/one-context-reconfigure-delayed##apk rename to src/etc/init.d/one-context-reconfigure-delayed##apk.one diff --git a/src/etc/init.d/vmcontext##rpm.sysv.ec2 b/src/etc/init.d/vmcontext##rpm.sysv.ec2 deleted file mode 100755 index b98a4b7..0000000 --- a/src/etc/init.d/vmcontext##rpm.sysv.ec2 +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash -# -# chkconfig: 2345 11 99 -# description: network reconfigure -# -# -------------------------------------------------------------------------- # -# 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. # -#--------------------------------------------------------------------------- # - -### BEGIN INIT INFO -# Provides: vmcontext -# Required-Start: $all -# Required-Stop: -# Should-Start: networking -# Should-Stop: networking -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: OpenNebula contextualization script -### END INIT INFO - -case "$1" in -"start") - /usr/sbin/one-contextd - -esac - diff --git a/src/etc/one-context.d/loc-05-grow-rootfs##one b/src/etc/one-context.d/loc-05-grow-rootfs similarity index 100% rename from src/etc/one-context.d/loc-05-grow-rootfs##one rename to src/etc/one-context.d/loc-05-grow-rootfs diff --git a/src/etc/one-context.d/loc-10-network##apk b/src/etc/one-context.d/loc-10-network##apk.one similarity index 100% rename from src/etc/one-context.d/loc-10-network##apk rename to src/etc/one-context.d/loc-10-network##apk.one diff --git a/src/etc/one-context.d/loc-10-network##arch b/src/etc/one-context.d/loc-10-network##arch.one similarity index 100% rename from src/etc/one-context.d/loc-10-network##arch rename to src/etc/one-context.d/loc-10-network##arch.one diff --git a/src/etc/one-context.d/net-15-hostname##one b/src/etc/one-context.d/net-15-hostname similarity index 86% rename from src/etc/one-context.d/net-15-hostname##one rename to src/etc/one-context.d/net-15-hostname index 293d0bf..b1e4a81 100755 --- a/src/etc/one-context.d/net-15-hostname##one +++ b/src/etc/one-context.d/net-15-hostname @@ -73,8 +73,20 @@ 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}") + +elif [ "${EC2_HOSTNAME}" = 'YES' ]; then + # try to quickly get hostname from the EC2 metadata server or + # create hostname based on the first IPv4 (format: "ip-1-2-3-4") + name=$(curl -sf -m 5 'http://169.254.169.254/latest/meta-data/local-hostname' 2>/dev/null) + if [ -z "${name}" ]; then + name="$(echo "${first_ip}" | grep -x '[0-9\.]\+' | tr . -)" + if [ -n "${name}" ]; then + name="ip-${name}" + fi + fi fi if [ -n "${name}" ]; then diff --git a/src/lib/udev/rules.d/65-context.rules##apk b/src/lib/udev/rules.d/65-context.rules##apk.one similarity index 100% rename from src/lib/udev/rules.d/65-context.rules##apk rename to src/lib/udev/rules.d/65-context.rules##apk.one diff --git a/src/usr/lib/systemd/system/one-context.service##arch b/src/usr/lib/systemd/system/one-context.service##arch.one similarity index 100% rename from src/usr/lib/systemd/system/one-context.service##arch rename to src/usr/lib/systemd/system/one-context.service##arch.one diff --git a/src/usr/lib/systemd/system/one-context.service##systemd.ec2 b/src/usr/lib/systemd/system/one-context.service##systemd.ec2 new file mode 100644 index 0000000..5ce78e5 --- /dev/null +++ b/src/usr/lib/systemd/system/one-context.service##systemd.ec2 @@ -0,0 +1,14 @@ +[Unit] +Description=OpenNebula contextualization script +Requires=network.target +After=multi-user.target +ConditionPathExists=!/var/run/one-context/context.sh.local +ConditionPathExists=!/var/run/one-context/context.sh.network + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/sbin/one-contextd all + +[Install] +WantedBy=multi-user.target diff --git a/src/usr/sbin/one-contextd b/src/usr/sbin/one-contextd index 6c27c66..af6d9f1 100755 --- a/src/usr/sbin/one-contextd +++ b/src/usr/sbin/one-contextd @@ -164,9 +164,15 @@ function get_new_context { 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 + elif curl -sf -m 30 -o ${CONTEXT_NEW} http://169.254.169.254/latest/user-data; then log debug "Reading EC2 user-data" - echo -n "" + echo -n "" >>"${CONTEXT_NEW}" + + # enable EC2 hostname configuration + export EC2_HOSTNAME=YES + else + log err 'Error: No contextualization found' 2 + exit 1 fi } diff --git a/targets.sh b/targets.sh index 9e8819c..195baff 100644 --- a/targets.sh +++ b/targets.sh @@ -4,10 +4,12 @@ case "${TARGET}" in RELSUFFIX=${RELSUFFIX:-.el6} TYPE=${TYPE:-rpm} TAGS=${TAGS:-rpm sysv one} - DEPENDS=${DEPENDS:-util-linux-ng bind-utils cloud-utils-growpart dracut-modules-growroot ruby rubygem-json sudo shadow-utils open-vm-tools qemu-guest-agent} + DEPENDS=${DEPENDS:-util-linux-ng bash curl bind-utils cloud-utils-growpart dracut-modules-growroot ruby rubygem-json sudo shadow-utils open-vm-tools qemu-guest-agent} + PROVIDES=${PROVIDES:-} REPLACES=${REPLACES:-cloud-init} - POSTIN=${POSTINST:-postinstall.one} - PREUN=${PREUN:-preuninstall.one} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context-ec2} + POSTIN=${POSTINST:-postinstall} + PREUN=${PREUN:-preuninstall} ;; @@ -16,10 +18,12 @@ case "${TARGET}" in RELSUFFIX=${RELSUFFIX:-.el6} TYPE=${TYPE:-rpm} TAGS=${TAGS:-rpm sysv ec2} - DEPENDS=${DEPENDS:-util-linux-ng bind-utils cloud-utils-growpart dracut-modules-growroot ruby rubygem-json sudo shadow-utils} + DEPENDS=${DEPENDS:-util-linux-ng bash curl bind-utils cloud-utils-growpart dracut-modules-growroot ruby rubygem-json sudo shadow-utils} + PROVIDES=${PROVIDES:-} REPLACES=${REPLACES:-cloud-init} - POSTIN=${POSTINST:-postinstall.ec2} - PREUN=${PREUN:-preuninstall.ec2} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context} + POSTIN=${POSTINST:-postinstall.ec2 postinstall} + PREUN=${PREUN:-preuninstall.ec2 preuninstall} ;; 'el7') @@ -27,21 +31,25 @@ case "${TARGET}" in RELSUFFIX=${RELSUFFIX:-.el7} TYPE=${TYPE:-rpm} TAGS=${TAGS:-rpm systemd one} - DEPENDS=${DEPENDS:-util-linux bind-utils cloud-utils-growpart ruby rubygem-json sudo shadow-utils open-vm-tools qemu-guest-agent} + DEPENDS=${DEPENDS:-util-linux bash curl bind-utils cloud-utils-growpart ruby rubygem-json sudo shadow-utils open-vm-tools qemu-guest-agent} + PROVIDES=${PROVIDES:-} REPLACES=${REPLACES:-cloud-init} - POSTIN=${POSTINST:-postinstall.one} - PREUN=${PREUN:-preuninstall.one} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context-ec2} + POSTIN=${POSTINST:-postinstall} + PREUN=${PREUN:-preuninstall} ;; '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 sudo shadow-utils} + TAGS=${TAGS:-rpm systemd ec2} + DEPENDS=${DEPENDS:-util-linux bash curl bind-utils cloud-utils-growpart ruby rubygem-json sudo shadow-utils} + PROVIDES=${PROVIDES:-} REPLACES=${REPLACES:-cloud-init} - POSTIN=${POSTINST:-postinstall.ec2} - PREUN=${PREUN:-preuninstall.ec2} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context} + POSTIN=${POSTINST:-postinstall.ec2 postinstall} + PREUN=${PREUN:-preuninstall.ec2 preuninstall} ;; 'suse') @@ -49,32 +57,52 @@ case "${TARGET}" in RELSUFFIX=${RELSUFFIX:-.suse} TYPE=${TYPE:-rpm} TAGS=${TAGS:-rpm systemd one} - DEPENDS=${DEPENDS:-util-linux bind-utils growpart ruby sudo shadow open-vm-tools qemu-guest-agent} # rubygem-json} + DEPENDS=${DEPENDS:-util-linux bash curl bind-utils growpart ruby sudo shadow open-vm-tools qemu-guest-agent} # rubygem-json} + PROVIDES=${PROVIDES:-} + REPLACES=${REPLACES:-cloud-init cloud-init-config-suse} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context-ec2} + POSTIN=${POSTINST:-postinstall} + PREUN=${PREUN:-preuninstall} + ;; + + 'suse_ec2') + NAME=${NAME:-one-context-ec2} + RELSUFFIX=${RELSUFFIX:-.suse} + TYPE=${TYPE:-rpm} + TAGS=${TAGS:-rpm systemd ec2} + DEPENDS=${DEPENDS:-util-linux bash curl bind-utils growpart ruby sudo shadow} # rubygem-json} + PROVIDES=${PROVIDES:-} REPLACES=${REPLACES:-cloud-init cloud-init-config-suse} - POSTIN=${POSTINST:-postinstall.one} - PREUN=${PREUN:-preuninstall.one} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context} + POSTIN=${POSTINST:-postinstall.ec2 postinstall} + PREUN=${PREUN:-preuninstall.ec2 preuninstall} ;; + 'deb') NAME=${NAME:-one-context} RELSUFFIX=${RELSUFFIX:-} TYPE=${TYPE:-deb} TAGS=${TAGS:-deb sysv systemd upstart one} - DEPENDS=${DEPENDS:-util-linux bind9-host cloud-utils ruby python ifupdown acpid sudo passwd open-vm-tools qemu-guest-agent} + DEPENDS=${DEPENDS:-util-linux bash curl bind9-host cloud-utils ruby python ifupdown acpid sudo passwd open-vm-tools qemu-guest-agent} + PROVIDES=${PROVIDES:-} REPLACES=${REPLACES:-cloud-init} - POSTIN=${POSTINST:-postinstall.one} - PREUN=${PREUN:-preuninstall.one} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context-ec2} + POSTIN=${POSTINST:-postinstall} + PREUN=${PREUN:-preuninstall} ;; '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 ifupdown sudo passwd} + TAGS=${TAGS:-deb sysv systemd upstart ec2} + DEPENDS=${DEPENDS:-util-linux bash curl bind9-host cloud-utils ruby python ifupdown sudo passwd resolvconf} + PROVIDES=${PROVIDES:-} REPLACES=${REPLACES:-cloud-init} - POSTIN=${POSTINST:-postinstall.ec2} - PREUN=${PREUN:-preuninstall.ec2} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context} + POSTIN=${POSTINST:-postinstall.ec2 postinstall} + PREUN=${PREUN:-preuninstall.ec2 preuninstall} ;; 'alpine') @@ -82,16 +110,34 @@ case "${TARGET}" in RELSUFFIX=${RELSUFFIX:-} TYPE=${TYPE:-apk} TAGS=${TAGS:-apk one} - DEPENDS=${DEPENDS:-util-linux bash curl rsync udev iptables sfdisk e2fsprogs-extra open-vm-tools qemu-guest-agent keepalived quagga sudo shadow} - REPLACES=${REPLACES:-} - POSTIN=${POSTINST:-postinstall.one} - PREUN=${PREUN:-preuninstall.one} + DEPENDS=${DEPENDS:-util-linux bash curl rsync udev iptables sfdisk e2fsprogs-extra keepalived quagga sudo shadow open-vm-tools qemu-guest-agent} + PROVIDES=${PROVIDES:-} + REPLACES=${REPLACES:-} #not respected + CONFLICTS=${CONFLICTS:-one-context-ec2} + POSTIN=${POSTINST:-postinstall} + PREUN=${PREUN:-preuninstall} ;; + 'alpine_ec2') + NAME=${NAME:-one-context-ec2} + RELSUFFIX=${RELSUFFIX:-} + TYPE=${TYPE:-apk} + TAGS=${TAGS:-apk ec2} + DEPENDS=${DEPENDS:-util-linux bash curl rsync udev iptables sfdisk e2fsprogs-extra keepalived quagga sudo shadow} + PROVIDES=${PROVIDES:-} + REPLACES=${REPLACES:-} #not respected + CONFLICTS=${CONFLICTS:-one-context} + POSTIN=${POSTINST:-postinstall.ec2 postinstall} + PREUN=${PREUN:-preuninstall.ec2 preuninstall} + ;; + + 'arch') NAME=${NAME:-one-context} TYPE=${TYPE:-dir} TAGS=${TAGS:-arch one} + echo 'ArchLinux target is currently not maintained' + exit 1 ;; *)