File: job_flow.rb

package info (click to toggle)
ruby-aws-sdk 1.52.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,164 kB
  • ctags: 4,821
  • sloc: ruby: 28,085; makefile: 7
file content (307 lines) | stat: -rw-r--r-- 9,662 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class EMR

    #
    # @attr_reader [String] name
    #   The name of the job flow.
    #
    # @attr_reader [String] ami_version
    #   The version of the AMI used to initialize Amazon EC2 instances in
    #   the job flow.
    #
    # @attr_reader [String,nil] log_uri
    #   The location in Amazon S3 where log files for the job are stored.
    #
    # @attr_reader [Array<String>] supported_products
    #   A list of strings set by third party software when the job flow is
    #   launched. If you are not using third party software to manage the
    #   job flow this value is empty.
    #
    # @attr_reader [Array<Hash>] bootstrap_actions
    #
    # @attr_reader [String] state
    #
    # @attr_reader [Time] created_at
    #
    # @attr_reader [Time] started_at
    #
    # @attr_reader [Time] ready_at
    #
    # @attr_reader [Time] ended_at
    #
    # @attr_reader [String] last_state_change_reason
    #
    # @attr_reader [String] master_instance_type
    #
    # @attr_reader [String] master_public_dns_name
    #
    # @attr_reader [String] master_instance_id
    #
    # @attr_reader [String] slave_instance_id
    #
    # @attr_reader [Integer] instance_count
    #
    # @attr_reader [Integer] normalized_instance_hours
    #
    # @attr_reader [String] ec2_key_name
    #
    # @attr_reader [String] ec2_subnet_id
    #
    # @attr_reader [String] availability_zone_name
    #
    # @attr_reader [Boolean] keep_job_flow_alive_when_no_steps
    #
    # @attr_reader [Boolean] termination_protected
    #
    # @attr_reader [String] hadoop_version
    #
    # @attr_reader [Array<Hash>] instance_group_details
    #
    # @attr_reader [Array<Hash>] step_details
    #
    class JobFlow < Core::Resource

      # @param [String] job_flow_id
      # @param [Hash] options
      # @api private
      def initialize job_flow_id, options = {}
        @job_flow_id = job_flow_id
        super
      end

      # @return [String]
      attr_reader :job_flow_id

      alias_method :id, :job_flow_id

      attribute :name, :static => true

      attribute :ami_version, :static => true

      attribute :log_uri, :static => true

      attribute :supported_products, :static => true

      # attributes from :execution_status_detail

      attribute :state,
        :from => [:execution_status_detail, :state]

      attribute :creation_date_time,
        :from => [:execution_status_detail, :creation_date_time],
        :static => true,
        :alias => :created_at

      attribute :start_date_time,
        :from => [:execution_status_detail, :start_date_time],
        :alias => :started_at

      attribute :ready_date_time,
        :from => [:execution_status_detail, :ready_date_time],
        :alias => :ready_at

      attribute :end_date_time,
        :from => [:execution_status_detail, :end_date_time],
        :alias => :ended_at

      attribute :last_state_change_reason,
        :from => [:execution_status_detail, :last_state_change_reason]

      # attributes from :instances

      attribute :master_instance_type,
        :from => [:instances, :master_instance_type]

      attribute :master_public_dns_name,
        :from => [:instances, :master_public_dns_name]

      attribute :master_instance_id,
        :from => [:instances, :master_instance_id]

      attribute :slave_instance_type,
        :from => [:instances, :slave_instance_type]

      attribute :instance_count,
        :from => [:instances, :instance_count]

      attribute :normalized_instance_hours,
        :from => [:instances, :normalized_instance_hours]

      attribute :ec2_key_name,
        :from => [:instances, :ec2_key_name]

      attribute :ec2_subnet_id,
        :from => [:instances, :ec2_subnet_id]

      attribute :availability_zone_name,
        :from => [:instances, :placement, :availability_zone]

      attribute :keep_job_flow_alive_when_no_steps,
        :from => [:instances, :keep_job_flow_alive_when_no_steps],
        :alias => :keep_job_flow_alive_when_no_steps?

      attribute :termination_protected,
        :from => [:instances, :termination_protected],
        :alias => :termination_protected?

      attribute :hadoop_version,
        :from => [:instances, :hadoop_version]

      attribute :instance_group_details,
        :from => [:instances, :instance_groups]

      attribute :step_details, :from => :steps

      attribute :bootstrap_actions

      populates_from(:describe_job_flows) do |resp|
        resp.data[:job_flows].find{|j| j[:job_flow_id] == job_flow_id }
      end

      # @return [EC2::Instance,nil]
      def master_instance
        if instance_id = master_instance_id
          AWS::EC2.new(:config => config).instances[instance_id]
        end
      end

      # @return [EC2::Instance,nil]
      def slave_instance
        if instance_id = slave_instance_id
          AWS::EC2.new(:config => config).instances[instance_id]
        end
      end

      # @return [EC2::AvailabilityZone,nil]
      def availability_zone
        if name = availability_zone_name
          AWS::EC2.new(:config => config).availability_zones[name]
        end
      end

      # Adds one (or more) steps to the current job flow.
      #
      #     emr.job_flows['job-flow-id'].add_steps([{
      #       :name => 'step-name',
      #       :action_on_failure => 'TERMINATE_JOB_FLOW',
      #       :hadoop_jar_step => {
      #         :jar => 'path/to/a/jar/file',
      #         :main_class => 'MainClassName',
      #         :args => %w(arg1 arg2 arg3)],
      #         :properties => [
      #           { :key => 'property-1-name', :value => 'property-1-value' }
      #           { :key => 'property-2-name', :value => 'property-2-value' }
      #         ],
      #       }
      #     }])
      #     emr.job_flows['job-flow-id'].add_steps([{
      #
      # @param [Array<Hash>] steps A list of one or more steps to add.
      #   Each step should be a hash with the following structure:
      #   * `:name` - *required* - (String) The name of the job flow step.
      #   * `:action_on_failure` - (String) Specifies the action to take if the
      #     job flow step fails.
      #   * `:hadoop_jar_step` - *required* - (Hash) Specifies the JAR file
      #     used for the job flow step.
      #     * `:properties` - (Array<Hash>) A list of Java properties that are
      #       set when the step runs. You can use these properties to pass key
      #       value pairs to your main function.
      #       * `:key` - (String) The unique identifier of a key value pair.
      #       * `:value` - (String) The value part of the identified key.
      #     * `:jar` - *required* - (String) A path to a JAR file run during
      #       the step.
      #     * `:main_class` - (String) The name of the main class in the
      #       specified Java file. If not specified, the JAR file should
      #       specify a Main-Class in its manifest file.
      #     * `:args` - (Array<String>) A list of command line arguments passed
      #       to the JAR file's main function when executed.
      #
      # @return [nil]
      #
      def add_steps *steps
        options = {}
        options[:job_flow_id] = job_flow_id
        options[:steps] = steps.flatten
        client.add_job_flow_steps(options)
        nil
      end

      # @return [InstanceGroupCollection]
      def instance_groups
        InstanceGroupCollection.new(self)
      end

      # Locks this job flow so the Amazon EC2 instances in the cluster
      # cannot be terminated by user intervention, an API call, or in the
      # event of a job-flow error.
      # @return [nil]
      def enable_termination_protection
        set_termination_protection(true)
      end

      # Removes a lock on this job flow so the Amazon EC2 instances in the
      # cluster may be terminated.
      # @return [nil]
      def disable_termination_protection
        set_termination_protection(false)
      end

      # @param [Boolean] state
      # @return [nil]
      def set_termination_protection state
        options = {}
        options[:termination_protected] = state
        options[:job_flow_ids] = [job_flow_id]
        client.set_termination_protection(options)
        nil
      end

      # @param [Boolean] state
      # @return [nil]
      def set_visible_to_all_users state
        options = {}
        options[:visible_to_all_users] = state
        options[:job_flow_ids] = [job_flow_id]
        client.set_visible_to_all_users(options)
        nil
      end

      # Terminates the current job flow.
      # @return [nil]
      def terminate
        client.terminate_job_flows(:job_flow_ids => [job_flow_id])
        nil
      end
      alias_method :delete, :terminate

      # @return [Boolean] Returns `true` if the job flow exists.
      def exists?
        !get_resource.data[:job_flows].empty?
      end

      protected

      def resource_identifiers
        [[:job_flow_id, job_flow_id]]
      end

      def get_resource attr = nil
        client.describe_job_flows(:job_flow_ids => [job_flow_id])
      end

    end
  end
end