Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added app variables pulled in from databag

  • Loading branch information...
commit 7d16c47b79ec56fae9cae2d5b2536d751f05ffc9 1 parent 72845df
Amos Lanka authored

Showing 1 changed file with 152 additions and 66 deletions. Show diff stats Hide diff stats

  1. 218  lib/capistrano/chef-solo.rb
218  lib/capistrano/chef-solo.rb
@@ -47,7 +47,6 @@ def self.load_into(configuration)
47 47
       
48 48
       set :application, nil
49 49
       set :environment, nil
50  
-      set :node, nil
51 50
       
52 51
       # Automatically loads all applications 
53 52
       def _register_chef_applications(options={})
@@ -63,12 +62,12 @@ def _register_chef_applications(options={})
63 62
             end
64 63
           end
65 64
           load_application app_name
66  
-          puts "Registering the only application: #{app_name}"
  65
+          # puts "Registering the only application: #{app_name}"
67 66
         else 
68 67
           apps.each do |name, data|
69 68
             # Define a root task for each application
70 69
             app_name = data['id'] || name
71  
-            puts "Registering an application: #{app_name}"
  70
+            # puts "Registering an application: #{app_name}"
72 71
             namespace :app do
73 72
               desc "Apply to the #{app_name} application"
74 73
               task app_name.to_sym do
@@ -79,26 +78,58 @@ def _register_chef_applications(options={})
79 78
         end
80 79
       end
81 80
       def load_application(name)
  81
+        raise "Really, you should only ever load an application once in a cap call." if application
82 82
         set :application, name
83  
-        ::Chef::DataBagItem.load("apps", name).each do |k, v|
84  
-          set k, v
  83
+        bag = ::Chef::DataBagItem.load("apps", name)
  84
+        
  85
+        # Set primary params from the app
  86
+        set_param_from_app :deploy_to
  87
+        set_param_from_app :deploy_via, nil, :remote_cache
  88
+        set_param_from_app :user, :owner
  89
+        set_param_from_app :runner, %w{user owner}
  90
+        set_param_from_app :repository, :repo
  91
+        set_param_from_app :scm
  92
+
  93
+        # How to authenticate deployment. If we have a deploy key in the app, we can simply 
  94
+        # pull in our deploy key specs, and carry on without using a password at all.
  95
+        # But if we don't have one, our second guess is to forward local agent to the server
  96
+        # and expect that there's a key between local machine and scm server. 
  97
+        if false && key = (bag.keys & %w{deploy_key deploy_ssh_key scm_key})
  98
+          puts "Found a deploy key. Now we should have a deploy key wrapper"
  99
+          set :use_sudo, false
  100
+        else
  101
+          # Assumes we'll use forwarded agents for scm repo authentication. No need to attach 
  102
+          set :scm_verbose, true
  103
+          set :scm_username, `whoami`
  104
+          ssh_options[:forward_agent] = true
  105
+          ssh_options[:paranoid] = false
85 106
         end
  107
+        
  108
+        assign_roles
86 109
       end
87 110
       
88  
-      # Loads all nodes
89  
-      def _register_chef_nodes(options={})
90  
-        get_nodes.each do |chef_node|
91  
-          puts "Registering a node: #{chef_node.name}"
92  
-          namespace :chef_node do
93  
-            desc "Apply to the #{chef_node.name} node"
94  
-            task chef_node.name do
95  
-              load_chef_node chef_node.name
96  
-              autoset_environment
97  
-            end
98  
-          end
99  
-        end
  111
+      def set_param_from_app(param_name, aliases=[], default=nil)
  112
+        bag = ::Chef::DataBagItem.load("apps", application)
  113
+        key = ((Array(param_name) + Array(aliases)).collect { |k| k.to_s rescue k } & bag.keys).first
  114
+        val = bag[key.to_s]
  115
+        val = default if val.nil?
  116
+        set param_name.to_sym, val unless val.nil?
100 117
       end
101 118
       
  119
+      # Loads all nodes
  120
+      # def _register_chef_nodes(options={})
  121
+      #   get_nodes.each do |chef_node|
  122
+      #     puts "Registering a node: #{chef_node.name}"
  123
+      #     namespace :chef_node do
  124
+      #       desc "Apply to the #{chef_node.name} node"
  125
+      #       task chef_node.name do
  126
+      #         load_chef_node chef_node.name
  127
+      #         autoset_environment
  128
+      #       end
  129
+      #     end
  130
+      #   end
  131
+      # end
  132
+      
102 133
       # Loads all environments
103 134
       def _register_chef_environments(options={})
104 135
         
@@ -125,58 +156,74 @@ def _register_chef_environments(options={})
125 156
         end
126 157
         
127 158
         list.compact.uniq.each do |env_name|
128  
-          puts "Registering an environment: #{env_name}"
  159
+          # puts "Registering an environment: #{env_name}"
129 160
           
130 161
           # Sets the environment tasks
131 162
           desc "Apply to the #{env_name} environment"
132 163
           task env_name.to_sym do
133  
-            set :environment, env_name
  164
+            set_environment env_name
134 165
           end
135 166
         end
136 167
       end
137 168
 
  169
+      task :chef_report do
  170
+        _chef_report
  171
+      end
138 172
       def _chef_report
139  
-        task :chef_report do
140  
-          puts "\nChef Report:"
141  
-          puts "  application: #{application}"
142  
-          puts "  environment: #{environment}"
143  
-          puts "  node:        #{node}"
144  
-          
145  
-          puts "  deploy_to:   #{deploy_to}"
  173
+        puts "\nChef Report:"
  174
+        puts "  application: #{application}"
  175
+        puts "  environment: #{environment}"
  176
+        # puts "  node:        #{node}"
  177
+        
  178
+        
  179
+        puts "  Cap roles: "
  180
+        roles.each do |role_name, r|
  181
+          puts "    #{role_name}: #{r.servers.join(', ')}"
  182
+        end
  183
+        
  184
+        puts "  Necessary deployment params:"
  185
+        %w{deploy_to user runner scm repository branch rails_env deploy_via use_sudo}.each do |attrib|
146 186
           
147  
-          puts ""
  187
+          puts "    #{attrib}: #{self[attrib.to_sym] if exists?(attrib)}" 
148 188
         end
149  
-      end
150  
-      
151  
-      
152  
-      # before 'deploy', 'chef:crossref'
  189
+        # puts "    deploy_to:   #{deploy_to}"
  190
+        # puts "    runner:      #{runner}"
  191
+        # puts "    scm:         #{scm}"
  192
+        # puts "    repository:  #{repository}"
  193
+        # puts "    branch:      #{branch}"
153 194
 
154  
-      def load_chef_node(name)
155  
-        set :node, name
156  
-        # Try to auto populate some more variables, such as ip address.
  195
+        puts ""
157 196
         
158 197
       end
159 198
 
160  
-      def autoset_environment
161  
-        set :environment, get_node_by_name(node).chef_environment if node
162  
-      end
163  
-      def autoset_node
164  
-        return unless application && environment
165  
-        
166  
-        get_nodes.each do |node|
167  
-          next if node.chef_environment != environment
168  
-          
169  
-          
170  
-          puts "...."
171  
-          puts node.name
172  
-          puts node.run_list
173  
-          puts node.chef_environment
174  
-          
175  
-          
176  
-        end
177  
-        
  199
+      def set_environment(env_name)
  200
+        # set :environment, get_node_by_name(node).chef_environment if node
  201
+        set :environment, env_name
  202
+        set :rails_env, env_name
  203
+
  204
+        # Set some environment specific params
  205
+        bag = ::Chef::DataBagItem.load("apps", application)
  206
+        set :branch, bag['revision'][environment]
  207
+
  208
+        assign_roles
178 209
       end
179 210
       
  211
+      def assign_roles
  212
+        return unless environment && application
  213
+        bag = ::Chef::DataBagItem.load("apps", application)
  214
+        # Set db roles
  215
+        db_roles = %w{database_master_role database_slave_role database_role}
  216
+        chef_db_roles(bag.values_at(*db_roles).flatten.compact)
  217
+        # Set web roles
  218
+        web_roles = %w{web_master_role web_slave_role web_role}
  219
+        chef_web_roles(bag.values_at(*db_roles).flatten.compact)
  220
+        # Set app roles
  221
+        app_roles = %w{app_master_role app_slave_role app_role}
  222
+        chef_app_roles(bag.values_at(*db_roles).flatten.compact)
  223
+
  224
+        # Report whats going to be used.
  225
+        _chef_report
  226
+      end
180 227
       
181 228
 
182 229
       
@@ -199,6 +246,28 @@ def get_node_by_name(name)
199 246
         end.first
200 247
       end
201 248
       
  249
+      def find_nodes(options={})
  250
+        # puts "Find nodes. #{options}"
  251
+        get_nodes.select do |node|
  252
+          valid = true
  253
+          
  254
+          if options.has_key?(:name)
  255
+            valid = valid && node.name == options[:name]
  256
+          end
  257
+          if options.has_key?(:role)
  258
+            valid = valid && node.role?(options[:role])
  259
+          end
  260
+          if options.has_key?(:environment)
  261
+            valid = valid && node.chef_environment == options[:environment]
  262
+          end
  263
+          if options.has_key?(:recipe)
  264
+            valid = valid && node.recipe?(options[:recipe])
  265
+          end
  266
+          
  267
+          valid
  268
+        end
  269
+      end
  270
+      
202 271
       # def get_proper_node
203 272
       #   raise "You must first specify an application and and an environment" unless application && environment
204 273
       #   get_nodes.each do |node|
@@ -212,23 +281,40 @@ def get_node_by_name(name)
212 281
       #     
213 282
       #   end
214 283
       # end
  284
+      
  285
+      def chef_app_roles(chef_role_names)
  286
+        assign_capistrano_roles(:app, chef_role_names)
  287
+      end
  288
+      def chef_web_roles(chef_role_names)
  289
+        assign_capistrano_roles(:web, chef_role_names)
  290
+      end
  291
+      def chef_db_roles(chef_role_names)
  292
+        assign_capistrano_roles(:db, chef_role_names)
  293
+      end
  294
+      def assign_capistrano_roles(capistrano_role, chef_role_names)
  295
+        Array(chef_role_names).each do |name|
  296
+          hosts = find_nodes(:environment => environment, :role => name).collect do |node|
  297
+            key = (%w{domain ip_address ip ipaddress} & node.keys).first
  298
+            node[key] || node.name
  299
+          end
  300
+          role capistrano_role.to_sym, *hosts
  301
+        end
  302
+      end
  303
+      
  304
+      
  305
+      # before "deploy:cold", "doublecheck_param_exist_from_chef"
  306
+      # 
  307
+      # task :doublecheck_param_exist_from_chef do
  308
+      #   %w{deploy_via}.each do |attrib|
  309
+      #     raise "The param \"#{attrib}\" is required for deployment." unless exists?(attrib)
  310
+      #     puts " >> #{attrib}: #{self[attrib.to_sym] if exists?(attrib)}" 
  311
+      #   end
  312
+      # 
  313
+      # end
  314
+      
215 315
         
216 316
       _register_chef_applications
217 317
       _register_chef_environments
218  
-      _register_chef_nodes
219  
-      _chef_report
220  
-
221  
-
222  
-
223  
-
224  
-      def chef_db_roles(names)
225  
-        names = [names] unless names.is_a?(Array)
226  
-        
227  
-        
228  
-        
229  
-      end
230  
-
231  
-
232 318
       
233 319
     end
234 320
   end

0 notes on commit 7d16c47

Please sign in to comment.
Something went wrong with that request. Please try again.