From 5f550ba8319b011928a831e5c47929c5eaccfad6 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Fri, 25 Sep 2015 10:53:18 -0400 Subject: [PATCH 1/4] Added method to set hostname to Hosts class --- lib/linux_admin/hosts.rb | 6 ++++++ spec/hosts_spec.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/linux_admin/hosts.rb b/lib/linux_admin/hosts.rb index f6a1dd1..dd1092e 100644 --- a/lib/linux_admin/hosts.rb +++ b/lib/linux_admin/hosts.rb @@ -1,5 +1,7 @@ module LinuxAdmin class Hosts + include Common + attr_accessor :filename attr_accessor :raw_lines attr_accessor :parsed_file @@ -36,6 +38,10 @@ def update_entry(address, hostname, comment = nil) end end + def hostname=(name) + run!(cmd('hostnamectl'), :params => ['set-hostname', name]) + end + private def parse_file @parsed_file = [] diff --git a/spec/hosts_spec.rb b/spec/hosts_spec.rb index 0a4af2e..a16dc53 100644 --- a/spec/hosts_spec.rb +++ b/spec/hosts_spec.rb @@ -50,4 +50,16 @@ expect(@instance.raw_lines).to eq(expected_array) end end + + describe "#hostname=" do + it "sets the hostname" do + new_hostname = "test-hostname" + spawn_args = [ + @instance.cmd('hostnamectl'), + :params => ['set-hostname', new_hostname] + ] + expect(AwesomeSpawn).to receive(:run!).with(*spawn_args) + @instance.hostname = new_hostname + end + end end From f8176d74a27b40fff53bbf1fd165cc57e784dbee Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Fri, 25 Sep 2015 14:14:55 -0400 Subject: [PATCH 2/4] Added method to get hostname to Hosts class --- lib/linux_admin/hosts.rb | 5 +++++ spec/hosts_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/linux_admin/hosts.rb b/lib/linux_admin/hosts.rb index dd1092e..b344a12 100644 --- a/lib/linux_admin/hosts.rb +++ b/lib/linux_admin/hosts.rb @@ -42,6 +42,11 @@ def hostname=(name) run!(cmd('hostnamectl'), :params => ['set-hostname', name]) end + def hostname + result = run(cmd("hostname")) + result.success? ? result.output : nil + end + private def parse_file @parsed_file = [] diff --git a/spec/hosts_spec.rb b/spec/hosts_spec.rb index a16dc53..5bcb149 100644 --- a/spec/hosts_spec.rb +++ b/spec/hosts_spec.rb @@ -62,4 +62,27 @@ @instance.hostname = new_hostname end end + + describe "#hostname" do + it "returns the hostname" do + hostname = "test-host" + spawn_args = [ + @instance.cmd('hostname'), + {} + ] + result = AwesomeSpawn::CommandResult.new("", hostname, nil, 0) + expect(AwesomeSpawn).to receive(:run).with(*spawn_args).and_return(result) + expect(@instance.hostname).to eq(hostname) + end + + it "returns nil when the command fails" do + spawn_args = [ + @instance.cmd('hostname'), + {} + ] + 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 From fca1a74debe99669ebb23fb0d8d9990d9c496131 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Mon, 28 Sep 2015 08:48:45 -0400 Subject: [PATCH 3/4] Added condition to handle the case where hostnamectl is not installed --- lib/linux_admin/hosts.rb | 7 ++++++- spec/hosts_spec.rb | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/linux_admin/hosts.rb b/lib/linux_admin/hosts.rb index b344a12..9a3a2ea 100644 --- a/lib/linux_admin/hosts.rb +++ b/lib/linux_admin/hosts.rb @@ -39,7 +39,12 @@ def update_entry(address, hostname, comment = nil) end def hostname=(name) - run!(cmd('hostnamectl'), :params => ['set-hostname', name]) + 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 diff --git a/spec/hosts_spec.rb b/spec/hosts_spec.rb index 5bcb149..6e71d11 100644 --- a/spec/hosts_spec.rb +++ b/spec/hosts_spec.rb @@ -52,12 +52,25 @@ end describe "#hostname=" do - it "sets the hostname" do + it "sets the hostname using hostnamectl when the command exists" do new_hostname = "test-hostname" spawn_args = [ @instance.cmd('hostnamectl'), :params => ['set-hostname', new_hostname] ] + expect(@instance).to receive(:cmd?).with("hostnamectl").and_return(true) + expect(AwesomeSpawn).to receive(:run!).with(*spawn_args) + @instance.hostname = new_hostname + end + + it "sets the hostname with hostname when hostnamectl does not exist" do + new_hostname = "test-hostname" + 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", new_hostname) expect(AwesomeSpawn).to receive(:run!).with(*spawn_args) @instance.hostname = new_hostname end From 44398ae941958cd2fc9c2e46cf2240556baa92dd Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Tue, 29 Sep 2015 08:26:01 -0400 Subject: [PATCH 4/4] Cleaned up host specs --- spec/hosts_spec.rb | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/spec/hosts_spec.rb b/spec/hosts_spec.rb index 6e71d11..880a7c3 100644 --- a/spec/hosts_spec.rb +++ b/spec/hosts_spec.rb @@ -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) @@ -53,46 +54,39 @@ describe "#hostname=" do it "sets the hostname using hostnamectl when the command exists" do - new_hostname = "test-hostname" spawn_args = [ @instance.cmd('hostnamectl'), - :params => ['set-hostname', new_hostname] + :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 = new_hostname + @instance.hostname = TEST_HOSTNAME end it "sets the hostname with hostname when hostnamectl does not exist" do - new_hostname = "test-hostname" 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", new_hostname) + expect(File).to receive(:write).with("/etc/hostname", TEST_HOSTNAME) expect(AwesomeSpawn).to receive(:run!).with(*spawn_args) - @instance.hostname = new_hostname + @instance.hostname = TEST_HOSTNAME end end describe "#hostname" do + let(:spawn_args) do + [@instance.cmd('hostname'), {}] + end + it "returns the hostname" do - hostname = "test-host" - spawn_args = [ - @instance.cmd('hostname'), - {} - ] - result = AwesomeSpawn::CommandResult.new("", hostname, nil, 0) + result = AwesomeSpawn::CommandResult.new("", TEST_HOSTNAME, nil, 0) expect(AwesomeSpawn).to receive(:run).with(*spawn_args).and_return(result) - expect(@instance.hostname).to eq(hostname) + expect(@instance.hostname).to eq(TEST_HOSTNAME) end it "returns nil when the command fails" do - spawn_args = [ - @instance.cmd('hostname'), - {} - ] 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