From a097e8dc9fb4d16cf25f1b3d0a4b7327fda37b92 Mon Sep 17 00:00:00 2001
From: Attila Farkas <x394097@fi.muni.cz>
Date: Wed, 6 Nov 2019 12:26:58 +0100
Subject: [PATCH] add ansible_local support and update readme

---
 README.md                 |  2 +-
 create.py                 | 22 +++++++++++++++++-----
 modules/device_creator.py |  8 ++++----
 modules/file_generator.py |  8 ++++----
 modules/provider.py       |  5 ++++-
 templates/vagrantfile     |  4 ++--
 6 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md
index fcb64fc..624ab0a 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@ create.py is a python program that generates a vagrant source file from a defini
 2. Install Python dependencies with `pip3 install -r requirements.txt`.
 3. Clone the project.
 4. Navigate to the project folder.
-5. Type `$ python3 create.py sandbox.yml`. 
+5. Type `$ python3 create.py sandbox.yml`. On Windows or in case of an error due to ansible/python use `python3 create.py -l sandbox.yml`. (note: This will rewrite the previously generated Vagrantfile and ansible files.)  
 6. Run `$ vagrant up`
 
 ### Input yaml file structure
diff --git a/create.py b/create.py
index 11047a9..5ad7906 100644
--- a/create.py
+++ b/create.py
@@ -8,13 +8,25 @@ from modules.file_generator import generate_vagrantfile, generate_ansible_files
 from modules.device_creator import open_file
 from modules.routing import create_border_router
 
-if len(sys.argv) != 2:
-    print("Error: Expecting 1 argument (yml file).")
+if len(sys.argv) == 3:
+    if str(sys.argv[1]) == "-l":
+        ansible_local = True
+        input_file_name = str(sys.argv[2])
+    elif str(sys.argv[2]) == "-l":
+        ansible_local = True
+        input_file_name = str(sys.argv[1])
+    else:
+        print("Error: Expecting a yml file and optionally a flag -l.")
+        sys.exit()
+elif len(sys.argv) == 2:
+    ansible_local = False
+    input_file_name = str(sys.argv[1])
+else:
+    print("Error: Expecting a yml file and optionally a flag -l.")
     sys.exit()
 
-device_definitions = open_file(str(sys.argv[1]))
+device_definitions = open_file(input_file_name)
 create_border_router(device_definitions)
 
-generate_vagrantfile(device_definitions)
+generate_vagrantfile(device_definitions, ansible_local)
 generate_ansible_files(device_definitions)
-
diff --git a/modules/device_creator.py b/modules/device_creator.py
index d91bf53..9b4d60f 100644
--- a/modules/device_creator.py
+++ b/modules/device_creator.py
@@ -46,19 +46,19 @@ def _create_hosts(yml, mappings, flavors):
     return host_definitions
 
 
-def _create_routers(yml):
+def _create_routers(yml, ansible_local):
     """ Creates a dictionary with formatted definition of each router. """
     router_definitions = {}
 
     for router in yml['routers']:
         router_definitions[router['name']] = []
         add_router_ip(router["name"], yml, router_definitions)
-        add_router_specification(router, router_definitions)
+        add_router_specification(router, router_definitions, ansible_local)
 
     return router_definitions
 
 
-def create_devices(definitions):
+def create_devices(definitions, ansible_local):
     """ Returns a merged dictionary of host and router definitions. """
 
     mappings = open_file(MAPPING_FILE)
@@ -66,4 +66,4 @@ def create_devices(definitions):
 
     return {
         **_create_hosts(definitions, mappings, flavors),
-        **_create_routers(definitions)}
+        **_create_routers(definitions, ansible_local)}
diff --git a/modules/file_generator.py b/modules/file_generator.py
index 93bb73e..a32fd5b 100644
--- a/modules/file_generator.py
+++ b/modules/file_generator.py
@@ -54,13 +54,13 @@ def _find_user_ansible_files(definitions):
 
     return host_names
 
-def generate_vagrantfile(definitions):
+def generate_vagrantfile(definitions, ansible_local):
     """ Writes the prepared output to a Vagrantfile. """
-    
-    device_definitions = create_devices(definitions)
+   
+    device_definitions = create_devices(definitions, ansible_local)
     user_ansible_files = _find_user_ansible_files(definitions)
     template = _load_template("vagrantfile")
-    output = template.render(devices=device_definitions, user_files=user_ansible_files)
+    output = template.render(devices=device_definitions, user_files=user_ansible_files, ansible_local=ansible_local)
     _generate_file("Vagrantfile", output)
     
     print("Info: Vagrantfile successfully created.")
diff --git a/modules/provider.py b/modules/provider.py
index c4b6941..ac68202 100644
--- a/modules/provider.py
+++ b/modules/provider.py
@@ -48,7 +48,7 @@ def add_prov_attributes(host, flavors, provider_attributes, definitions):
         definitions[host['name']].append("end")
 
 
-def add_router_specification(router, definitions):
+def add_router_specification(router, definitions, ansible_local):
     """ Adds the default specification for a router. """
 
     router_box = "generic/debian10"
@@ -62,3 +62,6 @@ def add_router_specification(router, definitions):
         "device.vm.provider \"virtualbox\" do |vb|")
     definitions[router['name']].append("  vb.memory = " + str(router_memory))
     definitions[router['name']].append("end")
+    if ansible_local:
+        definitions[router['name']].append(
+        "config.vm.synced_folder \".\", \"/vagrant\", type: \"rsync\", rsync__exclude: \".git/\"")
diff --git a/templates/vagrantfile b/templates/vagrantfile
index ceee423..e51a3ae 100644
--- a/templates/vagrantfile
+++ b/templates/vagrantfile
@@ -20,7 +20,7 @@ Vagrant.configure("2") do |config|
 {% endfor %}
 
   # configuration of devices with ansible
-  config.vm.provision "ansible" do |ansible|
+  config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible|
     ansible.playbook = "provisioning/playbook.yml"
     ansible.verbose = true
     ansible.extra_vars = {
@@ -29,7 +29,7 @@ Vagrant.configure("2") do |config|
   end
 
   {% for name in user_files %}
-  config.vm.provision "ansible" do |ansible|
+  config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible|
     ansible.playbook = "provisioning/{{ name }}.yml"
     ansible.verbose = true
     ansible.extra_vars = {
-- 
GitLab