formatron 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +12 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +3 -0
  6. data/.simplecov +7 -0
  7. data/.travis.yml +17 -0
  8. data/CODE_OF_CONDUCT.md +13 -0
  9. data/Gemfile +6 -0
  10. data/Guardfile +16 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +93 -0
  13. data/Rakefile +16 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +7 -0
  16. data/exe/formatron +20 -0
  17. data/formatron.gemspec +52 -0
  18. data/lib/formatron.rb +357 -0
  19. data/lib/formatron/aws.rb +197 -0
  20. data/lib/formatron/chef.rb +156 -0
  21. data/lib/formatron/chef/berkshelf.rb +55 -0
  22. data/lib/formatron/chef/keys.rb +48 -0
  23. data/lib/formatron/chef/knife.rb +169 -0
  24. data/lib/formatron/chef_clients.rb +73 -0
  25. data/lib/formatron/cli.rb +33 -0
  26. data/lib/formatron/cli/completion.rb +26 -0
  27. data/lib/formatron/cli/deploy.rb +57 -0
  28. data/lib/formatron/cli/destroy.rb +57 -0
  29. data/lib/formatron/cli/generators/bootstrap.rb +250 -0
  30. data/lib/formatron/cli/generators/credentials.rb +100 -0
  31. data/lib/formatron/cli/generators/instance.rb +118 -0
  32. data/lib/formatron/cli/provision.rb +59 -0
  33. data/lib/formatron/cloud_formation.rb +54 -0
  34. data/lib/formatron/cloud_formation/resources/cloud_formation.rb +27 -0
  35. data/lib/formatron/cloud_formation/resources/ec2.rb +336 -0
  36. data/lib/formatron/cloud_formation/resources/iam.rb +94 -0
  37. data/lib/formatron/cloud_formation/resources/route53.rb +54 -0
  38. data/lib/formatron/cloud_formation/scripts.rb +128 -0
  39. data/lib/formatron/cloud_formation/template.rb +114 -0
  40. data/lib/formatron/cloud_formation/template/parameters.rb +20 -0
  41. data/lib/formatron/cloud_formation/template/vpc.rb +181 -0
  42. data/lib/formatron/cloud_formation/template/vpc/subnet.rb +187 -0
  43. data/lib/formatron/cloud_formation/template/vpc/subnet/acl.rb +147 -0
  44. data/lib/formatron/cloud_formation/template/vpc/subnet/bastion.rb +66 -0
  45. data/lib/formatron/cloud_formation/template/vpc/subnet/chef_server.rb +205 -0
  46. data/lib/formatron/cloud_formation/template/vpc/subnet/instance.rb +162 -0
  47. data/lib/formatron/cloud_formation/template/vpc/subnet/instance/policy.rb +74 -0
  48. data/lib/formatron/cloud_formation/template/vpc/subnet/instance/security_group.rb +117 -0
  49. data/lib/formatron/cloud_formation/template/vpc/subnet/instance/setup.rb +68 -0
  50. data/lib/formatron/cloud_formation/template/vpc/subnet/nat.rb +94 -0
  51. data/lib/formatron/completion.rb +26 -0
  52. data/lib/formatron/completion/completion.sh.erb +35 -0
  53. data/lib/formatron/config.rb +31 -0
  54. data/lib/formatron/config/reader.rb +29 -0
  55. data/lib/formatron/dsl.rb +15 -0
  56. data/lib/formatron/dsl/formatron.rb +25 -0
  57. data/lib/formatron/dsl/formatron/global.rb +19 -0
  58. data/lib/formatron/dsl/formatron/global/ec2.rb +17 -0
  59. data/lib/formatron/dsl/formatron/vpc.rb +17 -0
  60. data/lib/formatron/dsl/formatron/vpc/subnet.rb +27 -0
  61. data/lib/formatron/dsl/formatron/vpc/subnet/acl.rb +18 -0
  62. data/lib/formatron/dsl/formatron/vpc/subnet/chef_server.rb +32 -0
  63. data/lib/formatron/dsl/formatron/vpc/subnet/chef_server/organization.rb +22 -0
  64. data/lib/formatron/dsl/formatron/vpc/subnet/instance.rb +29 -0
  65. data/lib/formatron/dsl/formatron/vpc/subnet/instance/chef.rb +22 -0
  66. data/lib/formatron/dsl/formatron/vpc/subnet/instance/policy.rb +21 -0
  67. data/lib/formatron/dsl/formatron/vpc/subnet/instance/policy/statement.rb +23 -0
  68. data/lib/formatron/dsl/formatron/vpc/subnet/instance/security_group.rb +21 -0
  69. data/lib/formatron/dsl/formatron/vpc/subnet/instance/setup.rb +22 -0
  70. data/lib/formatron/dsl/formatron/vpc/subnet/instance/setup/variable.rb +23 -0
  71. data/lib/formatron/external.rb +61 -0
  72. data/lib/formatron/external/dsl.rb +171 -0
  73. data/lib/formatron/external/outputs.rb +25 -0
  74. data/lib/formatron/generators/bootstrap.rb +90 -0
  75. data/lib/formatron/generators/bootstrap/config.rb +62 -0
  76. data/lib/formatron/generators/bootstrap/ec2.rb +17 -0
  77. data/lib/formatron/generators/bootstrap/formatronfile.rb +52 -0
  78. data/lib/formatron/generators/bootstrap/formatronfile/Formatronfile.erb +79 -0
  79. data/lib/formatron/generators/bootstrap/ssl.rb +35 -0
  80. data/lib/formatron/generators/credentials.rb +17 -0
  81. data/lib/formatron/generators/instance.rb +64 -0
  82. data/lib/formatron/generators/instance/config.rb +47 -0
  83. data/lib/formatron/generators/instance/formatronfile.rb +47 -0
  84. data/lib/formatron/generators/instance/formatronfile/Formatronfile.erb +16 -0
  85. data/lib/formatron/generators/util.rb +14 -0
  86. data/lib/formatron/generators/util/cookbook.rb +65 -0
  87. data/lib/formatron/generators/util/gitignore.rb +16 -0
  88. data/lib/formatron/generators/util/readme.rb +18 -0
  89. data/lib/formatron/logger.rb +8 -0
  90. data/lib/formatron/s3/chef_server_cert.rb +85 -0
  91. data/lib/formatron/s3/chef_server_keys.rb +103 -0
  92. data/lib/formatron/s3/cloud_formation_template.rb +61 -0
  93. data/lib/formatron/s3/configuration.rb +58 -0
  94. data/lib/formatron/s3/path.rb +30 -0
  95. data/lib/formatron/util/dsl.rb +107 -0
  96. data/lib/formatron/util/shell.rb +20 -0
  97. data/lib/formatron/util/vpc.rb +15 -0
  98. data/lib/formatron/version.rb +4 -0
  99. data/support/cloudformation_describe_stacks_response.rb +36 -0
  100. data/support/dsl_test.rb +123 -0
  101. data/support/route53_get_hosted_zone_response.rb +21 -0
  102. data/support/s3_get_object_response.rb +21 -0
  103. data/support/template_test.rb +41 -0
  104. metadata +414 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2fbc3682dfed44912c502471c91985385772edc
4
+ data.tar.gz: d47b4b18846677185d75bbe1caccb5d49d5e8711
5
+ SHA512:
6
+ metadata.gz: a352259c2fa5fa21a28fff086e279bfacd8e1c7ab7a9dd435e15c673ea1907a56e2a669f46f9a22ae60f255441a46d0196d48206d3864b79917e5d123580d59f
7
+ data.tar.gz: c31fc3dd6d65b832b820db576e92e3ee28b9f98f430a23dc5993a3277e8a7608d3a8d1e9b6a4d340af0e9eeb1937036e8f90e65778f06794cddf71538be34976
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /features_report.html
11
+ /*.log
12
+ /*.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
data/.simplecov ADDED
@@ -0,0 +1,7 @@
1
+ require 'coveralls'
2
+ SimpleCov.start do
3
+ add_filter '/spec/'
4
+ add_filter '/features/'
5
+ add_filter '/support/'
6
+ minimum_coverage 100
7
+ end
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.1.2
6
+
7
+ before_install:
8
+ - gem install bundler
9
+
10
+ script: 'bundle exec rake'
11
+
12
+ notifications:
13
+ email:
14
+ recipients:
15
+ - pghalliday@gmail.com
16
+ on_failure: change
17
+ on_success: never
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in formatron.gemspec
4
+ gemspec
5
+
6
+ gem 'fakefs', require: 'fakefs/safe'
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ guard 'livereload' do
2
+ watch(%r{^coverage/.+$})
3
+ end
4
+
5
+ guard :rake, task: 'default' do
6
+ watch(/.+\.rb$/)
7
+ watch(/.+\.gemspec$/)
8
+ watch(/^Rakefile$/)
9
+ watch(/^Gemfile$/)
10
+ watch(/^Guardfile$/)
11
+ watch(/^.simplecov$/)
12
+ watch(%r{(?:.+/)?\.rubocop\.yml$})
13
+ watch(%r{^exe/formatron$})
14
+ watch(%r{^lib/.+$})
15
+ watch(%r{^spec/.+$})
16
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Peter Halliday
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Formatron
2
+
3
+ [![Build Status](https://travis-ci.org/pghalliday/formatron.svg?branch=master)](https://travis-ci.org/pghalliday/formatron?branch=master)
4
+ [![Coverage Status](https://coveralls.io/repos/pghalliday/formatron/badge.svg?branch=master&service=github)](https://coveralls.io/github/pghalliday/formatron?branch=master)
5
+ [![Dependency Status](https://gemnasium.com/pghalliday/formatron.svg)](https://gemnasium.com/pghalliday/formatron)
6
+
7
+ Simple AWS CloudFormation configuration with Chef Server
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'formatron'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install formatron
24
+
25
+ ## Usage
26
+
27
+ For the full list of commands and options
28
+
29
+ ```
30
+ formatron help [COMMAND]
31
+ ```
32
+
33
+ ### Project generation
34
+
35
+ To initialize a bootstrap configuration including a VPC and Chef Server
36
+
37
+ ```
38
+ formatron generate bootstrap
39
+ ```
40
+
41
+ To initialize an instance configuration with a dependency on a named bootstrap configuration
42
+
43
+ ```
44
+ formatron generate instance
45
+ ```
46
+
47
+ To initialize an AWS credentials file
48
+
49
+ ```
50
+ formatron generate credentials
51
+ ```
52
+
53
+ ### Deploy and provision
54
+
55
+ To deploy a configuration with the given target specifier
56
+
57
+ ```
58
+ formatron deploy TARGET
59
+ ```
60
+
61
+ To provision a configuration with the given target specifier
62
+
63
+ ```
64
+ formatron provision TARGET
65
+ ```
66
+
67
+ To destroy a configuration and clean up its cookbooks, etc
68
+
69
+ ```
70
+ formatron destroy TARGET
71
+ ```
72
+
73
+ ### Bash command completion
74
+
75
+ Add the following to your `.bashrc`
76
+
77
+ ```
78
+ eval "$(formatron completion-script)"
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it ( https://github.com/[my-github-username]/formatron/fork )
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+ require 'rubocop/rake_task'
4
+ require 'coveralls/rake/task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ RuboCop::RakeTask.new(:rubocop)
9
+
10
+ Coveralls::RakeTask.new
11
+
12
+ task :clean do
13
+ rm_rf 'coverage'
14
+ end
15
+
16
+ task default: [:clean, :rubocop, :spec, 'coveralls:push']
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'formatron'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/formatron ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'formatron/cli'
4
+ require 'formatron/cli/generators/credentials'
5
+ require 'formatron/cli/generators/bootstrap'
6
+ require 'formatron/cli/generators/instance'
7
+ require 'formatron/cli/deploy'
8
+ require 'formatron/cli/destroy'
9
+ require 'formatron/cli/provision'
10
+ require 'formatron/cli/completion'
11
+
12
+ Formatron::CLI.include Formatron::CLI::Generators::Credentials
13
+ Formatron::CLI.include Formatron::CLI::Generators::Bootstrap
14
+ Formatron::CLI.include Formatron::CLI::Generators::Instance
15
+ Formatron::CLI.include Formatron::CLI::Deploy
16
+ Formatron::CLI.include Formatron::CLI::Destroy
17
+ Formatron::CLI.include Formatron::CLI::Provision
18
+ Formatron::CLI.include Formatron::CLI::Completion
19
+ cli = Formatron::CLI.new
20
+ cli.run
data/formatron.gemspec ADDED
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'formatron/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'formatron'
8
+ spec.version = Formatron::VERSION
9
+ spec.authors = ['Peter Halliday']
10
+ spec.email = ['pghalliday@gmail.com']
11
+
12
+ if spec.respond_to?(:metadata)
13
+ end
14
+
15
+ spec.summary = 'AWS/Chef Deployment Tool'
16
+
17
+ spec.description = <<-EOH
18
+ AWS/Chef deployment tool based around Chef Server and AWS CloudFormation
19
+ EOH
20
+
21
+ spec.homepage = 'https://github.com/pghalliday/formatron'
22
+ spec.license = 'MIT'
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec)/})
26
+ end
27
+
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.8'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.3'
35
+ spec.add_development_dependency 'rspec-nc', '~> 0.2'
36
+ spec.add_development_dependency 'guard', '~> 2.13'
37
+ spec.add_development_dependency 'pry', '~> 0.10'
38
+ spec.add_development_dependency 'pry-remote', '~> 0.1'
39
+ spec.add_development_dependency 'pry-nav', '~> 0.2'
40
+ spec.add_development_dependency 'coveralls', '~> 0.8'
41
+ spec.add_development_dependency 'rubocop', '0.33'
42
+ spec.add_development_dependency 'guard-livereload', '~> 2.4'
43
+ spec.add_development_dependency 'guard-rake', '~> 1.0'
44
+ spec.add_development_dependency 'fakefs', '~> 0.6'
45
+
46
+ spec.add_runtime_dependency 'aws-sdk', '~> 2.1'
47
+ spec.add_runtime_dependency 'commander', '~> 4.3'
48
+ spec.add_runtime_dependency 'curb', '~> 0.8'
49
+ spec.add_runtime_dependency 'deep_merge', '~> 1.0'
50
+ spec.add_runtime_dependency 'berkshelf', '~> 4.0'
51
+ spec.add_runtime_dependency 'chef', '~> 12.5'
52
+ end
data/lib/formatron.rb ADDED
@@ -0,0 +1,357 @@
1
+ require 'formatron/aws'
2
+ require 'formatron/config'
3
+ require 'formatron/dsl'
4
+ require 'formatron/s3/configuration'
5
+ require 'formatron/s3/chef_server_cert'
6
+ require 'formatron/s3/chef_server_keys'
7
+ require 'formatron/s3/cloud_formation_template'
8
+ require 'formatron/cloud_formation/template'
9
+ require 'formatron/cloud_formation'
10
+ require 'formatron/chef'
11
+ require 'formatron/logger'
12
+ require 'formatron/util/vpc'
13
+ require 'formatron/chef_clients'
14
+ require 'formatron/external'
15
+
16
+ # manages a Formatron stack
17
+ # rubocop:disable Metrics/ClassLength
18
+ class Formatron
19
+ FORMATRONFILE = 'Formatronfile'
20
+
21
+ attr_reader :protected
22
+ alias_method :protected?, :protected
23
+
24
+ # rubocop:disable Metrics/MethodLength
25
+ def initialize(credentials:, directory:, target:)
26
+ @target = target
27
+ @aws = AWS.new credentials: credentials
28
+ @config = Config.target(
29
+ directory: directory,
30
+ target: target
31
+ )
32
+ @external = External.new(
33
+ target: @target,
34
+ config: @config,
35
+ aws: @aws
36
+ )
37
+ @dsl = DSL.new(
38
+ file: File.join(directory, FORMATRONFILE),
39
+ config: @config,
40
+ target: @target,
41
+ external: @external
42
+ )
43
+ _initialize
44
+ end
45
+ # rubocop:enable Metrics/MethodLength
46
+
47
+ # rubocop:disable Metrics/MethodLength
48
+ # rubocop:disable Metrics/AbcSize
49
+ # rubocop:disable Metrics/CyclomaticComplexity
50
+ # rubocop:disable Metrics/PerceivedComplexity
51
+ def _initialize
52
+ @formatron = @dsl.formatron
53
+ @vpcs = @formatron.vpc
54
+ @name = @formatron.name
55
+ @bucket = @formatron.bucket
56
+ external_formatron = @external.formatron
57
+ @external_vpcs = external_formatron.vpc
58
+ external_global = external_formatron.global
59
+ global = @formatron.global || external_global
60
+ external_ec2 = external_global.ec2
61
+ ec2 = global.ec2 || external_ec2
62
+ key_pair = ec2.key_pair || external_ec2.key_pair
63
+ @ec2_key = ec2.private_key || external_ec2.private_key
64
+ @protected = global.protect || external_global.protect
65
+ @kms_key = global.kms_key || external_global.kms_key
66
+ @databag_secret = global.databag_secret || external_global.databag_secret
67
+ hosted_zone_id = global.hosted_zone_id || external_global.hosted_zone_id
68
+ @hosted_zone_name = @aws.hosted_zone_name hosted_zone_id
69
+ @configuration = @external.export formatron: @formatron
70
+ @cloud_formation_template = CloudFormation::Template.new(
71
+ formatron: @formatron,
72
+ hosted_zone_name: @hosted_zone_name,
73
+ key_pair: key_pair,
74
+ kms_key: @kms_key,
75
+ hosted_zone_id: hosted_zone_id,
76
+ target: @target,
77
+ external: @external
78
+ ).hash
79
+ _initialize_chef_clients
80
+ _initialize_instances
81
+ end
82
+ # rubocop:enable Metrics/PerceivedComplexity
83
+ # rubocop:enable Metrics/CyclomaticComplexity
84
+ # rubocop:enable Metrics/AbcSize
85
+ # rubocop:enable Metrics/MethodLength
86
+
87
+ # rubocop:disable Metrics/MethodLength
88
+ def _initialize_chef_clients
89
+ @chef_clients = {}
90
+ @vpcs.each do |key, vpc|
91
+ @chef_clients[key] = ChefClients.new(
92
+ aws: @aws,
93
+ bucket: @bucket,
94
+ name: @name,
95
+ target: @target,
96
+ ec2_key: @ec2_key,
97
+ hosted_zone_name: @hosted_zone_name,
98
+ vpc: vpc,
99
+ external: @external_vpcs[key],
100
+ configuration: @configuration,
101
+ databag_secret: @databag_secret
102
+ )
103
+ end
104
+ end
105
+ # rubocop:enable Metrics/MethodLength
106
+
107
+ # rubocop:disable Metrics/MethodLength
108
+ # rubocop:disable Metrics/AbcSize
109
+ def _initialize_instances
110
+ @chef_servers = {}
111
+ @bastions = {}
112
+ @nats = {}
113
+ @instances = {}
114
+ @all_instances = {}
115
+ @vpcs.each do |k, v|
116
+ chef_servers = @chef_servers[k] = {}
117
+ bastions = @bastions[k] = {}
118
+ nats = @nats[k] = {}
119
+ instances = @instances[k] = {}
120
+ all_instances = @all_instances[k] = {}
121
+ v.subnet.values.each do |s|
122
+ chef_servers.merge! s.chef_server
123
+ bastions.merge! s.bastion
124
+ nats.merge! s.nat
125
+ instances.merge! s.instance
126
+ end
127
+ all_instances.merge! chef_servers
128
+ all_instances.merge! bastions
129
+ all_instances.merge! nats
130
+ all_instances.merge! instances
131
+ end
132
+ end
133
+ # rubocop:enable Metrics/AbcSize
134
+ # rubocop:enable Metrics/MethodLength
135
+
136
+ def deploy
137
+ _deploy_configuration
138
+ _deploy_chef_server_certs
139
+ _deploy_template
140
+ _deploy_stack
141
+ end
142
+
143
+ def provision
144
+ @all_instances.each do |key, instances|
145
+ _provision_vpc key, instances
146
+ end
147
+ end
148
+
149
+ # rubocop:disable Metrics/MethodLength
150
+ # rubocop:disable Metrics/AbcSize
151
+ def _provision_vpc(key, instances)
152
+ chef_clients = @chef_clients[key]
153
+ chef_clients.init
154
+ chef_clients.deploy_databags
155
+ instances.values.each do |instance|
156
+ dsl_chef = instance.chef
157
+ next if dsl_chef.nil?
158
+ chef = chef_clients.get dsl_chef.server
159
+ cookbook = dsl_chef.cookbook
160
+ bastion = dsl_chef.bastion
161
+ sub_domain = instance.sub_domain
162
+ _provision_instance chef, cookbook, sub_domain, bastion
163
+ end
164
+ ensure
165
+ chef_clients.unlink
166
+ end
167
+ # rubocop:enable Metrics/AbcSize
168
+ # rubocop:enable Metrics/MethodLength
169
+
170
+ def _provision_instance(chef, cookbook, sub_domain, bastion)
171
+ chef.provision(
172
+ sub_domain: sub_domain,
173
+ cookbook: cookbook,
174
+ bastion: bastion
175
+ )
176
+ end
177
+
178
+ def destroy
179
+ _destroy_chef_instances
180
+ _destroy_configuration
181
+ _destroy_chef_server_cert
182
+ _destroy_chef_server_keys
183
+ _destroy_template
184
+ _destroy_stack
185
+ end
186
+
187
+ def _deploy_configuration
188
+ S3::Configuration.deploy(
189
+ aws: @aws,
190
+ kms_key: @kms_key,
191
+ bucket: @bucket,
192
+ name: @name,
193
+ target: @target,
194
+ configuration: @configuration
195
+ )
196
+ end
197
+
198
+ # rubocop:disable Metrics/MethodLength
199
+ def _deploy_chef_server_certs
200
+ @chef_servers.values.each do |chef_servers|
201
+ chef_servers.values.each do |chef_server|
202
+ S3::ChefServerCert.deploy(
203
+ aws: @aws,
204
+ kms_key: @kms_key,
205
+ bucket: @bucket,
206
+ name: @name,
207
+ target: @target,
208
+ guid: chef_server.guid,
209
+ cert: chef_server.ssl_cert,
210
+ key: chef_server.ssl_key
211
+ )
212
+ end
213
+ end
214
+ end
215
+ # rubocop:enable Metrics/MethodLength
216
+
217
+ def _deploy_template
218
+ S3::CloudFormationTemplate.deploy(
219
+ aws: @aws,
220
+ kms_key: @kms_key,
221
+ bucket: @bucket,
222
+ name: @name,
223
+ target: @target,
224
+ cloud_formation_template:
225
+ JSON.pretty_generate(@cloud_formation_template)
226
+ )
227
+ end
228
+
229
+ def _deploy_stack
230
+ CloudFormation.deploy(
231
+ aws: @aws,
232
+ bucket: @bucket,
233
+ name: @name,
234
+ target: @target,
235
+ parameters: @external.outputs.hash
236
+ )
237
+ end
238
+
239
+ def _destroy_configuration
240
+ S3::Configuration.destroy(
241
+ aws: @aws,
242
+ bucket: @bucket,
243
+ name: @name,
244
+ target: @target
245
+ )
246
+ rescue => error
247
+ LOG.warn error
248
+ end
249
+
250
+ # rubocop:disable Metrics/MethodLength
251
+ def _destroy_chef_server_cert
252
+ @chef_servers.values.each do |chef_servers|
253
+ chef_servers.values.each do |chef_server|
254
+ S3::ChefServerCert.destroy(
255
+ aws: @aws,
256
+ bucket: @bucket,
257
+ name: @name,
258
+ target: @target,
259
+ guid: chef_server.guid
260
+ )
261
+ end
262
+ end
263
+ rescue => error
264
+ LOG.warn error
265
+ end
266
+ # rubocop:enable Metrics/MethodLength
267
+
268
+ # rubocop:disable Metrics/MethodLength
269
+ def _destroy_chef_server_keys
270
+ @chef_servers.values.each do |chef_servers|
271
+ chef_servers.values.each do |chef_server|
272
+ S3::ChefServerKeys.destroy(
273
+ aws: @aws,
274
+ bucket: @bucket,
275
+ name: @name,
276
+ target: @target,
277
+ guid: chef_server.guid
278
+ )
279
+ end
280
+ end
281
+ rescue => error
282
+ LOG.warn error
283
+ end
284
+ # rubocop:enable Metrics/MethodLength
285
+
286
+ def _destroy_template
287
+ S3::CloudFormationTemplate.destroy(
288
+ aws: @aws,
289
+ bucket: @bucket,
290
+ name: @name,
291
+ target: @target
292
+ )
293
+ rescue => error
294
+ LOG.warn error
295
+ end
296
+
297
+ def _destroy_stack
298
+ CloudFormation.destroy(
299
+ aws: @aws,
300
+ name: @name,
301
+ target: @target
302
+ )
303
+ rescue => error
304
+ LOG.warn error
305
+ end
306
+
307
+ def _destroy_chef_instances
308
+ @all_instances.each do |key, instances|
309
+ _destroy_chef_vpc_instances key, instances
310
+ end
311
+ end
312
+
313
+ # rubocop:disable Metrics/MethodLength
314
+ def _destroy_chef_vpc_instances(key, instances)
315
+ chef_clients = @chef_clients[key]
316
+ chef_clients.init
317
+ chef_clients.delete_databags
318
+ instances.values.each do |instance|
319
+ dsl_chef = instance.chef
320
+ next if dsl_chef.nil?
321
+ chef = chef_clients.get dsl_chef.server
322
+ sub_domain = instance.sub_domain
323
+ _destroy_chef_instance chef, sub_domain
324
+ end
325
+ rescue => error
326
+ LOG.warn error
327
+ ensure
328
+ chef_clients.unlink
329
+ end
330
+ # rubocop:enable Metrics/MethodLength
331
+
332
+ def _destroy_chef_instance(chef, sub_domain)
333
+ chef.destroy(
334
+ sub_domain: sub_domain
335
+ )
336
+ rescue => error
337
+ LOG.warn error
338
+ end
339
+
340
+ private(
341
+ :_initialize,
342
+ :_initialize_chef_clients,
343
+ :_initialize_instances,
344
+ :_deploy_configuration,
345
+ :_deploy_template,
346
+ :_deploy_stack,
347
+ :_destroy_configuration,
348
+ :_destroy_template,
349
+ :_destroy_stack,
350
+ :_destroy_chef_instances,
351
+ :_destroy_chef_vpc_instances,
352
+ :_destroy_chef_instance,
353
+ :_provision_vpc,
354
+ :_provision_instance
355
+ )
356
+ end
357
+ # rubocop:enable Metrics/ClassLength