diff --git a/README.md b/README.md index fcb64fc06b5cab70db8fa49110efa3af3f20f1b3..624ab0a07eeb8589a8cb2d77b8afd283c4458790 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 11047a9490c7977361150c82a0c05170bfda9d37..5ad7906f65143bf67d7be9a16a1240b1b3ae5591 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 d91bf530fa6a2535965a0af78ca2aa5602373a94..9b4d60f38fba1bbda5e72ce914f2be61446684d0 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 93bb73eb9ca8606af369dda115f0db46d2aaea2e..a32fd5bdc70c3a8e21614f010c39221e8241dbf1 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 c4b6941783fb31150aedb535733a408ef37628d9..ac682023c801b329426c5bef3fef4e999f7eb9fd 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 ceee423975d251396e401aec25542a70c6dd21ef..e51a3ae6f2961d4e9ddbad1b411462a7b09ba189 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 = {