-
Notifications
You must be signed in to change notification settings - Fork 30
Added method to get and set hostname to Hosts class #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added method to get and set hostname to Hosts class #123
Conversation
lib/linux_admin/hosts.rb
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is all of LinuxAdmin systemctl based? If not, can we make this so that it does it properly depending on the system? @bdunne Thoughts on organizing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So traditionally /etc/hostname
and the hostname
command were used to deal with this.
With systemd systems we got the new systemd-hostnamed
service which seems to be adding a layer of complexity to this.
For example:
I would have expected running hostnamectl set-hostname new-host
to have the same effect as echo "new-host" > /etc/hostname && hostname -F /etc/hostname
, but after running the latter commands hostnamectl status
still shows the old name and doesn't change until I run systemctl restart systemd-hostnamed
So I'm thinking like this:
if cmd?("hostnamectl")
run!(cmd('hostnamectl'), :params => ['set-hostname', name])
else
File.write("/etc/hostname", name)
run!(cmd('hostname'), :params => ["-F", "/etc/hostname"])
end
What do you think @Fryguy ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that sounds good to me...We might want a higher level check for systemd instead of the individual commands, but this should work.
Also the params on the second one can be simplified to :params => {:F => "/etc/hostname"}
Note, if there's a full-word version of -F, let's use that...it documents the code better and doesn't lead to googling around for what that parameter means.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's --file
so I'll use :params => {:file => "/etc/hostname"}
But on a side note:
irb(main):004:0> AwesomeSpawn::CommandLineBuilder.new.build("hostname", {:F => "/etc/hostname"})
=> "hostname --F /etc/hostname"
Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh noes. That is a bug. single letter
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
👍 looks good, just the one comment. |
This is nice. I think being explicit around 👍 LGTM |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Checked commits carbonin/linux_admin@5f550ba~...44398ae with ruby 1.9.3, rubocop 0.34.2, and haml-lint 0.13.0 |
Added method to get and set hostname to Hosts class
Added a method for setting the host name via
hostnamectl
to the Hosts class.