class AWS::Flow::Utils::InitConfig
Public Class Methods
Validates various options
# 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
Creates the toplevel application directory
# File lib/aws/utils.rb, line 115 def self.create_app_dir(dirname) FileUtils.mkdir_p(dirname) end
Creates the flow/ directory structure for the user applicaiton and copies the activity and workflow files if a location is provided.
# 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
Main method that will be called by the FlowUtils
utility.
# 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
Generates the necessary file structure for a valid AWS
Flow
Ruby application that can be deployed to Elastic Beanstalk
# 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
Generates the appropriate 1-Click URL for beanstalk deployments based on the options passed by the user
# 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
Helper method to generate a worker config @api private
# 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
Helper method to copy files recursively into a directory and generate a require string @api private
# 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
Creates the Gemfile for the user application
# 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
Creates a rack config for the beanstalk deployment @api private
# 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
Helper method to write a string to a file
# File lib/aws/utils.rb, line 212 def self.write_to_file(name, string) File.open(name, 'wb') { |f| f.write(string) } end
Creates the json worker config for the user application
# 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