From 46402b9877f9f56027a64f0d957ecf87127ad0b1 Mon Sep 17 00:00:00 2001
From: Attila Farkas <x394097@fi.muni.cz>
Date: Wed, 24 Jul 2019 10:00:52 +0200
Subject: [PATCH] added support for all simple vagrant attributes

---
 README.md                |   3 +-
 generate.py              |  14 +++-
 name_mapping/vagrant.yml | 148 ++++++++++++++++++++-------------------
 templates/base           |   2 +-
 test.yaml                |   2 +
 5 files changed, 91 insertions(+), 78 deletions(-)

diff --git a/README.md b/README.md
index a4faf2f..6a4b56b 100644
--- a/README.md
+++ b/README.md
@@ -12,11 +12,10 @@ generate.py is a python program that generates a vagrant source file from a defi
 4. Run `$ vagrant up`
 
 ### Implemented attribute types:
-- all simple string vagrant attributes
+- all simple vagrant attributes
 - a simple network (assigning an ip address to a device)
 
 ### Not implemented yet:
-- numeric and other vagrant attributes
 - VirtualBox and OpenStack attributes
 - other network configuration
 
diff --git a/generate.py b/generate.py
index 7a66393..9b74d20 100644
--- a/generate.py
+++ b/generate.py
@@ -21,8 +21,16 @@ except IOError:
     print ("Error: cannot find mappings.")
 
 # Converts yaml attribute names to vagrant attribute names.
-def do_mapping(name):
-    return vagrant_mappings[name]
+def do_mapping(name, command_type):
+    return vagrant_mappings[str(command_type)][name]
+
+def add_formated_command(key, value):
+    if (key in vagrant_mappings['string']):
+        devices[host['name']].append('device.' + str(do_mapping(key, 'string')) + ' = \"' + str(value) + '\"')
+    elif (key in vagrant_mappings['integer']):
+        devices[host['name']].append('device.' + str(do_mapping(key, 'integer')) + ' = ' + str(value))
+    elif (key in vagrant_mappings['boolean']):
+        devices[host['name']].append('device.' + str(do_mapping(key, 'boolean')) + ' = ' + str(value).lower())
 
 def find_ip(name):
     for net_mapping in yml['net_mappings']:
@@ -35,7 +43,7 @@ for host in yml['hosts']:
     devices[host['name']] = []
     for key, value in host.items():
         if (key != 'name'):
-            devices[host['name']].append('device.' + str(do_mapping(key)) + ' = \"' + str(value) + '\"')
+            add_formated_command(key, value)
     if (yml['net_mappings']):      
         devices[host['name']].append('device.vm.network :private_network, ip: \"' + str(find_ip(host['name'])) + '\"')
 
diff --git a/name_mapping/vagrant.yml b/name_mapping/vagrant.yml
index cdf5060..be07364 100644
--- a/name_mapping/vagrant.yml
+++ b/name_mapping/vagrant.yml
@@ -1,73 +1,77 @@
-base_mac: vm.base_mac
-base_address: vm.base_address
-boot_timeout: vm.boot_timeout
-base_box: vm.box
-box_check_update: vm.box_check_update
-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_download_insecure: vm.box_download_insecure
-box_download_location_trusted: vm.box_download_location_trusted
-box_url: vm.box_url
-box_version: vm.box_version
-communicator: vm.communicator
-graceful_halt_timeout: vm.graceful_halt_timeout
-guest: vm.guest
-hostname: vm.hostname
-ignore_box_vagrantfile: vm.ignore_box_vagrantfile
-network: vm.network
-post_up_message: vm.post_up_message
-provider: vm.provider
-provision: vm.provision
-synced_folder: vm.synced_folder
-usable_port_range: vm.usable_port_range
-ssh_compression: ssh.compression
-ssh_config: ssh.config
-ssh_dsa_authentication: ssh.dsa_authentication
-ssh_export_command_template: ssh.export_command_template
-ssh_extra_args: ssh.extra_args
-ssh_forward_agent: ssh.forward_agent
-ssh_forward_env: ssh.forward_env
-ssh_forward_x11: ssh.forward_x11
-ssh_guest_port: ssh.guest_port
-ssh_host: ssh.host
-ssh_insert_key: ssh.insert_key
-ssh_keep_alive: ssh.keep_alive
-ssh_keys_only: ssh.keys_only
-ssh_paranoid: ssh.paranoid
-ssh_password: ssh.password
-ssh_port: ssh.port
-ssh_private_key_path: ssh.private_key_path
-ssh_proxy_command: ssh.proxy_command
-ssh_pty: ssh.pty
-ssh_remote_user: ssh.remote_user
-ssh_shell: ssh.shell 
-ssh_sudo_command: ssh.sudo_command
-ssh_username: ssh.username
-ssh_verify_host_key: ssh.verify_host_key
-winrm_username: winrm.username
-winrm_password: winrm.password
-winrm_host: winrm.host
-winrm_port: winrm.port
-winrm_guest_port: winrm.guest_port
-winrm_transport: winrm.transport
-winrm_basic_auth_only: winrm.basic_auth_only
-winrm_ssl_peer_verification: winrm.ssl_peer_verification
-winrm_timeout: winrm.timeout 
-winrm_retry_limit: winrm.retry_limit
-winrm_retry_delay: winrm.retry_delay
-winrm_codepage: winrm.codepage
-winssh_forward_agent: winssh.forward_agent
-winssh_forward_env: winssh.forward_env
-winssh_proxy_command: winssh.proxy_command
-winssh_keep_alive: winssh.keep_alive
-winssh_shell: winssh.shell
-winssh_export_command_template: winssh.export_command_template
-winssh_sudo_command: winssh.sudo_command
-winssh_upload_directory: winssh.upload_directory
-vagrant_host: vagrant.host
-vagrant_plugins: vagrant.plugins
-vagrant_sensitive: vagrant.sensitive
+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
+    hostname: 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
+    network: vm.network
+    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
 
diff --git a/templates/base b/templates/base
index a718e26..21dce80 100644
--- a/templates/base
+++ b/templates/base
@@ -12,7 +12,7 @@
 
 {# Device definitions #}
 Vagrant.configure("2") do |config|
-{% for name, attributes in devices.iteritems() %}
+{% for name, attributes in devices.items() %}
 
   # device: {{ name }}
   config.vm.define "{{ name }}" do |device|
diff --git a/test.yaml b/test.yaml
index 695a960..585a643 100644
--- a/test.yaml
+++ b/test.yaml
@@ -7,6 +7,8 @@ hosts:
 
   - name: home
     base_box: ubuntu/bionic64
+    boot_timeout: 1000
+    box_check_update: false
 
 routers:
   - name: server-router
-- 
GitLab