Archived
1
0

Can synchronize multiple target of ssh_keys

This commit is contained in:
Mercier Pierre-Olivier 2013-09-06 05:28:54 +02:00
parent 068e23a4ce
commit b49a2322d9
3 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,135 @@
#! /usr/bin/env perl
use v5.10.1;
use strict;
use warnings;
use XML::LibXML;
use lib "../../";
use ACU::LDAP;
use ACU::Log;
use ACU::Process;
use ACU::VCS::Git;
my %master_actions =
(
"update" => \&master_update,
"register" => \&master_register,
);
my %node_actions =
(
"git" => { "update" => \&node_git_update, },
);
my @nodes;
my $service;
sub master_register
{
my $args = shift;
if ($args->{param}{nodename}) {
my $nodename = "sync_ssh_keys_".$args->{param}{nodename};
log INFO, "New node: $nodename";
push @nodes, "$nodename";
}
else {
log WARN, "nodename empty, cannot register new node";
}
}
sub master_update
{
my $args = shift;
my %params = ("action" => "update");
for (my $i = $args->{unamed}; $i > 0; $i--) {
$params{"__$i"} = $args->{param}{$i};
}
log TRACE, %params;
log DEBUG, "Start updating nodes...";
Process::Client::paralaunch(\@nodes, \%params);
}
sub process_master
{
my ($given_args, $args) = @_;
my $action = $args->{param}{action} // "update";
if (! exists $master_actions{$action}) {
log WARN, "Unknown action '$action' for sync_ssh_keys master process.";
}
return $master_actions{$action}($args);
}
sub node_git_update
{
my $args = shift;
Git::init_conf();
my $commit_msg = "Updating keys for ";
for (my $i = $args->{unamed}; $i > 0; $i--)
{
Git::user_add($args->{param}{$i}, 1);
$commit_msg .= $args->{param}{$i}.", ";
}
Git::save_conf("$commit_msg from sync_ssh_keys");
return 1;
}
sub process_node
{
my ($given_args, $args) = @_;
my $action = $args->{param}{action} // "update";
if (! exists $node_actions{$service}{$action}) {
log WARN, "Unknown action '$action' for sync_ssh_keys $service node process.";
}
return $node_actions{$service}{$action}($args);
}
if ($0 =~ /^(?:.*\/)?sync_ssh_keys_(?:([a-zA-Z0-9]+)_)?([a-zA-Z0-9]+).pl$/) {
$service = $1 // "git";
log INFO, "Starting sync_ssh_keys.pl as $service for server $2";
if ($service eq "git")
{
$Git::git_server = $2;
log INFO, "Start by syncing all users key...";
Git::users_update();
log INFO, "Registering a new sync_ssh_keys node...";
Process::Client::launch("sync_ssh_keys", {"action" => "register", "nodename" => $service."_".$2});
Process::register("sync_ssh_keys_".$service."_$2", \&process_node);
}
else {
log FATAL, "Unknown service '$service', exiting.";
}
}
elsif ($0 =~ /^(?:.*\/)?sync_ssh_keys/) {
log INFO, "Starting sync_ssh_keys.pl as master process";
Process::register("sync_ssh_keys", \&process_master);
}
else {
die ("Bad filename.");
}

View File

@ -0,0 +1 @@
sync_ssh_keys.pl

View File

@ -0,0 +1 @@
sync_ssh_keys.pl