Skip to content

Commit

Permalink
add: service functions (closes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
metaist committed Mar 30, 2016
1 parent 7377e99 commit 31fde4b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/ib-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

### service - manage background services

# Install a service.
# Args:
# 1: label
# 2: quiet
# 3: name
ib-service-install() {
local label=${1:-''}
local quiet=${2:-''}
local name=${3:-''}
local skip=$(ib-ok? stat -t /etc/rc*/???$name 2>> /dev/null)
ib-action -l "$label" -s "$skip" $quiet -- update-rc.d "$name" defaults 98 02
}

# Set the service state.
# Args:
# 1: label
# 2: quiet
# 3: name
# 4: state
ib-service-state() {
local label=${1:-"[service] service $3 $4"}
local quiet=${2:-''}
local name=${3:-''}
local state=${4:-''}
local status=
local skip=false
local tried=false
local value=0

if ib-falsy? "$quiet"; then ib-action-start "$label"; fi

service postgresql status 2&>/dev/null
status=$?
case "$state" in
start) skip=$(ib-ok? [[ $status == 0 ]]);;
stop) skip=$(ib-ok? [[ $status != 0 ]]);;
restart) skip=
esac

if ib-falsy? $skip; then
tried=true
echo -e "\n\$ service $name $state" >> $IB_LOG
service $name $state &>> $IB_LOG
sleep 0.3
service $name status 2&> /dev/null
value=$?
if [[ "$state" == "stop" ]]; then
value=$([[ "$value" != 0 ]] && echo 0);
fi
fi

if ib-falsy? "$quiet"; then ib-action-stop "$label" "$tried" "$value"; fi
}
22 changes: 22 additions & 0 deletions test/ib-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
source "../src/$(basename ${BASH_SOURCE[0]})"

setup() { true; }

teardown() { true; }

# TODO: add service install test
test-ib-service-install() { true; }

test-ib-service-state() {
local status

ib service-state -q "apache2" "stop"

service $name status 2&> /dev/null
ib-assert-true [[ "$?" != "0" ]]

ib service-state -q "apache2" "start"
service $name status 2&> /dev/null
sleep 0.3
ib-assert-true [[ "$?" == "0" ]]
}

0 comments on commit 31fde4b

Please sign in to comment.