Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/linux_admin/hosts.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module LinuxAdmin
class Hosts
include Common

attr_accessor :filename
attr_accessor :raw_lines
attr_accessor :parsed_file
Expand Down Expand Up @@ -36,6 +38,20 @@ def update_entry(address, hostname, comment = nil)
end
end

def hostname=(name)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add the logic to add the machine's IP address to the /etc/hosts file (https://github.com/ManageIQ/manageiq-appliance/blob/master/LINK/usr/bin/miqnet.sh#L167) here or have the caller do it in a separate call?
@Fryguy @bdunne

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are setting ip address, would you ensure that if you set a different ip address, it clears out the old ip address from /etc/hosts
If you are changing the hostname, do we also clear that out.

man, the use cases around this are complicated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that the safest way to go would be to do as little as possible with each call. I don't really want to assume what people will want to do with these tools. I think it will be more clear from the calling code to do something like this https://github.com/ManageIQ/manageiq/pull/4558/files#diff-16bd2bb3c0abfe4c0381896614e7b987R238

if cmd?("hostnamectl")
run!(cmd('hostnamectl'), :params => ['set-hostname', name])
else
File.write("/etc/hostname", name)
run!(cmd('hostname'), :params => {:file => "/etc/hostname"})
end
end

def hostname
result = run(cmd("hostname"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already require activesupport/core_ext so you could simplify this if there is nothing on output when unsuccessful.

run(cmd("hostname")).output.presence

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about what will be on output. It depends really on what Open3.capture3 returns here (https://github.com/ManageIQ/awesome_spawn/blob/master/lib/awesome_spawn.rb#L119) and what hostname returns in whatever failure case happened.

result.success? ? result.output : nil
end

private
def parse_file
@parsed_file = []
Expand Down
42 changes: 42 additions & 0 deletions spec/hosts_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe LinuxAdmin::Hosts do
TEST_HOSTNAME = "test-hostname"
etc_hosts = "\n #Some Comment\n127.0.0.1\tlocalhost localhost.localdomain # with a comment\n127.0.1.1 my.domain.local"
before do
allow(File).to receive(:read).and_return(etc_hosts)
Expand Down Expand Up @@ -50,4 +51,45 @@
expect(@instance.raw_lines).to eq(expected_array)
end
end

describe "#hostname=" do
it "sets the hostname using hostnamectl when the command exists" do
spawn_args = [
@instance.cmd('hostnamectl'),
:params => ['set-hostname', TEST_HOSTNAME]
]
expect(@instance).to receive(:cmd?).with("hostnamectl").and_return(true)
expect(AwesomeSpawn).to receive(:run!).with(*spawn_args)
@instance.hostname = TEST_HOSTNAME
end

it "sets the hostname with hostname when hostnamectl does not exist" do
spawn_args = [
@instance.cmd('hostname'),
:params => {:file => "/etc/hostname"}
]
expect(@instance).to receive(:cmd?).with("hostnamectl").and_return(false)
expect(File).to receive(:write).with("/etc/hostname", TEST_HOSTNAME)
expect(AwesomeSpawn).to receive(:run!).with(*spawn_args)
@instance.hostname = TEST_HOSTNAME
end
end

describe "#hostname" do
let(:spawn_args) do
[@instance.cmd('hostname'), {}]
end

it "returns the hostname" do
result = AwesomeSpawn::CommandResult.new("", TEST_HOSTNAME, nil, 0)
expect(AwesomeSpawn).to receive(:run).with(*spawn_args).and_return(result)
expect(@instance.hostname).to eq(TEST_HOSTNAME)
end

it "returns nil when the command fails" do
result = AwesomeSpawn::CommandResult.new("", "", "An error has happened", 1)
expect(AwesomeSpawn).to receive(:run).with(*spawn_args).and_return(result)
expect(@instance.hostname).to be_nil
end
end
end