Skip to content

Commit

Permalink
#321 #314 #302 should implement project specific config support feature
Browse files Browse the repository at this point in the history
  • Loading branch information
phuonglm committed Jul 12, 2017
1 parent d1bec25 commit 7e27157
Show file tree
Hide file tree
Showing 5 changed files with 463 additions and 8 deletions.
37 changes: 33 additions & 4 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'json'
load File.dirname(__FILE__) + '/lib/utility.rb'
load File.dirname(__FILE__) + '/lib/provisioner.rb'
load File.dirname(__FILE__) + '/lib/os.rb'


# Load default setting
file = File.read(File.dirname(__FILE__) + '/vagrant_config.json')
Expand All @@ -12,18 +14,42 @@ override_hash = nil
# Check and override if exist any match JSON object from vagrant_config_override.json
if File.exist? (File.dirname(__FILE__) + '/vagrant_config_override.json')
override_file = File.read(File.dirname(__FILE__) + '/vagrant_config_override.json')

parsing_file = File.dirname(__FILE__) + '/vagrant_config_override.json'
begin
override_hash = JSON.parse(override_file)

data_hash = overrides(data_hash, override_hash)

if data_hash.key?('config_paths')
data_hash['config_paths'].each do |default_config_file_path, value|
overide_config_file_path = default_config_file_path.gsub(/default\.json$/, "overide.json")
if File.exist? (default_config_file_path)
default_config_file = File.read(default_config_file_path)
parsing_file = default_config_file_path
project_config_hash = JSON.parse(default_config_file)
end

if File.exist? (overide_config_file_path)
override_config_file = File.read(overide_config_file_path)
parsing_file = overide_config_file_path
overide_config_hash = JSON.parse(override_config_file)
project_config_hash = overrides(project_config_hash, overide_config_hash)
end
if !project_config_hash.nil?
overrides(data_hash, project_config_hash)
end
end
end
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]: ")
puts red('from ' + parsing_file )
ans = prompt yellow("some errors have occured and '" + parsing_file + "' file will not be used, do you want to continue? [y/N]: ")
if ans.downcase != 'y'
exit!
end
end
puts data_hash.inspect

end

Vagrant.configure("2") do |config|
Expand Down Expand Up @@ -125,7 +151,10 @@ Vagrant.configure("2") do |config|
end
when 'public_network'
options[:ip] = vm_network['ip'] unless vm_network['ip'].nil? or vm_network['ip'].strip().empty?
options[:bridge] = vm_network['bridge'] unless vm_network['bridge'].nil? or vm_network['bridge'].empty?
bridge_interface = ""
bridge_interface = vm_network['bridge'] unless vm_network['bridge'].nil? or vm_network['bridge'].empty?
bridge_interface = get_default_nic() if bridge_interface.empty?
options[:bridge] = bridge_interface unless bridge_interface.empty?
end

config.vm.network vm_network['mode'], options
Expand Down
298 changes: 298 additions & 0 deletions lib/os.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
require 'rbconfig'
require 'yaml'

# a set of friendly files for determining your Ruby runtime
# treats cygwin as linux
# also treats IronRuby on mono as...linux
class OS
attr_reader :config

def self.config
@config ||= RbConfig::CONFIG
end

# true if on windows [and/or jruby]
# false if on linux or cygwin on windows

def self.windows?
@windows ||= begin
if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin
false
elsif ENV['OS'] == 'Windows_NT'
true
else
false
end
end

end

# true for linux, os x, cygwin
def self.posix?
@posix ||=
begin
if OS.windows?
begin
begin
# what if we're on interix...
# untested, of course
Process.wait fork{}
true
rescue NotImplementedError, NoMethodError
false
end
end
else
# assume non windows is posix
true
end
end

end

# true for linux, false for windows, os x, cygwin
def self.linux?
if (host_os =~ /linux/)
true
else
false
end
end

def self.freebsd?
if (host_os =~ /freebsd/)
true
else
false
end
end

def self.iron_ruby?
@iron_ruby ||= begin
if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'ironruby')
true
else
false
end
end
end

def self.bits
@bits ||= begin
if host_cpu =~ /_64$/ || RUBY_PLATFORM =~ /x86_64/
64
elsif RUBY_PLATFORM == 'java' && ENV_JAVA['sun.arch.data.model'] # "32" or "64":http://www.ruby-forum.com/topic/202173#880613
ENV_JAVA['sun.arch.data.model'].to_i
elsif host_cpu == 'i386'
32
elsif host_os =~ /32$/ # mingw32, mswin32
32
else # cygwin only...I think
if 1.size == 8
64
else
32
end
end
end
end


def self.java?
@java ||= begin
if RUBY_PLATFORM =~ /java/
true
else
false
end
end
end

def self.ruby_bin
@ruby_exe ||= begin
File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
end
end

def self.mac?
@mac = begin
if host_os =~ /darwin/
true
else
false
end
end
end

def self.osx?
mac?
end

def self.x?
mac?
end


# amount of memory the current process "is using", in RAM
# (doesn't include any swap memory that it may be using, just that in actual RAM)
# raises 'unknown' on jruby currently
def self.rss_bytes
# attempt to do this in a jruby friendly way
if OS::Underlying.windows?
# MRI, Java, IronRuby, Cygwin
if OS.java?
# no win32ole on 1.5.x, so leave here for compatibility...maybe for awhile :P
require 'java'
mem_bean = java.lang.management.ManagementFactory.getMemoryMXBean
mem_bean.heap_memory_usage.used + mem_bean.non_heap_memory_usage.used
else
wmi = nil
begin
require 'win32ole'
wmi = WIN32OLE.connect("winmgmts://")
rescue LoadError, NoMethodError => e # NoMethod for IronRuby currently [sigh]
raise 'rss unknown for this platform ' + e.to_s
end
processes = wmi.ExecQuery("select * from win32_process where ProcessId = #{Process.pid}")
memory_used = nil
# only allow for one...
for process in processes
raise if memory_used
memory_used = process.WorkingSetSize.to_i
end
memory_used
end
elsif OS.posix? # linux [though I've heard it works in OS X]
`ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
else
raise 'unknown rss for this platform'
end
end

class Underlying

def self.bsd?
OS.osx?
end

def self.windows?
ENV['OS'] == 'Windows_NT'
end

def self.linux?
OS.host_os =~ /linux/ ? true : false
end

end

def self.cygwin?
@cygwin = begin
if RUBY_PLATFORM =~ /-cygwin/
true
else
false
end
end
end

def self.dev_null # File::NULL in 1.9.3+
@dev_null ||= begin
if OS.windows?
"NUL"
else
"/dev/null"
end
end
end

# provides easy way to see the relevant config entries
def self.report
relevant_keys = [
'arch',
'host',
'host_cpu',
'host_os',
'host_vendor',
'target',
'target_cpu',
'target_os',
'target_vendor',
]
RbConfig::CONFIG.reject {|key, val| !relevant_keys.include? key }.merge({'RUBY_PLATFORM' => RUBY_PLATFORM}).to_yaml
end

def self.cpu_count
@cpu_count ||=
case RUBY_PLATFORM
when /darwin9/
`hwprefs cpu_count`.to_i
when /darwin10/
(hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
when /linux/
`cat /proc/cpuinfo | grep processor | wc -l`.to_i
when /freebsd/
`sysctl -n hw.ncpu`.to_i
else
if RbConfig::CONFIG['host_os'] =~ /darwin/
(hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
elsif self.windows?
# ENV counts hyper threaded...not good.
# out = ENV['NUMBER_OF_PROCESSORS'].to_i
require 'win32ole'
wmi = WIN32OLE.connect("winmgmts://")
cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # don't count hyper-threaded in this
cpu.to_enum.first.NumberOfCores
else
raise 'unknown platform processor_count'
end
end
end

def self.open_file_command
if OS.doze? || OS.cygwin?
"start"
elsif OS.mac?
"open"
else
# linux...what about cygwin?
"xdg-open"
end

end

def self.app_config_path(name)
if OS.doze?
if ENV['LOCALAPPDATA']
return File.join(ENV['LOCALAPPDATA'], name)
end

File.join ENV['USERPROFILE'], 'Local Settings', 'Application Data', name
elsif OS.mac?
File.join ENV['HOME'], 'Library', 'Application Support', name
else
if ENV['XDG_CONFIG_HOME']
return File.join(ENV['XDG_CONFIG_HOME'], name)
end

File.join ENV['HOME'], '.config', name
end
end

class << self
alias :doze? :windows? # a joke name but I use it and like it :P
alias :jruby? :java?

# delegators for relevant config values
%w(host host_cpu host_os).each do |method_name|
define_method(method_name) { config[method_name] }
end

end

private

def self.hwprefs_available?
`which hwprefs` != ''
end

end
Loading

0 comments on commit 7e27157

Please sign in to comment.