From bde42f6b6bc862f7a9cfb3d9b2712addc7d67257 Mon Sep 17 00:00:00 2001 From: Govinda Fichtner Date: Tue, 2 Aug 2016 19:40:36 +0200 Subject: [PATCH] fix that hostname is added multiple times --- cmd/hostname_set.go | 67 ++++++++++++++++++++++++++++----------- specs/device-init_spec.rb | 55 +++++++++++++++++++++++++++----- 2 files changed, 96 insertions(+), 26 deletions(-) diff --git a/cmd/hostname_set.go b/cmd/hostname_set.go index 25b156a..4d8eccf 100644 --- a/cmd/hostname_set.go +++ b/cmd/hostname_set.go @@ -71,25 +71,10 @@ func set_hostname(args ...string) { panic(err) } - input, err := ioutil.ReadFile("/etc/hosts") - if err != nil { - panic(err) - } - - lines := strings.Split(string(input), "\n") - lines_new := []string{} + hostname_line := fmt.Sprintf("127.0.0.1 %s # added by device-init", hostname) - for i, line := range lines { - if strings.Contains(line, "127.0.0.1 localhost") { - lines_new = append(lines_new, lines[0:i+1]...) - lines_new = append(lines_new, fmt.Sprintf("127.0.0.1 %s", hostname)) - lines_new = append(lines_new, lines[i+1:]...) - } - } - output := strings.Join(lines_new, "\n") - err = ioutil.WriteFile("/etc/hosts", []byte(output), 0644) - if err != nil { - panic(err) + if !is_present_in_hosts_file(hostname_line) && !is_present_in_hosts_file(hostname) { + addHostname(hostname_line) } err = exec.Command("hostname", hostname).Run() @@ -140,3 +125,49 @@ func activeInterfaces() []string { } return result } + +func is_present_in_hosts_file(search_string string) bool { + found := false + for _, line := range readHostsFile() { + if strings.Contains(line, search_string) { + found = true + } + } + return found +} + +func addHostname(hostname_line string) { + lines_old := readHostsFile() + lines_new := []string{} + + if is_present_in_hosts_file("# added by device-init") { + for i, line := range lines_old { + if strings.Contains(line, "# added by device-init") { + lines_new = append(lines_new, lines_old[0:i]...) + lines_new = append(lines_new, hostname_line) + lines_new = append(lines_new, lines_old[i+1:]...) + } + } + } else { + for i, line := range lines_old { + if strings.Contains(line, "127.0.0.1 localhost") { + lines_new = append(lines_new, lines_old[0:i+1]...) + lines_new = append(lines_new, hostname_line) + lines_new = append(lines_new, lines_old[i+1:]...) + } + } + } + output := strings.Join(lines_new, "\n") + err := ioutil.WriteFile("/etc/hosts", []byte(output), 0644) + if err != nil { + panic(err) + } +} + +func readHostsFile() []string { + input, err := ioutil.ReadFile("/etc/hosts") + if err != nil { + panic(err) + } + return strings.Split(string(input), "\n") +} diff --git a/specs/device-init_spec.rb b/specs/device-init_spec.rb index 4cb30da..cf927f1 100644 --- a/specs/device-init_spec.rb +++ b/specs/device-init_spec.rb @@ -18,7 +18,7 @@ context "hostname" do before(:each) do - system('device-init hostname set device-tester >> /dev/null') + # system('device-init hostname set device-tester >> /dev/null') system('rm -f /boot/device-init.yaml') end @@ -31,28 +31,67 @@ end it "sets hostname" do - old_hostname_cmd_result = command('hostname').stdout - device_init_cmd_result = command('device-init hostname set black-pearl').stdout + device_init_cmd_result = command('device-init hostname set black-beauty').stdout new_hostname_cmd_result = command('hostname').stdout - expect(old_hostname_cmd_result).to contain("device-tester") expect(device_init_cmd_result).to contain("Set") - expect(new_hostname_cmd_result).to contain("black-pearl") + expect(new_hostname_cmd_result).to contain("black-beauty") + end + + it "does not set hostname twice" do + device_init_cmd_result_one = command('device-init hostname set black-widow').stdout + device_init_cmd_result_two = command('device-init hostname set black-widow').stdout + hosts_file_content = command('cat /etc/hosts').stdout + + expect(device_init_cmd_result_one).to contain("Set") + expect(device_init_cmd_result_two).to contain("Set") + expect(hosts_file_content.scan(/black-widow/).count).to eq(1) + end + + it "replaces existing device-init hostname entry if it already exists" do + device_init_cmd_result = command('device-init hostname set black-mamba').stdout + hosts_file_content = command('cat /etc/hosts').stdout + + expect(device_init_cmd_result).to contain("Set") + expect(hosts_file_content.scan(/black-mamba/).count).to eq(1) + expect(hosts_file_content.scan(/black-widow/).count).to eq(0) + end + + it "it does not replaces existing hostname entry if it already exists" do + hostname = 'black-pearl' + hosts_file = %Q( +127.0.0.1 localhost +127.0.0.1 #{hostname} +::1 localhost ip6-localhost ip6-loopback +fe00::0 ip6-localnet +ff00::0 ip6-mcastprefix +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters +172.17.0.2 device-tester + ) + + command(%Q(echo -n '#{hosts_file}' > /etc/hosts)).stdout + device_init_cmd_result = command("device-init hostname set #{hostname}").stdout + hosts_file_content = command('cat /etc/hosts').stdout + + expect(device_init_cmd_result).to contain("Set") + expect(hosts_file_content.scan(/\# added by device-init/).count).to eq(0) + expect(hosts_file_content.scan(/#{Regexp.quote(hostname)}/).count).to eq(1) end end context "with config-file" do it "sets hostname" do - status = command(%q(echo -n 'hostname: "black-pearl"\n' > /boot/device-init.yaml)).exit_status + status = command(%q(echo -n 'hostname: "black-mood"\n' > /boot/device-init.yaml)).exit_status expect(status).to be(0) old_hostname_cmd_result = command('hostname').stdout device_init_cmd_result = command('device-init --config').stdout new_hostname_cmd_result = command('hostname').stdout - expect(old_hostname_cmd_result).to contain("device-tester") + expect(old_hostname_cmd_result).to contain("black-pearl") expect(device_init_cmd_result).to contain("Set") - expect(new_hostname_cmd_result).to contain("black-pearl") + expect(new_hostname_cmd_result).to contain("black-mood") end end end