mystro-common 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.1.7:
4
+ * fix lingering error with Account.list change
5
+ * merge v0.1.6
6
+ * merge release v0.1.2
7
+ * new DSL framework
8
+ * ruby version file
9
+ * changelog rake tasks
10
+
3
11
  ## v0.1.6:
4
12
 
5
13
  ## v0.1.5:
data/Rakefile CHANGED
@@ -49,13 +49,3 @@ desc "show current changes (changelog output from HEAD to most recent tag)"
49
49
  task :current do
50
50
  changelog("HEAD",true)
51
51
  end
52
-
53
- task :test do
54
- require "mystro-common"
55
- require "awesome_print"
56
- %w{simple medium complex}.each do |n|
57
- file = "test/#{n}.rb"
58
- puts "processing: #{n} #{file}"
59
- Mystro::Template.load(file)
60
- end
61
- end
@@ -3,7 +3,7 @@ module Mystro
3
3
  module Version
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 7
6
+ TINY = 8
7
7
  TAG = nil
8
8
  STRING = [MAJOR, MINOR, TINY, TAG].compact.join(".")
9
9
  end
@@ -1,8 +1,281 @@
1
1
  module Mystro
2
2
  module DSL
3
- class Template < Base
4
- has_many :balancers, named: true
5
- has_many :computes, named: true
3
+ module Template
4
+ class << self
5
+ def load(name_or_file)
6
+ @templates ||= {}
7
+ template_name = nil
8
+ template_file = nil
9
+ if File.exists?(name_or_file)
10
+ template_name = File.basename(name_or_file).gsub(/\.rb$/, "").to_sym
11
+ template_file = name_or_file
12
+ elsif File.exists?("#{dir}/#{name_or_file}.rb")
13
+ template_name = name.to_sym
14
+ template_file = "#{dir}/#{name}.rb"
15
+ end
16
+ raise "could not load template #{template_name} (#{template_file})" unless template_file && File.file?(template_file)
17
+ #raise "template already loaded #{template_name}" if @templates[template_name]
18
+ @templates[template_name] ||= begin
19
+ t = Mystro::DSL::Template::DSL.new(template_name)
20
+ t.instance_eval(File.read(template_file), "Template(#{template_file})")
21
+ @templates[template_name] = t
22
+ end
23
+ end
24
+
25
+ def load_yaml_file(path)
26
+ file = File.expand_path(path)
27
+ raise "Configuration not found: #{path} (#{file})" unless File.exists?(file)
28
+ yaml = YAML.load_file(file)
29
+ yaml = yaml[yaml.keys.first] if yaml.keys.count == 1
30
+
31
+ yaml
32
+ end
33
+
34
+ def list
35
+ Dir["#{dir}/*"].inject({}) do |h, e|
36
+ f = e.gsub("#{dir}/", "")
37
+ f = File.basename(f, ".yml")
38
+ h[f.to_sym] = e
39
+ h
40
+ end
41
+ end
42
+
43
+ private
44
+ def dir
45
+ "#{Mystro.directory}/templates"
46
+ end
47
+ end
48
+
49
+ class DSL
50
+ attr_reader :balancers, :servers
51
+
52
+ def initialize(name)
53
+ @name = name.to_sym
54
+ @balancers = []
55
+ @servers = []
56
+ end
57
+
58
+ def template(&block)
59
+ instance_eval &block
60
+ end
61
+
62
+ def balancer(name, &block)
63
+ balancer = Balancer.new(name)
64
+ balancer.instance_eval &block
65
+ @balancers << balancer
66
+ end
67
+
68
+ def server(name, &block)
69
+ server = Server.new(name)
70
+ server.instance_eval &block
71
+ @servers << server
72
+ end
73
+ end
74
+
75
+ class Base
76
+ def attr(name, value=nil)
77
+ @attrs[name] = value unless value.nil?
78
+ @attrs[name]
79
+ end
80
+
81
+ def list_attr(name, value=nil)
82
+ @attrs[name] ||= []
83
+ unless value.nil?
84
+ if value.kind_of?(Array)
85
+ @attrs[name] += value
86
+ else
87
+ @attrs[name] << value
88
+ end
89
+ end
90
+ @attrs[name]
91
+ end
92
+ end
93
+
94
+ class Server < Base
95
+ def initialize(name)
96
+ @attrs = {
97
+ :name => name.to_sym,
98
+ :roles => [],
99
+ :groups => [],
100
+ :count => 1,
101
+ :image => nil,
102
+ :flavor => nil,
103
+ :keypair => nil,
104
+ :userdata => "default",
105
+ :dnsnames => [],
106
+ }
107
+ end
108
+
109
+ def name
110
+ @attrs[:name]
111
+ end
112
+
113
+ def roles
114
+ @attrs[:roles]
115
+ end
116
+
117
+ def groups
118
+ @attrs[:groups]
119
+ end
120
+
121
+ def dnsnames
122
+ @attrs[:dnsnames]
123
+ end
124
+
125
+ def role(r)
126
+ list_attr(:roles, r)
127
+ end
128
+
129
+ def count(c=nil)
130
+ attr(:count, c)
131
+ end
132
+
133
+ def image(i=nil)
134
+ attr(:image, i)
135
+ end
136
+
137
+ def flavor(f=nil)
138
+ attr(:flavor, f)
139
+ end
140
+
141
+ def group(g)
142
+ list_attr(:groups, g)
143
+ end
144
+
145
+ def keypair(k=nil)
146
+ attr(:keypair, k)
147
+ end
148
+
149
+ def userdata(u=nil)
150
+ attr(:userdata, u)
151
+ end
152
+
153
+ def balancer(b=nil, &block)
154
+ if block_given?
155
+ raise "balancer block must specify name" unless b
156
+ Mystro::DSL::Template::DSL.balancer(b, &block)
157
+ end
158
+ attr(:balancer, b)
159
+ end
160
+
161
+ def dns(name)
162
+ n = name.gsub(Mystro.get_config(:dns_zone), "")
163
+ list_attr(:dnsnames, n)
164
+ end
165
+ end
166
+
167
+ class Balancer
168
+ attr_reader :name, :sticky, :primary, :sticky_type, :sticky_arg
169
+
170
+ def initialize(name)
171
+ @listeners = []
172
+ @name = name.to_sym
173
+ @primary = false
174
+ @sticky = false
175
+ end
176
+
177
+ def primary(enable = nil)
178
+ enable.nil? ? @primary : @primary = enable
179
+ end
180
+
181
+ def listener(&block)
182
+ listener = Listener.new
183
+ listener.instance_eval &block
184
+ @listeners << listener
185
+ end
186
+
187
+ def health(&block)
188
+ healthcheck = HealthCheck.new
189
+ healthcheck.instance_eval &block
190
+ @healthcheck = healthcheck
191
+ end
192
+
193
+ def listeners
194
+ @listeners.map { |e| e.spec }
195
+ end
196
+
197
+ def sticky(type=nil, expires_or_cookie=nil)
198
+ if type && expires_or_cookie
199
+ @sticky = true
200
+ @sticky_type = type
201
+ @sticky_arg = expires_or_cookie
202
+ end
203
+ @sticky
204
+ end
205
+ end
206
+
207
+ class Listener
208
+ def initialize
209
+ @from_proto = nil
210
+ @from_port = nil
211
+ @to_proto = nil
212
+ @to_port = nil
213
+ @cert = nil
214
+ end
215
+
216
+ def from(proto, port)
217
+ @from_proto = proto
218
+ @from_port = port
219
+ end
220
+
221
+ def to(proto, port)
222
+ @to_proto = proto
223
+ @to_port = port
224
+ end
225
+
226
+ def cert(cert)
227
+ @cert = cert
228
+ end
229
+
230
+ def spec
231
+ {
232
+ :from => "#@from_proto:#@from_port",
233
+ :to => "#@to_proto:#@to_port",
234
+ :cert => @cert,
235
+ }
236
+ end
237
+ end
238
+
239
+ class HealthCheck
240
+ def initialize
241
+ @healthy = 10
242
+ @unhealthy = 2
243
+ @interval = 30
244
+ @target = nil
245
+ @timeout = 5
246
+ end
247
+
248
+ def healthy(v)
249
+ @healthy = v
250
+ end
251
+
252
+ def unhealthy(v)
253
+ @unhealthy = v
254
+ end
255
+
256
+ def interval(v)
257
+ @interval = v
258
+ end
259
+
260
+ def target(v)
261
+ @target = v
262
+ end
263
+
264
+ def timeout(v)
265
+ @timeout = v
266
+ end
267
+
268
+ def spec
269
+ raise "target not specified for health check" unless @target
270
+ {
271
+ healthy: @healthy,
272
+ unhealthy: @unhealthy,
273
+ interval: @interval,
274
+ target: @target,
275
+ timeout: @timeout,
276
+ }
277
+ end
278
+ end
6
279
  end
7
280
  end
8
281
  end
data/lib/mystro-common.rb CHANGED
@@ -58,12 +58,10 @@ end
58
58
  require "mystro/config"
59
59
  require "mystro/account"
60
60
  require "mystro/log"
61
-
62
- require "mystro/dsl"
61
+ require "mystro/dsl/template"
63
62
  require "mystro/plugin"
64
63
  require "mystro/connect"
65
64
  require "mystro/userdata"
66
- require "mystro/template"
67
65
 
68
66
  Mystro::Config.instance
69
67
  Mystro::Account.read
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mystro-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -131,25 +131,14 @@ files:
131
131
  - lib/mystro/connect/compute.rb
132
132
  - lib/mystro/connect/dns.rb
133
133
  - lib/mystro/connect/environment.rb
134
- - lib/mystro/dsl.rb
135
- - lib/mystro/dsl/balancer.rb
136
- - lib/mystro/dsl/balancer/health.rb
137
- - lib/mystro/dsl/balancer/listener.rb
138
- - lib/mystro/dsl/balancer/sticky.rb
139
- - lib/mystro/dsl/compute.rb
140
134
  - lib/mystro/dsl/template.rb
141
- - lib/mystro/dsl/template_file.rb
142
135
  - lib/mystro/ext/fog/balancer.rb
143
136
  - lib/mystro/job.rb
144
137
  - lib/mystro/log.rb
145
138
  - lib/mystro/model.rb
146
139
  - lib/mystro/plugin.rb
147
- - lib/mystro/template.rb
148
140
  - lib/mystro/userdata.rb
149
141
  - mystro-common.gemspec
150
- - test/complex.rb
151
- - test/medium.rb
152
- - test/simple.rb
153
142
  homepage: http://github.com/mystro
154
143
  licenses: []
155
144
  post_install_message:
@@ -174,7 +163,4 @@ rubygems_version: 1.8.25
174
163
  signing_key:
175
164
  specification_version: 3
176
165
  summary: common functionality for Mystro
177
- test_files:
178
- - test/complex.rb
179
- - test/medium.rb
180
- - test/simple.rb
166
+ test_files: []
@@ -1,11 +0,0 @@
1
- module Mystro
2
- module DSL
3
- class Health < Mystro::DSL::Base
4
- attribute :target, value: "HTTP:80/"
5
- attribute :interval, value: 30
6
- attribute :timer, value: 5
7
- attribute :healthy, value: 2
8
- attribute :unhealthy, value: 10
9
- end
10
- end
11
- end
@@ -1,9 +0,0 @@
1
- module Mystro
2
- module DSL
3
- class Listener < Mystro::DSL::Base
4
- attribute :from, value: "http:80"
5
- attribute :to, value: "http:80"
6
- attribute :cert
7
- end
8
- end
9
- end
@@ -1,8 +0,0 @@
1
- module Mystro
2
- module DSL
3
- class Sticky < Mystro::DSL::Base
4
- attribute :type
5
- attribute :arg
6
- end
7
- end
8
- end
@@ -1,10 +0,0 @@
1
- module Mystro
2
- module DSL
3
- class Balancer < Base
4
- attribute :primary, type: :boolean, value: false
5
- has_one :sticky
6
- has_one :health
7
- has_many :listeners
8
- end
9
- end
10
- end
@@ -1,17 +0,0 @@
1
- module Mystro
2
- module DSL
3
- class Compute < Base
4
- attribute :primary, type: :boolean, value: false
5
- attribute :num, value: 1
6
- attribute :image
7
- attribute :flavor
8
- attribute :keypair
9
- attribute :userdata, value: "default"
10
-
11
- attribute :roles, type: :array
12
- attribute :groups, type: :array
13
-
14
- references :balancer
15
- end
16
- end
17
- end
@@ -1,7 +0,0 @@
1
- module Mystro
2
- module DSL
3
- class TemplateFile < Base
4
- has_one :template
5
- end
6
- end
7
- end
data/lib/mystro/dsl.rb DELETED
@@ -1,145 +0,0 @@
1
- require "active_support/all"
2
-
3
- module Mystro
4
- module DSL
5
- class Base
6
- attr_reader :data
7
-
8
- def initialize
9
- @data = Marshal.load(Marshal.dump(self.class.attrs))
10
- end
11
-
12
- def method_missing(method, *args, &block)
13
- m = method.to_sym
14
- raise "unknown attribute #{method}: #{@data.inspect}" unless @data[m]
15
- o = @data[m]
16
-
17
- if o[:type] == :array
18
- #puts "setting #{m} << #{args.first}"
19
- o[:value] << args.first
20
- elsif o[:type] == :child
21
- #puts "setting child #{m} << #{o[:klass]}"
22
- k = o[:klass].constantize
23
- obj = k.new
24
- obj.instance_eval &block
25
- if o[:many]
26
- if o[:named]
27
- o[:value] ||= {}
28
- o[:value][args.first.to_sym] = obj.to_hash
29
- else
30
- o[:value] << obj.to_hash
31
- end
32
- else
33
- raise "setting more than one #{method}: previous: #{o[:value].inspect}" if o[:value]
34
- o[:value] = obj.to_hash
35
- end
36
- else
37
- #puts "setting #{m} = #{args.first}"
38
- o[:value] = args.first
39
- end
40
- end
41
-
42
- def validate!
43
- if @data[:block]
44
- block.call(self)
45
- end
46
- rescue => e
47
- raise "validation failed: #{e.message} at #{e.backtrace.first}"
48
- end
49
-
50
- def to_hash
51
- @hash ||= begin
52
- validate!
53
- out = {}
54
- @data.each do |k, v|
55
- out[k] = v[:value]
56
- end
57
- out
58
- end
59
- end
60
-
61
- #def to_mash
62
- # Hashie::Mash.new(to_hash)
63
- #end
64
-
65
- class << self
66
- @attrs = {}
67
- attr_accessor :attrs
68
-
69
- def attribute(name, options={}, &block)
70
- n = name.to_sym
71
- o = {
72
- value: nil,
73
- type: :string,
74
- klass: nil
75
- }.merge(options)
76
- #puts "ATTR: #{name} - #{o.inspect}"
77
-
78
- if o[:type] == :array
79
- n = name.to_s.singularize.to_sym
80
- o[:value] = [] unless o[:value]
81
- else
82
- if o[:type] == :child
83
- if o[:many]
84
- if o[:named]
85
- o[:value] = {} unless o[:value]
86
- else
87
- o[:value] = [] unless o[:value]
88
- end
89
- else
90
- o[:value] = nil
91
- end
92
- end
93
-
94
- end
95
-
96
- o[:block] = block if block_given?
97
-
98
- @attrs ||= {}
99
- @attrs[n] = o
100
- end
101
-
102
- def has_one(name, options={})
103
- o = {
104
- klass: find_class(name),
105
- type: :child,
106
- many: false,
107
- named: false,
108
- value: nil
109
- }.merge(options)
110
- attribute(name, o)
111
- end
112
-
113
- def has_many(names, options={})
114
- singular = names.to_s.singularize
115
- o = {
116
- klass: find_class(singular),
117
- type: :child,
118
- many: true,
119
- named: false,
120
- value: nil
121
- }.merge(options)
122
- attribute(singular, o)
123
- end
124
-
125
- def references(name, options={})
126
- attribute(name, options)
127
- end
128
-
129
- def find_class(name)
130
- c = "Mystro::DSL::#{name.capitalize}"
131
- #puts "FINDCLASS: #{name} #{c}"
132
- c
133
- rescue => e
134
- raise "could not find class: #{c}: #{e.message} at #{e.backtrace.first}"
135
- end
136
- end
137
- end
138
- end
139
- end
140
-
141
- dir = "#{File.dirname(__FILE__)}/dsl"
142
- Dir[File.join("#{dir}/**", "*.rb")].each do |file|
143
- #puts "loading: #{file}"
144
- require "#{file.gsub(/\.rb/, '')}"
145
- end
@@ -1,50 +0,0 @@
1
- module Mystro
2
- module Template
3
- class << self
4
- def load(name_or_file)
5
- @templates ||= {}
6
- template_name = nil
7
- template_file = nil
8
- if File.exists?(name_or_file)
9
- template_name = File.basename(name_or_file).gsub(/\.rb$/, "").to_sym
10
- template_file = name_or_file
11
- elsif File.exists?("#{dir}/#{name_or_file}.rb")
12
- template_name = name.to_sym
13
- template_file = "#{dir}/#{name}.rb"
14
- end
15
- raise "could not load template #{template_name} (#{template_file})" unless template_file && File.file?(template_file)
16
- #raise "template already loaded #{template_name}" if @templates[template_name]
17
-
18
- @templates[template_name] ||= begin
19
- d = File.read(template_file)
20
- t = Mystro::DSL::TemplateFile.new
21
- t.instance_eval(d, "TemplateFile(#{template_file})")
22
- t.to_hash
23
- end
24
- end
25
-
26
- #def load_yaml_file(path)
27
- # file = File.expand_path(path)
28
- # raise "Configuration not found: #{path} (#{file})" unless File.exists?(file)
29
- # yaml = YAML.load_file(file)
30
- # yaml = yaml[yaml.keys.first] if yaml.keys.count == 1
31
- #
32
- # yaml
33
- #end
34
-
35
- def list
36
- Dir["#{dir}/*"].inject({}) do |h, e|
37
- f = e.gsub("#{dir}/", "")
38
- f = File.basename(f, ".yml")
39
- h[f.to_sym] = e
40
- h
41
- end
42
- end
43
-
44
- private
45
- def dir
46
- "#{Mystro.directory}/templates"
47
- end
48
- end
49
- end
50
- end
data/test/complex.rb DELETED
@@ -1,74 +0,0 @@
1
- template do
2
- balancer :frontend do
3
- primary true
4
- listener do
5
- from "https:443"
6
- to "http:80"
7
- cert "arn:aws:iam::595408174370:server-certificate/2013inqlabs.com"
8
- end
9
- listener do
10
- from "http:80"
11
- to "http:80"
12
- end
13
- health do
14
- target "HTTP:80/"
15
- end
16
- end
17
-
18
- balancer :backend do
19
- listener do
20
- from "http:8080"
21
- to "http:8080"
22
- end
23
- health do
24
- target "HTTP:8080/INQReaderServer/rest/AjaxConfig/config"
25
- end
26
- end
27
-
28
- balancer :image do
29
- listener do
30
- from "http:80"
31
- to "http:80"
32
- end
33
- health do
34
- target "HTTP:80/getImages?url=http%3A%2F%2Fwww.bbc.co.uk%2Fnews%2Fworld-us-canada-21638727"
35
- interval 120
36
- timer 60
37
- unhealthy 3
38
- healthy 10
39
- end
40
- end
41
-
42
- compute :backend do
43
- num 3
44
- role :backend
45
- flavor "m1.xlarge"
46
- userdata "chef"
47
- image "ami-3236ad5b" # 0.6.2.1
48
- group "backend"
49
- keypair "live"
50
- balancer :backend
51
- end
52
-
53
- compute :frontend do
54
- num 3
55
- role :frontend
56
- flavor "m1.medium"
57
- userdata "chef"
58
- image "ami-0145d268"
59
- group "frontend"
60
- keypair "live"
61
- balancer :frontend
62
- end
63
-
64
- compute :image do
65
- num 5
66
- role :image
67
- balancer :image
68
- userdata "chef"
69
- flavor "m1.large"
70
- image "ami-ac8f13c5"
71
- group "frontend"
72
- keypair "live"
73
- end
74
- end
data/test/medium.rb DELETED
@@ -1,78 +0,0 @@
1
- template do
2
- balancer :frontend do
3
- primary true
4
- listener do
5
- from :https, 443
6
- to :http, 80
7
- cert "arn:aws:iam::595408174370:server-certificate/2013inqlabs.com"
8
- end
9
- listener do
10
- from :http, 80
11
- to :http, 80
12
- end
13
- health do
14
- target "HTTP:80/"
15
- end
16
- end
17
-
18
- balancer :backend do
19
- listener do
20
- from :http, 8080
21
- to :http, 8080
22
- end
23
- health do
24
- target "HTTP:8080/INQReaderServer/rest/AjaxConfig/config"
25
- end
26
- end
27
-
28
- balancer :image do
29
- listener do
30
- from :http, 80
31
- to :http, 80
32
- end
33
- listener do
34
- from :http, 8080
35
- to :http, 8080
36
- end
37
- health do
38
- target "HTTP:80/getImages?url=http%3A%2F%2Fwww.bbc.co.uk%2Fnews%2Fworld-us-canada-21638727"
39
- interval 120
40
- timer 60
41
- unhealthy 3
42
- healthy 10
43
- end
44
- end
45
-
46
- compute :image do
47
- num 2
48
- role :app
49
- balancer :image
50
- userdata "chef"
51
- flavor "m1.large"
52
- image "ami-ac8f13c5"
53
- group "frontend"
54
- keypair "stage"
55
- end
56
-
57
- compute :backend do
58
- num 2
59
- role :backend
60
- balancer :backend
61
- flavor "m1.xlarge"
62
- userdata "chef"
63
- image "ami-3236ad5b" # 0.6.2.1
64
- group "backend"
65
- keypair "stage"
66
- end
67
-
68
- compute :frontend do
69
- num 2
70
- role :frontend
71
- balancer :frontend
72
- flavor "m1.large"
73
- userdata "chef"
74
- image "ami-0145d268"
75
- group "frontend"
76
- keypair "stage"
77
- end
78
- end
data/test/simple.rb DELETED
@@ -1,31 +0,0 @@
1
- template do
2
- compute :backend do
3
- num 1
4
- role :backend
5
- flavor "m1.xlarge"
6
- userdata "chef"
7
- image "ami-3236ad5b" # 0.6.2.1
8
- group "backend"
9
- keypair "dev"
10
- end
11
-
12
- compute :frontend do
13
- num 1
14
- role :frontend
15
- flavor "m1.large"
16
- userdata "chef"
17
- image "ami-0145d268"
18
- group "frontend"
19
- keypair "dev"
20
- end
21
-
22
- compute :image do
23
- num 1
24
- role :image
25
- userdata "chef"
26
- flavor "m1.large"
27
- image "ami-ac8f13c5"
28
- group "frontend"
29
- keypair "dev"
30
- end
31
- end