diff options
| author | mo <mokha@cisco.com> | 2017-05-04 17:40:28 -0600 |
|---|---|---|
| committer | mo <mokha@cisco.com> | 2017-05-04 17:40:28 -0600 |
| commit | 4b783fab395c2dd5b0180e4667995c852fd4b13f (patch) | |
| tree | cb83c57356b7f0ea7519103f99837a1a83b6b566 | |
| parent | 8e7c2ac65493309de4098be6c72f123e28fd2441 (diff) | |
use chef to search for nodes to deploy to.
| -rw-r--r-- | Capfile | 2 | ||||
| -rw-r--r-- | Gemfile | 9 | ||||
| -rw-r--r-- | Gemfile.lock | 102 | ||||
| -rw-r--r-- | config/deploy.rb | 3 | ||||
| -rw-r--r-- | config/deploy/production.rb | 46 | ||||
| -rw-r--r-- | lib/capistrano/chef_search.rb | 58 |
6 files changed, 172 insertions, 48 deletions
@@ -1,4 +1,5 @@ # Load DSL and Setup Up Stages +$LOAD_PATH.unshift("#{Dir.pwd}/lib") require 'capistrano/setup' # Includes default deployment tasks @@ -20,6 +21,7 @@ require 'capistrano/rbenv' require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' +require 'capistrano/chef_search' # Loads custom tasks from `lib/capistrano/tasks' if you have any defined. Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } @@ -48,16 +48,17 @@ gem 'font-awesome-rails' gem 'bootstrap-sass', '~> 2.3.0' group :development do + gem 'better_errors' + gem 'bullet' gem 'capistrano', '3.3.5' - gem 'capistrano-rails' gem 'capistrano-bundler', '~> 1.1', require: false + gem 'capistrano-rails' gem 'capistrano-rbenv', '~> 2.0', require: false - gem 'bullet' - gem 'better_errors' + gem 'chef' gem 'lol_dba' gem 'pry-rails' - gem 'web-console', '~> 2.0' gem 'spring-commands-rspec' + gem 'web-console', '~> 2.0' end group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index b1111514..a01bd256 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -99,6 +99,46 @@ GEM rack-test (>= 0.5.4) xpath (~> 2.0) chartkick (2.2.3) + chef (12.20.3) + addressable + bundler (>= 1.10) + chef-config (= 12.20.3) + chef-zero (>= 4.8) + diff-lcs (~> 1.2, >= 1.2.4) + erubis (~> 2.7) + ffi-yajl (~> 2.2) + highline (~> 1.6, >= 1.6.9) + iniparse (~> 1.4) + mixlib-archive (~> 0.4) + mixlib-authentication (~> 1.4) + mixlib-cli (~> 1.7) + mixlib-log (~> 1.3) + mixlib-shellout (~> 2.0) + net-sftp (~> 2.1, >= 2.1.2) + net-ssh (>= 2.9, < 5.0) + net-ssh-multi (~> 1.2, >= 1.2.1) + ohai (>= 8.6.0.alpha.1, < 13) + plist (~> 3.2) + proxifier (~> 1.0) + rspec-core (~> 3.5) + rspec-expectations (~> 3.5) + rspec-mocks (~> 3.5) + rspec_junit_formatter (~> 0.2.0) + serverspec (~> 2.7) + specinfra (~> 2.10) + syslog-logger (~> 1.6) + uuidtools (~> 2.1.5) + chef-config (12.20.3) + addressable + fuzzyurl + mixlib-config (~> 2.0) + mixlib-shellout (~> 2.0) + chef-zero (4.9.0) + ffi-yajl (~> 2.2) + hashie (>= 2.0, < 4.0) + mixlib-log (~> 1.3) + rack (< 2) + uuidtools (~> 2.1) cliver (0.3.2) coderay (1.1.1) coffee-rails (4.1.1) @@ -154,6 +194,9 @@ GEM faraday (0.11.0) multipart-post (>= 1.2, < 3) ffaker (2.5.0) + ffi (1.9.18) + ffi-yajl (2.3.0) + libyajl2 (~> 1.2) fog-aws (1.3.0) fog-core (~> 1.38) fog-json (~> 1.0) @@ -174,6 +217,7 @@ GEM foreman (0.84.0) thor (~> 0.19.1) formatador (0.2.5) + fuzzyurl (0.9.0) geoip (1.6.3) gibbon (3.0.1) faraday (>= 0.9.1) @@ -208,6 +252,7 @@ GEM parser (>= 2.2.3.0) rainbow (~> 2.2) terminal-table (>= 1.5.1) + iniparse (1.4.2) ipaddress (0.8.3) jbuilder (2.6.3) activesupport (>= 3.0.0, < 5.2) @@ -239,6 +284,7 @@ GEM kaminari-core (= 1.0.1) kaminari-core (1.0.1) kgio (2.11.0) + libyajl2 (1.2.0) lol_dba (2.1.2) actionpack (>= 3.0) activerecord (>= 3.0) @@ -256,22 +302,52 @@ GEM mini_magick (4.7.0) mini_portile2 (2.1.0) minitest (5.10.1) + mixlib-archive (0.4.1) + mixlib-log + mixlib-authentication (1.4.1) + mixlib-log + mixlib-cli (1.7.0) + mixlib-config (2.2.4) + mixlib-log (1.7.1) + mixlib-shellout (2.2.7) multi_json (1.12.1) multipart-post (2.0.0) naught (1.1.0) net-scp (1.2.1) net-ssh (>= 2.6.5) + net-sftp (2.1.2) + net-ssh (>= 2.6.5) net-ssh (4.1.0) + net-ssh-gateway (2.0.0) + net-ssh (>= 4.0.0) + net-ssh-multi (1.2.1) + net-ssh (>= 2.6.5) + net-ssh-gateway (>= 1.2.0) + net-telnet (0.1.1) nokogiri (1.7.1) mini_portile2 (~> 2.1.0) + ohai (8.23.0) + chef-config (>= 12.5.0.alpha.1, < 13) + ffi (~> 1.9) + ffi-yajl (~> 2.2) + ipaddress + mixlib-cli + mixlib-config (~> 2.0) + mixlib-log (>= 1.7.1, < 2.0) + mixlib-shellout (~> 2.0) + plist (~> 3.1) + systemu (~> 2.6.4) + wmi-lite (~> 1.0) parser (2.4.0.0) ast (~> 2.2) pg (0.20.0) phantomjs (2.1.1.0) + plist (3.3.0) poltergeist (1.15.0) capybara (~> 2.1) cliver (~> 0.3.1) websocket-driver (>= 0.2.0) + proxifier (1.0.3) pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -314,11 +390,18 @@ GEM rake raindrops (0.18.0) rake (12.0.0) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) rspec-core (3.5.4) rspec-support (~> 3.5.0) rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.5.0) + rspec-its (1.2.0) + rspec-core (>= 3.0.0) + rspec-expectations (>= 3.0.0) rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.5.0) @@ -331,6 +414,9 @@ GEM rspec-mocks (~> 3.5.0) rspec-support (~> 3.5.0) rspec-support (3.5.0) + rspec_junit_formatter (0.2.3) + builder (< 4) + rspec-core (>= 2, < 4, != 2.12.0) safe_yaml (1.0.4) sass (3.4.23) sass-rails (5.0.6) @@ -339,6 +425,12 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + serverspec (2.38.0) + multi_json + rspec (~> 3.0) + rspec-its + specinfra (~> 2.53) + sfl (2.3) simple_oauth (0.3.1) simplecov (0.14.1) docile (~> 1.1.0) @@ -350,6 +442,11 @@ GEM hashie (~> 3.0) socksify (1.7.1) spank (1.0.1441140881) + specinfra (2.67.9) + net-scp + net-ssh (>= 2.7, < 5.0) + net-telnet + sfl spring (2.0.1) activesupport (>= 4.2) spring-commands-rspec (1.0.4) @@ -364,6 +461,8 @@ GEM sshkit (1.13.1) net-scp (>= 1.1.2) net-ssh (>= 2.8.0) + syslog-logger (1.6.8) + systemu (2.6.5) teaspoon (1.1.5) railties (>= 3.2.5, < 6) teaspoon-jasmine (2.3.4) @@ -399,6 +498,7 @@ GEM kgio (~> 2.6) raindrops (~> 0.7) uniform_notifier (1.10.0) + uuidtools (2.1.5) web-console (2.3.0) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) @@ -407,6 +507,7 @@ GEM websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.2) + wmi-lite (1.0.0) xpath (2.0.0) nokogiri (~> 1.3) @@ -430,6 +531,7 @@ DEPENDENCIES capistrano-rails capistrano-rbenv (~> 2.0) chartkick + chef coffee-rails (~> 4.1.0) curb daemons diff --git a/config/deploy.rb b/config/deploy.rb index d3e78eb6..2880411f 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -2,9 +2,8 @@ lock '3.3.5' set :application, 'cakeside' -set :repo_url, 'git@github.com:cakeside/cakeside.git' +set :repo_url, 'git@gitlab.com:cakeside/cakeside.git' set :branch, 'master' - set :user, "deployer" # Default branch is :master diff --git a/config/deploy/production.rb b/config/deploy/production.rb index e3fc9ef9..41355af9 100644 --- a/config/deploy/production.rb +++ b/config/deploy/production.rb @@ -1,46 +1,8 @@ -# Simple Role Syntax -# ================== -# Supports bulk-adding hosts to roles, the primary server in each group -# is considered to be the first unless any hosts have the primary -# property set. Don't declare `role :all`, it's a meta role. +servers = ChefSearch.new("production") -role :app, %w{deployer@green.cakeside.com} -role :web, %w{deployer@green.cakeside.com} -role :db, %w{deployer@green.cakeside.com} +role :app, servers.find_by(:app).map { |x| "#{fetch(:user)}@#{x}" } +role :web, servers.find_by(:www).map { |x| "#{fetch(:user)}@#{x}" } +role :db, servers.find_by(:db).map { |x| "#{fetch(:user)}@#{x}" } - -# Extended Server Syntax -# ====================== -# This can be used to drop a more detailed server definition into the -# server list. The second argument is a, or duck-types, Hash and is -# used to set extended properties on the server. - -server 'green.cakeside.com', user: 'deployer', roles: %w{web app db} set :rails_env, 'production' set :domain, 'www.cakeside.com' - -# Custom SSH Options -# ================== -# You may pass any option but keep in mind that net/ssh understands a -# limited set of options, consult[net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start). -# -# Global options -# -------------- -# set :ssh_options, { -# keys: %w(/home/rlisowski/.ssh/id_rsa), -# forward_agent: false, -# auth_methods: %w(password) -# } -# -# And/or per server (overrides global) -# ------------------------------------ -# server 'example.com', -# user: 'user_name', -# roles: %w{web app}, -# ssh_options: { -# user: 'user_name', # overrides user setting above -# keys: %w(/home/user_name/.ssh/id_rsa), -# forward_agent: false, -# auth_methods: %w(publickey password) -# # password: 'please use keys' -# } diff --git a/lib/capistrano/chef_search.rb b/lib/capistrano/chef_search.rb new file mode 100644 index 00000000..b045affb --- /dev/null +++ b/lib/capistrano/chef_search.rb @@ -0,0 +1,58 @@ +require 'chef' +require 'chef-config/workstation_config_loader' + +class ChefSearch + attr_reader :chef_environment + + def initialize(chef_environment = fetch(:chef_env_name)) + @chef_environment = chef_environment + load_configuration + end + + def find_by(role, &filter) + hosts, _, count = search(:node, query_for(role), filter_result: filters) + hostnames = hostnames_from(hosts, &filter) + puts "Using #{hostnames.count}/#{count} hosts for role: #{role}" + hostnames.each { |x| puts " #{x}" } + hostnames + end + + private + + def filters + { 'fqdn' => ['fqdn'], 'ip' => [ 'ec2', 'public_ipv4' ] } + end + + def query_for(role) + [ + "chef_environment:#{chef_environment}", + "roles:#{role}", + ].join(" AND ") + end + + def search(*params) + Chef::Search::Query.new.search(*params) + end + + def hostnames_from(hosts, &filter) + hostnames = hosts.map { |x| x['ip'] }.compact.sort + if filter.nil? + hostnames + else + hostnames = hostnames.find_all do |hostname| + yield hostname + end + end + end + + def load_configuration(system_knife_rb = '/etc/chef/client.pem') + if File.readable?(system_knife_rb) + puts "Loading chef config from #{system_knife_rb}" + Chef::Config.from_file(system_knife_rb) + else + @loader = ChefConfig::WorkstationConfigLoader.new(nil, nil) + puts "Loading chef config from #{@loader.config_location}" + @loader.load + end + end +end |
