first commit
This commit is contained in:
63
internal/space_creator/static/dd.sethostname.sh
Normal file
63
internal/space_creator/static/dd.sethostname.sh
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
# vi: set tabstop=4 shiftwidth=4 noexpandtab :
|
||||
#------------------------------------------------#
|
||||
# Модуль настройки hostname.
|
||||
#
|
||||
# В отличии от стокового модуля "hostname",
|
||||
# данный модуль так же патчит файл `/etc/hosts`.
|
||||
# . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
# - dd.sethostname:
|
||||
# name: my-virtual-machine
|
||||
#------------------------------------------------#
|
||||
source $1
|
||||
set -euo pipefail
|
||||
|
||||
__curr_hostname="$(hostname)"
|
||||
__changed=false
|
||||
|
||||
if [[ "$__curr_hostname" != "$name" ]]; then
|
||||
hostnamectl set-hostname "$name"
|
||||
perl - "$__curr_hostname" "$name" <<'EOP'
|
||||
#!perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use Data::Dumper qw(Dumper);
|
||||
|
||||
my ($oldhost, $newhost) = @ARGV;
|
||||
my %hosts;
|
||||
|
||||
open(FILE_HOSTS, "<", "/etc/hosts") || die $!;
|
||||
while(<FILE_HOSTS>) {
|
||||
chomp;
|
||||
if (/^#/ || /^\s*$/) {
|
||||
next;
|
||||
}
|
||||
my @pair = split(/\s+/, $_, 2);
|
||||
my @values = split(/\s+/, $pair[1]);
|
||||
$hosts{$pair[0]} = [@values];
|
||||
}
|
||||
close(FILE_HOSTS);
|
||||
|
||||
while (my ($key, $value) = each(%hosts)) {
|
||||
my $i = 0;
|
||||
foreach my $host (@{ $value }) {
|
||||
if ($host eq $oldhost) {
|
||||
@{ $value }[$i] = $newhost;
|
||||
$hosts{$key} = [@{ $value }[$i]];
|
||||
}
|
||||
$i = $i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
open(FILE_HOSTS, ">", "/etc/hosts") || die $!;
|
||||
foreach my $key (sort(keys(%hosts))) {
|
||||
print FILE_HOSTS "$key @{$hosts{$key}}\n";
|
||||
}
|
||||
close(FILE_HOSTS);
|
||||
EOP
|
||||
__changed=true
|
||||
fi
|
||||
|
||||
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
|
||||
|
||||
echo "{ \"changed\": $__changed }"
|
||||
Reference in New Issue
Block a user