Class: PoolParty::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/poolparty/application.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.options(opts = {}) ⇒ Object

The application options



11
12
13
# File 'lib/poolparty/application.rb', line 11

def options
  @options
end

.verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/poolparty/application.rb', line 8

def verbose
  @verbose
end

Class Method Details

.default_optionsObject

Basic default options All can be overridden by the command line or in a config.yml file



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
# File 'lib/poolparty/application.rb', line 82

def default_options
  @default_options ||= {
    :app_name => "application_name",
    :host_port => 80,
    :client_port => 8001,
    :environment => 'development',
    :verbose => false,
    :logging => true,
    :size => "m1.small",
    :polling_time => "30.seconds",
    :user_data => "",
    :heavy_load => 0.80,
    :light_load => 0.15,
    :minimum_instances => 2,
    :maximum_instances => 4,
    :public_ip => "",
    :access_key => ENV["AWS_ACCESS_KEY"],
    :secret_access_key => ENV["AWS_SECRET_ACCESS"],
    :config_file => if ENV["CONFIG_FILE"] && !ENV["CONFIG_FILE"].empty?
      ENV["CONFIG_FILE"]
    elsif File.file?("config/config.yml")
      "config/config.yml"
    else
      nil
    end,
    :username => "root",
    :ec2_dir => (ENV["EC2_HOME"].nil? || ENV["EC2_HOME"].empty?) ? "~/.ec2" : ENV["EC2_HOME"],
    :keypair => (ENV["KEYPAIR_NAME"].nil? || ENV["KEYPAIR_NAME"].empty?) ? File.basename(`pwd`).strip : ENV["KEYPAIR_NAME"],
    :ami => 'ami-44bd592d',
    :shared_bucket => "",
    :expand_when => "web < 1.5\n memory > 0.85",
    :contract_when => "cpu < 0.20\n memory < 0.10",
    :os => "ubuntu",
    :plugin_dir => "plugins",
    :install_on_load => false,
    :working_directory => Dir.pwd
  }
end

.environment=(env) ⇒ Object



168
169
170
# File 'lib/poolparty/application.rb', line 168

def environment=(env)
  environment = env
end

.hash_to_launch_withObject



128
129
130
131
132
133
134
135
136
# File 'lib/poolparty/application.rb', line 128

def hash_to_launch_with
  @hash ||= { :polling_time => polling_time, 
    :access_key => access_key, 
    :secret_access_key => secret_access_key,
    :user_data => user_data,
    :keypair => keypair,
    :keypair_path => "/mnt"
  }
end

.install_on_load?(bool = false) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/poolparty/application.rb', line 188

def install_on_load?(bool=false)
  options.install_on_load == true || bool
end

.keypair_nameObject



157
158
159
# File 'lib/poolparty/application.rb', line 157

def keypair_name
  "id_rsa-#{keypair}"
end

.keypair_pathObject

Keypair path Idiom:

/Users/username/.ec2/[name]


154
155
156
# File 'lib/poolparty/application.rb', line 154

def keypair_path
  options.keypair_path ? options.keypair_path : "#{ec2_dir}/#{keypair_name}"
end

.launching_user_dataObject



125
126
127
# File 'lib/poolparty/application.rb', line 125

def launching_user_data
  hash_to_launch_with.to_yaml
end

.load_options!(opts = {}) ⇒ Object

Load options via commandline



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
# File 'lib/poolparty/application.rb', line 36

def load_options!(opts={})
  require 'optparse'
  OptionParser.new do |op|
    op.banner = opts[:banner] if opts[:banner]
    op.on('-A key', '--access-key key', "Ec2 access key (ENV['AWS_ACCESS_KEY'])") { |key| default_options[:access_key] = key }
    op.on('-S key', '--secret-access-key key', "Ec2 secret access key (ENV['AWS_SECRET_ACCESS'])") { |key| default_options[:secret_access_key] = key }
    op.on('-I ami', '--image-id id', "AMI instance (default: 'ami-40bc5829')") {|id| default_options[:ami] = id }
    op.on('-k keypair', '--keypair name', "Keypair name (ENV['KEYPAIR_NAME'])") { |key| default_options[:keypair] = key }
    op.on('-b bucket', '--bucket bucket', "Application bucket") { |bucket| default_options[:shared_bucket] = bucket }
    # //THIS IS WHERE YOU LEFT OFF
    op.on('-D working directory', '--dir dir', "Working directory") { |d| default_options[:working_directory] = d }
    
    op.on('--ec2-dir dir', "Directory with ec2 data (default: '~/.ec2')") {|id| default_options[:ec2_dir] = id }
    op.on('-r names', '--services names', "Monitored services (default: '')") {|id| default_options[:services] = id }
    op.on('-c file', '--config-file file', "Config file (default: '')") {|file| default_options[:config_file] = file }
    op.on('-l plugin_dir', '--plugin-dir dir', "Plugin directory (default: '')") {|file| default_options[:plugin_dir] = file }
    op.on('-p port', '--host_port port', "Run on specific host_port (default: 7788)") { |host_port| default_options[:host_port] = host_port }
    op.on('-m monitors', '--monitors names', "Monitor instances using (default: 'web,memory,cpu')") {|s| default_options[:monitor_load_on] = s }          
    op.on('-o port', '--client_port port', "Run on specific client_port (default: 7788)") { |client_port| default_options[:client_port] = client_port }
    op.on('-O os', '--os os', "Configure for os (default: ubuntu)") { |os| default_options[:os] = os }          
    op.on('-e env', '--environment env', "Run on the specific environment (default: development)") { |env| default_options[:environment] = env }
    op.on('-a address', '--public-ip address', "Associate this public address with the master node") {|s| default_options[:public_ip] = s}
    op.on('-s size', '--size size', "Run specific sized instance") {|s| default_options[:size] = s}
    op.on('-a name', '--name name', "Application name") {|n| default_options[:app_name] = n}
    op.on('-u username', '--username name', "Login with the user (default: root)") {|s| default_options[:user] = s}
    op.on('-d user-data','--user-data data', "Extra data to send each of the instances (default: "")") { |data| default_options[:user_data] = data.to_str }
    op.on('-i', '--install-on-boot', 'Install the PoolParty and custom software on boot (default: false)') {|b| default_options[:install_on_load] = true}
    op.on('-t seconds', '--polling-time', "Time between polling in seconds (default 50)") {|t| default_options[:polling_time] = t }
    op.on('-v', '--[no-]verbose', 'Run verbosely (default: false)') {|v| default_options[:verbose] = true}
    op.on('-n number', '--minimum-instances', "The minimum number of instances to run at all times (default 1)") {|i| default_options[:minimum_instances] = i.to_i}
    op.on('-x number', '--maximum-instances', "The maximum number of instances to run (default 3)") {|x| default_options[:maximum_instances] = x.to_i}
    
    op.on_tail("-V", "Show version") do
      puts Application.version
      exit
    end
    op.on_tail("-h", "-?", "Show this message") do
      puts op
      exit
    end
  end.parse!(opts[:argv] ? opts.delete(:argv) : ARGV.dup)
end

.local_user_dataObject



137
138
139
140
141
142
143
144
145
# File 'lib/poolparty/application.rb', line 137

def local_user_data 
  @local_user_data ||= begin
    @@timer.timeout(2.seconds) do
      YAML.load(open("http://169.254.169.254/latest/user-data").read)
    end
  rescue Exception => e
    {}
  end
end

.maintain_pid_pathObject



171
172
173
# File 'lib/poolparty/application.rb', line 171

def maintain_pid_path
  "/var/run/pool_maintain.pid"
end

.make_options(opts = {}) ⇒ Object

Make the options with the config_file overrides included Default config file assumed to be at config/config.yml



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/poolparty/application.rb', line 16

def make_options(opts={})
  loading_options = opts.delete(:optsparse) || {}
  loading_options.merge!( opts || {})
  
  load_options!(loading_options) # Load command-line options
  config_file_location = (default_options[:config_file] || opts[:config_file])
  
  # If the config_file options are specified and not empty
  unless config_file_location.nil? || config_file_location.empty?
    require "yaml"
    # Try loading the file if it exists
    filedata = File.open("#{config_file_location}").read if File.file?("#{config_file_location}")
    default_options.merge!( YAML.load(filedata) ) if filedata rescue ""
  end
  # We want the command-line to overwrite the config file
  default_options.merge!(local_user_data) unless local_user_data.nil?        
  OpenStruct.new(default_options)
end

.master_managed_servicesObject Also known as: managed_services

Services monitored by Heartbeat



121
122
123
# File 'lib/poolparty/application.rb', line 121

def master_managed_services
  "cloud_master_takeover"
end

.method_missing(m, *args) ⇒ Object

Call the options from the Application



192
193
194
# File 'lib/poolparty/application.rb', line 192

def method_missing(m,*args)
  options.methods.include?("#{m}") ? options.send(m,args) : super
end

.reset!Object

For testing purposes



147
148
149
150
# File 'lib/poolparty/application.rb', line 147

def reset!
  @options = nil
  @local_user_data = nil
end

.versionObject



185
186
187
# File 'lib/poolparty/application.rb', line 185

def version
  PoolParty::Version.string
end