mystro-common 0.1.11 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +3 -0
  2. data/.rspec +2 -0
  3. data/CHANGELOG.md +42 -4
  4. data/Gemfile +7 -1
  5. data/Rakefile +137 -1
  6. data/lib/{mystro/ext/fog → fog/ext}/balancer.rb +0 -0
  7. data/lib/fog/ext/dynect/dns.rb +140 -0
  8. data/lib/fog/ext/dynect/models/dns/record.rb +66 -0
  9. data/lib/fog/ext/dynect/models/dns/records.rb +87 -0
  10. data/lib/fog/ext/dynect/models/dns/zone.rb +60 -0
  11. data/lib/fog/ext/dynect/models/dns/zones.rb +29 -0
  12. data/lib/fog/ext/dynect/requests/dns/delete_record.rb +56 -0
  13. data/lib/fog/ext/dynect/requests/dns/delete_zone.rb +42 -0
  14. data/lib/fog/ext/dynect/requests/dns/get_all_records.rb +56 -0
  15. data/lib/fog/ext/dynect/requests/dns/get_node_list.rb +56 -0
  16. data/lib/fog/ext/dynect/requests/dns/get_record.rb +85 -0
  17. data/lib/fog/ext/dynect/requests/dns/get_zone.rb +58 -0
  18. data/lib/fog/ext/dynect/requests/dns/post_record.rb +72 -0
  19. data/lib/fog/ext/dynect/requests/dns/post_session.rb +44 -0
  20. data/lib/fog/ext/dynect/requests/dns/post_zone.rb +71 -0
  21. data/lib/fog/ext/dynect/requests/dns/put_zone.rb +76 -0
  22. data/lib/fog/ext/dynect.rb +26 -0
  23. data/lib/mystro/cloud/action.rb +22 -0
  24. data/lib/mystro/cloud/connect/aws/balancer.rb +55 -0
  25. data/lib/mystro/cloud/connect/aws/compute.rb +151 -0
  26. data/lib/mystro/cloud/connect/aws/listener.rb +36 -0
  27. data/lib/mystro/cloud/connect/aws/record.rb +58 -0
  28. data/lib/mystro/cloud/connect/aws/zone.rb +35 -0
  29. data/lib/mystro/cloud/connect/aws.rb +14 -0
  30. data/lib/mystro/cloud/connect/dynect/record.rb +72 -0
  31. data/lib/mystro/cloud/connect/dynect/zone.rb +35 -0
  32. data/lib/mystro/cloud/connect/dynect.rb +17 -0
  33. data/lib/mystro/cloud/connect/fog.rb +66 -0
  34. data/lib/mystro/cloud/connect.rb +64 -0
  35. data/lib/mystro/cloud/model/balancer.rb +15 -0
  36. data/lib/mystro/cloud/model/compute.rb +27 -0
  37. data/lib/mystro/cloud/model/listener.rb +30 -0
  38. data/lib/mystro/cloud/model/record.rb +20 -0
  39. data/lib/mystro/cloud/model/volume.rb +12 -0
  40. data/lib/mystro/cloud/model/zone.rb +9 -0
  41. data/lib/mystro/cloud/model.rb +183 -0
  42. data/lib/mystro/cloud.rb +32 -0
  43. data/lib/mystro/common/version.rb +4 -3
  44. data/lib/mystro/dsl/balancer.rb +18 -0
  45. data/lib/mystro/dsl/compute.rb +57 -0
  46. data/lib/mystro/dsl/health.rb +7 -0
  47. data/lib/mystro/dsl/listener.rb +5 -0
  48. data/lib/mystro/dsl/oldtemplate.rb +281 -0
  49. data/lib/mystro/dsl/template.rb +12 -278
  50. data/lib/mystro/dsl/template_file.rb +18 -0
  51. data/lib/mystro/dsl/volume.rb +8 -0
  52. data/lib/mystro/dsl.rb +40 -0
  53. data/lib/mystro/organization.rb +83 -0
  54. data/lib/mystro/plugin.rb +4 -3
  55. data/lib/mystro/provider.rb +40 -0
  56. data/lib/mystro/userdata.rb +1 -1
  57. data/lib/mystro-common.rb +32 -31
  58. data/mystro-common.gemspec +2 -1
  59. data/spec/cloud/aws/balancer_spec.rb +10 -0
  60. data/spec/cloud/aws/compute_spec.rb +10 -0
  61. data/spec/cloud/aws/record_spec.rb +10 -0
  62. data/spec/cloud/dynect/record_spec.rb +10 -0
  63. data/spec/model/compute_spec.rb +36 -0
  64. data/spec/model/model_spec.rb +89 -0
  65. data/spec/model/record_spec.rb +27 -0
  66. data/spec/spec_helper.rb +36 -0
  67. data/spec/support/balancer.rb +49 -0
  68. data/spec/support/compute.rb +65 -0
  69. data/spec/support/record.rb +45 -0
  70. data/test/config.yml +71 -0
  71. metadata +99 -14
  72. data/lib/mystro/account.rb +0 -105
  73. data/lib/mystro/connect/balancer.rb +0 -91
  74. data/lib/mystro/connect/compute.rb +0 -100
  75. data/lib/mystro/connect/dns.rb +0 -51
  76. data/lib/mystro/connect/environment.rb +0 -31
  77. data/lib/mystro/connect.rb +0 -124
  78. data/lib/mystro/job.rb +0 -0
  79. data/lib/mystro/model.rb +0 -71
@@ -1,105 +0,0 @@
1
- module Mystro
2
- class Account
3
- class << self
4
- #attr_reader :name
5
- #attr_reader :list
6
- attr_reader :selected
7
-
8
- def [](name)
9
- get(name)
10
- end
11
-
12
- def get(name)
13
- @list[name]
14
- end
15
-
16
- def read
17
- dir = Mystro.directory
18
- @list = { }
19
-
20
- Dir["#{dir}/accounts/*.y*ml"].each do |file|
21
- name = file.gsub(/#{dir}\/accounts\//, "").gsub(/\.(\w+?)$/, "")
22
- Mystro::Log.debug "loading account '#{name}' '#{file}'"
23
- @list[name] = self.new(name, file)
24
- end
25
-
26
- @selected = default
27
- end
28
-
29
- def default
30
- return ENV['MYSTRO_ACCOUNT'] if ENV['MYSTRO_ACCOUNT']
31
- return Mystro.config.default_account if Mystro.config.default_account?
32
- return "default" if @list.keys.include?("default")
33
- @list.keys.first
34
- end
35
-
36
- def select(name)
37
- @selected = name
38
- end
39
-
40
- #def data(name = get)
41
- # if @name != name
42
- # @data = nil
43
- # @loaded = false
44
- # @name = nil
45
- # end
46
- #
47
- # @data ||= begin
48
- # Mystro::Log.debug "loading account"
49
- # a = Mystro::Model::Account.load(name)
50
- # a[:name] = name
51
- #
52
- # Mystro::Log.debug "loading plugins from account"
53
- # Mystro::Plugin.load(a[:plugins]) if a[:plugins]
54
- #
55
- # a
56
- # end
57
- # @name = @data[:name]
58
- # @loaded = true
59
- # @data
60
- #end
61
- #
62
- #def get
63
- # return ENV['RIG_ACCOUNT'] if ENV['RIG_ACCOUNT']
64
- # return Mystro.config[:account] if Mystro.config[:account]
65
- # return Mystro.config[:default_account] if Mystro.config[:default_account]
66
- # return Mystro.config[:accounts].first if Mystro.config[:accounts] && Mystro.config[:accounts].count > 0
67
- # "default"
68
- #end
69
- #
70
- #def save
71
- # name = Mystro.account[:name]
72
- # Mystro::Model::Account.save(name, Mystro.account)
73
- #end
74
- end
75
-
76
- attr_reader :data
77
- attr_reader :file
78
- attr_reader :name
79
-
80
- def initialize(name, file)
81
- cfg = Mystro.config.to_hash
82
- account = File.exists?(file) ? YAML.load_file(file) : { }
83
- @name = name
84
- @file = file
85
- @data = Hashie::Mash.new(cfg.deep_merge(account))
86
- @data.name = name
87
- end
88
-
89
- def compute
90
- @compute ||= Mystro::Connect::Compute.new(self) if @data.compute
91
- end
92
-
93
- def balancer
94
- @balancer ||= Mystro::Connect::Balancer.new(self) if @data.balancer
95
- end
96
-
97
- def dns
98
- @dns ||= Mystro::Connect::Dns.new(self) if @data.dns
99
- end
100
-
101
- def environment
102
- @environment ||= Mystro::Connect::Environment.new(self)
103
- end
104
- end
105
- end
@@ -1,91 +0,0 @@
1
-
2
- module Mystro
3
- module Connect
4
- class Balancer < Base
5
- self.model = "Fog::Balancer"
6
- self.collection = :load_balancers
7
-
8
- def all
9
- fog.send(collection).all
10
- end
11
-
12
- def create(model)
13
- balancer = fog.send(collection).create(model.fog_options)
14
- balancer.register_instances(model.computes.collect{|e| e.rid})
15
- balancer.save
16
- end
17
-
18
- def find_by_environment(name)
19
- all.select {|e| e.id =~ /^#{name}\-/}
20
- end
21
-
22
- def listener_find(id, from)
23
- balancer = find(id)
24
- (from_proto, from_port) = from.split(':')
25
- match = balancer.listeners.select {|l| l.protocol == from_proto && l.lb_port == from_port.to_i }
26
- raise "no listeners #{from} found" if match.count == 0
27
- raise "more than one listener matched #{from}" if match.count > 1
28
- match.first
29
- end
30
-
31
- def listener_create(id, model)
32
- Mystro::Log.debug "balancer#add_listener #{id} #{model.inspect}"
33
- lb = find(id)
34
- opts = model.fog_options
35
- ap opts
36
- lb.listeners.create(opts)
37
- end
38
-
39
- def listener_destroy(id, from)
40
- Mystro::Log.debug "balancer#rm_listener #{id} #{from}"
41
- listener = listener_find(id, from)
42
- listener.destroy
43
- end
44
-
45
- def health_check(id, health)
46
- Mystro::Log.debug "balancer#health_check #{id} #{health.inspect}"
47
- balancer = find(id)
48
- raise "balancer #{id} not found" unless balancer
49
-
50
- fog.configure_health_check(id, health.fog_options)
51
- end
52
-
53
- def sticky(id, type, expires_or_cookie, port, policy=nil)
54
- balancer = find(id)
55
- raise "balancer #{id} not found" unless balancer
56
-
57
- policy ||= "sticky-#{id}-#{type}-#{port}"
58
- policies = balancer.attributes["ListenerDescriptions"] ? balancer.attributes["ListenerDescriptions"].inject([]) {|a, e| a << e["PolicyNames"]}.flatten : []
59
- policies << policy
60
-
61
- case type.downcase.to_sym
62
- when :app, :application
63
- fog.create_app_cookie_stickiness_policy(id, policy, expires_or_cookie)
64
- fog.set_load_balancer_policies_of_listener(id, port.to_i, policies)
65
- when :lb, :loadbalancer, :load_balancer
66
- fog.create_lb_cookie_stickiness_policy(id, policy, expires_or_cookie.to_i)
67
- fog.set_load_balancer_policies_of_listener(id, port.to_i, policies)
68
- else
69
- raise "unknown sticky type #{type}"
70
- end
71
- end
72
-
73
- def unsticky(id, type, port, policy=nil)
74
- balancer = find(id)
75
- raise "balancer #{id} not found" unless balancer
76
-
77
- policy ||= "sticky-#{id}-#{type}-#{port}"
78
- policies = balancer.attributes["ListenerDescriptions"] ? balancer.attributes["ListenerDescriptions"].inject([]) {|a, e| a << e["PolicyNames"]}.flatten : []
79
- policies.delete(policy)
80
-
81
- case type.downcase.to_sym
82
- when :app, :application, :lb, :loadbalancer, :load_balancer
83
- fog.set_load_balancer_policies_of_listener(id, port.to_i, policies)
84
- fog.delete_load_balancer_policy(id, policy)
85
- else
86
- raise "unknown sticky type #{type}"
87
- end
88
- end
89
- end
90
- end
91
- end
@@ -1,100 +0,0 @@
1
- module Mystro
2
- module Connect
3
- class Compute < Base
4
- self.model = "Fog::Compute"
5
- self.collection = :servers
6
-
7
- def all(filters={ })
8
- fog.send(collection).all(filters)
9
- end
10
-
11
- def running
12
- all({ "instance-state-name" => "running" })
13
- end
14
-
15
- def find(id_or_name)
16
- if id_or_name =~ /^i-/
17
- super(id_or_name)
18
- else
19
- find_by_name(id_or_name)
20
- end
21
- end
22
-
23
- def find_by_name(name)
24
- list = find_by_tags(name: name)
25
- return list if list.count > 0
26
- find_by_nick(name)
27
- end
28
-
29
- def find_by_nick(nick)
30
- Mystro::Log.debug "compute#find_by_nick #{nick}"
31
- (name, env) = nick.match(/(\w+)\.(\w+)/)[1..2]
32
- find_by_environment(env).select { |s| s.tags['Name'] =~ /^#{name}/ }
33
- rescue => e
34
- Mystro::Log.error "error finding server by nickname #{nick}. name should be of the form: role#.environment"
35
- []
36
- end
37
-
38
- def find_by_environment(environment)
39
- find_by_tags(environment: environment)
40
- end
41
-
42
- def find_by_environment_and_role(environment, role)
43
- list = find_by_tags(environment: environment)
44
- list.select { |s| r = s.tags['Roles'] || s.tags['Role']; r.split(",").include?(role) }
45
- end
46
-
47
- def find_by_tags(tags)
48
- Mystro::Log.debug "compute#find_by_tags #{tags.inspect}"
49
- filters = tags.inject({ }) { |h, e| (k, v) = e; h["tag:#{k.to_s.capitalize}"] = v; h }
50
- all(filters)
51
- end
52
-
53
- def create(model)
54
- Mystro::Log.debug "#{cname}#create #{model.inspect}"
55
- e = fog.send(collection).create(model.fog_options)
56
- fog.create_tags(e.id, model.fog_tags)
57
- Mystro::Plugin.run "compute:create", e, model
58
- e
59
- end
60
-
61
- #after :create do |compute, model|
62
- # Mystro::Log.debug "compute#after_create #{compute.id} #{model.fog_tags}"
63
- # Mystro::Log.debug "#{cname}#create #{fog.inspect}"
64
- # sleep 3
65
- #end
66
-
67
- def destroy(models)
68
- list = [*models].flatten
69
- list.each do |m|
70
- Mystro::Log.debug "#{cname}#destroy #{m.rid}"
71
- e = find(m.rid)
72
- Mystro::Plugin.run "compute:destroy", e, m
73
- e.destroy
74
- tags = e.tags.keys.inject({ }) { |h, e| h[e] = nil; h }
75
- fog.create_tags([e.id], tags)
76
- end
77
- end
78
-
79
- #def destroy(list)
80
- # list = [*list]
81
- # if list.count > 0
82
- # ids = list.map {|e| e.kind_of?(String) ? e : e.id}
83
- # Mystro::Log.debug "compute#destroy: #{ids.inspect}"
84
- # list.each do |e|
85
- # Mystro::Plugin.run "compute:destroy", e
86
- # e.destroy
87
- # tags = e.tags.keys.inject({}) {|h, e| h[e] = nil; h }
88
- # fog.create_tags(ids, tags)
89
- # end
90
- # end
91
- #end
92
-
93
- #after :destroy do |compute, tags|
94
- # Mystro::Log.debug "#{cname}#after_destroy"
95
- # tags = compute.tags.keys.inject({ }) { |h, e| h[e] = nil; h }
96
- # fog.create_tags([compute.id], tags) if tags.count > 0
97
- #end
98
- end
99
- end
100
- end
@@ -1,51 +0,0 @@
1
-
2
- module Mystro
3
- module Connect
4
- class Dns < Base
5
- self.model = "Fog::DNS"
6
- self.collection = :records
7
-
8
- def find_by_endpoint(dns)
9
- all.select {|e| [*e.value].flatten.include?(dns)}
10
- end
11
-
12
- def find_by_name(name)
13
- connection.zones.all.detect {|e| e.domain == "#{name}."}
14
- end
15
-
16
- # customize the connect function, because we are defaulting
17
- # to the zone from the configuration
18
- def connect
19
- @fog ||= begin
20
- raise "must set zone" unless cfg.zone
21
- raise "must set options" unless opt
22
- find_by_name(cfg.zone)
23
- end
24
- rescue => e
25
- Mystro::Log.error "DNS connect failed: #{e.message} at #{e.backtrace.first}"
26
- Mystro::Log.error e
27
- end
28
-
29
- def connection
30
- Fog::DNS.new(opt.to_hash.symbolize_keys)
31
- end
32
-
33
- def zones
34
- connection.zones.all
35
- end
36
-
37
- def zone(name)
38
- n = "#{name}."
39
- z = connection.zones.get(n)
40
- raise("zone #{n} not found") unless z
41
- z
42
- rescue => e
43
- raise("#{e.message} at #{e.backtrace.first}")
44
- end
45
-
46
- def create_zone(model)
47
- connection.zones.create(model.fog_options)
48
- end
49
- end
50
- end
51
- end
@@ -1,31 +0,0 @@
1
- module Mystro
2
- module Connect
3
- class Environment < Base
4
- def connect
5
-
6
- end
7
-
8
- def connected?
9
- true
10
- end
11
-
12
- def find(name)
13
- Mystro::Model::Environment.load(name)
14
- end
15
-
16
- def all
17
- list = Mystro.compute.all
18
- list.inject([]) { |a, e| a << e.tags["Environment"] }.compact.uniq.sort
19
- end
20
-
21
- def create(model)
22
- Mystro::Log.info "model: #{model}"
23
- list = model.template_to_models
24
- end
25
-
26
- def destroy
27
-
28
- end
29
- end
30
- end
31
- end
@@ -1,124 +0,0 @@
1
- module Mystro
2
- module Connect
3
- class Base
4
- class << self
5
- attr_accessor :model
6
- attr_accessor :collection
7
-
8
- def cname
9
- self.name.split("::").last.downcase
10
- end
11
-
12
- def fog
13
- Mystro.send(cname).fog || nil rescue nil
14
- end
15
-
16
- def after(event, &block)
17
- @hooks ||= {}
18
- @hooks[event] ||= []
19
- @hooks[event] << block
20
- end
21
-
22
- def hooks(event, *args)
23
- Mystro::Log.debug "#{cname}:hooks:#{event} #{@hooks[event].count}"
24
- @hooks[event].each do |e|
25
- Mystro::Log.debug "#{cname}:hooks:#{event} #{e}"
26
- e.call(*args) if e
27
- end
28
- end
29
- end
30
-
31
- attr_reader :cfg
32
- attr_reader :opt
33
- attr_reader :fog
34
-
35
- def initialize(account)
36
- a = account.data
37
- c = cname
38
-
39
- # opt is used to initialize fog in connect classes
40
- defaults = a.connect!.fog? ? a.connect.fog : {}
41
- overrides = a[c] && a[c].fog? ? a[c].fog : {}
42
- raw = defaults.deep_merge(overrides)
43
- @opt = Hashie::Mash.new(raw)
44
-
45
- # cfg is the additional configuration parms (aside from the fog block)
46
- raw = {}
47
- if a[c]
48
- raw = a[c].to_hash
49
- raw.delete(:fog) if raw[:fog]
50
- end
51
- @cfg = Hashie::Mash.new(raw)
52
-
53
- self.class.after :create do |e, model|
54
- Mystro::Plugin.run "#{cname}:create", e, model
55
- end
56
-
57
- self.class.after :destroy do |e, tags|
58
- Mystro::Plugin.run "#{cname}:destroy", e, tags
59
- end
60
-
61
- connect
62
- end
63
-
64
- def connect
65
- #puts "connect: #{model}.new(#{opt.to_hash.symbolize_keys})"
66
- @fog ||= model.new(opt.to_hash.symbolize_keys) if opt
67
- rescue => e
68
- Mystro::Log.error "#{cname} connect failed: #{e.message} at #{e.backtrace.first}"
69
- end
70
-
71
- def connected?
72
- fog.nil? ? false : true
73
- end
74
-
75
- def find(id)
76
- fog.send(collection).get(id)
77
- end
78
-
79
- def all
80
- fog.send(collection).all
81
- end
82
-
83
- def create(model)
84
- Mystro::Log.debug "#{cname}#create #{model.inspect}"
85
- Mystro::Log.debug "#{cname}#create #{fog.inspect}"
86
- e = fog.send(collection).create(model.fog_options)
87
- e
88
- end
89
-
90
- def destroy(models)
91
- list = [*models].flatten
92
- list.each do |m|
93
- Mystro::Log.debug "#{cname}#destroy #{m.rid}"
94
- e = find(m.rid)
95
- e.destroy if e
96
- end
97
- end
98
-
99
- def model
100
- raise "model not set for '#{cname}'" unless self.class.model
101
- @model ||= self.class.model.constantize
102
- end
103
-
104
- def collection
105
- raise "collection not set for '#{cname}'" unless self.class.collection
106
- @collection ||= self.class.collection
107
- end
108
-
109
- def cname
110
- self.class.cname
111
- end
112
-
113
- def after(*args)
114
- self.class.after(*args)
115
- end
116
-
117
- def hooks(*args)
118
- self.class.hooks(*args)
119
- end
120
- end
121
- end
122
- end
123
-
124
- Dir["#{File.dirname(__FILE__)}/connect/*.rb"].each {|file| require "#{file.gsub(/\.rb/,'')}" }
data/lib/mystro/job.rb DELETED
File without changes
data/lib/mystro/model.rb DELETED
@@ -1,71 +0,0 @@
1
- require 'mystro-common'
2
-
3
- module Mystro
4
- module Model
5
- class Base
6
- attr_reader :attributes
7
-
8
- def initialize(attributes={ })
9
- # all the stringify keys calls are to make sure that we normalize EVERYTHING before merges
10
- d = defaults.stringify_keys!
11
- c = load_from_config.stringify_keys!
12
- a = attributes.stringify_keys!
13
- # attributes should ALWAYS be stringified keys
14
- @attributes = d.deep_merge(c).deep_merge(a).stringify_keys!
15
- end
16
-
17
- def defaults
18
- self.class.defaults || {}
19
- end
20
-
21
- def fog_tags(hash=self.class.tagnames.each {|t| hash[t] = send(t) })
22
- hash.inject({ }) do |h, e|
23
- (k, v) = e
24
- h["#{k.to_s.capitalize}"] = v
25
- h
26
- end
27
- end
28
-
29
- private
30
-
31
- def load_from_config
32
- cname = self.class.name.split("::").last.downcase
33
- config = { }
34
- if Mystro.account && Mystro.account[cname]
35
- config = Mystro.account[cname].to_hash
36
- end
37
- config["account"] = Mystro.selected
38
- config
39
- end
40
-
41
- class << self
42
- attr_reader :tagnames
43
- attr_reader :attrnames
44
- attr_reader :defaults
45
-
46
- def attr(*list)
47
- @attrnames = list
48
- list.each do |a|
49
- define_method(a) do
50
- @attributes[a.to_s]
51
- end
52
- define_method("#{a}=") do |val|
53
- @attributes[a.to_s] = val
54
- end
55
- end
56
- end
57
-
58
- def tag(*list)
59
- @tagnames = list
60
- attr(*list)
61
- end
62
-
63
- def default(hash)
64
- @defaults = hash
65
- end
66
- end
67
- end
68
- end
69
- end
70
-
71
- Dir["#{File.dirname(__FILE__)}/model/*.rb"].each { |file| require file.gsub(/\.rb/, '') }