Skip to content
This repository has been archived by the owner on Mar 2, 2021. It is now read-only.

Commit

Permalink
fix that hostname is added multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
Govinda Fichtner committed Aug 2, 2016
1 parent eb2d2da commit bde42f6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 26 deletions.
67 changes: 49 additions & 18 deletions cmd/hostname_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
55 changes: 47 additions & 8 deletions specs/device-init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit bde42f6

Please sign in to comment.