Class: PuppetX::PuppetLabs::ScheduledTask::Trigger::V2 Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/scheduled_task/trigger.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Task Scheduler API V2

Defined Under Namespace

Classes: Day, Days, Month, SessionStateChangeType, Type, WeeksOfMonth

Constant Summary collapse

SCHEDULE_BASED_TRIGGER_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Trigger type to day map

{
  Type::TASK_TRIGGER_DAILY      => 'daily',
  Type::TASK_TRIGGER_WEEKLY     => 'weekly',
  # NOTE: monthly uses context to determine MONTHLY or MONTHLYDOW
  Type::TASK_TRIGGER_MONTHLY    => 'monthly',
  Type::TASK_TRIGGER_MONTHLYDOW => 'monthly',
  Type::TASK_TRIGGER_TIME       => 'once',
}.freeze
EVENT_BASED_TRIGGER_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Event based trigger map

{
  Type::TASK_TRIGGER_BOOT                 => 'boot',
  Type::TASK_TRIGGER_LOGON                => 'logon',
  # The triggers below are not yet supported.
  # Type::TASK_TRIGGER_EVENT                => 'event',
  # Type::TASK_TRIGGER_IDLE                 => 'idle',
  # Type::TASK_TRIGGER_REGISTRATION         => 'task_registered',
  # Type::TASK_TRIGGER_SESSION_STATE_CHANGE => 'session_state_change',
}.freeze
TYPE_MANIFEST_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Type manifest map

SCHEDULE_BASED_TRIGGER_MAP.merge(EVENT_BASED_TRIGGER_MAP).freeze

Class Method Summary collapse

Class Method Details

.append_trigger(definition, manifest_hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds trigger to definition



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/puppet_x/puppetlabs/scheduled_task/trigger.rb', line 756

def self.append_trigger(definition, manifest_hash)
  manifest_hash = Trigger::Manifest.canonicalize_and_validate(manifest_hash)
  # create appropriate i_trigger based on 'schedule'
  i_trigger = definition.Triggers.Create(type_from_manifest_hash(manifest_hash))

  # Values for all Trigger Types
  if manifest_hash['minutes_interval']
    minutes_interval = manifest_hash['minutes_interval']
    if minutes_interval.positive?
      i_trigger.Repetition.Interval = "PT#{minutes_interval}M"
      # one day in minutes
      i_trigger.Repetition.Duration = 'PT1440M' unless manifest_hash.key?('minutes_duration')
    end
  end

  if manifest_hash['minutes_duration']
    minutes_duration = manifest_hash['minutes_duration']
    i_trigger.Repetition.Duration = "PT#{minutes_duration}M" unless minutes_duration.zero?
  end

  # manifests specify datetime in the local timezone, ITrigger accepts ISO8601
  # when start_date is null or missing, Time.parse returns today
  datetime_string = "#{manifest_hash['start_date']} #{manifest_hash['start_time']}"
  # Time.parse always assumes local time
  # If `disable_time_zone_synchronization` has been set to true then the timezone is removed from the start time
  unless datetime_string.strip.empty?
    start = if manifest_hash['disable_time_zone_synchronization'] && manifest_hash['disable_time_zone_synchronization'] == true
              Time.parse(datetime_string).iso8601.gsub(%r{Z|(\+..\:..$)|(\-..\:..$)}, '')
            else
              Time.parse(datetime_string).iso8601
            end
    i_trigger.StartBoundary = start
  end

  # ITrigger specific settings
  case i_trigger.Type
  when Type::TASK_TRIGGER_DAILY
    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa446858(v=vs.85).aspx
    i_trigger.DaysInterval = Integer(manifest_hash['every'] || 1)

  when Type::TASK_TRIGGER_WEEKLY
    days_of_week = manifest_hash['day_of_week'] || Day.names
    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa384019(v=vs.85).aspx
    i_trigger.DaysOfWeek = Day.names_to_bitmask(days_of_week)
    i_trigger.WeeksInterval = Integer(manifest_hash['every'] || 1)

  when Type::TASK_TRIGGER_MONTHLY
    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa382062(v=vs.85).aspx
    i_trigger.RunOnLastDayOfMonth = Days.last_day_of_month?(manifest_hash['on'])
    i_trigger.DaysOfMonth = Days.indexes_to_bitmask(manifest_hash['on'])
    i_trigger.MonthsOfYear = Month.indexes_to_bitmask(manifest_hash['months'] || Month.indexes)

  when Type::TASK_TRIGGER_MONTHLYDOW
    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa382055(v=vs.85).aspx
    i_trigger.DaysOfWeek = Day.names_to_bitmask(manifest_hash['day_of_week'])
    i_trigger.MonthsOfYear = Month.indexes_to_bitmask(manifest_hash['months'] || Month.indexes)
    # HACK: convert V1 week value to names, then back to V2 bitmask
    i_trigger.WeeksOfMonth = WeeksOfMonth.names_to_bitmask(manifest_hash['which_occurrence'])

  when Type::TASK_TRIGGER_LOGON
    i_trigger.UserId = manifest_hash['user_id']
  end

  nil
end

.to_manifest_hash(i_trigger) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts trigger to manifest hash



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'lib/puppet_x/puppetlabs/scheduled_task/trigger.rb', line 701

def self.to_manifest_hash(i_trigger)
  if TYPE_MANIFEST_MAP[i_trigger.Type].nil?
    raise ArgumentError, _('Unknown trigger type %{type}') % { type: i_trigger.ole_type.to_s }
  end

  # StartBoundary and EndBoundary may be empty strings per V2 API
  start_boundary = Trigger.iso8601_datetime_to_local(i_trigger.StartBoundary)
  _end_boundary = Trigger.iso8601_datetime_to_local(i_trigger.EndBoundary)

  manifest_hash = {
    'start_date'                        => start_boundary ? Manifest.format_date(start_boundary) : '',
    'start_time'                        => start_boundary ? Manifest.format_time(start_boundary) : '',
    'enabled'                           => i_trigger.Enabled,
    'minutes_interval'                  => Duration.to_minutes(i_trigger.Repetition.Interval) || 0,
    'minutes_duration'                  => Duration.to_minutes(i_trigger.Repetition.Duration) || 0,
    'disable_time_zone_synchronization' => start_boundary ? !%r{(Z|[+-]\d\d:\d\d)$}.match?(i_trigger.StartBoundary) : false,
  }

  case i_trigger.Type
  when Type::TASK_TRIGGER_TIME
    manifest_hash['schedule'] = 'once'
  when Type::TASK_TRIGGER_DAILY
    manifest_hash['schedule'] = 'daily'
    manifest_hash['every'] = i_trigger.DaysInterval
  when Type::TASK_TRIGGER_WEEKLY
    manifest_hash.merge!('schedule'    => 'weekly',
                         'every'       => i_trigger.WeeksInterval,
                         'day_of_week' => Day.bitmask_to_names(i_trigger.DaysOfWeek))
  when Type::TASK_TRIGGER_MONTHLY
    manifest_hash.merge!('schedule' => 'monthly',
                         'months'   => Month.bitmask_to_indexes(i_trigger.MonthsOfYear),
                         'on'       => Days.bitmask_to_indexes(i_trigger.DaysOfMonth, i_trigger.RunOnLastDayOfMonth))
  when Type::TASK_TRIGGER_MONTHLYDOW
    occurrences = V2::WeeksOfMonth.bitmask_to_names(i_trigger.WeeksOfMonth)
    manifest_hash.merge!('schedule'         => 'monthly',
                         'months'           => Month.bitmask_to_indexes(i_trigger.MonthsOfYear),
                         # HACK: choose only the first week selected when converting - this LOSES information
                         'which_occurrence' => occurrences.first || '',
                         'day_of_week'      => Day.bitmask_to_names(i_trigger.DaysOfWeek))
    # MODULES-10101: We will need to evaluate whether the value 'last' has been applied to the WeekOfMonth
    # parameter by inspecting the value of Trigger::RunOnLastWeekOfMonth. See JIRA ticket for more details.
    manifest_hash['which_occurrence'] = 'last' if i_trigger.RunOnLastWeekOfMonth
  when Type::TASK_TRIGGER_BOOT
    manifest_hash['schedule'] = 'boot'
  when Type::TASK_TRIGGER_LOGON
    # Resolve the UserID unless it is an empty string, which represents all users.
    user_id = (i_trigger.UserId == '') ? '' : Puppet::Util::Windows::SID.sid_to_name(Puppet::Util::Windows::SID.name_to_sid(i_trigger.UserId))
    manifest_hash['schedule'] = 'logon'
    manifest_hash['user_id'] = user_id
  end

  manifest_hash
end

.type_from_manifest_hash(manifest_hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a type based on a manifest hash



690
691
692
693
694
695
696
697
698
# File 'lib/puppet_x/puppetlabs/scheduled_task/trigger.rb', line 690

def self.type_from_manifest_hash(manifest_hash)
  # monthly schedule defaults to TASK_TRIGGER_MONTHLY unless...
  if manifest_hash['schedule'] == 'monthly' &&
     (manifest_hash.key?('which_occurrence') || manifest_hash.key?('day_of_week'))
    return Type::TASK_TRIGGER_MONTHLYDOW
  end

  TYPE_MANIFEST_MAP.key(manifest_hash['schedule'])
end