Class: ElasticWhenever::Option

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

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.



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/elastic_whenever/option.rb', line 33

def initialize(args)
  @identifier = nil
  @mode = DRYRUN_MODE
  @verbose = false
  @variables = []
  @cluster = nil
  @task_definition = nil
  @container = nil
  @assign_public_ip = 'DISABLED'
  @launch_type = 'EC2'
  @platform_version = 'LATEST'
  @security_groups = []
  @subnets = []
  @schedule_file = 'config/schedule.rb'
  @iam_role = 'ecsEventsRole'
  @rule_state = 'ENABLED'
  @profile = nil
  @access_key = nil
  @secret_key = nil
  @region = nil

  OptionParser.new do |opts|
    opts.on('-i', '--update identifier', 'Creates and deletes tasks as needed by schedule file') do |identifier|
      @identifier = identifier
      @mode = UPDATE_MODE
    end
    opts.on('-c', '--clear identifier', 'Clear scheduled tasks') do |identifier|
      @identifier = identifier
      @mode = CLEAR_MODE
    end
    opts.on('-l', '--list identifier', 'List scheduled tasks') do |identifier|
      @identifier = identifier
      @mode = LIST_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('--cluster cluster', 'ECS cluster to run tasks') do |cluster|
      @cluster = cluster
    end
    opts.on('--task-definition task_definition', 'Task definition name, If omit a revision, use the latest revision of the family automatically. Example: --task-definition oneoff-application:2') do |definition|
      @task_definition = definition
    end
    opts.on('--container container', 'Container name defined in the task definition') do |container|
      @container = container
    end
    opts.on('--launch-type launch_type', 'Launch type. EC2 or FARGATE. Default: EC2') do |launch_type|
      @launch_type = launch_type
    end
    opts.on('--assign-public-ip', 'Assign a public IP. Default: DISABLED (FARGATE only)') do
      @assign_public_ip = 'ENABLED'
    end
    opts.on('--security-groups groups', "Example: --security-groups 'sg-2c503655,sg-72f0cb0a' (FARGATE only)") do |groups|
      @security_groups = groups.split(',')
    end
    opts.on('--subnets subnets', "Example: --subnets 'subnet-4973d63f,subnet-45827d1d' (FARGATE only)") do |subnets|
      @subnets = subnets.split(',')
    end
    opts.on('--platform-version version', "Optionally specify the platform version. Default: LATEST (FARGATE only)") do |version|
      @platform_version = version
    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 CloudWatch Events. Default: ecsEventsRole') do |role|
      @iam_role = role
    end
    opts.on('--rule-state state', 'The state of the CloudWatch Events Rule. Default: ENABLED') do |state|
      @rule_state = state
    end
    opts.on('--profile profile_name', 'AWS shared profile name') do |profile|
      @profile = profile
    end
    opts.on('--access-key aws_access_key_id', 'AWS access key ID') do |key|
      @access_key = key
    end
    opts.on('--secret-key aws_secret_access_key', 'AWS secret access key') do |key|
      @secret_key = key
    end
    opts.on('--region region', 'AWS region') do |region|
      @region = region
    end
    opts.on('-v', '--version', 'Print version') do
      @mode = PRINT_VERSION_MODE
    end
    opts.on('-V', '--verbose', 'Run rake jobs without --silent') do
      @verbose = true
    end
  end.parse(args)

  @credentials = if access_key && secret_key
                   Aws::Credentials.new(access_key, secret_key)
                 end
end

Instance Attribute Details

#assign_public_ipObject (readonly)

Returns the value of attribute assign_public_ip.



18
19
20
# File 'lib/elastic_whenever/option.rb', line 18

def assign_public_ip
  @assign_public_ip
end

#aws_configObject (readonly)

Returns the value of attribute aws_config.



26
27
28
# File 'lib/elastic_whenever/option.rb', line 26

def aws_config
  @aws_config
end

#cloudwatch_events_clientObject (readonly)

Returns the value of attribute cloudwatch_events_client.



29
30
31
# File 'lib/elastic_whenever/option.rb', line 29

def cloudwatch_events_client
  @cloudwatch_events_client
end

#clusterObject (readonly)

Returns the value of attribute cluster.



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

def cluster
  @cluster
end

#containerObject (readonly)

Returns the value of attribute container.



17
18
19
# File 'lib/elastic_whenever/option.rb', line 17

def container
  @container
end

#ecs_clientObject (readonly)

Returns the value of attribute ecs_client.



27
28
29
# File 'lib/elastic_whenever/option.rb', line 27

def ecs_client
  @ecs_client
end

#iam_clientObject (readonly)

Returns the value of attribute iam_client.



28
29
30
# File 'lib/elastic_whenever/option.rb', line 28

def iam_client
  @iam_client
end

#iam_roleObject (readonly)

Returns the value of attribute iam_role.



24
25
26
# File 'lib/elastic_whenever/option.rb', line 24

def iam_role
  @iam_role
end

#identifierObject (readonly)

Returns the value of attribute identifier.



11
12
13
# File 'lib/elastic_whenever/option.rb', line 11

def identifier
  @identifier
end

#launch_typeObject (readonly)

Returns the value of attribute launch_type.



19
20
21
# File 'lib/elastic_whenever/option.rb', line 19

def launch_type
  @launch_type
end

#modeObject (readonly)

Returns the value of attribute mode.



12
13
14
# File 'lib/elastic_whenever/option.rb', line 12

def mode
  @mode
end

#platform_versionObject (readonly)

Returns the value of attribute platform_version.



20
21
22
# File 'lib/elastic_whenever/option.rb', line 20

def platform_version
  @platform_version
end

#rule_stateObject (readonly)

Returns the value of attribute rule_state.



25
26
27
# File 'lib/elastic_whenever/option.rb', line 25

def rule_state
  @rule_state
end

#schedule_fileObject (readonly)

Returns the value of attribute schedule_file.



23
24
25
# File 'lib/elastic_whenever/option.rb', line 23

def schedule_file
  @schedule_file
end

#security_groupsObject (readonly)

Returns the value of attribute security_groups.



21
22
23
# File 'lib/elastic_whenever/option.rb', line 21

def security_groups
  @security_groups
end

#subnetsObject (readonly)

Returns the value of attribute subnets.



22
23
24
# File 'lib/elastic_whenever/option.rb', line 22

def subnets
  @subnets
end

#task_definitionObject (readonly)

Returns the value of attribute task_definition.



16
17
18
# File 'lib/elastic_whenever/option.rb', line 16

def task_definition
  @task_definition
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

def variables
  @variables
end

#verboseObject (readonly)

Returns the value of attribute verbose.



13
14
15
# File 'lib/elastic_whenever/option.rb', line 13

def verbose
  @verbose
end

Instance Method Details

#keyObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/elastic_whenever/option.rb', line 160

def key
  Digest::SHA1.hexdigest(
    [
      identifier,
      variables,
      cluster,
      task_definition,
      container,
      assign_public_ip,
      launch_type,
      platform_version,
      security_groups,
      subnets,
      iam_role,
      rule_state,
    ].join
  )
end

#validate!Object



152
153
154
155
156
157
158
# File 'lib/elastic_whenever/option.rb', line 152

def validate!
  raise InvalidOptionException.new("Can't find file: #{schedule_file}") unless File.exist?(schedule_file)
  raise InvalidOptionException.new("You must set cluster") unless cluster
  raise InvalidOptionException.new("You must set task definition") unless task_definition
  raise InvalidOptionException.new("You must set container") unless container
  raise InvalidOptionException.new("Invalid rule state. Possible values are #{POSSIBLE_RULE_STATES.join(", ")}") unless POSSIBLE_RULE_STATES.include?(rule_state)
end