Topology cleanup fix + fix for requirements + fix for local physical servers 86/5986/1
authorMarcel Enguehard <[email protected]>
Fri, 31 Mar 2017 16:34:54 +0000 (18:34 +0200)
committerMarcel Enguehard <[email protected]>
Fri, 31 Mar 2017 16:34:54 +0000 (18:34 +0200)
Change-Id: Ifd090122348f362a65e5d86ce62784be2821d12b
Signed-off-by: Marcel Enguehard <[email protected]>
requirements.txt
scripts/topo_cleanup.sh [new file with mode: 0755]
vicn/resource/linux/physical.py

index c287a0c..77b44ea 100644 (file)
@@ -4,3 +4,4 @@ ws4py==0.3.4
 
 pyparsing
 lockfile
+autobahn
diff --git a/scripts/topo_cleanup.sh b/scripts/topo_cleanup.sh
new file mode 100755 (executable)
index 0000000..c2e60b8
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+if [ "$#" -ne 1 ]; then
+       echo "usage: ./topo_cleanup.sh [topology_file.json]"
+       exit 1
+fi
+
+TOPOLOGY=$1
+
+function get_by_type()
+{
+       echo $(python3 -c "import json, pprint; j = json.loads(open('$1').read()); print(' '.join(r['name'] for r in j['resources'] if r['type'] == '$2'))")
+}
+
+#grep configuration from JSON
+SERVERS=$(get_by_type $TOPOLOGY LxcContainer)
+NETWORK=$(sed -n '/network/{s/.*"\([^"]*\)"$/\1/;p}' $TOPOLOGY)
+
+# delete spurious interfaces on host
+route -n | awk '{print $8}' | tail -n -2 | grep vh | sort | uniq | xargs
+
+# Remove containers
+for server in $SERVERS; do
+       (lxc stop $server; lxc delete $server) &
+done
+wait
+
+#Kill the emulators
+killall -9 wifi_emulator
+killall -9 lte_emulator
+kill -9 $(ps aux | awk  '/\/usr\/bin\/python3 \/usr\/bin\/netmon/ {print $2}')
+service netmon stop
+
+# Clean ports on OVS
+for x in $(ovs-vsctl show | sed -n '/Bridge "br0"/,/Bridge/p' | grep "No such device" | sed "s/.*device \(.*\) (No such device).*/\1/"  ); do echo $x; ovs-vsctl del-port br0 $x; done
+
+for i in $(ip link | grep unk | cut -d: -f 2 | cut -d @ -f 1); do ip link delete $i; done
+for i in $(ip link | grep tmp-veth | cut -d: -f 2 | cut -d @ -f 1); do ip link delete $i; done
+for i in $(ip link | grep tap- | cut -d: -f 2 | cut -d @ -f 1); do ip link delete $i; done
+for i in $(ip link | grep mv- | cut -d: -f 2 | cut -d @ -f 1); do ip link delete $i; done
+
+# Remove bridge
+echo "Removing bridge..."
+ovs-vsctl --if-exists del-br br0
+
+# Remove taps
+echo "Removing interface..."
+for i in $(ip link show | egrep "(tap|macvlan|macvtap)" | cut -d : -f 2 | cut -d @ -f 1); do echo " - delete $i"; ip link delete $i; done
+
+#Remove routes
+echo "Removing stale routes"
+NETWORK=$(echo $NETWORK | cut -d'/' -f1 | rev | cut -d"." -f2- | rev | sed "s/\./\\\\./g")
+eval $(ip route show | sed -n '/$NETWORK\./ {s/^\(.*\) dev \(.*\)  scope link.*$/route del \1 dev \2;/;p}')
index 8decb51..d7c0b51 100644 (file)
@@ -28,7 +28,7 @@ from netmodel.util.socket           import check_port
 from vicn.core.attribute            import Attribute
 from vicn.core.commands             import Command, ReturnValue
 from vicn.core.exception            import ResourceNotFound, VICNException
-from vicn.core.task                 import Task, task
+from vicn.core.task                 import Task, task, EmptyTask
 from vicn.resource.linux.keypair    import Keypair
 from vicn.resource.node             import Node, DEFAULT_USERNAME
 from vicn.resource.node             import DEFAULT_SSH_PUBLIC_KEY
@@ -39,7 +39,7 @@ log = logging.getLogger(__name__)
 CMD_SSH_COPY_ID = 'ssh-copy-id {ssh_options} -i {public_key} -p {port} ' \
                   '{user}@{host}'
 CMD_SSH = 'ssh {ssh_options} -i {private_key} -p {port} ' \
-          '{user}@{host} {command}' 
+          '{user}@{host} {command}'
 CMD_SSH_NF = 'ssh -n -f {ssh_options} -i {private_key} -p {port} ' \
              '{user}@{host} {command}'
 
@@ -78,18 +78,21 @@ class Physical(Node):
         return Keypair(node = None, key = FN_KEY)
 
     def __initialize__(self):
-        """
-        Initialization require the ssh port to be open on the node, and the ssh
-        public key to be copied on the remote node.
-        """
-        if not check_port(self.hostname, self.ssh_port):
-            raise VICNException
-
-        tasks = list()
-        modes = (True, False) if DEFAULT_USERNAME != 'root' else (True,) 
-        for as_root in modes:
-            tasks.append(self._setup_ssh_key(as_root))
-        return Task.__concurrent__(*tasks)
+        if not is_local_host(self.hostname):
+            """
+            Initialization require the ssh port to be open on the node, and the ssh
+            public key to be copied on the remote node.
+            """
+            if not check_port(self.hostname, self.ssh_port):
+                raise VICNException
+
+            tasks = list()
+            modes = (True, False) if DEFAULT_USERNAME != 'root' else (True,)
+            for as_root in modes:
+                tasks.append(self._setup_ssh_key(as_root))
+            return Task.__concurrent__(*tasks)
+        else:
+            return EmptyTask()
 
     __delete__ = None
 
@@ -103,12 +106,12 @@ class Physical(Node):
         os.chmod(DEFAULT_SSH_PRIVATE_KEY, stat.S_IRUSR | stat.S_IWUSR)
         cmd_params = {
             'public_key'    : DEFAULT_SSH_PUBLIC_KEY,
-            'ssh_options'   : '', 
+            'ssh_options'   : '',
             'port'          : self.ssh_port,
             'user'          : 'root' if as_root else DEFAULT_USERNAME,
             'host'          : self.hostname,
         }
-        
+
         c = Command(CMD_SSH_COPY_ID, parameters = cmd_params)
 
         return self._do_execute_process(c.full_commandline, output=False)
@@ -126,18 +129,18 @@ class Physical(Node):
         p.wait()
         return ReturnValue(p.returncode)
 
-    def _do_execute_ssh(self, command, output=False, as_root=False, 
+    def _do_execute_ssh(self, command, output=False, as_root=False,
             ssh_options=None):
         if not ssh_options:
             ssh_options = dict()
         cmd_params = {
             'private_key'   : DEFAULT_SSH_PRIVATE_KEY,
-            'ssh_options'   : ' '.join(['-o {}={}'.format(k, v) 
+            'ssh_options'   : ' '.join(['-o {}={}'.format(k, v)
                     for k, v in ssh_options.items()]),
             'port'          : self.ssh_port,
             'user'          : 'root' if as_root else DEFAULT_USERNAME,
             'host'          : self.hostname,
-            'command'       : shlex.quote(command), 
+            'command'       : shlex.quote(command),
         }
 
         if (command[-1] != '&'):
@@ -151,6 +154,6 @@ class Physical(Node):
         if is_local_host(self.hostname):
             rv = self._do_execute_process(command, output = output)
         else:
-            rv = self._do_execute_ssh(command, output = output, 
+            rv = self._do_execute_ssh(command, output = output,
                     as_root = as_root)
         return rv