Skip to content

Commit

Permalink
WIP efl_kill_process
Browse files Browse the repository at this point in the history
  • Loading branch information
neilhwatson committed Aug 26, 2015
1 parent 3fa2564 commit 0a0d102
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 48 deletions.
7 changes: 7 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ all: $(EFL_FILES) $(AUTORUN) $(EFL_TEST_FILES)
$(EFL_FILES): $(EFL_LIB) src/includes/param_parser.cf \
src/includes/param_file_picker.cf src/$@
cp src/$@ $@
$(eflmaker) --tar $@ \
--tag no_csv_param_parser -i src/includes/no_csv_param_parser.cf
$(eflmaker) --tar $@ \
--tag param_parser -i src/includes/param_parser.cf
$(eflmaker) --tar $@ \
Expand Down Expand Up @@ -294,6 +296,11 @@ efl_edit_template: version syntax templates \
test/masterfiles/efl_data/$$@.json
prove t/$@.t

.PHONY: efl_kill_process
efl_kill_process: version syntax efl_start_service \
test/masterfiles/efl_data/efl_main.json
prove t/$@.t

.PHONY: check
# TODO how to make all json and yaml files here?
check: test/$(EFL_LIB) $(cfstdlib) $(EFL_FILES) \
Expand Down
82 changes: 82 additions & 0 deletions masterfiles/lib/3.7/EFL/evolve_freelib.cf
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,88 @@ bundle agent efl_start_service ( ref )
ifvarclass => canonify( "efl_start_service_command_${d[${i}][process_regex]}_${i}" );
}

bundle agent efl_kill_process ( ref )
{
meta:
"purpose" string => "Kll matching processes.";
"NOTE" string => "Does not accept CSV param file";
"class" string => "Class";
"process_regex" string => "Process regex";
"owners" string => "List of possible process owners";
"min_rsize" string => "Min rsize in kilobytes";
"min_elapsed_time" string => "Min run time in minutes";
"min_process_count" string => "Min number of matched processes";
"promisee" string => "Free form promisess";

# param spec
# {
# "class" : "promise_if_set",
# "process_regex" : "command_regex_to_match",
# "owners" : [ "process", "owners" ],
# "min_rsize" : "1",
# "min_elapsed_time" : "1",
# "min_process_count" : "1",
# "promisee" : "Who this promise is to"
# }

vars:
"param_file" string => canonifyuniquely( "${ref}" );

"data"
comment => "Parse json file",
if => "readdata_${param_file}",
data => readdata( "${ref}", "auto" );

"d"
comment => "Expand vars inside of container",
if => "readdata_${param_file}",
data => data_expand( "data" );

"i_unsorted"
comment => "Index data, but unsorted",
slist => getindices( "d" );

"i"
comment => "Ensure data is ordered because read functions may not",
slist => sort( i_unsorted, int );

# We must convert the internal container lists into standalone lists.
"owners_${param_file}"
comment => "convert internal container lists into standalone lists",
slist => getvalues( "d[owners]" );

processes:

"${d[${i}][process_regex]}" -> { "${d[${i}][promisee]}" }
comment => "Kill ALL matching processes",
handle => "efl_kill_process_processes_proc",
process_count => efl_process_count( "${min_process_count}" ),
process_select => efl_kill_process( "${process_regex}"
, "@{owners_${param_file}}", "${min_rsize}"
, "${min_elapsed_time}" ),
classes => efl_rkn( "${this.promiser}", "${this.handle}" ),
action => efl_delta_reporting( "${this.handle}"
, "${this.promiser}", "${d[${i}][promisee]}", "1" ),
ifvarclass => "${d[${i}][class]}",
signals => { "term", "kill" };
}

body process_count efl_process_count ( min_process_count )
{
match_range => irange( "${min_process_count}", "inf" );
}

body process_select efl_kill_process( process_regex, owners, min_rsize,
min_elapsed_time ) {
command => "${process_regex}";
# process_owner => { @{owners} };
rsize => irange( "${min_rsize}", "inf" );
ttime_range => irange( accumulated(0, 0, 0, 0, ${min_elapsed_time}, 0)
, "inf" );

process_result => "command.process_owner.rsize.ttime_range";
}

bundle agent efl_service_recurse ( ref )
{
meta:
Expand Down
19 changes: 19 additions & 0 deletions src/includes/no_csv_param_parser.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"param_file" string => canonifyuniquely( "${ref}" );

"data"
comment => "Parse json file",
if => "readdata_${param_file}",
data => readdata( "${ref}", "auto" );

"d"
comment => "Expand vars inside of container",
if => "readdata_${param_file}",
data => data_expand( "data" );

"i_unsorted"
comment => "Index data, but unsorted",
slist => getindices( "d" );

"i"
comment => "Ensure data is ordered because read functions may not",
slist => sort( i_unsorted, int );
65 changes: 65 additions & 0 deletions src/masterfiles/lib/3.7/EFL/evolve_freelib.cf
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,71 @@ bundle agent efl_start_service ( ref )
ifvarclass => canonify( "efl_start_service_command_${d[${i}][process_regex]}_${i}" );
}

bundle agent efl_kill_process ( ref )
{
# This is not ready for use
meta:
"purpose" string => "Kll matching processes.";
"NOTE" string => "Does not accept CSV param file";
"class" string => "Class";
"process_regex" string => "Process regex";
"owners" string => "List of possible process owners";
"min_rsize" string => "Min rsize in kilobytes";
"min_elapsed_time" string => "Min run time in hours";
"min_process_count" string => "Min number of matched processes";
"promisee" string => "Free form promisess";

# param spec
# {
# "class" : "promise_if_set",
# "process_regex" : "command_regex_to_match",
# "owners" : [ "process", "owners" ],
# "min_rsize" : "1",
# "min_elapsed_time" : "1",
# "min_process_count" : "1",
# "promisee" : "Who this promise is to"
# }

vars:
<efl no_csv_param_parser efl/>

# We must convert the internal container lists into standalone lists.
"owners_${param_file}"
comment => "convert internal container lists into standalone lists",
slist => getvalues( "d[owners]" );

processes:

"${d[${i}][process_regex]}" -> { "${d[${i}][promisee]}" }
comment => "Kill ALL matching processes",
handle => "efl_kill_process_processes_proc",
process_count => efl_process_count( "${min_process_count}" ),
process_select => efl_kill_process( "${process_regex}"
, "@{owners_${param_file}}", "${min_rsize}"
, "${min_elapsed_time}" ),
classes => efl_rkn( "${this.promiser}", "${this.handle}" ),
action => efl_delta_reporting( "${this.handle}"
, "${this.promiser}", "${d[${i}][promisee]}", "1" ),
ifvarclass => "${d[${i}][class]}",
signals => { "term", "kill" };
}

body process_count efl_process_count ( min_process_count )
{
match_range => irange( "${min_process_count}", "inf" );
}

body process_select efl_kill_process( process_regex, owners, min_rsize,
min_elapsed_time ) {
command => "${process_regex}";
# process_owner => { @{owners} };
rsize => irange( "${min_rsize}", "inf" );
ttime_range => irange( accumulated(0, 0, 0, 0, ${min_elapsed_time}, 0)
, "inf" );

process_result => "command.process_owner.rsize.ttime_range";
}

bundle agent efl_service_recurse ( ref )
{
meta:
Expand Down
82 changes: 82 additions & 0 deletions t/efl_kill_process.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw/ WIFEXITED /;
use Cwd;
use Git::Repository;
use File::Copy qw/ copy /;
use File::Path qw/ make_path remove_tree /;
use feature 'say';
use Test::More;
use Carp;

my $start_dir = getcwd();
my $version = '3.7';
my $bundle = 'efl_kill_process';
my @tests = (
{
name => "$bundle using json data.",
subtest => sub
{ run_agent_and_test({
Dclass => "test_json",
bundle => $bundle
}) },
},
);

# Include the extra daemon start test
my $number_of_tests = scalar @tests * 2 ;

for my $next_test ( @tests ) {

# Ensure test daemon to kill is running.
subtest "Start test daemon for later kill test" => sub {
run_agent_and_test({
Dclass => "test_json",
bundle => "efl_start_service"
}) };

# Minimum kill elapsed time in policy is 1 minutes, so we sleep
sleep 60;
subtest $next_test->{name} => $next_test->{subtest};
}

done_testing( $number_of_tests );

#
# subs
#

sub run_agent_and_test {
my ( $arg_ref ) = @_;
my $Dclass = $arg_ref->{Dclass};
my $bundle = $arg_ref->{bundle};

croak "No Dlcass provided" unless $Dclass;
croak "No bundle provided" unless $bundle;

# Run cf-agent test policy
chdir 'test/masterfiles' or croak "Cannot cd to test/masterfiles $!";
my $cf_agent = "cf-agent -D $Dclass,$bundle -Kf ./promises.cf";
ok( WIFEXITED( ( system $cf_agent ) >> 8), "Pull or new clone" );

# Test the results of cf-agent test policy
chdir '../serverspec' or croak "Cannot cd to test/serverspec $!";
my $rspec = "rspec spec/localhost/$bundle.rb >/dev/null";
ok( WIFEXITED( ( system $rspec ) >> 8), $rspec);

# Return to original dir
chdir $start_dir or croak "Cannot cd to $start_dir $!";

return 1;
}


=pod
=head1 SYNOPSIS
Testing efl_kill_process
=cut
Loading

0 comments on commit 0a0d102

Please sign in to comment.