Add a service enable/disable mechanism for upstart

Upstart doesn't have its own service enable/disable mechanism. We are in
need of a facility to enable/disable services without writing to /etc at
runtime to enable a readonly root for image based updates. This command
will be used by later changes to have a generic service enable/disable
mechanism for all init systems.

Change-Id: I931281988aa746f93de190794f68f4c9e628db19
This commit is contained in:
Clint Byrum 2013-12-05 12:19:00 -08:00
parent fd32965cd5
commit d359aed182
4 changed files with 99 additions and 0 deletions

View File

@ -6,6 +6,13 @@ Given a git repo url, pip-install the repo and all of its python dependencies in
## os-svc-daemon
Given a system service command line and run-as user, generate and install system service start script. See output of `os-svc-daemon -h` for online help.
## os-svc-enable-upstart
Given an upstart job and an action, acts on the enabled or disabled state
of jobs produced by os-svc-daemon. This requires the os-svc-enable upstart
job which is installed by this element as well. There is also an action,
'enabled', which allows checking whether or not a service is enabled;
the command exits 0 if it is enabled, or 1 if it is not. A disabled
service will not be started automatically nor can it be manually started.
## example usage
```bash

View File

@ -0,0 +1,75 @@
#!/bin/bash
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# 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 -eu
job_name=${1:-}
action=${2:-}
function usage {
echo "usage: $(basename $0) job_name [enable|disable|enabled|clear]"
}
if [ -z "$job_name" ] || [ -z "$action" ] ; then
usage
exit 1
fi
OS_UPSTART_STATE_DIR=${OS_UPSTART_STATE_DIR:-/var/lib/os-svc-enable-upstart}
if [ "$action" != "enabled" ] ; then
if ! [ -d "$OS_UPSTART_STATE_DIR" ] ; then
if ! mkdir -p $OS_UPSTART_STATE_DIR ; then
echo "ERROR: $OS_UPSTART_STATE_DIR does not exist or is not a directory."
exit 1
fi
fi
if ! [ -w "$OS_UPSTART_STATE_DIR" ] ; then
if ! chmod +w $OS_UPSTART_STATE_DIR ; then
echo "ERROR: $OS_UPSTART_STATE_DIR is not writable."
exit 1
fi
fi
fi
if ! [ -e "/etc/init/${job_name}.conf" ] ; then
echo "WARNING: $job_name does not exist!"
fi
enable_file="${OS_UPSTART_STATE_DIR}/${job_name}.enable"
case $action in
clear)
rm -f $enable_file
;;
enable)
# Upstart jobs can have sub directories
enable_file_home=$(dirname $enable_file)
mkdir -p $enable_file_home
touch $enable_file
;;
disable)
rm -f $enable_file
;;
enabled)
if [ -e "$enable_file" ] ; then
exit 0
fi
exit 1
;;
esac

View File

@ -12,5 +12,9 @@ pip install -U virtualenv
install -m 0755 -o root -g root $(dirname $0)/../bin/os-svc-install /usr/local/bin/os-svc-install
install -m 0755 -o root -g root $(dirname $0)/../bin/os-svc-daemon /usr/local/bin/os-svc-daemon
install -m 0755 -o root -g root $(dirname $0)/../bin/os-db-create /usr/local/bin/os-db-create
if [ "$(dib-init-system)" = "upstart" ] ; then
install -m 0755 -o root -g root $(dirname $0)/../bin/os-svc-enable-upstart /usr/local/bin/os-svc-enable-upstart
install -m 0644 -o root -g root $(dirname $0)/../upstart/os-svc-enable.conf /etc/init/os-svc-enable.conf
fi
install -m 0755 -o root -g root -d /opt/stack/venvs

View File

@ -0,0 +1,13 @@
# vim:set syntax=upstart:
#
description "TripleO Service Control Job"
start on starting OS_SVC_ENABLE_CONTROL=1
instance $JOB
task
console none
script
# --no-wait is extremely important as $JOB is already blocked on
# this job. Thus the change to the stop goal will be delayed until
# this job exits.
os-svc-enable-upstart $JOB enabled || exec stop --no-wait $JOB
end script