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
@@ -0,0 +1,26 @@
1
+ class Formatron
2
+ # command completion utilities
3
+ module Completion
4
+ # exports commands, etc to completion script ERB template
5
+ class Template
6
+ attr_reader :subcommands, :command
7
+
8
+ def initialize(command, subcommands)
9
+ @command = command
10
+ @subcommands = subcommands
11
+ end
12
+ end
13
+
14
+ def self.script(command, subcommands)
15
+ template = File.join(
16
+ File.dirname(File.expand_path(__FILE__)),
17
+ 'completion',
18
+ 'completion.sh.erb'
19
+ )
20
+ erb = ERB.new File.read(template)
21
+ erb.filename = template
22
+ erb_template = erb.def_class Template, 'render()'
23
+ erb_template.new(command, subcommands).render
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ _formatron_complete() {
2
+ local commands=(
3
+ <% subcommands.each do |subcommand| %> "<%= subcommand %>"
4
+ <% end %>)
5
+
6
+ COMPREPLY=()
7
+
8
+ local word="${COMP_WORDS[COMP_CWORD]}"
9
+
10
+ if [ "${COMP_WORDS[1]}" == "help" ]; then
11
+ local entered=("${COMP_WORDS[@]:2}")
12
+ local index=$(($COMP_CWORD-2))
13
+ else
14
+ local entered=("${COMP_WORDS[@]:1}")
15
+ local index=$(($COMP_CWORD-1))
16
+ fi
17
+
18
+ local matches=()
19
+ for command in "${commands[@]}"; do
20
+ local components=($command)
21
+ if [ ${#components[@]} -ge ${#entered[@]} ]; then
22
+ local entered_prefix=${entered[@]::$index}
23
+ local command_prefix=${components[@]::$index}
24
+ local component=${components[$index]}
25
+ if [[ "${entered_prefix[@]}" == "${command_prefix[@]}" && "$component" == "$word"* ]]; then
26
+ matches+=("$component")
27
+ fi
28
+ fi
29
+ done
30
+ local completions="${matches[@]}"
31
+
32
+ COMPREPLY=($(compgen -W "$completions" -- "$word"))
33
+ }
34
+
35
+ complete -o default -F _formatron_complete <%= command %>
@@ -0,0 +1,31 @@
1
+ require_relative 'config/reader'
2
+ require 'deep_merge'
3
+
4
+ class Formatron
5
+ # Processes the config directory
6
+ module Config
7
+ CONFIG_DIR = 'config'
8
+ DEFAULT_CONFIG = '_default'
9
+ DEFAULT_JSON = '_default.json'
10
+
11
+ def self.targets(directory:)
12
+ config = File.join directory, CONFIG_DIR
13
+ Dir.entries(config).select do |entry|
14
+ path = File.join config, entry
15
+ File.directory?(path) && !%W(#{DEFAULT_CONFIG} . ..).include?(entry)
16
+ end
17
+ end
18
+
19
+ def self.target(directory:, target:)
20
+ Reader.read(
21
+ File.join(directory, CONFIG_DIR, DEFAULT_CONFIG),
22
+ DEFAULT_JSON
23
+ ).deep_merge!(
24
+ Reader.read(
25
+ File.join(directory, CONFIG_DIR, target),
26
+ DEFAULT_JSON
27
+ )
28
+ )
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+ require 'deep_merge'
3
+
4
+ class Formatron
5
+ module Config
6
+ # reads config directories into config hash
7
+ module Reader
8
+ def self.read(dir, default_file)
9
+ default = File.join(dir, default_file)
10
+ config = File.file?(default) ? JSON.parse(File.read(default)) : {}
11
+ entries = Dir.glob(File.join(dir, '*'), File::FNM_DOTMATCH)
12
+ do_entries entries, default_file, config
13
+ config
14
+ end
15
+
16
+ def self.do_entries(entries, default_file, config)
17
+ entries.each do |entry|
18
+ basename = File.basename(entry)
19
+ next if ['.', '..', default_file].include?(basename)
20
+ config[basename] = {} unless config[basename].is_a? Hash
21
+ config[basename].deep_merge!(
22
+ read(entry, default_file)
23
+ ) if File.directory?(entry)
24
+ config[basename] = File.read(entry) if File.file?(entry)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'dsl/formatron'
2
+
3
+ class Formatron
4
+ # context for evaluating the Formatronfile
5
+ class DSL
6
+ attr_reader :formatron, :config, :target
7
+
8
+ def initialize(file:, config:, target:, external:)
9
+ @formatron = Formatron.new external: external
10
+ @config = config
11
+ @target = target
12
+ instance_eval File.read(file), file
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'formatron/global'
2
+ require_relative 'formatron/vpc'
3
+ require 'formatron/util/dsl'
4
+
5
+ class Formatron
6
+ class DSL
7
+ # formatron top level DSL object
8
+ class Formatron
9
+ extend Util::DSL
10
+
11
+ dsl_initialize_block do |external:|
12
+ @external = external
13
+ end
14
+
15
+ dsl_property :name
16
+ dsl_property :bucket
17
+ dsl_block :global, 'Global'
18
+ dsl_hash :vpc, 'VPC'
19
+
20
+ def depends(dependency)
21
+ @external.merge bucket: @bucket, dependency: dependency
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ require 'formatron/util/dsl'
2
+ require_relative 'global/ec2'
3
+
4
+ class Formatron
5
+ class DSL
6
+ class Formatron
7
+ # Global configuration
8
+ class Global
9
+ extend Util::DSL
10
+ dsl_initialize_block
11
+ dsl_property :protect
12
+ dsl_property :kms_key
13
+ dsl_property :databag_secret
14
+ dsl_property :hosted_zone_id
15
+ dsl_block :ec2, 'EC2'
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'formatron/util/dsl'
2
+
3
+ class Formatron
4
+ class DSL
5
+ class Formatron
6
+ class Global
7
+ # EC2 key pair configuration
8
+ class EC2
9
+ extend Util::DSL
10
+ dsl_initialize_block
11
+ dsl_property :key_pair
12
+ dsl_property :private_key
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'vpc/subnet'
2
+ require 'formatron/util/dsl'
3
+
4
+ class Formatron
5
+ class DSL
6
+ class Formatron
7
+ # VPC configuration
8
+ class VPC
9
+ extend Util::DSL
10
+ dsl_initialize_hash
11
+ dsl_property :guid
12
+ dsl_property :cidr
13
+ dsl_hash :subnet, 'Subnet'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ require 'formatron/util/dsl'
2
+ require_relative 'subnet/acl'
3
+ require_relative 'subnet/instance'
4
+ require_relative 'subnet/chef_server'
5
+
6
+ class Formatron
7
+ class DSL
8
+ class Formatron
9
+ class VPC
10
+ # Subnet configuration
11
+ class Subnet
12
+ extend Util::DSL
13
+ dsl_initialize_hash
14
+ dsl_property :guid
15
+ dsl_property :cidr
16
+ dsl_property :availability_zone
17
+ dsl_property :gateway
18
+ dsl_block :acl, 'ACL'
19
+ dsl_hash :nat, 'Instance'
20
+ dsl_hash :bastion, 'Instance'
21
+ dsl_hash :instance, 'Instance'
22
+ dsl_hash :chef_server, 'ChefServer'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ require 'formatron/util/dsl'
2
+
3
+ class Formatron
4
+ class DSL
5
+ class Formatron
6
+ class VPC
7
+ class Subnet
8
+ # Subnet ACL configuration
9
+ class ACL
10
+ extend Util::DSL
11
+ dsl_initialize_block
12
+ dsl_array :source_cidr
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'chef_server/organization'
2
+ require_relative 'instance'
3
+ require 'formatron/util/dsl'
4
+
5
+ class Formatron
6
+ class DSL
7
+ class Formatron
8
+ class VPC
9
+ class Subnet
10
+ # Chef Server instance configuration
11
+ class ChefServer < Instance
12
+ extend Util::DSL
13
+ dsl_initialize_hash
14
+ dsl_property :version
15
+ dsl_property :cookbooks_bucket
16
+ dsl_property :organization
17
+ dsl_property :username
18
+ dsl_property :email
19
+ dsl_property :first_name
20
+ dsl_property :last_name
21
+ dsl_property :password
22
+ dsl_property :ssl_key
23
+ dsl_property :ssl_cert
24
+ dsl_property :ssl_verify
25
+ dsl_property :stack
26
+ dsl_block :organization, 'Organization'
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../instance'
2
+ require 'formatron/util/dsl'
3
+
4
+ class Formatron
5
+ class DSL
6
+ class Formatron
7
+ class VPC
8
+ class Subnet
9
+ class ChefServer < Instance
10
+ # Chef Server organization configuration
11
+ class Organization
12
+ extend Util::DSL
13
+ dsl_initialize_block
14
+ dsl_property :short_name
15
+ dsl_property :full_name
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'instance/chef'
2
+ require_relative 'instance/policy'
3
+ require_relative 'instance/security_group'
4
+ require_relative 'instance/setup'
5
+ require 'formatron/util/dsl'
6
+
7
+ class Formatron
8
+ class DSL
9
+ class Formatron
10
+ class VPC
11
+ class Subnet
12
+ # Generic instance configuration
13
+ class Instance
14
+ extend Util::DSL
15
+ dsl_initialize_hash
16
+ dsl_property :guid
17
+ dsl_property :sub_domain
18
+ dsl_property :source_dest_check
19
+ dsl_property :instance_type
20
+ dsl_block :chef, 'Chef'
21
+ dsl_block :policy, 'Policy'
22
+ dsl_block :security_group, 'SecurityGroup'
23
+ dsl_block :setup, 'Setup'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ require 'formatron/util/dsl'
2
+
3
+ class Formatron
4
+ class DSL
5
+ class Formatron
6
+ class VPC
7
+ class Subnet
8
+ class Instance
9
+ # Generic instance configuration
10
+ class Chef
11
+ extend Util::DSL
12
+ dsl_initialize_block
13
+ dsl_property :server
14
+ dsl_property :cookbook
15
+ dsl_property :bastion
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ require 'formatron/util/dsl'
2
+ require_relative 'policy/statement'
3
+
4
+ class Formatron
5
+ class DSL
6
+ class Formatron
7
+ class VPC
8
+ class Subnet
9
+ class Instance
10
+ # IAM policy configuration
11
+ class Policy
12
+ extend Util::DSL
13
+ dsl_initialize_block
14
+ dsl_block_array :statement, 'Statement'
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require 'formatron/util/dsl'
2
+
3
+ class Formatron
4
+ class DSL
5
+ class Formatron
6
+ class VPC
7
+ class Subnet
8
+ class Instance
9
+ class Policy
10
+ # IAM policy statement configuration
11
+ class Statement
12
+ extend Util::DSL
13
+ dsl_initialize_block
14
+ dsl_array :action
15
+ dsl_array :resource
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require 'formatron/util/dsl'
2
+
3
+ class Formatron
4
+ class DSL
5
+ class Formatron
6
+ class VPC
7
+ class Subnet
8
+ class Instance
9
+ # Instance security group configuration
10
+ class SecurityGroup
11
+ extend Util::DSL
12
+ dsl_initialize_block
13
+ dsl_array :open_tcp_port
14
+ dsl_array :open_udp_port
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end