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

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

Overview

Initializes an AWS Flow Framework for Ruby application

Class Method Summary (collapse)

Class Method Details

+ (Object) check_options(options)

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 'aws-flow-ruby/aws-flow/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

+ (Object) create_app_dir(dirname)

Creates the toplevel application directory



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

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

+ (Object) create_flow_dir(options)

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 'aws-flow-ruby/aws-flow/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

+ (Object) generate(options)

Main method that will be called by the FlowUtils utility.



245
246
247
248
249
250
251
252
253
# File 'aws-flow-ruby/aws-flow/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

+ (Object) generate_files(options)

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 'aws-flow-ruby/aws-flow/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

+ (Object) generate_url(options)

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 'aws-flow-ruby/aws-flow/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

+ (Object) write_gemfile(dirname)

Creates the Gemfile for the user application



204
205
206
207
208
209
# File 'aws-flow-ruby/aws-flow/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

+ (Object) write_to_file(name, string)

Helper method to write a string to a file



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

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

+ (Object) write_worker_config(options, dirname)

Creates the json worker config for the user application



177
178
179
180
181
182
183
184
# File 'aws-flow-ruby/aws-flow/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