Add Agent.user attribute.

This commit is contained in:
Daniel Nichter
2013-04-10 15:52:18 -06:00
parent 19287b6526
commit aecdca7420
2 changed files with 43 additions and 26 deletions

View File

@@ -1256,6 +1256,12 @@ has 'alias' => (
required => 0, required => 0,
); );
has 'user' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'versions' => ( has 'versions' => (
is => 'ro', is => 'ro',
isa => 'Maybe[HashRef]', isa => 'Maybe[HashRef]',
@@ -5005,15 +5011,7 @@ sub load_local_agent {
my $agent; my $agent;
my $agent_file = $lib_dir . "/agent"; my $agent_file = $lib_dir . "/agent";
if ( $agent_uuid ) { if ( -f $agent_file ) {
_info("Re-creating Agent with UUID $agent_uuid");
chomp(my $hostname = `hostname`);
$agent = Percona::WebAPI::Resource::Agent->new(
uuid => $agent_uuid,
hostname => $hostname,
);
}
elsif ( -f $agent_file ) {
_info("Reading saved Agent from $agent_file"); _info("Reading saved Agent from $agent_file");
my $agent_hashref = decode_json(slurp($agent_file)); my $agent_hashref = decode_json(slurp($agent_file));
$agent = Percona::WebAPI::Resource::Agent->new(%$agent_hashref); $agent = Percona::WebAPI::Resource::Agent->new(%$agent_hashref);
@@ -5050,6 +5048,12 @@ sub init_agent {
# Optional args # Optional args
my $_oktorun = $args{oktorun} || sub { return $oktorun }; my $_oktorun = $args{oktorun} || sub { return $oktorun };
# Update these attribs every time the agent is initialized.
# Other optional attribs, like versions, are left to the caller.
chomp(my $hostname = `hostname`);
$agent->{hostname} = $hostname;
$agent->{user} = $ENV{USER} || $ENV{LOGNAME};
# Try forever to create/update the Agent. The tool can't # Try forever to create/update the Agent. The tool can't
# do anything without an Agent, so we must succeed to proceed. # do anything without an Agent, so we must succeed to proceed.
while ( $_oktorun->() ) { while ( $_oktorun->() ) {
@@ -5188,27 +5192,34 @@ sub run_agent {
# Load and update the local (i.e. existing) agent, or create a new one. # Load and update the local (i.e. existing) agent, or create a new one.
my ($action, $link); my ($action, $link);
if ( !$agent ) { if ( !$agent ) {
$agent = load_local_agent ( if ( $agent_uuid ) {
_info("Re-creating Agent with UUID $agent_uuid");
$agent = Percona::WebAPI::Resource::Agent->new(
uuid => $agent_uuid,
versions => $versions,
);
}
else {
# First try to load the local agent.
$agent = load_local_agent(
lib_dir => $lib_dir, lib_dir => $lib_dir,
agent_uuid => $agent_uuid,
); );
if ( $agent ) { if ( $agent ) {
# Loaded (or re-created) local agent. # Loaded local agent.
$action = 'put'; # update $action = 'put'; # update
$link = $entry_links->{agents} . '/' . $agent->uuid; $link = $entry_links->{agents} . '/' . $agent->uuid;
$agent->{versions} = $versions; $agent->{versions} = $versions;
} }
else { else {
# No local agent and --agent-uuid wasn't give. # No local agent and --agent-uuid wasn't give.
chomp(my $hostname = `hostname`);
$agent = Percona::WebAPI::Resource::Agent->new( $agent = Percona::WebAPI::Resource::Agent->new(
hostname => $hostname,
versions => $versions, versions => $versions,
); );
$action = 'post'; # create $action = 'post'; # create
$link = $entry_links->{agents}; $link = $entry_links->{agents};
} }
} }
}
$agent = init_agent( $agent = init_agent(
agent => $agent, agent => $agent,
action => $action, action => $action,

View File

@@ -40,6 +40,12 @@ has 'alias' => (
required => 0, required => 0,
); );
has 'user' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'versions' => ( has 'versions' => (
is => 'ro', is => 'ro',
isa => 'Maybe[HashRef]', isa => 'Maybe[HashRef]',