<commit_before>module Propellor.Property.Hostname where

import Propellor
import qualified Propellor.Property.File as File

-- | Ensures that the hostname is set to the HostAttr value.
-- Configures both /etc/hostname and the current hostname.
--
-- When the hostname is a FQDN, also configures /etc/hosts,
-- with an entry for 127.0.1.1, which is standard at least on Debian
-- to set the FDQN (127.0.0.1 is localhost).
sane :: Property
sane = Property ("sane hostname") (ensureProperty . setTo =<< getHostName)

setTo :: HostName -> Property
setTo hn = combineProperties desc go
	`onChange` cmdProperty "hostname" [basehost]
  where
	desc = "hostname " ++ hn
	(basehost, domain) = separate (== '.') hn

	go = catMaybes
		[ Just $ "/etc/hostname" `File.hasContent` [basehost]
		, if null domain
			then Nothing 
			else Just $ File.fileProperty desc
				addhostline "/etc/hosts"
		]
	
	hostip = "127.0.1.1"
	hostline = hostip ++ "\t" ++ hn ++ " " ++ basehost

	addhostline ls = hostline : filter (not . hashostip) ls
	hashostip l = headMaybe (words l) == Just hostip<commit_msg>propellor spin<commit_after>module Propellor.Property.Hostname where

import Propellor
import qualified Propellor.Property.File as File

-- | Ensures that the hostname is set to the HostAttr value.
-- Configures /etc/hostname and the current hostname.
--
-- When the hostname is a FQDN, also configures /etc/mailname
-- with the domain part.
-- A FQDN also configures /etc/hosts, with an entry for 127.0.1.1, which is
-- standard at least on Debian to set the FDQN (127.0.0.1 is localhost).
sane :: Property
sane = Property ("sane hostname") (ensureProperty . setTo =<< getHostName)

setTo :: HostName -> Property
setTo hn = combineProperties desc go
	`onChange` cmdProperty "hostname" [basehost]
  where
	desc = "hostname " ++ hn
	(basehost, domain) = separate (== '.') hn

	go = catMaybes
		[ Just $ "/etc/hostname" `File.hasContent` [basehost]
		, if null domain
			then Nothing 
			else Just $ File.fileProperty desc
				addhostline "/etc/hosts"
		, if null domain
			then Nothing
			else Just $ "/etc/mailname" `File.hasContent` [domain]
		]
	
	hostip = "127.0.1.1"
	hostline = hostip ++ "\t" ++ hn ++ " " ++ basehost

	addhostline ls = hostline : filter (not . hashostip) ls
	hashostip l = headMaybe (words l) == Just hostip
{'fraction_non_alphanumeric': 0.11779991338241663, 'fraction_numerical': 0.015591165006496318, 'mean_word_length': 3.502923976608187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '1529841', 'n_tokens_mistral': 823, 'n_tokens_neox': 769, 'n_words': 262}