Skip to content

Commit

Permalink
Merge pull request #241 from hoatle/features/#239-config-override-object
Browse files Browse the repository at this point in the history
Features/#239 config override object
  • Loading branch information
hoatle authored Feb 9, 2017
2 parents 74caf08 + c2bcd11 commit 3f7c729
Show file tree
Hide file tree
Showing 7 changed files with 602 additions and 105 deletions.
3 changes: 2 additions & 1 deletion .travis/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ set -e

if [[ $BUILD_TYPE == "dev" ]]; then
docker_build
docker run -v $(pwd):/opt/app $CONTAINER_IMAGE rake build
docker run --rm -v $(pwd):/opt/app $CONTAINER_IMAGE rake build
docker run --rm -v $(pwd):/opt/app $CONTAINER_IMAGE bundle exec rspec
elif [[ $BUILD_TYPE == "docs" ]]; then
cd docs
docker_build
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ group:development do
gem 'foodcritic', '~> 8.1'
gem 'knife-spork', '~> 1.6'
gem 'berkshelf', '~> 5.2'
gem "rspec"
end
39 changes: 20 additions & 19 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Vagrant.configure("2") do |config|
rescue Exception => msg
puts red(msg)
puts red('from vagrant_config_override.json')
ans = prompt yellow("some errors have occured and 'vagrant_config_override.json' file will not be used, do you want to continue? [y/n]: ")
ans = prompt yellow("some errors have occured and 'vagrant_config_override.json' file will not be used, do you want to continue? [y/N]: ")
if ans.downcase != 'y'
exit!
end
Expand Down Expand Up @@ -199,8 +199,9 @@ Vagrant.configure("2") do |config|
# plugins config
plugins_hash = data_hash['plugins']

plugins_hash.each do |plugin_name, plugin_value|
if plugin_value['required'] == true
plugins_hash.each do |plugin|
plugin_name = plugin['name']
if plugin['required'] == true
unless Vagrant.has_plugin?(plugin_name)
puts red("required: '$ vagrant plugin install #{plugin_name}'")
exit!
Expand All @@ -210,41 +211,41 @@ Vagrant.configure("2") do |config|
# this is current fixed config, not dynamic plugins config
# FIXME(hoatle): #186 should fix this

if Vagrant.has_plugin?(plugin_name) and plugin_value.key?('config_key')
config_key = plugin_value['config_key']
if Vagrant.has_plugin?(plugin_name) and plugin.key?('config_key')
config_key = plugin['config_key']
if 'gatling' == config_key

unless plugin_value['latency'].nil?
config.gatling.latency = plugin_value['latency']
unless plugin['latency'].nil?
config.gatling.latency = plugin['latency']
end

unless plugin_value['time_format'].nil? or plugin_value['time_format'].empty?
config.gatling.time_format = plugin_value['time_format']
unless plugin['time_format'].nil? or plugin['time_format'].empty?
config.gatling.time_format = plugin['time_format']
end

unless plugin_value['rsync_on_startup'].nil?
config.gatling.rsync_on_startup = plugin_value['rsync_on_startup']
unless plugin['rsync_on_startup'].nil?
config.gatling.rsync_on_startup = plugin['rsync_on_startup']
end

elsif 'hostsupdater' == config_key
unless plugin_value['aliases'].nil? or plugin_value['aliases'].empty?
config.hostsupdater.aliases = plugin_value['aliases']
unless plugin['aliases'].nil? or plugin['aliases'].empty?
config.hostsupdater.aliases = plugin['aliases']
end

unless plugin_value['remove_on_suspend'].nil? or plugin_value['remove_on_suspend'].empty?
config.hostsupdater.remove_on_suspend = plugin_value['remove_on_suspend']
unless plugin['remove_on_suspend'].nil? or plugin['remove_on_suspend'].empty?
config.hostsupdater.remove_on_suspend = plugin['remove_on_suspend']
end
end
end

# if plugin_value.key?('config_key')
# config_key = plugin_value['config_key']
# if plugin.key?('config_key')
# config_key = plugin['config_key']
# if Vagrant.has_plugin?(plugin_name) and !config_key.nil? and !config_key.empty?
# puts red(config[config_key.to_sym])
# # TODO(hoatle): remove config_key and required keys?
# #config.instance_variable_set("@#{config_key}", plugin_value)
# #config.instance_variable_set("@#{config_key}", plugin)
# # new_config = Vagrant::Config::V2::Root.new({
# # config_key => plugin_value
# # config_key => plugin
# # })
# # config.merge(config, new_config)
# end
Expand Down
62 changes: 61 additions & 1 deletion lib/utility.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,70 @@
# Utility functions
def overrides(obj1, obj2)
obj2.each do |key, value|
# replace
replaced_key = key.sub(/_r_/, '')
if key.start_with?('_r_') and obj1.has_key?(replaced_key) and value.class.name == 'Array'
obj1[replaced_key] = []
key = replaced_key
end

if obj1.has_key?(key)
if value.class.name == 'Hash'
obj1[key] = overrides(obj1[key], obj2[key])
elsif value.class.name == 'Array'
obj1_value = obj1[key].clone
if value[0].class.name == 'String'
obj1_value = value
else
value.map! do |val|
if val.class.name == 'Hash'
id_existing = false
obj1[key].each do |val1|
if val1['_id'] == val['_id']
id_existing = true
break
end
end
if id_existing == false
if !val['_op'].nil? and val['_op'] != 'a'
# warnings
puts yellow("_op = #{val['_op']} is invalid for non-existing id: #{val}")
end
val['_op'] = 'a'
elsif val['_op'].nil?
val['_op'] = 'o'
end

if val['_op'] == 'a'
if val['_idx'].nil?
obj1_value.push(val)
else
obj1_value.insert(val['_idx'], val)
end
elsif val['_op'] == 'o'
obj1_value.map! do |val2|
if val2['_id'] == val['_id']
val2 = overrides(val2, val)
end
val2
end
elsif val['_op'] == 'r'
obj1_value.map! do |val3|
if val3['_id'] == val['_id']
val3 = val
end
val3
end
elsif val['_op'] == 'd'
obj1_value.delete_if {|val4| val4['_id'] == val['_id'] }
end
else
obj1_value = value
end
val
end
end
obj1[key] = obj1_value
else
obj1[key] = value
end
Expand All @@ -13,8 +73,8 @@ def overrides(obj1, obj2)
obj1[key] = value
# puts yellow('ADDED: try to override non-existing key: ' + key + ' with value: ' + value.to_s)
end

end
#puts JSON.pretty_generate(obj1)
return obj1
end

Expand Down
162 changes: 162 additions & 0 deletions spec/fixture/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"vm": {
"hostname": "teracy.dev",
"box": "bento/ubuntu-16.04",
"box_version": "",
"networks": [{ // see: http://docs.vagrantup.com/v2/networking/index.html
"_id": "0",
"mode": "public_network", // one of "forwarded_port", "private_network" or "public_network"
//by default, 192.168.99.100 is used for Boot2Docker
// for convention, use 192.168.99.101 for teracy-dev, 192.168.99.102 for teracy-staging, etc.
//"ip": "" // for private_network, public_network mode
//"type":"dhcp", // for private_network mode; if ip and type are both defined, ip takes precedence
//"auto_config":true, // for private_network mode + ip
"bridge": [
"eth0", //wired LAN, the first priority
"en0: Wi-Fi (AirPort)", // macOS 10.12
"en1: Wi-Fi (AirPort)", // macOS 10.11
"wlan0" // linux
] // for public_network mode
}],
"synced_folders":[{ //see: http://docs.vagrantup.com/v2/synced-folders/index.html
"_id": "0",
"type": "virtual_box",
"host": ".",
"guest": "/vagrant",
"disabled": true
}, {
"_id": "1",
"type": "rsync",
"host": "./workspace",
"guest": "/home/vagrant/workspace",
"rsync__exclude": [".git", ".idea/", "node_modules/", "bower_components/", ".npm/"]
}, { // should persist docker auth token after login success #170
"_id": "2",
"type": "virtual_box",
"host": "./home/.docker",
"guest": "/home/vagrant/.docker",
"mount_options": [
"dmode=775",
"fmode=755"
]
// {
// "type":"virtual_box", // virtual_box, "nfs", "rsync" or "smb", incase smb mount failed,
// // ssh to vm and install cifs-utils allow vm mount smb directory
// // mount.cifs needs to be used with dir_mode file_mode instead of dmode fmode
// // Windows 7 needs powershell 3.0 to create smb share on windows
// // http://www.microsoft.com/en-us/download/details.aspx?id=34595
// "host":"./docker",
// "guest":"/var/lib/docker/",
// "mount_options":[
// "dmode=775",
// "fmode=755"
// ]
// },
//TODO: how to keep and reused existing Docker images after destroying the VM?
// or even attaching the existing data into it, so that developers should share the Docker images
// remove /vagrant default file sync on the VM guest machine #184
}]
// Other configs
// "boot_timeout": 300,
// "box_check_update": true,
// "box_download_checksum": "",
// "box_download_checksum_type": "", // "md5", "sha1", "sha256"
// "box_download_client_cert": "",
// "box_download_ca_cert": "",
// "box_download_ca_path": "",
// "box_download_insecure": true, // true, false
// "communicator": "ssh", // ssh, winrm
// "graceful_halt_timeout": 60,
// "guest": "linux", // linux
// "hostname": "",
// "post_up_message": "",
// "usable_port_range": "2200..2250" // 2200..2250
},
"vb": { //virtualbox settings from https://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvm
//"gui":true,
//"name":"teracy-dev",
"memory": 1024,
//"cpus":1,
"description": "teracy-dev #{Time.now.getutc.to_i}"
},
"provisioners": [{
"_id": "0",
"type": "chef_solo",
"log_level": "info", //one of debug, info, warn, error, fatal. Default: info
"cookbooks_path": [
"vendor-cookbooks",
"main-cookbooks"
],
"roles_path": "roles",
"nodes_path": "nodes",
"data_bags_path": "data_bags",
"run_list": [
"vim",
"teracy-dev"
],
"json": {
// we mix teracy-dev config along with vender-cookbooks config, hopefully this is better than
// separated as we did before, this is for advanded users only
"docker": {
"enabled": true,
"version": "", // use this to install a specific docker version, default: ""
// used along with the version key, this is default for Ubuntu
"package_options": "--force-yes -o Dpkg::Options::='--force-confold' -o Dpkg::Options::='--force-all'",
"repo": "main", // one of main, test, experimental, default: "main"
"members": ["vagrant"], // to append this member to "docker" group
"action": "create" // one of create, delete. Default: create
},
"docker_compose": {
"release": "1.10.0", // more: https://github.com/docker/compose/releases/
"enabled": true // "docker" must be enabled to get this
},
"docker_machine": {
"enabled": false,
"release": "v0.8.2" // more: https://github.com/docker/machine/releases/
},
"teracy-dev": {
"directories": [{
"_id": "0",
"path": "/home/vagrant/workspace",
"owner": "vagrant",
"group": "vagrant",
"mode": "0775",
"action": "create" // one of create, delete, nothing. Default: create.
// See more: https://docs.chef.io/resource_directory.html
}],
"alias": [{
"_id": "0",
"name": "ws",
"command": "cd ~/workspace",
"action": "add" // one of add, remove. Default: add.
// See more: https://github.com/customink-webops/magic_shell/blob/master/resources/alias.rb
}],
"env": [{
"_id": "0",
"key": "EDITOR",
"value": "vim",
"action": "add" // one of add, remove. Default: add.
// See more: https://github.com/customink-webops/magic_shell/blob/master/resources/environment.rb
}],
"inotify": {
"max_user_watches": 524288 // see: https://github.com/teracyhq/dev/issues/208, set value = 0 to unset this setting
}
}
}
}],
"plugins": [{
"_id": "0",
"name": "vagrant-gatling-rsync",
"config_key": "gatling",
"required": true,
// auto rsync watch when up/ reload done
// should try to reduce rsync latency to 0.5s instead of 1s #173
"latency": 0.5, //0.5s
"time_format": "%H:%M:%S",
"rsync_on_startup": true
}, {
"_id": "1",
"name": "vagrant-rsync-back",
"required": true
}]
}
Loading

0 comments on commit 3f7c729

Please sign in to comment.