-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/usr/bin/env bash | ||
|
||
# NOTE: DOES NOT APPLY TO FUNCTIONS CALLED INSIDE IF CONDITIONS OR WITH ||/&& CHAINS | ||
set -e | ||
|
||
eval "$(nk plugin helper bash 2>/dev/null)" | ||
|
||
hostname::_provision_macos() { | ||
declare current_host_name | ||
current_host_name="$(scutil --get 'HostName')" | ||
if [[ "$current_host_name" != "$hostname" ]]; then | ||
sudo scutil --set 'HostName' "$hostname" \ | ||
|| return "$(nk::error "$?" 'failed setting HostName')" | ||
changed='true' | ||
fi | ||
|
||
declare current_local_host_name | ||
current_local_host_name="$(scutil --get 'LocalHostName')" | ||
if [[ "$current_local_host_name" != "$hostname" ]]; then | ||
sudo scutil --set 'LocalHostName' "$hostname" \ | ||
|| return "$(nk::error "$?" 'failed setting LocalHostName')" | ||
changed='true' | ||
fi | ||
|
||
declare current_computer_name | ||
current_computer_name="$(scutil --get 'ComputerName')" | ||
if [[ "$current_computer_name" != "$hostname" ]]; then | ||
sudo scutil --set 'ComputerName' "$hostname" \ | ||
|| return "$(nk::error "$?" 'failed setting ComputerName')" | ||
changed='true' | ||
fi | ||
|
||
if [[ "$changed" == 'true' ]]; then | ||
dscacheutil -flushcache \ | ||
|| return "$(nk::error "$?" 'failed flushing ds cache')" | ||
fi | ||
} | ||
|
||
hostname::_provision_linux() { | ||
declare current_hostname | ||
current_hostname="$(hostnamectl hostname)" | ||
|
||
if [[ "$current_hostname" != "$hostname" ]]; then | ||
sudo hostnamectl hostname "$hostname" \ | ||
|| return "$(nk::error "$?" 'failed setting hostname')" | ||
changed='true' | ||
fi | ||
} | ||
|
||
hostname::_provision_unix() { | ||
declare current_hostname | ||
current_hostname="$(cat /etc/hostname)" | ||
|
||
if [[ "$current_hostname" != "$hostname" ]]; then | ||
sudo tee /etc/hostname <<< "$hostname" \ | ||
|| return "$(nk::error "$?" 'failed setting hostname')" | ||
changed='true' | ||
fi | ||
} | ||
|
||
hostname::_provision() { | ||
if [[ "$OSTYPE" == darwin* ]]; then | ||
hostname::_provision_macos | ||
elif which hostnamectl >/dev/null; then | ||
hostname::hostname::_provision_linux | ||
else | ||
hostname::hostname::_provision_unix | ||
fi | ||
} | ||
|
||
hostname::provision() { | ||
while read -r hostname; do | ||
# provision | ||
declare status='success' | ||
declare changed='false' | ||
declare output='' | ||
if ! nk::run_for_output output hostname::_provision; then | ||
status='failed' | ||
fi | ||
|
||
declare description="hostname ${hostname}" | ||
|
||
# log state details | ||
nk::log_result \ | ||
"$status" \ | ||
"$changed" \ | ||
"$description" \ | ||
"$output" | ||
done <<< "$(jq -r --compact-output '.[].state')" | ||
} | ||
|
||
case "$1" in | ||
provision) | ||
hostname::provision "${@:2}" | ||
;; | ||
*) | ||
echo "hostname: unrecognized subcommand ${1}" 1>&2 | ||
exit 1 | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: hostname | ||
when: family == "unix" | ||
|
||
provision: | ||
when: declaration == "hostname" | ||
|
||
executable: hostname.sh | ||
|
||
schema: | ||
$schema: https://json-schema.org/draft/2020-12/schema | ||
type: string |