Class: ElasticWhenever::Option

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

Defined Under Namespace

Classes: InvalidOptionException

Constant Summary collapse

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.



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

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'
  @profile = nil
  @access_key = nil
  @secret_key = nil
  @region = nil

  OptionParser.new do |opts|
    opts.on('-i', '--update identifier', 'Clear and create scheduled tasks 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-deifinition 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. Defualt: 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('--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 profile
                   Aws::SharedCredentials.new(profile_name: profile)
                 elsif 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.



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

def assign_public_ip
  @assign_public_ip
end

#clusterObject (readonly)

Returns the value of attribute cluster.



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

def cluster
  @cluster
end

#containerObject (readonly)

Returns the value of attribute container.



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

def container
  @container
end

#iam_roleObject (readonly)

Returns the value of attribute iam_role.



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

def iam_role
  @iam_role
end

#identifierObject (readonly)

Returns the value of attribute identifier.



9
10
11
# File 'lib/elastic_whenever/option.rb', line 9

def identifier
  @identifier
end

#launch_typeObject (readonly)

Returns the value of attribute launch_type.



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

def launch_type
  @launch_type
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#platform_versionObject (readonly)

Returns the value of attribute platform_version.



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

def platform_version
  @platform_version
end

#schedule_fileObject (readonly)

Returns the value of attribute schedule_file.



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

def schedule_file
  @schedule_file
end

#security_groupsObject (readonly)

Returns the value of attribute security_groups.



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

def security_groups
  @security_groups
end

#subnetsObject (readonly)

Returns the value of attribute subnets.



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

def subnets
  @subnets
end

#task_definitionObject (readonly)

Returns the value of attribute task_definition.



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

def task_definition
  @task_definition
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

def variables
  @variables
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#aws_configObject



127
128
129
# File 'lib/elastic_whenever/option.rb', line 127

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

#validate!Object



131
132
133
134
135
136
# File 'lib/elastic_whenever/option.rb', line 131

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
end