Class: AWS::Flow::Utils::InitConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/utils.rb

Overview

Initializes an AWS Flow Framework for Ruby application

Class Method Summary collapse

Class Method Details

.check_options(options) ⇒ Object

Validates various options

Raises:

  • (ArgumentError)


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
# File 'lib/aws/utils.rb', line 217

def self.check_options(options)
  raise ArgumentError, "You must specify a name for your application" unless options[:name]
  options[:region] ||= ENV['AWS_REGION'].downcase if ENV['AWS_REGION']
  options[:path] ||= "."

  # We need the region for the 1-Click URL
  if options[:eb]
    raise ArgumentError, "You must specify an AWS region argument or export a "\
      "variable AWS_REGION in your shell" unless options[:region]
  end

  # If a location for activity classes is provided, then ensure that it
  # exists
  if options[:act] && !File.exist?(options[:act])
    raise ArgumentError, "Please provide a valid location where your activity "\
      "classes are located."
  end

  # If a location for workflow classes is provided, then ensure that it
  # exists
  if options[:wf] && !File.exist?(options[:wf])
    raise ArgumentError, "Please provide a valid location where your workflow "\
      "classes are located."
  end

end

.create_app_dir(dirname) ⇒ Object

Creates the toplevel application directory



115
116
117
# File 'lib/aws/utils.rb', line 115

def self.create_app_dir(dirname)
  FileUtils.mkdir_p(dirname)
end

.create_flow_dir(options) ⇒ Object

Creates the flow/ directory structure for the user applicaiton and copies the activity and workflow files if a location is provided.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/aws/utils.rb', line 121

def self.create_flow_dir(options)
  dirname = "#{options[:path]}/#{options[:name]}/flow"
  FileUtils.mkdir_p(dirname)

  str = require_files(options[:act], dirname)
  # Default to a helpful string if no activity files are given
  str = str.empty? ? "# You can require your activity files here." : str
  # Write the generated string to the activities.rb file
  write_to_file("#{dirname}/activities.rb", str)

  str = require_files(options[:wf], dirname)
  # Default to a helpful string if no workflow files are given
  str = str.empty? ? "# You can require your workflow files here." : str
  # Write the generated string to the workflows.rb file
  write_to_file("#{dirname}/workflows.rb", str)

end

.generate(options) ⇒ Object

Main method that will be called by the FlowUtils utility.



245
246
247
248
249
250
251
252
253
# File 'lib/aws/utils.rb', line 245

def self.generate(options)
  return unless options[:deploy][:enabled]
  opts = options[:deploy]
  check_options(opts)
  puts "---"
  generate_files(opts)
  generate_url(opts) if opts[:eb]
  puts "---"
end

.generate_files(options) ⇒ Object

Generates the necessary file structure for a valid AWS Flow Ruby application that can be deployed to Elastic Beanstalk



103
104
105
106
107
108
109
110
111
112
# File 'lib/aws/utils.rb', line 103

def self.generate_files(options)
  dirname = "#{options[:path]}/#{options[:name]}"
  puts "Your AWS Flow Framework for Ruby application will be located at: "\
    "#{File.absolute_path(dirname)}/"
  create_app_dir(dirname)
  create_flow_dir(options)
  write_rack_config(dirname) if options[:eb]
  write_worker_config(options, dirname)
  write_gemfile(dirname)
end

.generate_url(options) ⇒ Object

Generates the appropriate 1-Click URL for beanstalk deployments based on the options passed by the user



92
93
94
95
96
97
98
99
# File 'lib/aws/utils.rb', line 92

def self.generate_url(options)
  url = "https://console.aws.amazon.com/elasticbeanstalk/"
  url = "#{url}?region=#{options[:region]}#/newApplication?"
  url = "#{url}applicationName=#{options[:name]}"
  url = "#{url}&solutionStackName=Ruby"
  url = "#{url}&instanceType=m1.large"
  puts "AWS Elastic Beanstalk 1-Click URL: #{url}"
end

.generate_worker_spec(name, key, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method to generate a worker config



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/aws/utils.rb', line 188

def self.generate_worker_spec(name, key, options)
  spec = {}
  if options[key]
    spec = {
      "#{name}_workers" => [
        {
          "#{name}_classes" => options[key],
          "number_of_workers" => 10
        }
      ]
    }
  end
  return spec
end

.require_files(src, dest) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method to copy files recursively into a directory and generate a require string



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aws/utils.rb', line 142

def self.require_files(src, dest)
  str = ""
  return str if src.nil?
  # If a valid location is passed in, copy the file(s) into the flow/
  # directory.
  FileUtils.cp_r(src, dest)
  if File.directory?(src)
    # If the given path is a directory, recursively get the relative
    # paths of all the files in the directory and generated the
    # require_relative string.
    Dir.glob("#{dest}/#{File.basename src}/**/*.rb").each do |x|
      a = Pathname.new(x)
      rel_path = a.relative_path_from(Pathname.new("#{dest}"))
      rel_path = rel_path.to_s.split(".rb").first
      str = "require_relative '#{rel_path}'\n#{str}"
    end
  else
    # If the given path is a file, just require the basename of the file
    str = "require_relative '#{File.basename src}'"
  end
  str
end

.write_gemfile(dirname) ⇒ Object

Creates the Gemfile for the user application



204
205
206
207
208
209
# File 'lib/aws/utils.rb', line 204

def self.write_gemfile(dirname)
  write_to_file(
    "#{dirname}/Gemfile",
    "source 'http://www.rubygems.org'\n\ngem 'aws-flow', '~> 2.4.0'"
  )
end

.write_rack_config(dirname) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a rack config for the beanstalk deployment



167
168
169
170
171
172
173
174
# File 'lib/aws/utils.rb', line 167

def self.write_rack_config(dirname)
  write_to_file(
    "#{dirname}/config.ru",
    "# Rack config file.\n\n"\
    "system(\"pkill -9 ruby\")\n\nsystem(\"aws-flow-ruby -f worker.json&\")\n\n"\
    "run Proc.new { |env| [200, {'Content-Type' => 'text/html'}, ['Done!']] }"
  )
end

.write_to_file(name, string) ⇒ Object

Helper method to write a string to a file



212
213
214
# File 'lib/aws/utils.rb', line 212

def self.write_to_file(name, string)
  File.open(name, 'wb') { |f| f.write(string) }
end

.write_worker_config(options, dirname) ⇒ Object

Creates the json worker config for the user application



177
178
179
180
181
182
183
184
# File 'lib/aws/utils.rb', line 177

def self.write_worker_config(options, dirname)
  spec = {}
  spec.merge!(generate_worker_spec("activity", :activities, options))
  spec.merge!(generate_worker_spec("workflow", :workflows, options))

  spec = spec.empty? ? "" : JSON.pretty_generate(spec)
  write_to_file("#{dirname}/worker.json", spec)
end