Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
DRY up the data sync start/end times
Browse files Browse the repository at this point in the history
These values are beginning to leak into multiple places. It is good
maintenance practice to try to keep them in one place.

There is probably a cleaner way of doing this with YML config, but
given that some places need the start/end times and others need
specifically the start/end hour and minute, a function seems a
reasonable thing to create to encapsulate all that knowledge.

In any case, it is an improvement on trying to duplicate the
values throughout the repository.
  • Loading branch information
ChrisBAshton committed Oct 19, 2020
1 parent 1dcf035 commit f6daa94
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
38 changes: 38 additions & 0 deletions modules/govuk/lib/puppet/parser/functions/data_sync_times.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Puppet::Parser::Functions
newfunction(:format_data_sync_time, :type => :rvalue, :doc => <<-EOS
Return information about the data sync times, depending on the argument passed
EOS
) do |args|
start_hour = 22
start_minute = 0
finish_hour = 8
finish_minute = 0

if args.size != 1
raise ArgumentError, "format_data_sync_time: Wrong number of arguments " +
"(given #{args.size})"
end

type = args[0]

case type
when 'time_range'
"#{start_hour}:#{start_minute}-#{finish_hour}:#{finish_minute}"
when 'start_time'
"#{start_hour}:#{start_minute}"
when 'start_hour'
start_hour
when 'start_minute'
start_minute
when 'finish_time'
"#{finish_hour}:#{finish_minute}"
when 'finish_hour'
finish_hour
when 'finish_minute'
finish_minute
else
raise ArgumentError, "format_data_sync_time: Unknown argument #{type}"
end
end
end

2 changes: 1 addition & 1 deletion modules/govuk/manifests/deploy/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@

if $::aws_migration and ($::aws_environment != 'production') {
govuk_envvar {
'GOVUK_DATA_SYNC_PERIOD': value => '22:00-8:00'; # This should match up with the govuk_data_sync_in_progress class
'GOVUK_DATA_SYNC_PERIOD': value => format_data_sync_time('time_range');
}
}

Expand Down
22 changes: 8 additions & 14 deletions modules/govuk_data_sync_in_progress/manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
) {
$fact_path = '/etc/govuk/env.d/FACTER_data_sync_in_progress'

$start_hour = 22
$start_minute = 0

$finish_hour = 8
$finish_minute = 00

if !defined(File[$fact_path]) {
file { $fact_path:
ensure => present,
Expand All @@ -35,35 +29,35 @@
cron { 'data_sync_started':
command => "echo 'true' > ${fact_path}",
user => 'deploy',
hour => $start_hour,
minute => $start_minute,
hour => format_data_sync_time('start_hour'),
minute => format_data_sync_time('start_minute'),
}
}

if !defined(Cron['data_sync_finished']) {
cron { 'data_sync_finished':
command => "echo '' > ${$fact_path}",
user => 'deploy',
hour => $finish_hour,
minute => $finish_minute,
hour => format_data_sync_time('finish_hour'),
minute => format_data_sync_time('finish_minute'),
}
}

if $start_command {
cron { "data_sync_started_${title}":
command => $start_command,
user => 'deploy',
hour => $start_hour,
minute => $start_minute,
hour => format_data_sync_time('start_hour'),
minute => format_data_sync_time('start_minute'),
}
}

if $finish_command {
cron { "data_sync_finished_${title}":
command => $finish_command,
user => 'deploy',
hour => $finish_hour,
minute => $finish_minute,
hour => format_data_sync_time('finish_hour'),
minute => format_data_sync_time('finish_minute'),
}
}
}
5 changes: 2 additions & 3 deletions modules/monitoring/manifests/contacts.pp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@
$midday = '11:00'
$office_day_end = '16:30'
$midnight_day_end = '24:00'
# This should match up with the govuk_data_sync_in_progress class
$data_sync_start = '22:00'
$data_sync_end = '08:00'
$data_sync_start = format_data_sync_time('start_time')
$data_sync_end = format_data_sync_time('finish_time')

$all_day = "${midnight_day_start}-${midnight_day_end}"
$office_day = "${office_day_start}-${office_day_end}"
Expand Down

0 comments on commit f6daa94

Please sign in to comment.