cfcm 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cfcm/cli.rb CHANGED
@@ -6,10 +6,12 @@ module CFCM
6
6
  class Command < Thor
7
7
 
8
8
  desc "soft USERNAME PASSWORD APP", "Removes/Adds instances via VMC APIs"
9
- method_option :target, :alias => ["--target"], :type => :string, :desc => "Cloud Foundry API Target URL"
9
+ method_option :target, :aliases => ["--target", "-t"], :type => :string, :desc => "Cloud Foundry API Target URL"
10
10
  method_option :min, :alias => ["--min"], :type => :numeric, :desc => "Minimum number of instances"
11
11
  method_option :max, :alias => ["--max"], :type => :numeric, :desc => "Maximum number of instances"
12
- def soft(username, password, app, target = "http://api.cloudfoundry.com", min = 1, max = 5)
12
+ method_option :probability, :aliases => ["--probability", "-p"], :type => :numeric, :desc => "[1-100] Probablity for the Chaos Monkey to add/remove an instance"
13
+ method_option :frequency, :aliases => ["--frequency", "-f"], :type => :numeric, :desc => "Number of times to attempt to add/remove an instance"
14
+ def soft(username, password, app, target = "http://api.cloudfoundry.com", min = 1, max = 5, probability = 10, frequency = 10)
13
15
 
14
16
  if options[:target]
15
17
  target = options[:target]
@@ -18,22 +20,34 @@ module CFCM
18
20
  min = options[:min]
19
21
  end
20
22
  if options[:max]
21
- min = options[:max]
23
+ max = options[:max]
24
+ end
25
+ if options[:probability]
26
+ if options[:probability] < 1 || options[:probability] > 100
27
+ puts "Probability must be between 1 and 100"
28
+ return
29
+ else
30
+ probability = options[:probability]
31
+ end
32
+ end
33
+ if options[:frequency]
34
+ frequency = options[:frequency]
22
35
  end
23
36
 
24
37
  require 'cfcm/cf'
25
38
  session = CFCM::CF::Session.new(target, username, password)
26
39
  if session.is_logged_in
27
- if session.app_exists(app)
28
- puts "RELEASE THE MONKEY!"
29
- else
40
+ if !session.app_exists(app)
30
41
  puts "Application not found"
42
+ return
31
43
  end
32
44
  else
33
45
  puts "Could not log in"
46
+ return
34
47
  end
35
48
 
36
- monkey = CFCM::Monkey::SoftMonkey.new(session, app, 50, 1, 10)
49
+ puts "Awakening the monkey on #{app} with a #{probability}% chance of chaos every #{frequency} second#{frequency > 1? "s" : ""}"
50
+ monkey = CFCM::Monkey::SoftMonkey.new(session, app, probability, min, max, frequency)
37
51
  monkey.start
38
52
  end
39
53
  end
data/lib/cfcm/monkey.rb CHANGED
@@ -3,27 +3,35 @@ require "eventmachine"
3
3
  module CFCM
4
4
  module Monkey
5
5
  class SoftMonkey
6
- def initialize(session, app_name, probability, min, max)
6
+ def initialize(session, app_name, probability, min, max, frequency)
7
7
  @session = session
8
8
  @app_name = app_name
9
9
  @app = @session.get_app(app_name)
10
10
  @probability = probability
11
11
  @min_instances = min
12
12
  @max_instances = max
13
+ @frequency = frequency
13
14
  end
14
15
 
15
16
  def start
16
17
  EventMachine.run do
17
- EventMachine.add_periodic_timer(10) do
18
+ EventMachine.add_periodic_timer(@frequency) do
18
19
  # Determine if we should unleash the monkey
19
20
  if (Random.rand(100) + 1) <= @probability
20
21
  @app = @session.get_app(@app_name)
21
22
  instances = @app[:instances]
22
- max_instances = @session.max_instance_growth(@app_name)
23
- if max_instances == 0 || @max_instances == instances
24
- # Shrink
23
+ max_growth = @session.max_instance_growth(@app_name)
24
+
25
+ if instances > @max_instances
25
26
  puts "Shrink due to instance limitations"
27
+ @app = @session.remove_instance(@app)
28
+ elsif instances < @min_instances
29
+ puts "Grow due to instance limitations"
26
30
  @app = @session.add_instance(@app)
31
+ elsif max_growth == 0 || @max_instances == instances
32
+ # Shrink
33
+ puts "Shrink due to instance limitations"
34
+ @app = @session.remove_instance(@app)
27
35
  elsif instances == @min_instances
28
36
  # Grow
29
37
  puts "Grow due to instance limitations"
data/lib/cfcm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CFCM
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: