Skip to content

Commit

Permalink
Merge pull request #158 from Kegeruneku/bug_3047/fix_rudder_detection
Browse files Browse the repository at this point in the history
Bug 3047/fix rudder detection
  • Loading branch information
g-bougard committed Sep 23, 2016
2 parents 4006aff + c94547e commit 8285f24
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/FusionInventory/Agent/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ my %fields = (
CMD/ ],
REGISTRY => [ qw/NAME REGVALUE HIVE/ ],
REMOTE_MGMT => [ qw/ID TYPE/ ],
RUDDER => [ qw/AGENT UUID HOSTNAME/ ],
RUDDER => [ qw/AGENT UUID HOSTNAME SERVER_ROLES/ ],
SLOTS => [ qw/DESCRIPTION DESIGNATION NAME STATUS/ ],
SOFTWARES => [ qw/COMMENTS FILESIZE FOLDER FROM HELPLINK INSTALLDATE
NAME NO_REMOVE RELEASE_TYPE PUBLISHER
Expand Down
82 changes: 56 additions & 26 deletions lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,101 @@ use English qw(-no_match_vars);
use FusionInventory::Agent::Tools;

sub isEnabled {
return -r '/opt/rudder/etc/uuid.hive';
return -r getUuidFile();
}

sub getUuidFile {
return (($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\etc\uuid.hive' : '/opt/rudder/etc/uuid.hive');
}

sub doInventory {
my (%params) = @_;

my $inventory = $params{inventory};
my $logger = $params{logger};
my $inventory = $params{inventory};
my $logger = $params{logger};

my $uuid_hive = getUuidFile();

# get Rudder UUID
# Get Rudder UUID
my $Uuid = getFirstLine(
logger => $logger, file => '/opt/rudder/etc/uuid.hive'
logger => $logger, file => $uuid_hive
);
# get all agents running on that machine
# Get all agents running on that machine
my @agents = _manageAgent(
logger => $logger, command => 'ls /var/rudder'
logger => $logger
);
# get machine hostname
my $command = $OSNAME eq 'linux' ? 'hostname --fqd' : 'hostname';
# Get machine hostname
my $command = $OSNAME eq 'linux' ? 'hostname --fqdn' : 'hostname';
my $hostname = getFirstLine(
logger => $logger, command => $command
);
# Get server roles
my @serverRoles = _listServerRoles();

my $rudder = {
HOSTNAME => $hostname,
UUID => $Uuid,
AGENT => \@agents,
SERVER_ROLES => { SERVER_ROLE => \@serverRoles },
};

$inventory->addEntry(
section => 'RUDDER', entry => $rudder
);
}

sub _listServerRoles {
my $server_roles_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\etc\server-roles.d' : '/opt/rudder/etc/server-roles.d';
my @server_roles;

if (-d "$server_roles_dir") {
opendir(DIR, $server_roles_dir); # or die $!;

while (my $file = readdir(DIR)) {
# Use a regular expression to ignore files beginning with a period
next if ($file =~ m/^\./);
push @server_roles, $file;
}
closedir(DIR);
}
return @server_roles;
}

sub _manageAgent {
my $handle = getFileHandle(@_);
my %params = @_;
my $logger = $params{logger};

my @agents;

# each line could be a new agent
while(my $name = <$handle>){
# Potential agent directory candidates
my %agent_candidates = ( '/var/rudder/cfengine-community' => 'cfengine-community',
'/var/cfengine' => 'cfengine-nova',
'C:/Program Files/Cfengine' => 'cfengine-nova',
);

foreach my $candidate (keys(%agent_candidates)){

chomp $name;
# verify agent name
next unless $name =~ /cfengine/;
# Verify if the candidate is installed and configured
next unless ( -e "${candidate}/policy_server.dat" );

my $server_hostname_file = "/var/rudder/$name/policy_server.dat";
my $uuid_file = "/var/rudder/$name/rudder-server-uuid.txt";
my $cfengine_key_file = "/var/rudder/$name/ppkeys/localhost.pub";
# Get a list of useful file paths to key Rudder components
my $agent_name = "$agent_candidates{${candidate}}";
my $server_hostname_file = "${candidate}/policy_server.dat";
my $uuid_file = "${candidate}/rudder-server-uuid.txt";
my $cfengine_key_file = "${candidate}/ppkeys/localhost.pub";

# get policy server hostname
my $serverHostname = getFirstLine (
logger => $logger,
file => $server_hostname_file
file => $server_hostname_file
);
chomp $serverHostname;

# get policy server uuid
# Get the policy server UUID
#
# the default file is no longer /var/rudder/tmp/uuid.txt since the
# The default file is no longer /var/rudder/tmp/uuid.txt since the
# change in http://www.rudder-project.org/redmine/issues/2443.
# we gracefully fallback to the old default if we can not find the
# new file.
# We gracefully fallback to the old default if the new file cannot
# be found.
my $serverUuid = getFirstLine (
logger => $logger,
file => ( -e "$uuid_file" ) ? $uuid_file : "/var/rudder/tmp/uuid.txt"
Expand All @@ -91,7 +122,7 @@ sub _manageAgent {

# build agent from datas
my $agent = {
AGENT_NAME => $name,
AGENT_NAME => $agent_name,
POLICY_SERVER_HOSTNAME => $serverHostname,
CFENGINE_KEY => $cfengineKey,
OWNER => $owner,
Expand All @@ -102,7 +133,6 @@ sub _manageAgent {

}

close $handle;
return @agents;
}

Expand Down

0 comments on commit 8285f24

Please sign in to comment.