cfcm 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1 +1,2 @@
1
- gem 'vmc', "0.4.0.beta.36"
1
+ gem 'vmc', "0.4.0.beta.36"
2
+ gem 'eventmachine', "0.12.10"
data/Gemfile.lock CHANGED
@@ -6,6 +6,7 @@ GEM
6
6
  rest-client (~> 1.6.1)
7
7
  rubyzip (~> 0.9.5)
8
8
  crack (0.3.1)
9
+ eventmachine (0.12.10)
9
10
  interact (0.4.7)
10
11
  json_pure (1.6.7)
11
12
  manifests-vmc-plugin (0.4.3)
@@ -54,4 +55,5 @@ PLATFORMS
54
55
  ruby
55
56
 
56
57
  DEPENDENCIES
58
+ eventmachine (= 0.12.10)
57
59
  vmc (= 0.4.0.beta.36)
data/lib/cfcm/cf.rb CHANGED
@@ -8,12 +8,16 @@ module CFCM
8
8
  @client = VMC::Client.new(target)
9
9
  @client.login(username, password)
10
10
  rescue
11
- puts "Could not login!"
11
+ #puts "Could not login!"
12
12
  #TODO: Handle error
13
13
  return
14
14
  end
15
15
  end
16
16
 
17
+ def is_logged_in
18
+ return @client.auth_token != nil
19
+ end
20
+
17
21
  def get_app(app_name)
18
22
  if !@client
19
23
  puts "Must be loged in"
@@ -27,11 +31,16 @@ module CFCM
27
31
  end
28
32
  end
29
33
 
30
- puts "Application Not Found"
34
+ #puts "Application Not Found"
31
35
  #TODO: Handle error
32
36
  return nil
33
37
  end
34
38
 
39
+ def app_exists(app_name)
40
+ app = self.get_app(app_name)
41
+ return app != nil
42
+ end
43
+
35
44
  def remaining_memory
36
45
  if !@client
37
46
  puts "Must be loged in"
@@ -57,6 +66,19 @@ module CFCM
57
66
  max_new_instances = (remaining_memory / app_memory).floor
58
67
  return max_new_instances
59
68
  end
69
+
70
+ def add_instance(app)
71
+ app[:instances] += 1
72
+ @client.update_app(app[:name], app)
73
+ return app
74
+ end
75
+
76
+ def remove_instance(app)
77
+ app[:instances] -= 1
78
+ @client.update_app(app[:name], app)
79
+ return app
80
+ end
81
+
60
82
  end
61
83
  end
62
84
  end
data/lib/cfcm/cli.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require "thor"
2
+ require "cfcm/monkey"
2
3
 
3
4
  module CFCM
4
5
  module CLI
5
6
  class Command < Thor
6
-
7
+
7
8
  desc "soft USERNAME PASSWORD APP", "Removes/Adds instances via VMC APIs"
8
9
  method_option :target, :alias => ["--target"], :type => :string, :desc => "Cloud Foundry API Target URL"
9
10
  method_option :min, :alias => ["--min"], :type => :numeric, :desc => "Minimum number of instances"
@@ -22,6 +23,18 @@ module CFCM
22
23
 
23
24
  require 'cfcm/cf'
24
25
  session = CFCM::CF::Session.new(target, username, password)
26
+ if session.is_logged_in
27
+ if session.app_exists(app)
28
+ puts "RELEASE THE MONKEY!"
29
+ else
30
+ puts "Application not found"
31
+ end
32
+ else
33
+ puts "Could not log in"
34
+ end
35
+
36
+ monkey = CFCM::Monkey::SoftMonkey.new(session, app, 50, 1, 10)
37
+ monkey.start
25
38
  end
26
39
  end
27
40
  end
@@ -0,0 +1,46 @@
1
+ require "eventmachine"
2
+
3
+ module CFCM
4
+ module Monkey
5
+ class SoftMonkey
6
+ def initialize(session, app_name, probability, min, max)
7
+ @session = session
8
+ @app_name = app_name
9
+ @app = @session.get_app(app_name)
10
+ @probability = probability
11
+ @min_instances = min
12
+ @max_instances = max
13
+ end
14
+
15
+ def start
16
+ EventMachine.run do
17
+ EventMachine.add_periodic_timer(10) do
18
+ # Determine if we should unleash the monkey
19
+ if (Random.rand(100) + 1) <= @probability
20
+ @app = @session.get_app(@app_name)
21
+ instances = @app[:instances]
22
+ max_instances = @session.max_instance_growth(@app_name)
23
+ if max_instances == 0 || @max_instances == instances
24
+ # Shrink
25
+ puts "Shrink due to instance limitations"
26
+ @app = @session.add_instance(@app)
27
+ elsif instances == @min_instances
28
+ # Grow
29
+ puts "Grow due to instance limitations"
30
+ @app = @session.add_instance(@app)
31
+ elsif Random.rand(2) == 0
32
+ # Shrink
33
+ puts "Shrink due to randomness"
34
+ @app = @session.remove_instance(@app)
35
+ else
36
+ #Grow
37
+ puts "Grow due to randomness"
38
+ @app = @session.add_instance(@app)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/cfcm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CFCM
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -60,6 +60,7 @@ files:
60
60
  - cfcm.gemspec
61
61
  - lib/cfcm/cf.rb
62
62
  - lib/cfcm/cli.rb
63
+ - lib/cfcm/monkey.rb
63
64
  - lib/cfcm/version.rb
64
65
  homepage: https://github.com/BrianMMcClain/cloudfoundry-chaos-monkey
65
66
  licenses: []