<commit_before>#
# Cookbook Name:: system
# Provider:: hostname
#
# Copyright 2013, Tacit Knowledge, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

action :set do

require 'socket'

def local_ip
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily
  UDPSocket.open do |s|
    s.connect '172.16.31.10', 1
    s.addr.last
  end
  ensure
    Socket.do_not_reverse_lookup = orig
end

# get node IP
node_ip = "#{local_ip}"
log("Node IP: #{node_ip}") { level :debug }

# ensure the required short hostname is lower case
new_resource.short_hostname.downcase!

# set hostname from short or long (when domain_name set)
unless new_resource.domain_name.nil? || new_resource.domain_name == ''
  hostname = "#{new_resource.short_hostname}.#{new_resource.domain_name}"
  hosts_list = "#{new_resource.short_hostname}.#{new_resource.domain_name} #{new_resource.short_hostname}"
else
  hostname = "#{new_resource.short_hostname}"
  hosts_list = "#{new_resource.short_hostname}"
end
log("Setting hostname for '#{hostname}'.") { level :debug }

# Update /etc/hosts
log('Configure /etc/hosts.') { level :debug }
template "/etc/hosts" do
  source "hosts.erb"
  variables(
    :node_ip => node_ip,
    :hosts_list => hosts_list,
    :static_hosts => new_resource.static_hosts
    )
  mode 0744
end

# Update /etc/hostname
log('Configure /etc/hostname') { level :debug }
file "/etc/hostname" do
  owner "root"
  group "root"
  mode "0755"
  content "#{new_resource.short_hostname}"
  action :create
end

# Call hostname command
log('Setting hostname.') { level :debug }<commit_msg>Comply to FC002: Avoid string interpolation where not required.<commit_after>#
# Cookbook Name:: system
# Provider:: hostname
#
# Copyright 2013, Tacit Knowledge, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

action :set do

require 'socket'

def local_ip
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily
  UDPSocket.open do |s|
    s.connect '172.16.31.10', 1
    s.addr.last
  end
  ensure
    Socket.do_not_reverse_lookup = orig
end

# get node IP
node_ip = local_ip
log("Node IP: #{node_ip}") { level :debug }

# ensure the required short hostname is lower case
new_resource.short_hostname.downcase!

# set hostname from short or long (when domain_name set)
unless new_resource.domain_name.nil? || new_resource.domain_name == ''
  hostname = "#{new_resource.short_hostname}.#{new_resource.domain_name}"
  hosts_list = "#{new_resource.short_hostname}.#{new_resource.domain_name} #{new_resource.short_hostname}"
else
  hostname = new_resource.short_hostname
  hosts_list = new_resource.short_hostname
end
log("Setting hostname for '#{hostname}'.") { level :debug }

# Update /etc/hosts
log('Configure /etc/hosts.') { level :debug }
template "/etc/hosts" do
  source "hosts.erb"
  variables(
    :node_ip => node_ip,
    :hosts_list => hosts_list,
    :static_hosts => new_resource.static_hosts
    )
  mode 0744
end

# Update /etc/hostname
log('Configure /etc/hostname') { level :debug }
file "/etc/hostname" do
  owner "root"
  group "root"
  mode "0755"
  content new_resource.short_hostname
  action :create
end

# Call hostname command
log('Setting hostname.') { level :debug }
{'fraction_non_alphanumeric': 0.10577819198508853, 'fraction_numerical': 0.012814538676607642, 'mean_word_length': 4.447969543147208, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5356879', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1347, 'n_words': 436}