From c8bf1eda55dc4945053bf336cffc65e16f73ab32 Mon Sep 17 00:00:00 2001
From: Attila Farkas <ati@mail.muni.cz>
Date: Fri, 21 Feb 2020 09:33:39 +0100
Subject: [PATCH] add a part of new parsing module for vagrantfile generation

---
 conf/vagrant_mapping.yml     | 81 ++++++++++++++++++++++++++++++++++++
 modules/vagrant_generator.py | 49 ++++++++++++++++++++++
 2 files changed, 130 insertions(+)
 create mode 100644 conf/vagrant_mapping.yml
 create mode 100644 modules/vagrant_generator.py

diff --git a/conf/vagrant_mapping.yml b/conf/vagrant_mapping.yml
new file mode 100644
index 0000000..cc903ad
--- /dev/null
+++ b/conf/vagrant_mapping.yml
@@ -0,0 +1,81 @@
+string:
+    base_mac: vm.base_mac
+    base_address: vm.base_address
+    base_box: vm.box
+    box_download_checksum: vm.box_download_checksum
+    box_download_checksum_type: vm.box_download_checksum_type
+    box_download_client_cert: vm.box_download_client_cert
+    box_download_ca_cert: vm.box_download_ca_cert
+    box_download_ca_path: vm.box_download_ca_path
+    box_version: vm.box_version
+    communicator: vm.communicator
+    name: vm.hostname
+    post_up_message: vm.post_up_message
+    ssh_config: ssh.config
+    ssh_export_command_template: ssh.export_command_template
+    ssh_host: ssh.host
+    ssh_password: ssh.password
+    ssh_proxy_command: ssh.proxy_command
+    ssh_remote_user: ssh.remote_user
+    ssh_shell: ssh.shell 
+    ssh_sudo_command: ssh.sudo_command
+    ssh_username: ssh.username
+    winrm_username: winrm.username
+    winrm_password: winrm.password
+    winrm_host: winrm.host
+    winrm_codepage: winrm.codepage
+    winssh_proxy_command: winssh.proxy_command
+    winssh_shell: winssh.shell
+    winssh_export_command_template: winssh.export_command_template
+    winssh_sudo_command: winssh.sudo_command
+    winssh_upload_directory: winssh.upload_directory
+integer:
+    boot_timeout: vm.boot_timeout
+    graceful_halt_timeout: vm.graceful_halt_timeout
+    ssh_guest_port: ssh.guest_port
+    ssh_port: ssh.port
+    winrm_port: winrm.port
+    winrm_guest_port: winrm.guest_port
+    winrm_timeout: winrm.timeout 
+    winrm_retry_limit: winrm.retry_limit
+    winrm_retry_delay: winrm.retry_delay
+boolean:
+    box_check_update: vm.box_check_update
+    box_download_insecure: vm.box_download_insecure
+    box_download_location_trusted: vm.box_download_location_trusted
+    ignore_box_vagrantfile: vm.ignore_box_vagrantfile
+    ssh_compression: ssh.compression
+    ssh_dsa_authentication: ssh.dsa_authentication
+    ssh_forward_agent: ssh.forward_agent
+    ssh_forward_x11: ssh.forward_x11
+    ssh_insert_key: ssh.insert_key
+    ssh_keep_alive: ssh.keep_alive
+    ssh_keys_only: ssh.keys_only
+    ssh_paranoid: ssh.paranoid
+    ssh_pty: ssh.pty
+    winrm_basic_auth_only: winrm.basic_auth_only
+    winrm_ssl_peer_verification: winrm.ssl_peer_verification
+    winssh_forward_agent: winssh.forward_agent
+    winssh_keep_alive: winssh.keep_alive
+other:
+    box_url: vm.box_url
+    guest: vm.guest
+    provider: vm.provider
+    provision: vm.provision
+    synced_folder: vm.synced_folder
+    usable_port_range: vm.usable_port_range
+    ssh_extra_args: ssh.extra_args
+    ssh_forward_env: ssh.forward_env
+    ssh_private_key_path: ssh.private_key_path
+    ssh_verify_host_key: ssh.verify_host_key
+    winrm_transport: winrm.transport
+    winssh_forward_env: winssh.forward_env
+    vagrant_host: vagrant.host
+    vagrant_plugins: vagrant.plugins
+    vagrant_sensitive: vagrant.sensitive
+need_provider:
+    flavor: flavor
+    memory: memory
+    cpus: cpus
+    
+
diff --git a/modules/vagrant_generator.py b/modules/vagrant_generator.py
new file mode 100644
index 0000000..f0c6ee8
--- /dev/null
+++ b/modules/vagrant_generator.py
@@ -0,0 +1,49 @@
+""" This module generates a Vagrantfile from input device definitions. """
+
+
+def _create_commands(device_name, device_type, input_definitions, flags):
+    """ This function creates basic vagrant definition commands for a device. """
+
+    # TODO create vagrant commands
+
+
+def _build_vagrant_definitions(input_definitions, flags):
+    """
+    Creates a definition structure that is more suitable for Vagrantfile
+    generation than input definitions.
+    """
+
+    vagrant_definitions = []
+
+    for router in input_definitions["routers"]:
+        device = dict()
+        device["device_name"] = router["name"]
+        device["device_type"] = "router"
+        device["commands"] = _create_commands(router["name"], "router", input_definitions, flags)
+        vagrant_definitions.append(device)
+
+    for host in input_definitons["hosts"]:
+        device = dict()
+        device["device_name"] = host["name"]
+        device["device_type"] = "host"
+        device["commands"] = _create_commands(host["name"], "host", input_definitions, flags)
+        vagrant_definitions.append(device)
+        
+    return vagrant_definitions
+
+
+def generate_vagrantfile(input_definitions, flags):
+    """
+    This method is responsible for Vagrantfile generation.
+
+    :param input_definitions: device definitions from the input file
+    :param flags: command line flags
+    """
+
+    try:
+        vagrant_definitions = _build_vagrant_definitions(input_definitions, flags)
+   except Exception:
+       print("Could not create definitions for Vagrantfile.")
+       raise
+
+   # TODO build Vagrantfile using a template
-- 
GitLab