Reorder network script to get network-manager out of the way

This stops network-manager (and networking) as early as
possible and starts these as late as possible.  This is
important because different machines have different
network configurations that may be unpredictable, and
network-manager could cause interference as we're
modifying various network config parameters.

The safest thing to do is push these services out of
the way for as long as we can.

BUG: 143727596
Test: local run
Change-Id: I31954addff5bc6817fe77076959ef2120c84b623
diff --git a/tools/network-setup.sh b/tools/network-setup.sh
index 621692e..019c193 100755
--- a/tools/network-setup.sh
+++ b/tools/network-setup.sh
@@ -24,6 +24,25 @@
 	exec sudo bash "$0"
 fi
 
+cleanup() {
+	echo "Starting up network-manager..."
+	service network-manager start
+	if [ $? != 0 ]; then
+		echo "error: failed to start network-manager"
+		exit 1
+	fi
+
+	echo "Starting up networking..."
+	service networking start
+	if [ $? != 0 ]; then
+		echo "error: failed to start networking"
+		exit 1
+	fi
+	if [ ! -z "$1" ]; then
+		exit $1
+	fi
+}
+
 sleep_time=0.1
 max_attempts=100
 DEFAULTNET=$1
@@ -116,44 +135,25 @@
 echo "Found Rock Pi network at ${ROCKNET}"
 sudo ifconfig ${ROCKNET} down
 
-echo "Configuring udev rules..."
-cat >/etc/udev/rules.d/82-${ROCKNET}.rules <<EOF
-ACTION=="add", SUBSYSTEM=="net", KERNEL=="${ROCKNET}", ENV{NM_UNMANAGED}="1"
-EOF
+echo "Downloading dnsmasq..."
+apt-get install -d -y dnsmasq >/dev/null
 
-echo "Configuring network interface..."
-cat >/etc/network/interfaces.d/${ROCKNET}.conf <<EOF
-auto ${ROCKNET}
-iface ${ROCKNET} inet static
-	address 192.168.0.1
-	netmask 255.255.255.0
-EOF
-
-echo "Restarting network interface..."
-service network-manager restart
+echo "Shutting down network-manager to prevent interference..."
+service network-manager stop
 if [ $? != 0 ]; then
-	echo "error: failed to restart network-manager"
-	exit 1
-fi
-service networking restart
-if [ $? != 0 ]; then
-	echo "error: failed to restart networking"
-	exit 1
+	echo "error: failed to stop network-manager"
+	cleanup 1
 fi
 
-# Verify the Rock Pi was configured correctly
-ip link show ${ROCKNET} >/dev/null
+echo "Shutting down networking to prevent interference..."
+service networking stop
 if [ $? != 0 ]; then
-	echo "error: wasn't able to successfully configure connection to Rock Pi"
-	exit 1
+	echo "error: failed to stop networking"
+	cleanup 1
 fi
 
-# Check if dnsmasq is already installed
-dpkg -l | grep " dnsmasq " >/dev/null
-if [ $? != 0 ]; then
-	echo "Installing dnsmasq..."
-	apt-get install dnsmasq >/dev/null
-fi
+echo "Installing dnsmasq..."
+apt-get install dnsmasq >/dev/null
 
 echo "Enabling dnsmasq daemon..."
 cat /etc/default/dnsmasq | grep "ENABLED" >/dev/null
@@ -174,12 +174,18 @@
 dhcp-range=192.168.0.100,192.168.0.199
 EOF
 
-echo "Restarting dnsmasq service..."
-service dnsmasq restart
-if [ $? != 0 ]; then
-	echo "error: failed to restart dnsmasq"
-	exit 1
-fi
+echo "Configuring udev rules..."
+cat >/etc/udev/rules.d/82-${ROCKNET}.rules <<EOF
+ACTION=="add", SUBSYSTEM=="net", KERNEL=="${ROCKNET}", ENV{NM_UNMANAGED}="1"
+EOF
+
+echo "Configuring network interface..."
+cat >/etc/network/interfaces.d/${ROCKNET}.conf <<EOF
+auto ${ROCKNET}
+iface ${ROCKNET} inet static
+	address 192.168.0.1
+	netmask 255.255.255.0
+EOF
 
 echo "Enabling IP forwarding..."
 echo 1 >/proc/sys/net/ipv4/ip_forward
@@ -217,6 +223,22 @@
 sudo systemctl enable iptables-rockpi
 sudo systemctl start iptables-rockpi
 
+cleanup
+
+echo "Restarting dnsmasq service..."
+service dnsmasq restart
+if [ $? != 0 ]; then
+	echo "error: failed to restart dnsmasq"
+	exit 1
+fi
+
+# Verify the Rock Pi was configured correctly
+ip link show ${ROCKNET} >/dev/null
+if [ $? != 0 ]; then
+	echo "error: wasn't able to successfully configure connection to Rock Pi"
+	exit 1
+fi
+
 echo "Searching for Rock Pi's IP address..."
 while true; do
 	rockip=`cat /proc/net/arp | grep ${ROCKNET} | grep -v 00:00:00:00:00:00 | cut -d" " -f1`