From f2ab0a0de9fe0d0fdf7c6a4262f22fe4b76843d8 Mon Sep 17 00:00:00 2001
From: Attila Farkas <x394097@fi.muni.cz>
Date: Thu, 3 Oct 2019 09:38:20 +0200
Subject: [PATCH] add interface naming

---
 README.md                         |  4 ++++
 generate.py                       |  1 +
 modules/ansible_data_generator.py | 18 +++++++++++++++++-
 modules/file_generator.py         |  6 +++---
 name_mapping/interface.yml        |  3 +++
 templates/device_configuration    |  2 +-
 test.yml                          |  4 ++--
 7 files changed, 31 insertions(+), 7 deletions(-)
 create mode 100644 name_mapping/interface.yml

diff --git a/README.md b/README.md
index 72f8155..1ac4f7b 100644
--- a/README.md
+++ b/README.md
@@ -64,6 +64,10 @@ Flavors provide a quick way to choose hardware specs (like number of cpus and me
 | csirtmu.jumbo16x32 | 16 | 32768 |
 | csirtmu.jumbo16x64 | 16 | 65536 |
 
+### Interface names and supported boxes
+
+The name of the correct network interface can be different for every box. The supported boxes are listed in the file name_mappings/interface.yml. If your box is not on the list, the most common "eth1" will be used. If you get an error during `$ vagrant up` about non-existing network interface, you can add the name of your box and the correct network interface to the list manually.
+
 ### Implemented attribute types:
 - all simple vagrant attributes
 - flavors, memory, cpus
diff --git a/generate.py b/generate.py
index 99a9e96..40a6116 100644
--- a/generate.py
+++ b/generate.py
@@ -15,3 +15,4 @@ device_definitions = open_file(str(sys.argv[1]))
 
 generate_vagrantfile(device_definitions)
 generate_ansible_files(device_definitions)
+
diff --git a/modules/ansible_data_generator.py b/modules/ansible_data_generator.py
index 1768e83..e585ea1 100644
--- a/modules/ansible_data_generator.py
+++ b/modules/ansible_data_generator.py
@@ -1,3 +1,7 @@
+from modules.device_creator import open_file
+
+INTERFACE_FILE = "name_mapping/interface.yml"
+
 def create_network_map(net_mappings, router_mappings):
     """ Creates a structure with network topology for Jinja2 template. """
 
@@ -18,7 +22,18 @@ def _find_router_ip(network_name, router_mappings):
             return router_mapping["ip"]
 
 
-def create_host_map(net_mappings, router_mappings):
+def _find_interface(host_name, hosts):
+
+    for host in hosts:
+        if host["name"] == host_name:
+            interfaces = open_file(INTERFACE_FILE)
+            if host["base_box"] in interfaces:
+                return interfaces[host["base_box"]]
+
+    return "eth1"
+
+
+def create_host_map(net_mappings, router_mappings, host_list):
     """ Creates a structure with hosts and their primary routers ip """
 
     hosts = []
@@ -29,5 +44,6 @@ def create_host_map(net_mappings, router_mappings):
         host["host_ip"] = net_mapping["ip"]
         host["router_ip"] = _find_router_ip(
             net_mapping["network"], router_mappings)
+        host["interface"] = _find_interface(net_mapping["host"], host_list)
         hosts.append(host)
     return hosts
diff --git a/modules/file_generator.py b/modules/file_generator.py
index e956770..8e7d1f8 100644
--- a/modules/file_generator.py
+++ b/modules/file_generator.py
@@ -72,7 +72,7 @@ def _generate_playbook():
 def _generate_device_configuration(definitions):
     """ Generates a playbook with basic device configutarion. """
 
-    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"])
+    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
     network = create_network_map(
             definitions["net_mappings"], definitions["router_mappings"]) 
 
@@ -90,7 +90,7 @@ def _generate_device_configuration(definitions):
 def _generate_hosts_role(definitions):
     """ Generates hosts role. """
 
-    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"])
+    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
 
     network = create_network_map(
             definitions["net_mappings"], definitions["router_mappings"])
@@ -109,7 +109,7 @@ def _generate_routers_role(definitions):
         print("Info: No router definition was found. Skipping router creation.")
         return
 
-    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"])
+    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
 
     network = create_network_map(
             definitions["net_mappings"], definitions["router_mappings"]) 
diff --git a/name_mapping/interface.yml b/name_mapping/interface.yml
new file mode 100644
index 0000000..0664b76
--- /dev/null
+++ b/name_mapping/interface.yml
@@ -0,0 +1,3 @@
+kalilinux/rolling-light: eth1
+xubuntu-16.04-desktop-amd64: eth1
+ubuntu/xenial64: enp0s8
diff --git a/templates/device_configuration b/templates/device_configuration
index cfc241e..73a852f 100644
--- a/templates/device_configuration
+++ b/templates/device_configuration
@@ -13,7 +13,7 @@
   become: yes
   tasks:
   - name: Change default gateway
-    command: route add default gw {{ host.router_ip }} eth1
+    command: route add default gw {{ host.router_ip }} {{ host.interface }}
 
 {% endfor %}
 - name: Configuring all routers
diff --git a/test.yml b/test.yml
index 69d11fc..c53b54d 100644
--- a/test.yml
+++ b/test.yml
@@ -1,12 +1,12 @@
 name: small-sandbox
 hosts:
   - name: server
-    base_box: kalilinux/rolling-light
+    base_box: ubuntu/xenial64
     flavor: csirtmu.tiny1x4
     cpus: 2
 
   - name: home
-    base_box: kalilinux/rolling-light
+    base_box: ubuntu/xenial64
     boot_timeout: 1000
     box_check_update: false
 
-- 
GitLab