Class: LambdaWhenever::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_whenever/option.rb

Overview

The Option class handles parsing and validation of command-line options.

Defined Under Namespace

Classes: InvalidOptionException

Constant Summary collapse

POSSIBLE_RULE_STATES =
%w[ENABLED DISABLED].freeze
DRYRUN_MODE =
1
UPDATE_MODE =
2
CLEAR_MODE =
3
LIST_MODE =
4
5
SYNC_MODE =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Option

Returns a new instance of Option.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lambda_whenever/option.rb', line 20

def initialize(args)
  @mode = DRYRUN_MODE
  @verbose = false
  @variables = []
  @schedule_file = "config/schedule.rb"
  @iam_role = nil
  @rule_state = "ENABLED"
  @lambda_name = nil
  @scheduler_group = "lambda-whenever-dev-group"
  @region = nil

  OptionParser.new do |opts|
    opts.on("--dryrun", "dry-run") do
      @mode = DRYRUN_MODE
    end
    opts.on("--update", "clear and create schedules") do
      @mode = UPDATE_MODE
    end
    opts.on("--sync", "Creates and deletes tasks as needed by schedule file") do
      @mode = SYNC_MODE
    end
    opts.on("-c", "--clear", "Clear scheduled tasks") do
      @mode = CLEAR_MODE
    end
    opts.on("-l", "--list", "List scheduled tasks") do
      @mode = LIST_MODE
    end
    opts.on("-v", "--version", "Print version") do
      @mode = PRINT_VERSION_MODE
    end
    opts.on("-s", "--set variables", "Example: --set 'environment=staging'") do |set|
      pairs = set.split("&")
      pairs.each do |pair|
        unless pair.include?("=")
          Logger.instance.warn("Ignore variable set: #{pair}")
          next
        end
        key, value = pair.split("=")
        @variables << { key: key, value: value }
      end
    end
    opts.on("--lambda-name name", "Lambda function name") do |name|
      @lambda_name = name
    end
    opts.on("--scheduler-group group_name",
            "Optionally specify event bridge scheduler group name") do |group|
      @scheduler_group = group
    end
    opts.on("-f", "--file schedule_file", "Default: config/schedule.rb") do |file|
      @schedule_file = file
    end
    opts.on("--iam-role name", "IAM role name used by EventBridgeScheduler.") do |role|
      @iam_role = role
    end
    opts.on("--rule-state state", "The state of the EventBridgeScheduler Rule. Default: ENABLED") do |state|
      @rule_state = state
    end
    opts.on("--region region", "AWS region") do |region|
      @region = region
    end
    opts.on("-V", "--verbose", "Run rake jobs without --silent") do
      @verbose = true
    end
  end.parse(args)
end

Instance Attribute Details

#iam_roleObject (readonly)

Returns the value of attribute iam_role.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def iam_role
  @iam_role
end

#lambda_nameObject (readonly)

Returns the value of attribute lambda_name.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def lambda_name
  @lambda_name
end

#modeObject (readonly)

Returns the value of attribute mode.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def mode
  @mode
end

#rule_stateObject (readonly)

Returns the value of attribute rule_state.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def rule_state
  @rule_state
end

#schedule_fileObject (readonly)

Returns the value of attribute schedule_file.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def schedule_file
  @schedule_file
end

#scheduler_groupObject (readonly)

Returns the value of attribute scheduler_group.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def scheduler_group
  @scheduler_group
end

#variablesObject (readonly)

Returns the value of attribute variables.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def variables
  @variables
end

#verboseObject (readonly)

Returns the value of attribute verbose.



15
16
17
# File 'lib/lambda_whenever/option.rb', line 15

def verbose
  @verbose
end

Instance Method Details

#aws_configObject



95
96
97
# File 'lib/lambda_whenever/option.rb', line 95

def aws_config
  @aws_config ||= { region: region }.delete_if { |_k, v| v.nil? }
end

#iam_clientObject



99
100
101
# File 'lib/lambda_whenever/option.rb', line 99

def iam_client
  @iam_client ||= Aws::IAM::Client.new(aws_config)
end

#keyObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lambda_whenever/option.rb', line 111

def key
  Digest::SHA1.hexdigest(
    [
      variables,
      iam_role,
      rule_state,
      lambda_name,
      scheduler_group
    ].join
  )
end

#lambda_clientObject



103
104
105
# File 'lib/lambda_whenever/option.rb', line 103

def lambda_client
  @lambda_client ||= Aws::Lambda::Client.new(aws_config)
end

#scheduler_clientObject



107
108
109
# File 'lib/lambda_whenever/option.rb', line 107

def scheduler_client
  @scheduler_client ||= Aws::Scheduler::Client.new(aws_config)
end

#validate!Object



86
87
88
89
90
91
92
93
# File 'lib/lambda_whenever/option.rb', line 86

def validate!
  raise InvalidOptionException, "Can't find file: #{schedule_file}" unless File.exist?(schedule_file)
  raise InvalidOptionException, "You must set lambda-name" unless lambda_name
  raise InvalidOptionException, "You must set iam-role" unless iam_role
  return if POSSIBLE_RULE_STATES.include?(rule_state)

  raise InvalidOptionException, "Invalid rule state. Possible values are #{POSSIBLE_RULE_STATES.join(", ")}"
end