Skip to content

Commit

Permalink
Add hostconfig
Browse files Browse the repository at this point in the history
Import hostconfig from its own repository mariux64/hostconfig. No need
to import history, because there are only three commits (one import, one
infrastructure and a single change).
  • Loading branch information
donald committed Nov 27, 2018
1 parent 1c8e076 commit 74d8a45
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
141 changes: 141 additions & 0 deletions hostconfig/hostconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#! /usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

my %TAGS; # ( host => { tag=>1, ... }, ... )
my %options;

$0 =~ /get-hostconfig/ and warn "$0: deprecated. Please use hostconfig\n";

sub read_hostconfig {
open my $in,'<','/etc/hostconfig' or die "/etc/hostconfig: $!\n";
while (<$in>) {
s/#.*//;
chomp;
my ($host,$attr,$value)=split ' ',$_,3;
$host or next;
$attr or next;
$attr ne 'tag' and $TAGS{$host}->{$attr}=2;
$attr eq 'tag' or next;
$TAGS{$host}||={};
my @tag=split ' ',$value;
for my $tag (@tag) {
my ($neg,$tag)=$tag=~/^(!?)(.+)/;
$neg and next;
$TAGS{$host}->{$tag}=1;
}
}
}


sub evalue_expression {

my ($hostname,$expression)=@_;

exists $TAGS{$hostname} or warn "$0: hostname $hostname : no info\n";
my $tags=$TAGS{$hostname};

my @l = grep /\S/,split /([&\|\(\)!]|\s+)/,$expression;

$options{'debug'} and warn join(' ',map("'$_'",@l)),"\n";
for (@l) {
s/\s*(.+?)\s*/$1/;
if (/^[a-z][a-z0-9$_-]*$/i) {
$_=exists $tags->{$_} ? 1 : 0;
}
}
$options{'debug'} and warn join(' ',map("'$_'",@l)),"\n";

my $result=eval join(' ',@l);
$@ and die "$0: syntax error in expression: $expression\n";
$options{'debug'} and warn "result: ".($result ? 'true' : 'false'),"\n";

return $result;
}

sub populate_node {
my ($hostname)=@_;
my $DIR='/node/tags';
-d $DIR and system "rm -rf $DIR" and exit 1;
mkdir $DIR or die "$DIR: $!\n";
my $tags=$TAGS{$hostname};
for (keys %$tags) {
open my $touch,'>',"$DIR/$_";
}
}

sub list {
my ($expression)=@_;
my @ret;
for my $hostname (sort keys %TAGS) {
if (evalue_expression($hostname,$expression)) {
push @ret,$hostname;
}
}
return @ret;
}


sub USAGE {
<<"EOF";
usage: $0 [options] - show tags
$0 [options] expression... - evaluate and set exit status
$0 --list [options] expression... - enumerate hosts for which expression is true
$0 [options] --populate-node - (re-)create /node/tags/
options:
--hostname host : evaluate for another host
--debug : guess what this does
expressions:
boolean terms: tags from hostconfig
unary operator: '!'
binary operators: '&','|'
precedence: '(',')'
expression example:
if $0 distmaster ;then echo "lala";fi
if $0 'mx64 & desktop' ;then echo "lala";fi
if $0 'testing | (mx64b & !desktop)' ;then echo "lala";fi
EOF
}

my $hostname;

GetOptions
(
'hostname=s' => \$options{'hostname'},
'debug' => \$options{'debug'},
'populate-node' => \$options{'populate-node'},
'list' => \$options{'list'},

) or die USAGE;

chomp($hostname = $options{'hostname'} ? $options{'hostname'} : `uname -n`);$hostname=~s/\..*//;
read_hostconfig();

my $expression = join ' ',@ARGV;

if ($options{'populate-node'}) {
populate_node($hostname);
} elsif ($options{'list'}) {
@ARGV or die USAGE;
print join(' ',list($expression)),"\n";
} else {
if (@ARGV) {
exit (evalue_expression($hostname,$expression) ? 0 : 1);
} else {
exists $TAGS{$hostname} or warn "$0: hostname $hostname : no info\n";
my $tags=$TAGS{$hostname};
print join (' ',sort keys %$tags),"\n";
}
}


1 change: 1 addition & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,5 @@ install_exec setuid/setuid "$DESTDIR$usr_sbindir/s
install_exec uvpn/uvpn "$DESTDIR$usr_bindir/uvpn"
install_exec mxmount/mxmount "$DESTDIR$usr_bindir/mxmount"
install_data mxmount/mxmount.service "$DESTDIR$systemdunitdir/mxmount.service"
install_exec hostconfig/hostconfig "$DESTDIR$usr_sbindir/hostconfig"
exit

0 comments on commit 74d8a45

Please sign in to comment.