foreman-tasks 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e10fb3f1d62ca016568197df4df727b25c73b68
4
- data.tar.gz: 506d7b4cc63c11fbd931667258122ee48f03a504
3
+ metadata.gz: ae915bf659a976d2dfcac1f2e51cb0a447d080a8
4
+ data.tar.gz: 41bf2fb543e2ba405f828be97d0727ff9ec73c17
5
5
  SHA512:
6
- metadata.gz: 4fe503d8cbfce8eec1ea48f0add2442d1df0acb2729f53d391dfd5068ebf975722f69ffd0801780fae770c02958b3909c466668d368ad39ca345ca6acb9dd2d9
7
- data.tar.gz: 910f71926669c46ef32a8c55a1b666c2a74ac810d176e27c0dfcf451e49b814af871ebf0639d70dc1378d01be48167c0d53c4bf3bd514e02d8302f6c3bb49355
6
+ metadata.gz: 3b641565b6e1360887774df9489089c2e5e960d299b976140f307b15e2b8e7d92815900541124265eead2a6294a57fae798371bab3074f4026f71cc6298ce6f4
7
+ data.tar.gz: b9304f40c6ab2f54a37bc92f93d68e6fb81f3cae68e63d5299994f9e5a5c1bc466d3d081e6047067cac2901e3664e58734421549c3892f2fce400347f871d7ac
@@ -9,18 +9,43 @@
9
9
  # :action:
10
10
  # :enabled: true
11
11
 
12
+ # Task backup configuration can be changed by altering the values in
13
+ # the backup section
14
+ #
15
+ :backup:
16
+ #
17
+ # Whether to back up tasks when they are removed
18
+ #
19
+ :backup_deleted_tasks: true
20
+ #
21
+ # Where to put the tasks which were backed up
22
+ #
23
+ :backup_dir: /var/lib/foreman/tasks-backup
12
24
 
13
25
  # Cleaning configuration: how long should the actions be kept before deleted
14
26
  # by `rake foreman_tasks:clean` task
15
27
  #
16
- # :cleanup:
28
+ :cleanup:
17
29
  #
18
30
  # the period after which to delete all the tasks (by default all tasks are not being deleted after some period)
31
+ # will be deprecated in Foreman 1.18 and the use of rules is recommended.
19
32
  #
20
- # :after: 365d
33
+ # :after: 30d
21
34
  #
22
35
  # per action settings to override the default defined in the actions (self.cleanup_after method)
23
36
  #
24
37
  # :actions:
25
38
  # - :name: Actions::Foreman::Host::ImportFacts
26
39
  # :after: 10d
40
+ #
41
+ # Rules defined in this section by default don't operate
42
+ # on tasks specified in the actions section. This behavior
43
+ # can be overriden by setting the override_actions to true
44
+ :rules:
45
+ # Delete successful tasks after a month
46
+ - :filter: result = success
47
+ :after: 30d
48
+ # Delete everything (any action, any state) after one year
49
+ - :states: all # Either list of state names or all
50
+ :after: 1y
51
+ :override_actions: true
@@ -1,3 +1,5 @@
1
+ require 'csv'
2
+
1
3
  module ForemanTasks
2
4
  # Represents the cleanup mechanism for tasks
3
5
  class Cleaner
@@ -11,11 +13,16 @@ module ForemanTasks
11
13
  end
12
14
  end
13
15
  if cleanup_settings[:after]
16
+ Foreman::Deprecation.deprecation_warning('1.18', _(':after setting in tasks cleanup section is deprecated, use :after in :rules section to set the value. to cleanup rules'))
14
17
  new(options.merge(:filter => '', :after => cleanup_settings[:after])).delete
15
18
  end
16
- actions_with_default_cleanup.each do |action_class, period|
19
+ with_periods = actions_with_default_cleanup
20
+ with_periods.each do |action_class, period|
17
21
  new(options.merge(:filter => "label = #{action_class.name}", :after => period)).delete
18
22
  end
23
+ actions_by_rules(with_periods).each do |hash|
24
+ new(options.merge(hash)).delete
25
+ end
19
26
  end
20
27
  end
21
28
 
@@ -44,6 +51,19 @@ module ForemanTasks
44
51
  @cleanup_settings = SETTINGS[:'foreman-tasks'] && SETTINGS[:'foreman-tasks'][:cleanup] || {}
45
52
  end
46
53
 
54
+ def self.actions_by_rules(actions_with_periods)
55
+ disable_actions_with_periods = "label !^ (#{actions_with_periods.keys.join(', ')})"
56
+ cleanup_settings.fetch(:rules, []).map do |hash|
57
+ next if hash[:after].nil?
58
+ conditions = []
59
+ conditions << disable_actions_with_periods unless hash[:override_actions]
60
+ conditions << hash[:filter] if hash[:filter]
61
+ hash[:states] = [] if hash[:states] == 'all'
62
+ hash[:filter] = conditions.map { |condition| "(#{condition})" }.join(' AND ')
63
+ hash
64
+ end.compact
65
+ end
66
+
47
67
  attr_reader :filter, :after, :states, :verbose, :batch_size, :noop, :full_filter
48
68
 
49
69
  # @param filter [String] scoped search matching the tasks to be deleted
@@ -56,7 +76,8 @@ module ForemanTasks
56
76
  :verbose => false,
57
77
  :batch_size => 1000,
58
78
  :noop => false,
59
- :states => ['stopped'] }
79
+ :states => ['stopped'],
80
+ :backup_dir => ForemanTasks.dynflow.world.persistence.current_backup_dir }
60
81
  options = default_options.merge(options)
61
82
 
62
83
  @filter = options[:filter]
@@ -65,6 +86,7 @@ module ForemanTasks
65
86
  @verbose = options[:verbose]
66
87
  @batch_size = options[:batch_size]
67
88
  @noop = options[:noop]
89
+ @backup_dir = options[:backup_dir]
68
90
 
69
91
  raise ArgumentError, 'filter not speficied' if @filter.nil?
70
92
 
@@ -91,12 +113,33 @@ module ForemanTasks
91
113
  end
92
114
 
93
115
  def delete_tasks(chunk)
94
- ForemanTasks::Task.where(:id => chunk.map(&:id)).delete_all
116
+ tasks = ForemanTasks::Task.where(:id => chunk.map(&:id))
117
+ tasks_to_csv(tasks, @backup_dir, 'foreman_tasks.csv') if @backup_dir
118
+ tasks.delete_all
119
+ end
120
+
121
+ def tasks_to_csv(dataset, backup_dir, file_name)
122
+ with_backup_file(backup_dir, file_name) do |csv, appending|
123
+ csv << ForemanTasks::Task.attribute_names.to_csv unless appending
124
+ dataset.each do |row|
125
+ csv << row.attributes.values.to_csv
126
+ end
127
+ end
128
+ dataset
129
+ end
130
+
131
+ def with_backup_file(backup_dir, file_name)
132
+ FileUtils.mkdir_p(backup_dir) unless File.directory?(backup_dir)
133
+ csv_file = File.join(backup_dir, file_name)
134
+ appending = File.exist?(csv_file)
135
+ File.open(csv_file, 'a') do |f|
136
+ yield f, appending
137
+ end
95
138
  end
96
139
 
97
140
  def delete_dynflow_plans(chunk)
98
141
  dynflow_ids = chunk.find_all { |task| task.is_a? Task::DynflowTask }.map(&:external_id)
99
- ForemanTasks.dynflow.world.persistence.delete_execution_plans({ 'uuid' => dynflow_ids }, batch_size)
142
+ ForemanTasks.dynflow.world.persistence.delete_execution_plans({ 'uuid' => dynflow_ids }, batch_size, @backup_dir)
100
143
  end
101
144
 
102
145
  def prepare_filter
@@ -6,6 +6,37 @@ require 'foreman_tasks/dynflow/persistence'
6
6
  module ForemanTasks
7
7
  # Import all Dynflow configuration from Foreman, and add our own for Tasks
8
8
  class Dynflow::Configuration < ::Foreman::Dynflow::Configuration
9
+ def world_config
10
+ super.tap do |config|
11
+ config.backup_deleted_plans = backup_settings[:backup_deleted_plans]
12
+ config.backup_dir = backup_settings[:backup_dir]
13
+ end
14
+ end
15
+
16
+ def backup_settings
17
+ return @backup_settings if @backup_settings
18
+ backup_options = {
19
+ :backup_deleted_plans => true,
20
+ :backup_dir => default_backup_dir
21
+ }
22
+ settings = SETTINGS[:'foreman-tasks'] && SETTINGS[:'foreman-tasks'][:backup]
23
+ backup_options.merge!(settings) if settings
24
+ @backup_settings = with_environment_override backup_options
25
+ end
26
+
27
+ def default_backup_dir
28
+ File.join(Rails.root, 'tmp', 'task-backup')
29
+ end
30
+
31
+ def with_environment_override(options)
32
+ env_var = ENV['TASK_BACKUP']
33
+ unless env_var.nil?
34
+ # Everything except 0, n, no, false is considered to be a truthy value
35
+ options[:backup_deleted_plans] = !%w[0 n no false].include?(env_var.downcase)
36
+ end
37
+ options
38
+ end
39
+
9
40
  def initialize_persistence
10
41
  ForemanTasks::Dynflow::Persistence.new(default_sequel_adapter_options)
11
42
  end
@@ -5,10 +5,11 @@ namespace :foreman_tasks do
5
5
 
6
6
  * TASK_SEARCH : scoped search filter (example: 'label = "Actions::Foreman::Host::ImportFacts"')
7
7
  * AFTER : delete tasks created after *AFTER* period. Expected format is a number followed by the time unit (s,h,m,y), such as '10d' for 10 days
8
- * STATES : comma separated list of task states to touch with the cleanup, by default only stopped tasks are covered
9
- * NOOP : set to "true" if the task should not actuall perform the deletion
8
+ * STATES : comma separated list of task states to touch with the cleanup, by default only stopped tasks are covered, special value all can be used to clean the tasks, disregarding their states
9
+ * NOOP : set to "true" if the task should not actually perform the deletion
10
10
  * VERBOSE : set to "true" for more verbose output
11
11
  * BATCH_SIZE : the size of batches the tasks get processed in (1000 by default)
12
+ * TASK_BACKUP : set to "true" or "false" to enable/disable task backup
12
13
 
13
14
  If none of TASK_SEARCH, BEFORE, STATES is specified, the tasks will be cleaned based
14
15
  configuration in settings
@@ -21,6 +22,7 @@ namespace :foreman_tasks do
21
22
  options[:after] = ENV['AFTER'] if ENV['AFTER']
22
23
 
23
24
  options[:states] = ENV['STATES'].to_s.split(',') if ENV['STATES']
25
+ options[:states] = [] if options[:states] == ['all']
24
26
 
25
27
  options[:noop] = true if ENV['NOOP']
26
28
 
@@ -52,6 +54,24 @@ namespace :foreman_tasks do
52
54
  printf("%-50s %s\n", action.name, after)
53
55
  end
54
56
  end
57
+ puts
58
+ by_rules = ForemanTasks::Cleaner.actions_by_rules(ForemanTasks::Cleaner.actions_with_default_cleanup)
59
+ if by_rules.empty?
60
+ puts _('No cleanup rules are configured')
61
+ else
62
+ printf("%-50s %-15s %s\n", _('states'), _('delete after'), _('filter'))
63
+ by_rules.each do |hash|
64
+ state = case hash[:states]
65
+ when []
66
+ _('ANY')
67
+ when nil
68
+ 'stopped'
69
+ else
70
+ hash[:states]
71
+ end
72
+ printf("%-50s %-15s %s\n", state, hash[:after], hash[:filter])
73
+ end
74
+ end
55
75
  end
56
76
  end
57
77
 
@@ -1,3 +1,3 @@
1
1
  module ForemanTasks
2
- VERSION = '0.10.2'.freeze
2
+ VERSION = '0.10.3'.freeze
3
3
  end
@@ -38,6 +38,9 @@ msgstr ""
38
38
  msgid "%{value} is wrong format"
39
39
  msgstr ""
40
40
 
41
+ msgid ":after setting in tasks cleanup section is deprecated, use :after in :rules section to set the value. to cleanup rules"
42
+ msgstr ""
43
+
41
44
  msgid "Action"
42
45
  msgstr ""
43
46
 
@@ -207,6 +210,9 @@ msgstr ""
207
210
  msgid "Last occurrence"
208
211
  msgstr ""
209
212
 
213
+ msgid "Last start time"
214
+ msgstr ""
215
+
210
216
  msgid "Latest Warning/Error Tasks"
211
217
  msgstr ""
212
218
 
@@ -268,9 +274,6 @@ msgstr ""
268
274
  msgid "Parent task"
269
275
  msgstr ""
270
276
 
271
- msgid "Play Ansible roles"
272
- msgstr ""
273
-
274
277
  msgid "Please inspect their state, fix their errors and resume them."
275
278
  msgstr ""
276
279
 
@@ -394,6 +397,9 @@ msgstr ""
394
397
  msgid "Task Status"
395
398
  msgstr ""
396
399
 
400
+ msgid "Task aborted: the task might be still running on the proxy"
401
+ msgstr ""
402
+
397
403
  msgid "Task count"
398
404
  msgstr ""
399
405
 
@@ -424,6 +430,9 @@ msgstr ""
424
430
  msgid "The targets are of different types"
425
431
  msgstr ""
426
432
 
433
+ msgid "The task cannot be aborted at the moment."
434
+ msgstr ""
435
+
427
436
  msgid "The task cannot be cancelled at the moment."
428
437
  msgstr ""
429
438
 
@@ -451,6 +460,9 @@ msgstr ""
451
460
  msgid "Triggered by"
452
461
  msgstr ""
453
462
 
463
+ msgid "Trying to abort the task"
464
+ msgstr ""
465
+
454
466
  msgid "Trying to cancel step %s"
455
467
  msgstr ""
456
468
 
@@ -8,8 +8,8 @@ msgid ""
8
8
  msgstr ""
9
9
  "Project-Id-Version: foreman_tasks 1.0.0\n"
10
10
  "Report-Msgid-Bugs-To: \n"
11
- "POT-Creation-Date: 2017-07-19 17:30+0200\n"
12
- "PO-Revision-Date: 2017-07-19 17:30+0200\n"
11
+ "POT-Creation-Date: 2017-09-21 20:56+0200\n"
12
+ "PO-Revision-Date: 2017-09-21 20:56+0200\n"
13
13
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
14
  "Language-Team: LANGUAGE <LL@li.org>\n"
15
15
  "Language: \n"
@@ -90,19 +90,19 @@ msgstr ""
90
90
  msgid "Data to be sent to the action"
91
91
  msgstr ""
92
92
 
93
- #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:181
93
+ #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:182
94
94
  msgid "User search_params requires user_id to be specified"
95
95
  msgstr ""
96
96
 
97
- #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:190
97
+ #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:191
98
98
  msgid "Resource search_params requires resource_type and resource_id to be specified"
99
99
  msgstr ""
100
100
 
101
- #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:197
101
+ #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:198
102
102
  msgid "Task search_params requires task_id to be specified"
103
103
  msgstr ""
104
104
 
105
- #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:201
105
+ #: ../app/controllers/foreman_tasks/api/tasks_controller.rb:202
106
106
  msgid "Type %s for search_params is not supported"
107
107
  msgstr ""
108
108
 
@@ -118,30 +118,40 @@ msgstr ""
118
118
  msgid "The task cannot be cancelled at the moment."
119
119
  msgstr ""
120
120
 
121
- #: ../app/controllers/foreman_tasks/tasks_controller.rb:43
121
+ #: ../app/controllers/foreman_tasks/tasks_controller.rb:42
122
+ msgid "Trying to abort the task"
123
+ msgstr ""
124
+
125
+ #: ../app/controllers/foreman_tasks/tasks_controller.rb:44
126
+ msgid "The task cannot be aborted at the moment."
127
+ msgstr ""
128
+
129
+ #: ../app/controllers/foreman_tasks/tasks_controller.rb:53
122
130
  msgid "The execution was resumed."
123
131
  msgstr ""
124
132
 
125
- #: ../app/controllers/foreman_tasks/tasks_controller.rb:45
133
+ #: ../app/controllers/foreman_tasks/tasks_controller.rb:55
126
134
  msgid "The execution has to be resumable."
127
135
  msgstr ""
128
136
 
129
- #: ../app/controllers/foreman_tasks/tasks_controller.rb:55
137
+ #: ../app/controllers/foreman_tasks/tasks_controller.rb:65
130
138
  msgid "The task resources were unlocked."
131
139
  msgstr ""
132
140
 
133
- #: ../app/controllers/foreman_tasks/tasks_controller.rb:57
141
+ #: ../app/controllers/foreman_tasks/tasks_controller.rb:67
134
142
  msgid "The execution has to be paused."
135
143
  msgstr ""
136
144
 
137
- #: ../app/controllers/foreman_tasks/tasks_controller.rb:66
145
+ #: ../app/controllers/foreman_tasks/tasks_controller.rb:76
138
146
  msgid "The task resources were unlocked with force."
139
147
  msgstr ""
140
148
 
141
149
  #: ../app/helpers/foreman_tasks/foreman_tasks_helper.rb:38
142
150
  #: ../app/helpers/foreman_tasks/foreman_tasks_helper.rb:47
143
151
  #: ../app/models/foreman_tasks/recurring_logic.rb:101
144
- #: ../app/models/foreman_tasks/task/dynflow_task.rb:105
152
+ #: ../app/models/foreman_tasks/task/dynflow_task.rb:109
153
+ #: ../app/views/foreman_tasks/tasks/dashboard/_latest_tasks_in_error_warning.html.erb:14
154
+ #: ../app/views/foreman_tasks/tasks/dashboard/_tasks_status.html.erb:14
145
155
  msgid "N/A"
146
156
  msgstr ""
147
157
 
@@ -151,6 +161,7 @@ msgstr ""
151
161
 
152
162
  #: ../app/helpers/foreman_tasks/foreman_tasks_helper.rb:40
153
163
  #: ../app/views/foreman_tasks/tasks/dashboard/_latest_tasks_in_error_warning.html.erb:14
164
+ #: ../app/views/foreman_tasks/tasks/dashboard/_tasks_status.html.erb:14
154
165
  msgid "%s ago"
155
166
  msgstr ""
156
167
 
@@ -303,11 +314,11 @@ msgstr ""
303
314
  msgid "The targets are of different types"
304
315
  msgstr ""
305
316
 
306
- #: ../app/lib/actions/foreman/host/import_facts.rb:38 action_names.rb:3
317
+ #: ../app/lib/actions/foreman/host/import_facts.rb:38 action_names.rb:5
307
318
  msgid "Import facts"
308
319
  msgstr ""
309
320
 
310
- #: ../app/lib/actions/foreman/puppetclass/import.rb:18 action_names.rb:4
321
+ #: ../app/lib/actions/foreman/puppetclass/import.rb:18 action_names.rb:3
311
322
  msgid "Import Puppet classes"
312
323
  msgstr ""
313
324
 
@@ -355,15 +366,19 @@ msgstr ""
355
366
  msgid "All proxies with the required feature are unavailable at the moment"
356
367
  msgstr ""
357
368
 
358
- #: ../app/lib/actions/proxy_action.rb:59
369
+ #: ../app/lib/actions/proxy_action.rb:61
359
370
  msgid "The smart proxy task %s failed."
360
371
  msgstr ""
361
372
 
362
- #: ../app/lib/actions/proxy_action.rb:73
373
+ #: ../app/lib/actions/proxy_action.rb:75
363
374
  msgid "Cancel enforced: the task might be still running on the proxy"
364
375
  msgstr ""
365
376
 
366
- #: ../app/lib/actions/proxy_action.rb:114
377
+ #: ../app/lib/actions/proxy_action.rb:85
378
+ msgid "Task aborted: the task might be still running on the proxy"
379
+ msgstr ""
380
+
381
+ #: ../app/lib/actions/proxy_action.rb:121
367
382
  msgid "Initialization error: %s"
368
383
  msgstr ""
369
384
 
@@ -718,6 +733,10 @@ msgstr ""
718
733
  msgid "No. of Tasks"
719
734
  msgstr ""
720
735
 
736
+ #: ../app/views/foreman_tasks/tasks/dashboard/_tasks_status.html.erb:7
737
+ msgid "Last start time"
738
+ msgstr ""
739
+
721
740
  #: ../app/views/foreman_tasks/tasks/index.html.erb:1
722
741
  #: ../lib/foreman_tasks/engine.rb:48
723
742
  msgid "Tasks"
@@ -751,7 +770,13 @@ msgstr ""
751
770
  msgid "Raw"
752
771
  msgstr ""
753
772
 
754
- #: ../lib/foreman_tasks/dynflow.rb:102
773
+ #: ../lib/foreman_tasks/cleaner.rb:16
774
+ msgid ""
775
+ ":after setting in tasks cleanup section is deprecated, use :after in :rules se"
776
+ "ction to set the value. to cleanup rules"
777
+ msgstr ""
778
+
779
+ #: ../lib/foreman_tasks/dynflow.rb:19
755
780
  msgid "Back to tasks"
756
781
  msgstr ""
757
782
 
@@ -763,10 +788,6 @@ msgstr ""
763
788
  msgid "Action with sub plans"
764
789
  msgstr ""
765
790
 
766
- #: action_names.rb:5
791
+ #: action_names.rb:4
767
792
  msgid "Remote action:"
768
793
  msgstr ""
769
-
770
- #: action_names.rb:6
771
- msgid "Play Ansible roles"
772
- msgstr ""
@@ -1,6 +1,11 @@
1
1
  require 'foreman_tasks_test_helper'
2
2
 
3
3
  class TasksTest < ActiveSupport::TestCase
4
+ before do
5
+ # To stop dynflow from backing up actions, execution_plans and steps
6
+ ForemanTasks.dynflow.world.persistence.adapter.stubs(:backup_to_csv)
7
+ end
8
+
4
9
  describe ForemanTasks::Cleaner do
5
10
  it 'is able to delete tasks (including the dynflow plans) based on filter' do
6
11
  cleaner = ForemanTasks::Cleaner.new(:filter => 'label = "Actions::User::Create"', :after => '10d')
@@ -12,6 +17,7 @@ class TasksTest < ActiveSupport::TestCase
12
17
  task.save
13
18
  end,
14
19
  FactoryGirl.create(:dynflow_task, :product_create_task)]
20
+ cleaner.expects(:tasks_to_csv)
15
21
  cleaner.delete
16
22
  ForemanTasks::Task.where(id: tasks_to_delete).must_be_empty
17
23
  ForemanTasks::Task.where(id: tasks_to_keep).order(:id).map(&:id).must_equal tasks_to_keep.map(&:id).sort
@@ -32,6 +38,7 @@ class TasksTest < ActiveSupport::TestCase
32
38
  end]
33
39
 
34
40
  tasks_to_keep = [FactoryGirl.create(:dynflow_task, :product_create_task)]
41
+ cleaner.expects(:tasks_to_csv)
35
42
  cleaner.delete
36
43
  ForemanTasks::Task.where(id: tasks_to_delete).must_be_empty
37
44
  ForemanTasks::Task.where(id: tasks_to_keep).must_equal tasks_to_keep
@@ -46,11 +53,32 @@ class TasksTest < ActiveSupport::TestCase
46
53
  task.started_at = task.ended_at = Time.zone.now
47
54
  task.save
48
55
  end]
56
+ cleaner.expects(:tasks_to_csv)
49
57
  cleaner.delete
50
58
  ForemanTasks::Task.where(id: tasks_to_delete).must_be_empty
51
59
  ForemanTasks::Task.where(id: tasks_to_keep).must_equal tasks_to_keep
52
60
  end
53
61
 
62
+ it 'backs tasks up before deleting' do
63
+ dir = '/tmp'
64
+ cleaner = ForemanTasks::Cleaner.new(:filter => '', :after => '10d', :backup_dir => dir)
65
+ tasks_to_delete = [FactoryGirl.create(:dynflow_task, :user_create_task),
66
+ FactoryGirl.create(:dynflow_task, :product_create_task)]
67
+
68
+ r, w = IO.pipe
69
+ cleaner.expects(:with_backup_file)
70
+ .with(dir, 'foreman_tasks.csv')
71
+ .yields(w, false)
72
+ cleaner.delete
73
+ w.close
74
+ header, *data = r.readlines.map(&:chomp)
75
+ header.must_equal ForemanTasks::Task.attribute_names.join(',')
76
+ expected_lines = tasks_to_delete.map { |task| task.attributes.values.join(',') }
77
+ data.count.must_equal expected_lines.count
78
+ expected_lines.each { |line| data.must_include line }
79
+ ForemanTasks::Task.where(id: tasks_to_delete).must_be_empty
80
+ end
81
+
54
82
  class ActionWithCleanup < Actions::Base
55
83
  def self.cleanup_after
56
84
  '15d'
@@ -68,6 +96,29 @@ class TasksTest < ActiveSupport::TestCase
68
96
  { :actions => [{ :name => ActionWithCleanup.name, :after => '5d' }] })
69
97
  ForemanTasks::Cleaner.actions_with_default_cleanup[ActionWithCleanup].must_equal '5d'
70
98
  end
99
+
100
+ it 'deprecates the usage of :after' do
101
+ Foreman::Deprecation.expects(:deprecation_warning)
102
+ ForemanTasks::Cleaner.any_instance.expects(:delete)
103
+ ForemanTasks::Cleaner.stubs(:cleanup_settings =>
104
+ { :after => '1d' })
105
+ ForemanTasks::Cleaner.stubs(:actions_with_default_cleanup).returns({})
106
+ ForemanTasks::Cleaner.run({})
107
+ end
108
+
109
+ it 'generates filters from rules properly' do
110
+ actions_with_default = { 'action1' => nil, 'action2' => nil }
111
+ rules = [{ :after => nil },
112
+ { :after => '10d', :filter => 'label = something', :states => %w[stopped paused] },
113
+ { :after => '15d', :filter => 'label = something_else',
114
+ :override_actions => true, :states => 'all' }]
115
+ ForemanTasks::Cleaner.stubs(:cleanup_settings).returns(:rules => rules)
116
+ r1, r2 = ForemanTasks::Cleaner.actions_by_rules actions_with_default
117
+ r1[:filter].must_equal '(label !^ (action1, action2)) AND (label = something)'
118
+ r1[:states].must_equal %w[stopped paused]
119
+ r2[:filter].must_equal '(label = something_else)'
120
+ r2[:states].must_equal []
121
+ end
71
122
  end
72
123
  end
73
124
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman-tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.10.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Nečas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-19 00:00:00.000000000 Z
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foreman-tasks-core