Class: VagrantPlugins::GuestOpenWrt::Cap::ChangeHostName

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, new_hostname) ⇒ ChangeHostName

Returns a new instance of ChangeHostName.



11
12
13
14
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 11

def initialize(machine, new_hostname)
  @machine = machine
  @new_hostname = new_hostname
end

Instance Attribute Details

#machineObject (readonly)

Returns the value of attribute machine.



9
10
11
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 9

def machine
  @machine
end

#new_hostnameObject (readonly)

Returns the value of attribute new_hostname.



9
10
11
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 9

def new_hostname
  @new_hostname
end

Class Method Details

.change_host_name(machine, name) ⇒ Object



5
6
7
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 5

def self.change_host_name(machine, name)
  new(machine, name).change!
end

Instance Method Details

#change!Object



16
17
18
19
20
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 16

def change!
  return unless should_change?

  update_etc_hostname
end

#current_hostnameObject



26
27
28
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 26

def current_hostname
  @current_hostname ||= get_current_hostname
end

#execute(cmd, &block) ⇒ Object



43
44
45
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 43

def execute(cmd, &block)
  machine.communicate.execute(cmd, &block)
end

#get_current_hostnameObject



30
31
32
33
34
35
36
37
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 30

def get_current_hostname
  hostname = ""
  execute "uci get system.@system[0].hostname" do |type, data|
    hostname = data.chomp if type == :stdout && hostname.empty?
  end

  hostname
end

#should_change?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 22

def should_change?
  new_hostname != current_hostname
end

#update_etc_hostnameObject



39
40
41
# File 'lib/vagrant/plugins/guests/openwrt/cap/change_host_name.rb', line 39

def update_etc_hostname
  execute(%{uci set system.@system[0].hostname="#{new_hostname}" && uci commit system})
end