cloudformation-ruby-dsl 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ # Copyright 2013-2014 Bazaarvoice, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # This list of prices was sourced from the on-demand prices current as of 12/3/2012.
16
+ # We expect the actual price we pay per instance to be roughly 1/10 the prices below.
17
+ SPOT_PRICES_BY_INSTANCE_TYPE = {
18
+ "m1.small" => 0.065,
19
+ "m1.medium" => 0.130,
20
+ "m1.large" => 0.260,
21
+ "m1.xlarge" => 0.520,
22
+ "m3.xlarge" => 0.580,
23
+ "m3.2xlarge" => 1.160,
24
+ "t1.micro" => 0.020,
25
+ "m2.xlarge" => 0.450,
26
+ "m2.2xlarge" => 0.900,
27
+ "m2.4xlarge" => 1.800,
28
+ "c1.medium" => 0.165,
29
+ "c1.xlarge" => 0.660,
30
+ "cc1.4xlarge" => 1.300,
31
+ "cc2.8xlarge" => 2.400,
32
+ "cg1.4xlarge" => 2.100,
33
+ "hi1.4xlarge" => 3.100,
34
+ "hs1.8xlarge" => 4.600,
35
+ "cr1.8xlarge" => 4.000,
36
+ }
37
+
38
+ def spot_price(spot_price_string, instance_type)
39
+ case spot_price_string
40
+ when 'false', '' then no_value()
41
+ when 'true' then spot_price_for_instance_type(instance_type)
42
+ else spot_price_string
43
+ end
44
+ end
45
+
46
+ def spot_price_for_instance_type(instance_type)
47
+ # Add 10% to ensure that we have a small buffer against current spot prices increasing
48
+ # to the on-demand prices, which theoretically could happen often.
49
+ SPOT_PRICES_BY_INSTANCE_TYPE[instance_type] * 1.10
50
+ end
@@ -0,0 +1,93 @@
1
+ # Copyright 2013-2014 Bazaarvoice, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'detabulator'
16
+
17
+ class Table
18
+ def self.load(filename)
19
+ self.new File.read filename
20
+ end
21
+
22
+ def initialize(table_as_text)
23
+ raw_header, *raw_data = Detabulator.new.detabulate table_as_text
24
+ header = raw_header.map(&:to_sym)
25
+ @records = raw_data.map { |row| Hash[header.zip(row)] }
26
+ end
27
+
28
+ # Selects all rows in the table which match the name/value pairs of the predicate object and returns
29
+ # the single distinct value from those rows for the specified key.
30
+ def get(key, predicate)
31
+ distinct_values(filter(predicate), key, false)
32
+ end
33
+
34
+ # Selects all rows in the table which match the name/value pairs of the predicate object and returns
35
+ # all distinct values from those rows for the specified key.
36
+ def get_list(key, predicate)
37
+ distinct_values(filter(predicate), key, true)
38
+ end
39
+
40
+ # Selects all rows in the table which match the name/value pairs of the predicate object and returns a
41
+ # set of nested maps, where the key for the map at level n is the key at index n in the specified keys,
42
+ # except for the last key in the specified keys which is used to determine the value of the leaf-level map.
43
+ # In the simple case where keys is a list of 2 elements, this returns a map from key[0] to key[1].
44
+ def get_map(predicate, *keys)
45
+ build_nested_map(filter(predicate), keys, false)
46
+ end
47
+
48
+ # Selects all rows in the table which match the name/value pairs of the predicate object and returns a
49
+ # set of nested maps, where the key for the map at level n is the key at index n in the specified keys,
50
+ # except for the last key in the specified keys which is used to determine the list of values in the
51
+ # leaf-level map. In the simple case where keys is a list of 2 elements, this returns a map from key[0]
52
+ # to a list of values for key[1].
53
+ def get_multimap(predicate, *keys)
54
+ build_nested_map(filter(predicate), keys, true)
55
+ end
56
+
57
+ private
58
+
59
+ # Return the subset of records that match the predicate for all keys in the predicate.
60
+ def filter(predicate)
61
+ @records.select { |record| predicate.all? { |key, value| record[key] == value } }
62
+ end
63
+
64
+ def build_nested_map(records, path, multi)
65
+ key, *rest = path
66
+ if rest.empty?
67
+ # Build the leaf level of the data structure
68
+ distinct_values(records, key, multi)
69
+ else
70
+ # Return a hash keyed by the distinct values of the first key and values are the result of a
71
+ # recursive invocation of arrange() with the rest of the keys
72
+ result = {}
73
+ records.group_by do |record|
74
+ record[key]
75
+ end.map do |value, group|
76
+ result[value] = build_nested_map(group, rest, multi)
77
+ end
78
+ result
79
+ end
80
+ end
81
+
82
+ def distinct_values(records, key, multi)
83
+ values = records.map { |record| record[key] }.uniq
84
+ if multi
85
+ # In a multimap the leaf level is a list of string values
86
+ values
87
+ else
88
+ # In a non-multimap the leaf level is a single string value
89
+ raise "Multiple distinct values for the same key '#{key}': #{records.inspect}" if values.length > 1
90
+ values[0]
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright 2013-2014 Bazaarvoice, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Cfn
16
+ module Ruby
17
+ module Dsl
18
+ VERSION = "0.4.0"
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudformation-ruby-dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shawn Smith
9
+ - Dave Barcelo
10
+ - Morgan Fletcher
11
+ - Csongor Gyuricza
12
+ - Igor Polishchuk
13
+ - Nathaniel Eliot
14
+ - Jona Fenocchi
15
+ - Tony Cui
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+ date: 2014-04-21 00:00:00.000000000 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: detabulator
23
+ requirement: !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ type: :runtime
30
+ prerelease: false
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ description: Ruby DSL library that provides a wrapper around the cfn-cmd.
38
+ email:
39
+ - Shawn.Smith@bazaarvoice.com
40
+ - Dave.Barcelo@bazaarvoice.com
41
+ - Morgan.Fletcher@bazaarvoice.com
42
+ - Csongor.Gyuricza@bazaarvoice.com
43
+ - Igor.Polishchuk@bazaarvoice.com
44
+ - Nathaniel.Eliot@bazaarvoice.com
45
+ - Jona.Fenocchi@bazaarvoice.com
46
+ - Tony.Cui@bazaarvoice.com
47
+ executables:
48
+ - cfntemplate-to-ruby
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - .gitignore
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/cfntemplate-to-ruby
58
+ - cloudformation-ruby-dsl.gemspec
59
+ - examples/cloudformation-ruby-script.rb
60
+ - examples/maps/json_map.json
61
+ - examples/maps/more_maps/map1.json
62
+ - examples/maps/more_maps/map2.json
63
+ - examples/maps/more_maps/map3.json
64
+ - examples/maps/ruby_map.rb
65
+ - examples/maps/text_table.txt
66
+ - examples/maps/yaml_map.yaml
67
+ - examples/userdata.sh
68
+ - initial_contributions.md
69
+ - lib/cloudformation-ruby-dsl.rb
70
+ - lib/cloudformation-ruby-dsl/cfntemplate.rb
71
+ - lib/cloudformation-ruby-dsl/spotprice.rb
72
+ - lib/cloudformation-ruby-dsl/table.rb
73
+ - lib/cloudformation-ruby-dsl/version.rb
74
+ homepage: http://github.com/bazaarvoice/cloudformation-ruby-dsl
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ - bin
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.23
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Ruby DSL library that provides a wrapper around the cfn-cmd. Written by
99
+ [Bazaarvoice](http://www.bazaarvoice.com).
100
+ test_files: []