vagrant-opsworks 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.simplecov +7 -0
  4. data/.travis.yml +13 -0
  5. data/Gemfile +44 -0
  6. data/Guardfile +17 -0
  7. data/Thorfile +59 -0
  8. data/integration/Gemfile +11 -0
  9. data/integration/Vagrantfile +8 -0
  10. data/lib/berkshelf-monkey-patch.rb +26 -0
  11. data/lib/vagrant-opsworks/action/checkout_cookbooks.rb +71 -0
  12. data/lib/vagrant-opsworks/action/configure_berks.rb +26 -0
  13. data/lib/vagrant-opsworks/action/configure_chef.rb +28 -0
  14. data/lib/vagrant-opsworks/action/create_roles.rb +69 -0
  15. data/lib/vagrant-opsworks/action/inject_boxes.rb +44 -0
  16. data/lib/vagrant-opsworks/action/merge_cookbooks.rb +27 -0
  17. data/lib/vagrant-opsworks/action/setup_environment.rb +34 -0
  18. data/lib/vagrant-opsworks/action.rb +51 -0
  19. data/lib/vagrant-opsworks/application.rb +26 -0
  20. data/lib/vagrant-opsworks/client/app.rb +50 -0
  21. data/lib/vagrant-opsworks/client/instance.rb +23 -0
  22. data/lib/vagrant-opsworks/client/layer.rb +39 -0
  23. data/lib/vagrant-opsworks/client/stack.rb +47 -0
  24. data/lib/vagrant-opsworks/client.rb +46 -0
  25. data/lib/vagrant-opsworks/client_old.rb +94 -0
  26. data/lib/vagrant-opsworks/config.rb +156 -0
  27. data/lib/vagrant-opsworks/custom_json.rb +22 -0
  28. data/lib/vagrant-opsworks/env.rb +19 -0
  29. data/lib/vagrant-opsworks/env_old.rb +162 -0
  30. data/lib/vagrant-opsworks/errors.rb +13 -0
  31. data/lib/vagrant-opsworks/loader/client.rb +19 -0
  32. data/lib/vagrant-opsworks/loader/instances.rb +78 -0
  33. data/lib/vagrant-opsworks/loader/stack.rb +27 -0
  34. data/lib/vagrant-opsworks/loader.rb +24 -0
  35. data/lib/vagrant-opsworks/plugin.rb +32 -0
  36. data/lib/vagrant-opsworks/stack/app.rb +0 -0
  37. data/lib/vagrant-opsworks/stack/instance.rb +62 -0
  38. data/lib/vagrant-opsworks/stack/stack.rb +84 -0
  39. data/lib/vagrant-opsworks/stack.rb +20 -0
  40. data/lib/vagrant-opsworks/util/configuration_builder.rb +44 -0
  41. data/lib/vagrant-opsworks/util/configuration_hash.rb +27 -0
  42. data/lib/vagrant-opsworks/util/dummy_configuration.rb +33 -0
  43. data/lib/vagrant-opsworks/util/env_helpers.rb +44 -0
  44. data/lib/vagrant-opsworks/version.rb +5 -0
  45. data/lib/vagrant-opsworks.rb +24 -0
  46. data/locales/en.yml +13 -0
  47. data/provisioning/client.yml +16 -0
  48. data/provisioning/install-agent.sh +75 -0
  49. data/spec/spec_helper.rb +23 -0
  50. data/spec/unit/vagrant-opsworks/action/create_roles_spec.rb +5 -0
  51. data/spec/unit/vagrant-opsworks/config_spec.rb +109 -0
  52. data/vagrant-opsworks.gemspec +34 -0
  53. metadata +199 -0
@@ -0,0 +1,39 @@
1
+ module VagrantPlugins::OpsWorks
2
+ class Client
3
+ class Layer < VagrantPlugins::OpsWorks::Util::DummyConfiguration
4
+
5
+ def layer_id(value=nil)
6
+ set_or_return(:layer_id, value)
7
+ end
8
+
9
+ def type(value=nil)
10
+ set_or_return(:type, value)
11
+ end
12
+
13
+ def name(value=nil)
14
+ set_or_return(:description, value)
15
+ end
16
+
17
+ def shortname(value=nil)
18
+ set_or_return(:name, value)
19
+ end
20
+
21
+ def attributes(value=nil)
22
+ set_or_return(:attributes, value)
23
+ end
24
+
25
+ def packages(value=nil)
26
+ set_or_return(:packages, value)
27
+ end
28
+
29
+ def default_recipes(value=nil)
30
+ set_or_return(:default_recipes, value)
31
+ end
32
+
33
+ def custom_recipes(value=nil)
34
+ set_or_return(:custom_recipes, value)
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,47 @@
1
+ module VagrantPlugins::OpsWorks
2
+ class Client
3
+ class Stack < VagrantPlugins::OpsWorks::Util::DummyConfiguration
4
+
5
+ def name(value=nil)
6
+ set_or_return(:name, value)
7
+ end
8
+
9
+ def default_os(value=nil)
10
+ set_or_return(:default_os, value)
11
+ end
12
+
13
+ def configuration_manager(value=nil)
14
+ set_or_return(:configuration_manager, value)
15
+ end
16
+
17
+ def chef_configuration(value=nil)
18
+ set_or_return(:chef_configuration, value)
19
+ end
20
+
21
+ def berks_enabled?
22
+ return self['chef_configuration'][:manage_berkshelf]
23
+ end
24
+
25
+ def use_custom_cookbooks?
26
+ self['use_custom_cookbooks']
27
+ end
28
+
29
+ def use_custom_cookbooks(value=nil)
30
+ set_or_return(:use_custom_cookbooks, value)
31
+ end
32
+
33
+ def custom_cookbooks_source(value=nil)
34
+ set_or_return(:custom_cookbooks_source, value)
35
+ end
36
+
37
+ def custom_json(value=nil)
38
+ require_relative '../custom_json'
39
+ unless value.nil?
40
+ value = CustomJson.new(JSON.parse(value)) if value.is_a?(String)
41
+ end
42
+ set_or_return(:custom_json, value)
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,46 @@
1
+ module VagrantPlugins::OpsWorks
2
+ class Client
3
+ require_relative 'util/dummy_configuration'
4
+ require_relative 'client/app'
5
+ require_relative 'client/instance'
6
+ require_relative 'client/layer'
7
+ require_relative 'client/stack'
8
+
9
+ # @return [String]
10
+ attr_reader :stack_id
11
+
12
+ # @paran [String] stack_id the stack id to retrieve metadata about
13
+ def initialize(stack_id)
14
+ @stack_id = stack_id
15
+ end
16
+
17
+ # @return [Array<App>]
18
+ def apps
19
+ @apps ||= aws_client.describe_apps(stack_id: @stack_id)[:apps].map{|a| App.new(a)}
20
+ end
21
+
22
+ # @return [Array<Instance>]
23
+ def instances
24
+ @instances ||= aws_client.describe_instances(stack_id: @stack_id)[:instances].map{|i| Instance.new(i)}
25
+ end
26
+
27
+ # @return [Array<Layer>]
28
+ def layers
29
+ @layers ||= aws_client.describe_layers(stack_id: @stack_id)[:layers].map{|l| Layer.new(l)}
30
+ end
31
+
32
+ # @return [Stack]
33
+ def stack
34
+ @stack ||= Stack.new(aws_client.describe_stacks(stack_ids: [@stack_id])[:stacks].first)
35
+ end
36
+
37
+ protected
38
+
39
+ # @return [AWS::OpsWorks::Client]
40
+ def aws_client
41
+ require 'aws-sdk'
42
+ @client ||= AWS::OpsWorks::Client.new
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,94 @@
1
+ module VagrantPlugins
2
+ module OpsWorks
3
+ class Client
4
+
5
+ attr_reader :stack_id
6
+
7
+ def initialize(stack_id, cache=nil)
8
+ @stack_id = stack_id
9
+ @registry = Vagrant::Registry.new
10
+ @cache = cache
11
+
12
+ %w(apps instances layers).each{ |m| @registry.register(m) { build_metadata_proc(m).call } }
13
+ @registry.register('stacks') {
14
+ api_proc = Proc.new {
15
+ unless opsworks_reachable?
16
+ raise VagrantPlugins::OpsWorks::Errors::ConnectError
17
+ end
18
+
19
+ aws_client.describe_stacks(stack_ids: [@stack_id])[:stacks]
20
+ }
21
+ build_api_proc('stacks', api_proc).call
22
+ }
23
+ end
24
+
25
+ def apps
26
+ @registry['apps']
27
+ end
28
+
29
+ def instances
30
+ @registry['instances']
31
+ end
32
+
33
+ def layers
34
+ @registry['layers']
35
+ end
36
+
37
+ def stack
38
+ @registry['stacks'].first
39
+ end
40
+
41
+ protected
42
+
43
+ def aws_client
44
+ require 'aws-sdk'
45
+ @client ||= AWS::OpsWorks::Client.new
46
+ end
47
+
48
+ def build_metadata_proc(type)
49
+ method = "describe_#{type}"
50
+ api_proc = Proc.new {
51
+ unless opsworks_reachable?
52
+ raise VagrantPlugins::OpsWorks::Errors::ConnectError
53
+ end
54
+
55
+ aws_client.send(method, stack_id: @stack_id)[type.to_sym]
56
+ }
57
+ build_api_proc(type, api_proc)
58
+ end
59
+
60
+ def build_api_proc(type, metadata_proc)
61
+ if @cache.is_a?(Pathname)
62
+ unless File.directory?(@cache)
63
+ FileUtils.mkdir_p(@cache)
64
+ end
65
+
66
+ api_proc = metadata_proc
67
+
68
+ metadata_proc = Proc.new {
69
+ cache_file = @cache.join(type)
70
+
71
+ if cache_file.file?
72
+ Marshal.load(File.read(cache_file))
73
+ else
74
+ data = api_proc.call
75
+ File.open(cache_file, 'w') {|f| f.puts Marshal.dump(data)}
76
+ data
77
+ end
78
+ }
79
+ end
80
+
81
+ metadata_proc
82
+ end
83
+
84
+ def opsworks_reachable?
85
+ require 'open-uri'
86
+ begin
87
+ true if open('https://opsworks.us-east-1.amazonaws.com', { read_timeout: 2 })
88
+ rescue
89
+ false
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,156 @@
1
+ module VagrantPlugins
2
+ module OpsWorks
3
+ class Config < ::Vagrant.plugin('2', :config)
4
+ # @return [Boolean]
5
+ # disable use of OpsWorks in Vagrant
6
+ attr_accessor :enabled
7
+
8
+ # @return [Boolean]
9
+ # disable use of OpsWorks Cache
10
+ attr_accessor :cache
11
+
12
+ # @return [String]
13
+ # OpsWorks agent version
14
+ attr_accessor :agent_version
15
+
16
+ # @return [String]
17
+ # OpsWorks agent bucket
18
+ attr_accessor :agent_bucket
19
+
20
+ # @return [String]
21
+ # OpsWorks asset bucket
22
+ attr_accessor :asset_bucket
23
+
24
+ # @return [String]
25
+ # OpsWorks stack id
26
+ attr_accessor :stack_id
27
+
28
+ # @return [String]
29
+ # OpsWorks hostname suffix
30
+ attr_accessor :hostname_suffix
31
+
32
+ # @return [Array]
33
+ # Applications to ignore in OpsWorks stack
34
+ attr_accessor :ignore_apps
35
+
36
+ # @return [Array]
37
+ # Instances to ignore in OpsWorks stack
38
+ attr_accessor :ignore_instances
39
+
40
+ # @return [Array]
41
+ # Layers to ignore in OpsWorks stack
42
+ attr_accessor :ignore_layers
43
+
44
+ # @return [Array]
45
+ # Recipes to ignore in OpsWorks stack
46
+ attr_accessor :ignore_recipes
47
+
48
+ # @return [Hash]
49
+ attr_accessor :supplimental_json
50
+
51
+ def initialize
52
+ super
53
+
54
+ @agent_version = UNSET_VALUE
55
+ @agent_bucket = UNSET_VALUE
56
+ @asset_bucket = UNSET_VALUE
57
+ @cache = UNSET_VALUE
58
+ @enabled = UNSET_VALUE
59
+ @stack_id = UNSET_VALUE
60
+ @hostname_suffix = UNSET_VALUE
61
+ @ignore_apps = UNSET_VALUE
62
+ @ignore_instances = UNSET_VALUE
63
+ @ignore_layers = UNSET_VALUE
64
+ @ignore_recipes = UNSET_VALUE
65
+ @supplimental_json = UNSET_VALUE
66
+ end
67
+
68
+ def finalize!
69
+ @agent_version = "327" if @agent_version == UNSET_VALUE
70
+ @agent_bucket = "opsworks-instance-agent.s3.amazonaws.com" if @agent_bucket == UNSET_VALUE
71
+ @asset_bucket = "opsworks-instance-assets.s3.amazonaws.com" if @asset_bucket == UNSET_VALUE
72
+ @cache = true if @cache == UNSET_VALUE
73
+ @enabled = @stack_id == UNSET_VALUE ? false : true if @enabled == UNSET_VALUE
74
+ @hostname_suffix = '.vm' if @hostname_suffix == UNSET_VALUE
75
+ @ignore_apps = Array.new if @ignore_apps == UNSET_VALUE
76
+ @ignore_instances = Array.new if @ignore_instances == UNSET_VALUE
77
+ @ignore_layers = Array.new if @ignore_layers == UNSET_VALUE
78
+ @ignore_recipes = Array.new if @ignore_recipes == UNSET_VALUE
79
+ @supplimental_json = Hash.new if @supplimental_json == UNSET_VALUE
80
+ @supplimental_json = merge_hash({
81
+ :opsworks => {
82
+ :agent_version => @agent_version,
83
+ :ruby_version => '2.0.0',
84
+ :ruby_stack => 'ruby',
85
+ :stack => {
86
+ :rds_instances => [{:engine => 'mysql'}]
87
+ }
88
+ },
89
+ :ssh_users => {
90
+ Process.uid => {
91
+ :name => Etc.getlogin,
92
+ :public_key => File.exists?(File.expand_path('~/.ssh/id_rsa.pub')) ? File.read(File.expand_path('~/.ssh/id_rsa.pub')) : nil,
93
+ :sudoer => true
94
+ },
95
+ },
96
+ :sudoers => [{:name => 'vagrant'}]
97
+ }, @supplimental_json)
98
+ end
99
+
100
+ # TODO: refactor hash merging logic to mixin
101
+ # def merge(other)
102
+ # super.tap do |result|
103
+ # result.ignore_apps = Array.new if other.ignore_apps.nil?
104
+ # result.ignore_instances = Array.new if other.ignore_instances.nil?
105
+ # result.ignore_layers = Array.new if other.ignore_layers.nil?
106
+ # result.ignore_recipes = Array.new if other.ignore_recipes.nil?
107
+ # result.stack_id = @stack_id unless other.stack_id == UNSET_VALUE
108
+ # result.ignore_instances = @ignore_instances.concat(other.ignore_instances) if other.ignore_instances.is_a?(Array) && @ignore_instances != UNSET_VALUE
109
+ # result.ignore_layers = @ignore_layers.concat(other.ignore_layers) if other.ignore_layers.is_a?(Array) && @ignore_layers != UNSET_VALUE
110
+ # result.ignore_recipes = @ignore_recipes.concat(other.ignore_recipes) if other.ignore_recipes.is_a?(Array) && @ignore_recipes != UNSET_VALUE
111
+ # result.supplimental_json = merge_hash(@supplimental_json, other.supplimental_json) if other.supplimental_json.is_a?(Hash) && @supplimental_json != UNSET_VALUE
112
+ # end
113
+ # end
114
+
115
+ def validate(machine)
116
+ errors = Array.new
117
+ unless [TrueClass, FalseClass].include?(enabled.class)
118
+ errors << I18n.t('vagrant_opsworks.config.not_a_bool', {
119
+ :config_key => 'opsworks.enabled'
120
+ })
121
+ end
122
+
123
+ unless [TrueClass, FalseClass].include?(cache.class)
124
+ errors << I18n.t('vagrant_opsworks.config.not_a_bool', {
125
+ :config_key => 'opsworks.cache'
126
+ })
127
+ end
128
+
129
+ {'opsworks configuration' => errors}
130
+ end
131
+
132
+ protected
133
+
134
+ def merge_hash(old, new)
135
+ old = unsymbolize_keys(old)
136
+ new = unsymbolize_keys(new)
137
+
138
+ new.each_pair{|current_key,new_value|
139
+ old_value = old[current_key]
140
+
141
+ old[current_key] = if old_value.is_a?(Hash) && new_value.is_a?(Hash)
142
+ merge_hash(old_value, new_value)
143
+ else
144
+ new_value
145
+ end
146
+ }
147
+ old
148
+ end
149
+
150
+ def unsymbolize_keys(h)
151
+ return Hash[h.map{ |k,v| [k.to_s, v] }]
152
+ end
153
+
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,22 @@
1
+ module VagrantPlugins::OpsWorks
2
+ require 'vagrant/util/hash_with_indifferent_access'
3
+ class CustomJson < ::Vagrant::Util::HashWithIndifferentAccess
4
+
5
+ def deep_merge(other_json, &block)
6
+ dup.deep_merge!(other_json, &block)
7
+ end
8
+
9
+ def deep_merge!(other_json, &block)
10
+ other_json.each_pair do |k,theirs|
11
+ ours = self[k]
12
+ if ours.is_a?(CustomJson) && theirs.is_a?(Hash)
13
+ self[k] = ours.deep_merge!(theirs, &block)
14
+ else
15
+ self[k] = block && ours ? block.call(k, ours, theirs) : theirs
16
+ end
17
+ end
18
+ self
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module VagrantPlugins
2
+ module OpsWorks
3
+ require_relative 'stack'
4
+ class Env
5
+ # @return [Vagrant::UI::Colored]
6
+ attr_accessor :ui
7
+ # @return [Pathname]
8
+ attr_accessor :data_directory
9
+
10
+ attr_accessor :client
11
+
12
+ def initialize
13
+ @ui = ::Vagrant::UI::Colored.new
14
+ @ui.opts[:target] = 'OpsWorks'
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,162 @@
1
+ module VagrantPlugins
2
+ module OpsWorks
3
+ require_relative 'stack'
4
+ class Env
5
+ # @return [Vagrant::UI::Colored]
6
+ attr_accessor :ui
7
+ # @return [VagrantPlugins::OpsWorks::Config]
8
+ attr_accessor :config
9
+ # @return [Pathname]
10
+ attr_accessor :data_directory
11
+
12
+ def initialize
13
+ @ui = ::Vagrant::UI::Colored.new
14
+ @ui.opts[:target] = 'OpsWorks'
15
+ @logger = Log4r::Logger.new("vagrantplugins::opsworks::env")
16
+ end
17
+
18
+ # @return [Boolean]
19
+ def enabled?
20
+ begin
21
+ config.enabled
22
+ rescue
23
+ false
24
+ end
25
+ end
26
+
27
+ # @return [Boolean]
28
+ def cache_enabled?
29
+ begin
30
+ config.cache
31
+ rescue
32
+ false
33
+ end
34
+ end
35
+
36
+ # @return [Pathname]
37
+ def cache_directory
38
+ @data_directory.join('cache') if cache_enabled?
39
+ end
40
+
41
+ # @return [String]
42
+ def agent_version
43
+ config.agent_version unless config.nil?
44
+ end
45
+
46
+ # @return [String]
47
+ def agent_bucket
48
+ config.agent_bucket unless config.nil?
49
+ end
50
+
51
+ # @return [String]
52
+ def asset_bucket
53
+ config.asset_bucket unless config.nil?
54
+ end
55
+
56
+ # @return [String]
57
+ def stack_id
58
+ config.stack_id unless config.nil?
59
+ end
60
+
61
+ # @return [String]
62
+ def hostname_suffix
63
+ config.hostname_suffix unless config.nil?
64
+ end
65
+
66
+ # @return [Array]
67
+ def ignore_apps
68
+ config.ignore_apps unless config.nil?
69
+ end
70
+
71
+ # @return [Array]
72
+ def ignore_instances
73
+ config.ignore_instances unless config.nil?
74
+ end
75
+
76
+ # @return [Array]
77
+ def ignore_layers
78
+ config.ignore_layers unless config.nil?
79
+ end
80
+
81
+ # @return [Array]
82
+ def ignore_recipes
83
+ config.ignore_recipes unless config.nil?
84
+ end
85
+
86
+ # @return [Array]
87
+ def supplimental_json
88
+ config.supplimental_json unless config.nil?
89
+ end
90
+
91
+ # @return [VagrantPlugins::OpsWorks::Stack::Stack]
92
+ def stack
93
+ if stack_id
94
+ VagrantPlugins::OpsWorks::Stack::Stack.new(self, client.stack)
95
+ end
96
+ end
97
+
98
+ # @return [Hash]
99
+ def apps
100
+ if stack_id
101
+ apps = {}
102
+ client.apps.each do |a|
103
+ unless ignore_apps.include?(a[:shortname])
104
+ app = {
105
+ :application => a[:shortname],
106
+ :application_type => a[:type],
107
+ :environment => a[:environment].nil? ? {} : Hash[a[:environment].select{|e| !e[:secure] }.map{|e| [e[:key], e[:value]]}],
108
+ :auto_bundle_on_deploy => a[:attributes]['AutoBundleOnDeploy'],
109
+ :deploying_user => Etc.getlogin,
110
+ :document_roos => a[:attributes]['DocumentRoot'],
111
+ :domains => a[:domains],
112
+ :database => {},
113
+ :memcached => { :host => nil, :port => 11211 },
114
+ :scm => {}
115
+ }
116
+ if a[:app_source][:type] == 'git'
117
+ app[:scm] = {
118
+ :scm_type => 'git',
119
+ :repository => a[:app_source][:url],
120
+ :revision => a[:app_source][:revision],
121
+ :ssh_key => nil,
122
+ :user => nil,
123
+ :password => nil
124
+ }
125
+ end
126
+ apps[a[:shortname]] = app
127
+ end
128
+ end
129
+ apps
130
+ end
131
+ end
132
+
133
+ # @return [Vagrant::Registry]
134
+ def instances
135
+ if stack_id
136
+ instances = Vagrant::Registry.new
137
+ client.instances.sort_by{|i| i[:hostname]}.each do |i|
138
+ unless ignore_instances.include?(i[:hostname])
139
+ instances.register(i[:hostname]) {
140
+ VagrantPlugins::OpsWorks::Stack::Instance.new(self, i)
141
+ }
142
+ end
143
+ end
144
+ instances
145
+ end
146
+ end
147
+
148
+ # @return [Vagrant::Registry]
149
+ def layers
150
+ client.layers
151
+ end
152
+
153
+ protected
154
+
155
+ # @return [VagrantPlugins::OpsWorks::Client]
156
+ def client
157
+ require_relative 'client'
158
+ @client ||= Client.new(stack_id, cache_directory)
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,13 @@
1
+ module VagrantPlugins
2
+ module OpsWorks
3
+ module Errors
4
+ class OpsWorksError < ::Vagrant::Errors::VagrantError
5
+ error_namespace('vagrant_opsworks.errors')
6
+ end
7
+
8
+ class ConnectError < OpsWorksError
9
+ error_key(:connect)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module VagrantPlugins::OpsWorks::Loader
2
+ class Client
3
+
4
+ def initialize(app, opsworks)
5
+ @app = app
6
+ @opsworks = opsworks
7
+ end
8
+
9
+ def call(env)
10
+ return @app.pass(env) unless @opsworks.enabled
11
+
12
+ require_relative '../client'
13
+ env[:client] = VagrantPlugins::OpsWorks::Client.new(@opsworks.stack_id)
14
+
15
+ @app.pass(env)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,78 @@
1
+ module VagrantPlugins::OpsWorks::Loader
2
+ class Instances
3
+
4
+ def initialize(app, opsworks)
5
+ @app = app
6
+ @opsworks = opsworks
7
+ end
8
+
9
+ def call(env)
10
+ return @app.pass(env) unless @opsworks.enabled
11
+
12
+ instances = env[:client].instances.sort_by{|i| i['hostname']}
13
+
14
+ apps = {}
15
+ env[:client].apps.each do |a|
16
+ next if @opsworks.ignore_apps.include?(a['name'])
17
+ app = {
18
+ 'application' => a['name'],
19
+ 'application_type' => a['type'],
20
+ 'environment' => a['environment'].nil? ? {} : Hash[a['environment'].select{|e| !e[:secure] }.map{|e| [e[:key], e[:value]]}],
21
+ 'auto_bundle_on_deploy' => a['attributes']['AutoBundleOnDeploy'],
22
+ 'deploying_user' => Etc.getlogin,
23
+ 'document_root' => a['attributes']['DocumentRoot'],
24
+ 'domains' => a['domains'],
25
+ 'database' => {},
26
+ 'memcached' => { 'host' => nil, 'port' => 11211 },
27
+ 'scm' => {}
28
+ }
29
+ if a['app_source'][:type] == 'git'
30
+ app['scm'] = {
31
+ 'scm_type' => 'git',
32
+ 'repository' => a[:app_source][:url],
33
+ 'ssh_key' => nil,
34
+ 'user' => nil,
35
+ 'password' => nil
36
+ }
37
+ end
38
+ apps[a['name']] = app
39
+ end
40
+
41
+ instances.each do |i|
42
+ next if @opsworks.ignore_instances.include?(i['hostname'])
43
+ env[:config].vm.define i['hostname'] do |node|
44
+ VagrantPlugins::OpsWorks::Loader.select_vmbox(env[:config], i['os'])
45
+
46
+ node.vm.provision :chef_solo do |chef|
47
+ pkgs = []
48
+ i['layer_ids'].each do |l|
49
+ next if @opsworks.ignore_layers.include?(l['name'])
50
+ layer = env[:client].layers.select{|x| x['layer_id'] == l}.first
51
+ chef.add_role layer['name']
52
+ pkgs << layer['packages']
53
+ end
54
+
55
+ custom_json = env[:client].stack.custom_json.dup
56
+ custom_json.merge!({
57
+ 'opsworks' => {
58
+ 'activity' => 'setup',
59
+ 'agent_version' => @opsworks.agent_version,
60
+ 'sent_at' => Time.new.to_i,
61
+ 'deployment' => SecureRandom.uuid
62
+ },
63
+ 'dependencies' => {
64
+ 'debs' => Hash[pkgs.flatten.map{|x| [x, nil]}]
65
+ },
66
+ 'deploy' => apps
67
+ })
68
+
69
+ chef.json = custom_json
70
+ end
71
+ end
72
+ end
73
+
74
+ @app.pass(env)
75
+ end
76
+
77
+ end
78
+ end