Class: VagrantPlugins::GuestFedora::Cap::ChangeHostName
- Inherits:
-
Object
- Object
- VagrantPlugins::GuestFedora::Cap::ChangeHostName
- Defined in:
- plugins/guests/fedora/cap/change_host_name.rb
Instance Attribute Summary (collapse)
-
- (Object) machine
readonly
Returns the value of attribute machine.
-
- (Object) new_hostname
readonly
Returns the value of attribute new_hostname.
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) change!
- - (Object) current_hostname
- - (Object) fqdn
- - (Object) get_current_hostname
-
- (ChangeHostName) initialize(machine, new_hostname)
constructor
A new instance of ChangeHostName.
- - (Object) refresh_hostname_service
- - (Object) short_hostname
- - (Boolean) should_change?
- - (Object) sudo(cmd, &block)
- - (Object) update_etc_hostname
-
- (Object) update_etc_hosts
/etc/hosts should resemble: 127.0.0.1 localhost 127.0.1.1 host.fqdn.com host.fqdn host.
Constructor Details
- (ChangeHostName) initialize(machine, new_hostname)
Returns a new instance of ChangeHostName
11 12 13 14 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 11 def initialize(machine, new_hostname) @machine = machine @new_hostname = new_hostname end |
Instance Attribute Details
- (Object) machine (readonly)
Returns the value of attribute machine
9 10 11 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 9 def machine @machine end |
- (Object) new_hostname (readonly)
Returns the value of attribute new_hostname
9 10 11 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 9 def new_hostname @new_hostname end |
Class Method Details
+ (Object) change_host_name(machine, name)
5 6 7 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 5 def self.change_host_name(machine, name) new(machine, name).change! end |
Instance Method Details
- (Object) change!
16 17 18 19 20 21 22 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 16 def change! return unless should_change? update_etc_hostname update_etc_hosts refresh_hostname_service end |
- (Object) current_hostname
28 29 30 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 28 def current_hostname @current_hostname ||= get_current_hostname end |
- (Object) fqdn
61 62 63 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 61 def fqdn new_hostname end |
- (Object) get_current_hostname
32 33 34 35 36 37 38 39 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 32 def get_current_hostname hostname = "" sudo "hostname -f" do |type, data| hostname = data.chomp if type == :stdout && hostname.empty? end hostname end |
- (Object) refresh_hostname_service
57 58 59 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 57 def refresh_hostname_service sudo("hostname -F /etc/hostname") end |
- (Object) short_hostname
65 66 67 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 65 def short_hostname new_hostname.split('.').first end |
- (Boolean) should_change?
24 25 26 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 24 def should_change? new_hostname != current_hostname end |
- (Object) sudo(cmd, &block)
69 70 71 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 69 def sudo(cmd, &block) machine.communicate.sudo(cmd, &block) end |
- (Object) update_etc_hostname
41 42 43 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 41 def update_etc_hostname sudo("echo '#{short_hostname}' > /etc/hostname") end |
- (Object) update_etc_hosts
/etc/hosts should resemble: 127.0.0.1 localhost 127.0.1.1 host.fqdn.com host.fqdn host
48 49 50 51 52 53 54 55 |
# File 'plugins/guests/fedora/cap/change_host_name.rb', line 48 def update_etc_hosts ip_address = '([0-9]{1,3}\.){3}[0-9]{1,3}' search = "^(#{ip_address})\\s+#{Regexp.escape(current_hostname)}(\\s.*)?$" replace = "\\1 #{fqdn} #{short_hostname}" expression = ['s', search, replace, 'g'].join('@') sudo("sed -ri '#{expression}' /etc/hosts") end |