bbc-cosmos-tools 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f480d30ff3c7c874e62527ae5cc5a87e0dc33aba
4
- data.tar.gz: 767d42b73f3de1212cecfff750efcfd43f1f2db7
3
+ metadata.gz: d312899466e33b1d4a65a7335be17846bd070f73
4
+ data.tar.gz: f83b250a15485d6d15ae407c4f3e2d11c89d484e
5
5
  SHA512:
6
- metadata.gz: 462b70abdd71a6bdc81e3dd7658a0000cfdb9524ba131eae420279aeaf3e4dc1a6b68fd4291c06059c2f0c370d01116810f2a9e1f10023e0a37603b26444c64e
7
- data.tar.gz: ea96eaaee94d9c15014caabd1a6ea4b501c1eaec6d821f4fefec8126b2b10bc0f1ae62cd4157e19bfd09d9749ba4ae0983797e83482e7329adfa64d64120de1b
6
+ metadata.gz: abe8c0da8cdf40c91c63df435b7e858bb304830f73a7ceac96b335b15cdb704328462a959fc35b5c7c55af53465381446211daa91be15cb27575b0fc2631ba9a
7
+ data.tar.gz: d1b54bb031dc796f72a6692c491da4a3906a0b926e278f335ff3a2aefeb8714771decd417645f9b627f4f3b6cc8ca6ca2e388c356cd010e1c36dffdef993e27b
data/README.md CHANGED
@@ -159,7 +159,20 @@ bbc-cosmos-tools cf create
159
159
  bbc-cosmos-tools cf update
160
160
  ```
161
161
 
162
- On each command you can use `--help` at the end to see the list of parameters and options that need to be passed into the command.
162
+ ### Help?
163
+
164
+ You can use the built-in `help` command to see a list of parameters and options that need to be passed into the command you wish to execute. For example, if we were unsure of the options for the `stack` command then we would execute `bbc-cosmos-tools help stack` and this would display the following output:
165
+
166
+ ```sh
167
+ Commands:
168
+ bbc-cosmos-tools stack help [COMMAND] # Describe subcommands or one specific subco...
169
+ bbc-cosmos-tools stack create [COMPONENT, MAIN_STACK = false] # Generates and create the cloudformation te...
170
+ bbc-cosmos-tools stack delete [COMPONENT] # Deletes a stack
171
+ bbc-cosmos-tools stack events [COMPONENT = nil] # Shows the stack events for a component
172
+ bbc-cosmos-tools stack generate [COMPONENT] # Generates a cloudformation template based ...
173
+ bbc-cosmos-tools stack list [COMPONENT = nil] # Lists stacks for a component
174
+ bbc-cosmos-tools stack update [COMPONENT = nil] # Generates and updates the cloudformation t...
175
+ ```
163
176
 
164
177
  ## Contributing
165
178
 
data/bin/bbc-cosmos-tools CHANGED
@@ -4,11 +4,11 @@ require 'pathname'
4
4
 
5
5
  root = Pathname.new(__FILE__).dirname.parent
6
6
  lib_path = (root + 'lib').realdirpath
7
- config_path = root + 'configs'
8
7
 
9
8
  $LOAD_PATH.unshift(lib_path)
10
9
 
11
10
  require 'bbc/cosmos/tools/app'
11
+
12
12
  include BBC::Cosmos::Tools
13
13
 
14
14
  App.start
@@ -16,11 +16,10 @@ module BBC
16
16
  end
17
17
 
18
18
  def self.create(component, config, options, to_json = false, &block)
19
-
20
19
  template = CfnDsl::CloudFormationTemplate.new
21
20
  template.declare(&block)
22
21
 
23
- if template.checkRefs() || CfnDsl::Errors.errors?
22
+ if template.checkRefs || CfnDsl::Errors.errors?
24
23
  self.raise_error template
25
24
  else
26
25
  hash = JSON.parse(template.to_json)
@@ -33,12 +32,11 @@ module BBC
33
32
 
34
33
  to_json ? JSON.pretty_generate(hash) : hash
35
34
  end
36
-
37
35
  end
38
36
 
39
37
  def self.parameters(data)
40
38
  if data['Parameters']
41
- parameters = data['Parameters'].reduce({}) do |object, (key, value)|
39
+ data['Parameters'].reduce({}) do |object, (key, value)|
42
40
  object.tap { |o| o[key] = value['Default'] }
43
41
  end
44
42
  else
@@ -14,7 +14,7 @@ module BBC
14
14
 
15
15
  package_name "BBC Cosmos config tool"
16
16
 
17
- class_option :key_path, :type => :string, :default => ENV['DEV_CERT_PATH'], :desc => "The path to your dev cert, by default the value of the env variable $DEV_CERT_PATH is used"
17
+ class_option :key_path, :type => :string, :default => ENV['BBC_COSMOS_TOOLS_CERT'], :desc => "The path to your dev cert, by default the value of the env variable $BBC_COSMOS_TOOLS_CERT is used"
18
18
  class_option :env, :type => :string, :default => 'int', :desc => "The environment to target"
19
19
  class_option :base, :type => :string, :desc => "The base path for the configs repo"
20
20
  class_option :project, :type => :string, :default => ENV['BBC_COSMOS_TOOLS_PROJECT'], :desc => "The name of the project, by default the value of the env variable $BBC_COSMOS_TOOLS_PROJECT is used"
@@ -1,6 +1,8 @@
1
+ require "json"
1
2
  require "bbc/cosmos/tools/config"
2
3
  require 'bbc/cosmos/tools/commands/base'
3
4
  require 'bbc/cosmos/tools/cloudformation/generator'
5
+ require "bbc/cosmos/tools/types/factory"
4
6
 
5
7
  module BBC
6
8
  module Cosmos
@@ -10,9 +12,17 @@ module BBC
10
12
  attr_accessor :api
11
13
 
12
14
  class_option :stack, :type => :string, :default => 'main', :desc => "The name of the stack to use"
15
+ class_option :type, :type => :string, :default => 'cfndsl', :desc => "The format of your your CloudFormation (default is cfndsl, but can also be json or yaml)", :enum => ["cfndsl", "json", "yaml"]
13
16
 
14
17
  def initialize(args = [], local_options = {}, thor_config = {})
15
18
  super(args, local_options, thor_config)
19
+
20
+ @l = -> data, component, environment do
21
+ if data['Parameters'] && data['Parameters']['ImageId']
22
+ data['Parameters']['ImageId']['Default'] = latest_ami_for(component, environment)
23
+ end
24
+ end
25
+
16
26
  @api = BBC::Cosmos::Tools::API.new(config.app['api'], options[:key_path])
17
27
  end
18
28
 
@@ -57,7 +67,13 @@ module BBC
57
67
  def generate(component)
58
68
 
59
69
  say "#{banner(component)}\n"
60
- say cloudformation(component, options), :white
70
+
71
+ type = Tools::Types::Factory.create(component, options, config)
72
+ data = type.generate_data.tap do |d|
73
+ @l.(d, component, options[:env])
74
+ end
75
+
76
+ say data, :white
61
77
 
62
78
  end
63
79
 
@@ -72,7 +88,10 @@ module BBC
72
88
  components(options, component).each do |id|
73
89
  say get_key_value("\nComponent", id)
74
90
 
75
- data = cloudformation(id, options, false)
91
+ type = Tools::Types::Factory.create(id, options, config)
92
+ data = type.generate_data.tap do |d|
93
+ @l.(d, id, options[:env])
94
+ end
76
95
 
77
96
  post_data = {
78
97
  'name_suffix' => options[:stack],
@@ -106,11 +125,17 @@ module BBC
106
125
  say get_key_value("\nComponent", id)
107
126
 
108
127
  stack_name = "#{options[:env]}-#{id}-#{options[:stack]}"
109
- data = cloudformation(id, options, false)
128
+
129
+ type = Tools::Types::Factory.create(id, options, config)
130
+ data = type.generate_data.tap do |d|
131
+ @l.(d, id, options[:env])
132
+ end
133
+
134
+ params = Tools::Cloudformation::Generator.parameters(data)
110
135
 
111
136
  post_data = {
112
137
  'template' => data,
113
- 'parameters' => Tools::Cloudformation::Generator.parameters(data)
138
+ 'parameters' => params
114
139
  }
115
140
 
116
141
  response = api.post sprintf(config.app['stack_update_endpoint'], options[:env], id, stack_name), JSON.pretty_generate(post_data)
@@ -187,20 +212,6 @@ module BBC
187
212
  api_error response unless response.success?
188
213
  JSON.parse(response.body).first['image']['ami_id']
189
214
  end
190
-
191
- def cloudformation(component, options, to_json = true)
192
- config_data = config
193
-
194
- Tools::Cloudformation::Generator.create(component, config, options, to_json) do
195
- component_identifier = options[:group].nil? ? component : options[:group]
196
- instance_eval config_data.cf_templates(component_identifier, options[:stack])
197
- end.tap do |data|
198
- if data['Parameters'] && data['Parameters']['ImageId']
199
- data['Parameters']['ImageId']['Default'] = latest_ami_for(component, options[:env])
200
- end
201
-
202
- end
203
- end
204
215
  end
205
216
  end
206
217
  end
@@ -0,0 +1,47 @@
1
+ require "bbc/cosmos/tools/cloudformation/generator"
2
+
3
+ module BBC
4
+ module Cosmos
5
+ module Tools
6
+ module Types
7
+ class BaseType
8
+ def initialize(id, options, config)
9
+ @id = id
10
+ @options = options
11
+ @config = config
12
+ end
13
+
14
+ def generate_data
15
+ if valid_component_and_stack
16
+ get_parameters.each do |key, value|
17
+ data['Parameters'][key]['Default'] = value if data['Parameters'][key]
18
+ end
19
+ data
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ def data
26
+ raise NotImplementedError
27
+ end
28
+
29
+ def construct_file_path type
30
+ File.join(@config.base_path, 'stacks', @id, "#{@options[:stack]}.#{type}")
31
+ end
32
+
33
+ private
34
+
35
+ def valid_component_and_stack
36
+ Tools::Cloudformation::Generator.component_exists?(@id, @config) &&
37
+ Tools::Cloudformation::Generator.stack_exists?(@id, @config, @options[:stack])
38
+ end
39
+
40
+ def get_parameters
41
+ @config.resources['cloudformation']['components'][@id][@options[:stack]]['variants'][@options[:variant]]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,32 @@
1
+ require "bbc/cosmos/tools/cloudformation/generator"
2
+
3
+ module BBC
4
+ module Cosmos
5
+ module Tools
6
+ module Types
7
+ class Cfndsl
8
+ def initialize(id, options, config)
9
+ @id = id
10
+ @options = options
11
+ @config = config
12
+ end
13
+
14
+ def generate_data
15
+ to_json = false
16
+
17
+ # Because the block is evaluated in the context of another class
18
+ # i.e. instance_eval &block
19
+ # we need to store off the values first and *then* reference them inside the block
20
+ component_identifier = @options[:group].nil? ? @id : @options[:group]
21
+ config = @config
22
+ stack = @options[:stack]
23
+
24
+ Tools::Cloudformation::Generator.create(@id, @config, @options, to_json) {
25
+ instance_eval config.cf_templates(component_identifier, stack)
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ require "bbc/cosmos/tools/types/yaml"
2
+ require "bbc/cosmos/tools/types/json"
3
+ require "bbc/cosmos/tools/types/cfndsl"
4
+
5
+ module BBC
6
+ module Cosmos
7
+ module Tools
8
+ module Types
9
+ class Factory
10
+ def self.create(id, options, config)
11
+ BBC::Cosmos::Tools::Types.const_get(options[:type].capitalize).new(id, options, config)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ require "json"
2
+ require "bbc/cosmos/tools/types/base_type"
3
+
4
+ module BBC
5
+ module Cosmos
6
+ module Tools
7
+ module Types
8
+ class Json < BaseType
9
+ protected
10
+
11
+ def data
12
+ JSON.parse(IO.read(construct_file_path "json"))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ require "bbc/cosmos/tools/types/base_type"
2
+
3
+ module BBC
4
+ module Cosmos
5
+ module Tools
6
+ module Types
7
+ class Yaml < BaseType
8
+ protected
9
+
10
+ def data
11
+ YAML.load_file(construct_file_path "yaml")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,7 +1,7 @@
1
1
  module BBC
2
2
  module Cosmos
3
3
  module Tools
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bbc-cosmos-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Jack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-21 00:00:00.000000000 Z
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -120,6 +120,11 @@ files:
120
120
  - lib/bbc/cosmos/tools/commands/stack.rb
121
121
  - lib/bbc/cosmos/tools/config.rb
122
122
  - lib/bbc/cosmos/tools/cosmos_configuration.rb
123
+ - lib/bbc/cosmos/tools/types/base_type.rb
124
+ - lib/bbc/cosmos/tools/types/cfndsl.rb
125
+ - lib/bbc/cosmos/tools/types/factory.rb
126
+ - lib/bbc/cosmos/tools/types/json.rb
127
+ - lib/bbc/cosmos/tools/types/yaml.rb
123
128
  - lib/bbc/cosmos/tools/version.rb
124
129
  homepage: ''
125
130
  licenses: