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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Option

Returns a new instance of Option.



19
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
# File 'lib/lambda_whenever/option.rb', line 19

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", "Creates and deletes tasks as needed by schedule file") do
      @mode = UPDATE_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.



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

def iam_role
  @iam_role
end

#lambda_nameObject (readonly)

Returns the value of attribute lambda_name.



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

def lambda_name
  @lambda_name
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#rule_stateObject (readonly)

Returns the value of attribute rule_state.



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

def rule_state
  @rule_state
end

#schedule_fileObject (readonly)

Returns the value of attribute schedule_file.



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

def schedule_file
  @schedule_file
end

#scheduler_groupObject (readonly)

Returns the value of attribute scheduler_group.



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

def scheduler_group
  @scheduler_group
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

def variables
  @variables
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#aws_configObject



91
92
93
# File 'lib/lambda_whenever/option.rb', line 91

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

#iam_clientObject



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

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

#keyObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lambda_whenever/option.rb', line 107

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

#lambda_clientObject



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

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

#scheduler_clientObject



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

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

#validate!Object



82
83
84
85
86
87
88
89
# File 'lib/lambda_whenever/option.rb', line 82

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