From db1afd6420a63780c2f1d22e27038b0fad7e1251 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 27 Jun 2015 12:49:23 -0400 Subject: [PATCH] Move init scripts and installation of them in tree glean is not complete without the simple-init script and init scripts. Move them here. Also, rename them to glean. Change-Id: I2ed25ce434023bfc8b6a88a08c0c06c1cef63982 --- glean/init/__init__.py | 0 glean/init/glean-udev.rules | 1 + glean/init/glean.conf | 15 +++++++++++ glean/init/glean.init | 31 ++++++++++++++++++++++ glean/init/glean.sh | 53 +++++++++++++++++++++++++++++++++++++ glean/init/glean@.service | 15 +++++++++++ glean/install.py | 47 ++++++++++++++++++++++++++++++++ setup.cfg | 5 ++++ 8 files changed, 167 insertions(+) create mode 100644 glean/init/__init__.py create mode 100644 glean/init/glean-udev.rules create mode 100644 glean/init/glean.conf create mode 100755 glean/init/glean.init create mode 100755 glean/init/glean.sh create mode 100644 glean/init/glean@.service create mode 100755 glean/install.py diff --git a/glean/init/__init__.py b/glean/init/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/glean/init/glean-udev.rules b/glean/init/glean-udev.rules new file mode 100644 index 0000000..ffef97d --- /dev/null +++ b/glean/init/glean-udev.rules @@ -0,0 +1 @@ +SUBSYSTEM=="net", ACTION=="add", TAG+="systemd", ENV{SYSTEMD_WANTS}+="glean@$name.service" diff --git a/glean/init/glean.conf b/glean/init/glean.conf new file mode 100644 index 0000000..8438c11 --- /dev/null +++ b/glean/init/glean.conf @@ -0,0 +1,15 @@ +# Call a script to generate a /etc/network/interfaces file to DHCP all available interfaces +# Then remove this config file so the script is never run again + +description "Run glean to configure network interfaces" + +# console output connects stdout/stderr to the console log. This is important +# for debuggability of cloud images that wind up not booting +console output + +start on starting network-interface +instance $INTERFACE + +task + +exec /usr/local/bin/glean.sh $INTERFACE diff --git a/glean/init/glean.init b/glean/init/glean.init new file mode 100755 index 0000000..54bb88b --- /dev/null +++ b/glean/init/glean.init @@ -0,0 +1,31 @@ +#!/bin/sh -e +### BEGIN INIT INFO +# Provides: glean +# Required-Start: $local_fs +# Required-Stop: $local_fs +# Default-Start: S +# Default-Stop: 0 6 +# X-Start-Before: networking +# Short-Description: Autodetect network interfaces +# Description: Autodetect network interfaces during boot and configure them for DHCP +### END INIT INFO + +NAME=glean +INIT_NAME=/etc/init.d/${NAME} +SCRIPT_NAME=/usr/local/bin/${NAME}.sh + +[ -x $SCRIPT_NAME ] || exit 0 + +case "$1" in + start) + $SCRIPT_NAME + ;; + stop) + ;; + *) + echo "Usage: $INIT_NAME {start|stop}" + exit 1 + ;; +esac + +exit 0 diff --git a/glean/init/glean.sh b/glean/init/glean.sh new file mode 100755 index 0000000..7737c50 --- /dev/null +++ b/glean/init/glean.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# 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. + +# dib-lint: disable=dibdebugtrace +set -eu +set -o pipefail + +PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin +INTERFACE=${1:-} #optional, if not specified configure all available interfaces + +function config_exists() { + local interface=$1 + if [ "$CONF_TYPE" == "netscripts" ]; then + if [ -f "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then + return 0 + fi + else + ifquery $interface >/dev/null 2>&1 && return 0 || return 1 + fi + return 1 +} + + +# Test to see if config-drive exists. If not, skip and assume DHCP networking +# will work becasue sanity +if blkid -t LABEL="config-2" ; then + # Mount config drive + mkdir -p /mnt/config + mount -o mode=0700 $(blkid -t LABEL="config-2" | cut -d ':' -f 1) /mnt/config || true + glean --ssh --skip-network --hostname +fi + +if [ -f /usr/bin/dpkg ] ; then + test -f /etc/ssh/ssh_host_rsa_key || dpkg-reconfigure openssh-server +fi + +if [ -n "$INTERFACE" ]; then + glean --interface $INTERFACE +else + glean +fi diff --git a/glean/init/glean@.service b/glean/init/glean@.service new file mode 100644 index 0000000..e58b057 --- /dev/null +++ b/glean/init/glean@.service @@ -0,0 +1,15 @@ +[Unit] +Description=DHCP interface %I +After=network.service network.target + +ConditionPathExists=!/etc/sysconfig/network-scripts/ifcfg-%I + +[Service] +Type=oneshot +User=root +ExecStartPre=/usr/local/bin/glean.sh %I +ExecStart=/sbin/ifup %I +RemainAfterExit=true + +[Install] +WantedBy=multi-user.target diff --git a/glean/install.py b/glean/install.py new file mode 100755 index 0000000..b2797ab --- /dev/null +++ b/glean/install.py @@ -0,0 +1,47 @@ +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# 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. + +import os + + +def install(source_file, target_file, mode='0755'): + script_dir = os.path.join( + os.path.dirname(os.path.realpath(__file__)), 'init') + os.system( + 'install -D -g root -o root' + ' -m {mode} {source_file} {target_file}'.format( + source_file=os.path.join(script_dir, source_file), + target_file=target_file, + mode=mode)) + + +def main(): + + if os.path.exists('/etc/init'): + install('glean.conf', '/etc/init/glean.conf') + elif os.path.exists('/usr/lib/systemd'): + install( + 'glean@.service', + '/usr/lib/systemd/system/glean@.service') + install( + 'glean-udev.rules', + '/etc/udev/rules.d/99-glean.rules', + mode='0644') + else: + install('glean.init', '/etc/init.d/glean') + os.system('update-rc.d glean defaults') + +if __name__ == '__main__': + main() diff --git a/setup.cfg b/setup.cfg index 7a91cb6..1e1a6fc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,6 +23,11 @@ classifier = [entry_points] console_scripts = glean = glean.cmd:main + glean-install = glean.install:main + +[files] +scripts = + glean/init/glean.sh [build_sphinx] source-dir = doc/source