Skip to content

Commit

Permalink
[script] add flags support (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
bukepo authored and jwhui committed Aug 15, 2017
1 parent 5a5cb82 commit b8e6540
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 24 deletions.
30 changes: 30 additions & 0 deletions examples/platforms/fedora/default
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh
#
# Copyright (c) 2017, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

NAT64=0
30 changes: 30 additions & 0 deletions examples/platforms/ubuntu/default
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh
#
# Copyright (c) 2017, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

NAT64=1
56 changes: 54 additions & 2 deletions script/_initrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,60 @@ die()
exit 1
}

have()
{
# This function checks if a tool is available
#

which "$1" > /dev/null 2> /dev/null
}

have_or_die()
{
# This function verifies a tool is available and dies with proper
# information if not available.
#

have "$1" || die "$1 not available!"
}

with()
{
# This function verifies a flag is on.
#
# NOTE environment settings takes higher priority than default files.
#

VALUE=$(printenv "$1")
if test -z "$VALUE"; then
eval VALUE="\$$1"
fi

test "$VALUE" = 1
}

without()
{
# This function verifies a flag is off.
#
# NOTE environment settings takes higher priority than default files.
#

! with "$1"
}

# Platform information is needed to load hooks and default settings.
#
if test -z "$PLATFORM"; then
have_or_die lsb_release
PLATFORM=$(lsb_release -i | cut -c17- | tr '[:upper:]' '[:lower:]')
fi
echo "Current platform is $PLATFORM"

if test -f examples/platforms/$PLATFORM/default; then
. examples/platforms/$PLATFORM/default
fi

STAGE_DIR=$(pwd)/._stage
BUILD_DIR=$(pwd)/._build

Expand All @@ -44,9 +98,7 @@ BUILD_DIR=$(pwd)/._build

export PATH=$STAGE_DIR/usr/bin:$STAGE_DIR/usr/sbin:$PATH

PLATFORM=$(lsb_release -i | cut -c17- | tr '[:upper:]' '[:lower:]')
TASKNAME=$(basename $0)
echo "Current platform is $PLATFORM"
BEFORE_HOOK=examples/platforms/$PLATFORM/before_$TASKNAME
AFTER_HOOK=examples/platforms/$PLATFORM/after_$TASKNAME
if test ! -f $BEFORE_HOOK; then
Expand Down
22 changes: 17 additions & 5 deletions script/_nat64
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ TAYGA_IPV6_ADDR=fdaa:bb:1::1
NAT44_SERVICE=/etc/init.d/otbr-nat44
WLAN_IFNAMES=eth0

# Currently only ubuntu is verified.
#
without NAT64 || test $PLATFORM = ubuntu || die "nat64 is not tested under $PLATFORM."

nat64_install()
{
with NAT64 || return 0

test -f $TAYGA_DEFAULT -a -f $TAYGA_CONF || die 'Cannot find tayga configuration file!'
sudo sed -i 's/^RUN="no"/RUN="yes"/' $TAYGA_DEFAULT
sudo sed -i 's/^prefix /##prefix /' $TAYGA_CONF
Expand Down Expand Up @@ -110,42 +116,48 @@ EOF
esac
EOF
sudo chmod a+x $NAT44_SERVICE
if which systemctl; then
if have systemctl; then
sudo systemctl stop tayga || die 'Unable to stop tayga service!'
sudo systemctl enable otbr-nat44 || die 'Unable to enable nat44 service!'
fi
}

nat64_uninstall()
{
with NAT64 || return 0

nat64_stop
sudo sed -i 's/^RUN="yes"/RUN="no"/' $TAYGA_DEFAULT
sudo sed -i 's/^prefix 64:ff9b::\/96/# prefix 64:ff9b::\/96/' $TAYGA_CONF
sudo sed -i 's/^##prefix /prefix /' $TAYGA_CONF
sudo sed -i '/^ipv6-addr '$TAYGA_IPV6_ADDR'/d' $TAYGA_CONF

if which systemctl; then
if have systemctl; then
sudo systemctl disable otbr-nat44 || true
fi

# systemctl disable doesn't remove sym-links
if which update-rc.d; then
if have update-rc.d; then
sudo update-rc.d otbr-nat44 remove || true
fi
test ! -f $NAT44_SERVICE || sudo rm $NAT44_SERVICE
}

nat64_start()
{
if which systemctl; then
with NAT64 || return 0

if have systemctl; then
sudo systemctl start tayga || die 'Failed to start tayga!'
sudo systemctl start otbr-nat44 || die 'Failed to start NAT44!'
fi
}

nat64_stop()
{
if which systemctl; then
with NAT64 || return 0

if have systemctl; then
sudo systemctl stop tayga || true
sudo systemctl stop otbr-nat44 || true
fi
Expand Down
6 changes: 3 additions & 3 deletions script/_otbr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

otbr_uninstall()
{
if which systemctl; then
if have systemctl; then
sudo systemctl stop otbr-web otbr-agent wpantund || true
sudo systemctl disable otbr-web otbr-agent wpantund || true
! sudo systemctl is-enabled otbr-web
Expand All @@ -39,7 +39,7 @@ otbr_uninstall()
sudo killall otbr-web otbr-agent || true

test ! -f config.status || sudo make uninstall
if which systemctl; then
if have systemctl; then
sudo systemctl daemon-reload
fi
}
Expand All @@ -49,7 +49,7 @@ otbr_install()
test -f config.status || ./configure --prefix=/usr --sysconfdir=/etc
make -j$(getconf _NPROCESSORS_ONLN)
sudo make install
if which systemctl; then
if have systemctl; then
sudo systemctl reload dbus
sudo systemctl daemon-reload
sudo systemctl enable wpantund otbr-web otbr-agent || true
Expand Down
24 changes: 12 additions & 12 deletions script/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ install_packages_apt()
sudo apt-get update
sudo apt-get install -y build-essential libtool git

test "$RELEASE" = 1 || sudo apt-get install -y cmake
with RELEASE || sudo apt-get install -y cmake

# For wpantund
sudo apt-get install -y libdbus-1-dev
Expand All @@ -60,42 +60,42 @@ install_packages_apt()
sudo apt-get install -y libboost-dev libboost-filesystem-dev libboost-system-dev

# nat64
sudo apt-get install -y tayga iptables
without NAT64 || sudo apt-get install -y tayga iptables

# libjsoncpp
sudo apt-get install -y libjsoncpp-dev
}

install_packages_opkg()
{
echo 'opkg not supported currently' && false
die 'opkg not supported currently'
}

install_packages_rpm()
{
echo 'rpm not supported currently' && false
die 'rpm not supported currently'
}

install_packages_brew()
{
echo 'macOS not supported currently' && false
die 'macOS not supported currently'
}

install_packages_source()
{
echo 'source not supported currently' && false
die 'source not supported currently'
}

install_packages()
{
PM=source
if which apt-get; then
if have apt-get; then
PM=apt
elif which rpm; then
elif have rpm; then
PM=rpm
elif which opkg; then
elif have opkg; then
PM=opkg
elif which brew; then
elif have brew; then
PM=brew
fi
install_packages_$PM
Expand Down Expand Up @@ -145,8 +145,8 @@ main()
{
. $BEFORE_HOOK
install_packages
test "$RELEASE" = 1 || install_uncrustify
test "$RELEASE" = 1 || install_cpputest
with RELEASE || install_uncrustify
with RELEASE || install_cpputest
./bootstrap
. $AFTER_HOOK
}
Expand Down
2 changes: 1 addition & 1 deletion script/console
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ on_exit()
main()
{
. $BEFORE_HOOK
if which systemctl; then
if have systemctl; then
sudo systemctl stop otbr-web otbr-agent wpantund || true
fi
killall_services
Expand Down
2 changes: 1 addition & 1 deletion script/server
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ main()
. $BEFORE_HOOK
sudo sysctl --system
nat64_start || die 'Failed to start NAT64!'
if which systemctl; then
if have systemctl; then
systemctl is-active wpantund || sudo systemctl start wpantund || die 'Failed to start wpantund!'
systemctl is-active otbr-web || sudo systemctl start otbr-web || die 'Failed to start otbr-web!'
systemctl is-active otbr-agent || sudo systemctl start otbr-agent || die 'Failed to start otbr-agent!'
Expand Down

0 comments on commit b8e6540

Please sign in to comment.