Class: VMC::KNIFE::VCAPUpdateEtcHosts
- Inherits:
-
Object
- Object
- VMC::KNIFE::VCAPUpdateEtcHosts
- Defined in:
- lib/vmc_knife/vmc_knife.rb
Overview
This is really a server-side feature. Replace the 127.0.0.1 localhost #old_uri with the new uri
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(uri, etc_hosts_path = nil, man = nil) ⇒ VCAPUpdateEtcHosts
constructor
uri the uris (hostname) to set.
- #set_all_uris(uris_arr) ⇒ Object
- #update_pending ⇒ Object
-
#was_changed ⇒ Object
Edit the /etc/hostname file: (not a necessity so far).
Constructor Details
#initialize(uri, etc_hosts_path = nil, man = nil) ⇒ VCAPUpdateEtcHosts
uri the uris (hostname) to set. accepted type for uri: space separated list; array of strings; port appended to the hostname are also tolerated and will be stripped out.
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 |
# File 'lib/vmc_knife/vmc_knife.rb', line 1292 def initialize(uri, etc_hosts_path=nil,man=nil) @config = etc_hosts_path @config ||="/etc/hosts" if uri if uri.kind_of? String uris_arr = uri.split(' ') set_all_uris(uris_arr) elsif uri.kind_of? Array set_all_uris(uri) end end raise "The config file #{@config} does not exist." unless File.exists? @config end |
Instance Method Details
#execute ⇒ Object
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 |
# File 'lib/vmc_knife/vmc_knife.rb', line 1329 def execute() return unless update_pending @changed = false # look for the line that starts with external_uri: # replace it with the new uri if indeed there was a change. if true # use sudo. puts "Executing sudo sed -i 's/^127\.0\.0\.1[[:space:]]*localhost.*$/127.0.0.1 localhost #{@uri}/g' #{@config}" `sudo sed -i 's/^127\.0\.0\.1[[:space:]]*localhost.*$/127.0.0.1 localhost #{@uri}/g' #{@config}` else lines = IO.readlines @config File.open(@config, "w") do |file| lines.each do |s| if /^127.0.0.1[\s]+localhost[\s]*/ =~ s @changed = true unless /^127.0.0.1[\s]+localhost[\s]+#{@uri}[\s]*/ =~ s file.puts "127.0.0.1\tlocalhost #{@uri}\n" else file.puts s end end end end #Edit the /etc/hostname file: (not a necessity so far). #`sudo hostname #{@uri}` end |
#set_all_uris(uris_arr) ⇒ Object
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 |
# File 'lib/vmc_knife/vmc_knife.rb', line 1305 def set_all_uris(uris_arr) if @uri if @uri.kind_of? String uris_arr.push @uri elsif @uri.kind_of? Array uris_arr = @uri + uris_arr end end #remove the port if there is one. uris_arr.collect! do |u| u.split(':')[0] end uris_arr.sort! uris_arr.uniq! # filter out the local uris: we rely on mdns for them. #uris_arr.delete_if do |u| u =~ /\.local$/ end # for now only filter the local url with a '-' uris_arr.delete_if do |u| u =~ /-.*\.local$/ end @uri = uris_arr.join(' ') end |
#update_pending ⇒ Object
1323 1324 1325 1326 1327 1328 |
# File 'lib/vmc_knife/vmc_knife.rb', line 1323 def update_pending() #could also use: found_it=`sed -n '/^127\.0\.0\.1[[:space:]]*localhost[[:space:]]*#{@uri}/p' #{@config}` return true unless found_it && found_it.strip.length != 0 return false end |
#was_changed ⇒ Object
Edit the /etc/hostname file: (not a necessity so far). ‘sudo hostname #@uri`
1354 1355 1356 |
# File 'lib/vmc_knife/vmc_knife.rb', line 1354 def was_changed() @changed end |