Class: Host
- Inherits:
-
Object
- Object
- Host
- Defined in:
- lib/host.rb
Instance Attribute Summary collapse
-
#autocreate ⇒ Object
Returns the value of attribute autocreate.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#found_host ⇒ Object
readonly
Returns the value of attribute found_host.
-
#host ⇒ Object
Returns the value of attribute host.
-
#host_dir ⇒ Object
readonly
Returns the value of attribute host_dir.
-
#host_yml ⇒ Object
Returns the value of attribute host_yml.
-
#host_yml_values ⇒ Object
readonly
Returns the value of attribute host_yml_values.
-
#hosts_dir ⇒ Object
readonly
Returns the value of attribute hosts_dir.
Instance Method Summary collapse
- #find_host ⇒ Object
- #host_dir? ⇒ Boolean
- #host_valid? ⇒ Boolean
- #host_yml? ⇒ Boolean
- #hosts_dir? ⇒ Boolean
-
#initialize(hostname = "") ⇒ Host
constructor
A new instance of Host.
- #prepare_host ⇒ Object
- #prepare_host_dir ⇒ Object
- #prepare_host_yml ⇒ Object
- #prepare_hosts_dir ⇒ Object
- #prompt_host_dir ⇒ Object
- #set_hostname_domain ⇒ Object
- #yml_valid? ⇒ Boolean
Constructor Details
#initialize(hostname = "") ⇒ Host
Returns a new instance of Host.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/host.rb', line 8 def initialize(hostname="") @autocreate = false @hostname = hostname @host = "" @domain = "" @host_dir = "" @host_yml = "" @found_host = "" @host_yml_values = Hash.new @hosts_dir = SYSTEM_CONFIG['hosts_dir'] prepare_hosts_dir set_hostname_domain end |
Instance Attribute Details
#autocreate ⇒ Object
Returns the value of attribute autocreate.
5 6 7 |
# File 'lib/host.rb', line 5 def autocreate @autocreate end |
#domain ⇒ Object
Returns the value of attribute domain.
5 6 7 |
# File 'lib/host.rb', line 5 def domain @domain end |
#found_host ⇒ Object (readonly)
Returns the value of attribute found_host.
6 7 8 |
# File 'lib/host.rb', line 6 def found_host @found_host end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/host.rb', line 5 def host @host end |
#host_dir ⇒ Object (readonly)
Returns the value of attribute host_dir.
6 7 8 |
# File 'lib/host.rb', line 6 def host_dir @host_dir end |
#host_yml ⇒ Object
Returns the value of attribute host_yml.
5 6 7 |
# File 'lib/host.rb', line 5 def host_yml @host_yml end |
#host_yml_values ⇒ Object (readonly)
Returns the value of attribute host_yml_values.
6 7 8 |
# File 'lib/host.rb', line 6 def host_yml_values @host_yml_values end |
#hosts_dir ⇒ Object (readonly)
Returns the value of attribute hosts_dir.
6 7 8 |
# File 'lib/host.rb', line 6 def hosts_dir @hosts_dir end |
Instance Method Details
#find_host ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/host.rb', line 33 def find_host begin Find.find(@hosts_dir) do |path| # These are the 3 things which define a 'host' in the system, 2 if there's no domain if path =~ /#{@domain}/ and path =~ /#{@host}$/ and File.directory? "#{path}/overrides" @host_dir = path @host_yml = "#{@host_dir}/host.yml" # Since this is a path with more than just the domain and hostname in it, # we need to test if this is a hostname without a domain in front. If so # we need to set @found_host differently. We test to see if the full path # minus the hostname is equal to the '@hosts_dir' because someone might # name create a host without it having a domain component. if "/#{path.split('/')[1..-2].join('/')}" == @hosts_dir # If we get here, it means this path did not have a domain component @found_host = "#{path.split('/')[-1]}" else # This path did have a domain component @found_host = "#{path.split('/')[-2]}/#{path.split('/')[-1]}" end # This should set @host_yml_values unless host_valid? return false end else prepare_host end end rescue Exception => e puts "Tried to find host: #{@hostname} in #{@hosts_dir} during Host.find_host, received exception #{e}" exit 1 end end |
#host_dir? ⇒ Boolean
152 153 154 155 156 157 158 |
# File 'lib/host.rb', line 152 def host_dir? if File.directory? @host_dir true else prepare_host_dir end end |
#host_valid? ⇒ Boolean
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/host.rb', line 77 def host_valid? if host_dir? if host_yml? return true else puts "Error: @host_yml: #{@host_yml} isn't valid!" return false end else puts "Error: @host_dir: #{@host_dir} does not exist!" return false end end |
#host_yml? ⇒ Boolean
91 92 93 94 95 96 97 98 |
# File 'lib/host.rb', line 91 def host_yml? unless File.exists? @host_yml then prepare_host_yml yml_valid? else yml_valid? end end |
#hosts_dir? ⇒ Boolean
160 161 162 163 164 165 166 |
# File 'lib/host.rb', line 160 def hosts_dir? if File.directory? @hosts_dir true else prepare_hosts_dir end end |
#prepare_host ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/host.rb', line 66 def prepare_host if @autocreate @host_dir = "#{@hosts_dir}/#{@domain}/#{@host}" @host_yml = "#{@host_dir}/host.yml" # This should set @host_yml_values unless host_valid? return false end end end |
#prepare_host_dir ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/host.rb', line 175 def prepare_host_dir begin FileUtils.mkdir_p "#{@host_dir}/overrides" true rescue Exception => e puts "Tried to create #{@host_dir} during 'Host.prepare_host_dir', received exception: #{e}" false end end |
#prepare_host_yml ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/host.rb', line 100 def prepare_host_yml begin f = open(@host_yml, 'w+') f.puts "config:" f.puts " package_base: test_dist" f.puts " release_tag: current" f.puts " rsync_path:" f.puts " ssh_port:" f.puts " session_mode:" f.puts "include:" f.puts " # - section/packagename" f.puts "exclude:" f.puts " # - /somefile" f.puts "exclude_backup:" f.puts " # - /somefile" f.puts "execute:" f.puts " # - some arbitrary command" f.close rescue Exception => e puts "Tried to open #{@host_yml} during 'Host.prepare_host_yml', received exception: #{e}" false end end |
#prepare_hosts_dir ⇒ Object
185 186 187 188 189 190 191 192 193 |
# File 'lib/host.rb', line 185 def prepare_hosts_dir begin FileUtils.mkdir_p @hosts_dir true rescue Exception => e puts "Tried to create #{@hosts_dir} during 'Host.prepare_hosts_dir', received exception: #{e}" false end end |
#prompt_host_dir ⇒ Object
168 169 170 171 172 173 |
# File 'lib/host.rb', line 168 def prompt_host_dir # This isn't used right now print "Configuration directory doesn't exist for #{@host}, create? (N/y): " input = gets.chomp if input =~ /[yY]/ then true else exit end end |
#set_hostname_domain ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/host.rb', line 23 def set_hostname_domain if @hostname.include? "." @host = @hostname.split(".").shift @domain = @hostname.split(".")[1..-1].join(".") else @host = @hostname @domain = "" end end |
#yml_valid? ⇒ Boolean
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/host.rb', line 124 def yml_valid? begin @host_yml_values = YAML.load_file(@host_yml) valid_keys = ['config','include','exclude','exclude_backup','execute'] config_valid_subkeys = ['package_base','release_tag','rsync_path','ssh_port','session_mode'] # Begin check for basic elements @host_yml_values.each_pair do |key,val| if key == 'config' and val.class == Hash val.each_pair do |skey,sval| unless config_valid_subkeys.include? skey puts "Invalid sub-option for 'config:' => #{skey} detected in #{@host_yml}. Exiting..." return false end end else unless valid_keys.include? key puts "Invalid option => #{key} detected in #{@host_yml}. Exiting..." return false end end end rescue Exception => e puts "Tried to load #{@host_yml} during 'Host.yml_valid?', received exception: #{e}" end end |