From e1427ed89b69dfb9892379f9d71276113627c36d Mon Sep 17 00:00:00 2001 From: Attila Farkas <x394097@fi.muni.cz> Date: Wed, 31 Jul 2019 08:09:07 +0200 Subject: [PATCH] add flavors support --- flavors.txt | 10 ------ generate.py | 24 ++++++++++---- modules/network_parser.py | 4 ++- modules/provider.py | 35 ++++++++++++++++++++ name_mapping/flavors.yml | 40 +++++++++++++++++++++++ name_mapping/{vagrant.yml => mapping.yml} | 6 +++- test.yaml | 2 ++ 7 files changed, 103 insertions(+), 18 deletions(-) delete mode 100644 flavors.txt create mode 100644 modules/provider.py create mode 100644 name_mapping/flavors.yml rename name_mapping/{vagrant.yml => mapping.yml} (97%) diff --git a/flavors.txt b/flavors.txt deleted file mode 100644 index ed20242..0000000 --- a/flavors.txt +++ /dev/null @@ -1,10 +0,0 @@ -csirtmu.tiny1x2 1 2 GB 20 GB -csirtmu.tiny1x4 1 4 GB 20 GB -csirtmu.small2x4 2 4 GB 20 GB -csirtmu.small2x8 2 8 GB 40 GB -csirtmu.medium4x8 4 8 GB 40 GB -csirtmu.medium4x16 4 16 GB 40 GB -csirtmu.large8x16 8 16 GB 80 GB -csirtmu.large8x32 8 32 GB 80 GB -csirtmu.jumbo16x32 16 32 GB 100 GB -csirtmu.jumbo16x64 16 64 GB 100 GB \ No newline at end of file diff --git a/generate.py b/generate.py index 16f0402..fc6ef56 100644 --- a/generate.py +++ b/generate.py @@ -5,11 +5,14 @@ import yaml import jinja2 from modules.network_parser import add_networks +from modules.provider import print_prov_attributes if (len(sys.argv) != 2): print ("Error: Expecting 1 argument (yml file).") sys.exit(); + +# Loading external files try: ymlfile = open(str(sys.argv[1])) yml = yaml.safe_load(ymlfile) @@ -17,25 +20,32 @@ except IOError: print ("Error: file does not exist.") try: - v_mappings_file = open("name_mapping/vagrant.yml") - vagrant_mappings = yaml.safe_load(v_mappings_file) + mapping_file = open("name_mapping/mapping.yml") + name_mappings = yaml.safe_load(mapping_file) except IOError: print ("Error: cannot find mappings.") +try: + flavors_file = open("name_mapping/flavors.yml") + flavors = yaml.safe_load(flavors_file) +except IOError: + print ("Error: cannot find the list of flavors.") + + # Converts yaml attribute names to vagrant attribute names. def do_mapping(name, command_type): - return vagrant_mappings[str(command_type)][name] + return name_mappings[str(command_type)][name] def add_formated_command(key, value): - if (key in vagrant_mappings['string']): + if (key in name_mappings['string']): devices[host['name']].append( 'device.' + str(do_mapping(key, 'string')) + ' = \"' + str(value) + '\"') - elif (key in vagrant_mappings['integer']): + elif (key in name_mappings['integer']): devices[host['name']].append( 'device.' + str(do_mapping(key, 'integer')) + ' = ' + str(value)) - elif (key in vagrant_mappings['boolean']): + elif (key in name_mappings['boolean']): devices[host['name']].append( 'device.' + str(do_mapping(key, 'boolean')) + ' = ' + str(value).lower()) @@ -50,6 +60,8 @@ for host in yml['hosts']: if (key != 'name'): add_formated_command(key, value) add_networks(host['name'], yml, devices) + print_prov_attributes(host, flavors + , name_mappings['need_provider'], devices) # Generating output via Jinja2 diff --git a/modules/network_parser.py b/modules/network_parser.py index 9b412d0..52f6d3d 100644 --- a/modules/network_parser.py +++ b/modules/network_parser.py @@ -23,6 +23,8 @@ def add_networks(hostname, yml, devices): if (mapping['host'] == hostname): _add_ip(hostname, yml['net_mappings'], devices) if (yml['networks']): - _add_netmask(hostname, mapping['network'], yml['networks'], devices) + _add_netmask( + hostname, mapping['network'] + , yml['networks'], devices) diff --git a/modules/provider.py b/modules/provider.py new file mode 100644 index 0000000..7631857 --- /dev/null +++ b/modules/provider.py @@ -0,0 +1,35 @@ +def _print_flavor(host, flavors, provider_attributes, devices): + if ('memory' not in host): + devices[host['name']].append(' vb.' + + provider_attributes['memory'] + ' = ' + + str(flavors[host['flavor']]['memory'])) + if ('cpus' not in host): + devices[host['name']].append(' vb.' + + provider_attributes['cpus'] + ' = ' + + str(flavors[host['flavor']]['cores'])) + +def _add_params(host, flavors, provider_attributes, devices): + if ('memory' in host): + devices[host['name']].append(' vb.' + + provider_attributes['memory'] + ' = ' + + str(host['memory'])) + if ('cpus' in host): + devices[host['name']].append(' vb.' + + provider_attributes['cpus'] + ' = ' + + str(host['cpus'])) + if ('flavor' in host and host['flavor'] in flavors): + _print_flavor(host, flavors, provider_attributes, devices) + +def _need_provider(host, provider_attributes): + for attribute in provider_attributes: + if (attribute in host): + return True + return False + +def print_prov_attributes(host, flavors, provider_attributes, devices): + if (_need_provider(host, provider_attributes)): + devices[host['name']].append( + "device.vm.provider \"virtualbox\" do |vb|") + _add_params(host, flavors, provider_attributes, devices) + devices[host['name']].append("end") + diff --git a/name_mapping/flavors.yml b/name_mapping/flavors.yml new file mode 100644 index 0000000..55a4ceb --- /dev/null +++ b/name_mapping/flavors.yml @@ -0,0 +1,40 @@ +csirtmu.tiny1x2: + cores: 1 + memory: 2048 + hd: 20480 +csirtmu.tiny1x4: + cores: 1 + memory: 4096 + hd: 20480 +csirtmu.small2x4: + cores: 2 + memory: 4096 + hd: 20480 +csirtmu.small2x8: + cores: 2 + memory: 8192 + hd: 40960 +csirtmu.medium4x8: + cores: 4 + memory: 8192 + hd: 40960 +csirtmu.medium4x16: + cores: 4 + memory: 16384 + hd: 40960 +csirtmu.large8x16: + cores: 8 + memory: 16384 + hd: 81920 +csirtmu.large8x32: + cores: 8 + memory: 32768 + hd: 81920 +csirtmu.jumbo16x32: + cores: 16 + memory: 32768 + hd: 102400 +csirtmu.jumbo16x64: + cores: 16 + memory: 65536 + hd: 102400 diff --git a/name_mapping/vagrant.yml b/name_mapping/mapping.yml similarity index 97% rename from name_mapping/vagrant.yml rename to name_mapping/mapping.yml index be07364..95c4a0f 100644 --- a/name_mapping/vagrant.yml +++ b/name_mapping/mapping.yml @@ -60,7 +60,6 @@ boolean: other: box_url: vm.box_url guest: vm.guest - network: vm.network provider: vm.provider provision: vm.provision synced_folder: vm.synced_folder @@ -74,4 +73,9 @@ other: vagrant_host: vagrant.host vagrant_plugins: vagrant.plugins vagrant_sensitive: vagrant.sensitive +need_provider: + flavor: flavor + memory: memory + cpus: cpus + diff --git a/test.yaml b/test.yaml index 585a643..6e01df1 100644 --- a/test.yaml +++ b/test.yaml @@ -4,6 +4,8 @@ name: small-sandbox hosts: - name: server base_box: ubuntu/bionic64 + flavor: csirtmu.tiny1x4 + cpus: 2 - name: home base_box: ubuntu/bionic64 -- GitLab