Class: ConfigHelper

Inherits:
ChefSoloWrapper show all
Defined in:
lib/config_helper.rb

Instance Method Summary collapse

Methods inherited from ChefSoloWrapper

#download_and_run_cookbook_recipe, #run_wrapper

Constructor Details

#initialize(setup_defaults, debug, facility_log_level) ⇒ ConfigHelper

constructor method



4
5
6
7
8
9
10
11
12
13
# File 'lib/config_helper.rb', line 4

def initialize(setup_defaults, debug, facility_log_level)
  
  @setup_defaults = setup_defaults
  @debug = debug
  @facility_log_level = facility_log_level
  
  super(facility_log_level)
  
  @l = EasyLogger.new(facility_log_level)
end

Instance Method Details

#command?(command) ⇒ Boolean

accessor methods

Returns:

  • (Boolean)


17
18
19
# File 'lib/config_helper.rb', line 17

def command?(command)
  system("which #{ command} > /dev/null 2>&1")
end

#install_chefObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/config_helper.rb', line 71

def install_chef
  @l.log "Setting up Chef Solo."
  if command?('lsb_release')
    lsb_release = `lsb_release -si`.strip
  else
    @l.log "lsb_release command not found, os detection skipped.", 'debug'
    lsb_release = 'none'
  end
  case "#{lsb_release}"
    when 'Ubuntu'
      @l.log 'Ubuntu detected; installing from opscode apt.', 'debug'
      install_chef_opscode_apt
      return
    when 'CentOS'
      @l.log 'Installing native extensions build dependencies...'
      system("sudo yum -y install libxml2 libxml2-devel libxslt-devel")
      @l.log '[gem] Install Chef from RubyGem...'
      system("sudo gem install chef --no-rdoc --no-ri")
  else
    puts "    DEBUG: == RubyGems Sources ==\n#{`gem sources`}\n==\n" if @debug
    @l.log 'Installing Chef RubyGem...'
    install_rubygem('chef')
  end
end

#install_chef_opscode_aptObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/config_helper.rb', line 55

def install_chef_opscode_apt
  if ! system("dpkg -l | grep chef")
    system("DEBIAN_FRONTEND=noninteractive")
    system("sudo mkdir -p /etc/apt/trusted.gpg.d")
    system("gpg --keyserver keys.gnupg.net --recv-keys 83EF826A")
    system("gpg --export [email protected] | sudo tee /etc/apt/trusted.gpg.d/opscode-keyring.gpg > /dev/null")
    system('echo "deb http://apt.opscode.com/ $(lsb_release -cs)-0.10 main" > /etc/apt/sources.list.d/opscode.list')
    system("sudo apt-get -y update")
    system("sudo apt-get -y upgrade")
    system("sudo apt-get -y install libxml2 libxml2-dev libxslt-dev")
    system("sudo apt-get -y install chef")
  else
    @l.log "[dpkg] Chef already installed, skipping."
  end
end

#install_gem(gem) ⇒ Object



21
22
23
24
25
# File 'lib/config_helper.rb', line 21

def install_gem(gem)
  install_opts = '--no-rdoc --no-ri'
  system("gem install #{gem} #{install_opts}")
  Gem.clear_paths
end

#install_rest_connection(auto = false) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/config_helper.rb', line 162

def install_rest_connection(auto=false)
  @l.log 'Setting up RestConnection.'
  begin
    if auto or @setup_defaults
      install_rc = 'y'
    else
      puts 'Install rest_connection [y/n] <enter> ?'
      install_rc = 'n'
      install_rc = gets.chomp
    end
    if install_rc.downcase == 'y'
      @l.log 'Installing rest_connection RubyGem...'
      # installing deps for native extensions building
      # manual libiconv install
      
      system("(type -P yum >/dev/null && sudo yum -y install libxml2 libxml2-devel libxslt-devel rubygem-nokogiri) || (type -P apt-get >/dev/null && sudo apt-get -y install libxml2 libxml2-dev libxslt-dev)")        
      install_rubygem('rest_connection')
    end
  end
end

#install_rubygem(gem) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/config_helper.rb', line 27

def install_rubygem(gem)
  begin
     @l.log "[Gem] #{gem} already installed, version: #{Gem::Specification.find_by_name(gem).version}."
  rescue Gem::LoadError
    install_gem(gem)
  rescue
    install_gem(gem) unless Gem.available?(gem)
  rescue
    raise "Failed to install #{chef} Rubygem!"
  end
end

#pre_checksObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/config_helper.rb', line 195

def pre_checks
  # ensure a solo.rb exists for run
  if File.file?('/etc/chef/solo.rb')
    solo = '/etc/chef/solo.rb'
  else
    @l.log "/etc/chef/solo.rb: not found.", 'debug'
  end
  if File.file?("#{ENV['HOME']}/solo.rb")
    solo = "#{ENV['HOME']}/solo.rb"
    @l.log "Using #{ENV['HOME']}/solo.rb as preferred.", 'debug'     
  end
  unless solo
    raise 'FATAL: No solo.rb file found (see http://wiki.opscode.com/display/chef/Chef+Solo), exiting.'
    exit 1
  else
    @l.log "DEBUG: Using #{solo}.", 'debug'
    if File.zero?(solo) then
      raise "FATAL: #{solo} is empty, exiting."
      exit 1
    end 
    @l.log "== solo.rb ==\n#{File.new(solo, 'r').read.strip}\n==", 'debug'
  end
end

#setup_node_json(file, auto = @setup_defaults) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/config_helper.rb', line 136

def setup_node_json(file, auto=@setup_defaults)
  @l.log "Setting up #{file}."
  if auto
    create_empty = 'y'
  else
    puts 'Create empty node.json or edit existing [y/n/e] <enter> ?'
    create_empty = 'n'
    create_empty = gets.chomp
  end
  if create_empty.downcase == 'y'
    json = '{}'
  elsif create_empty == 'e'
    editor = `which nano`.strip   # fallback
    editor = ENV['EDITOR'] if ENV['EDITOR']
    puts "    DEBUG: Using #{editor}"
    system("#{editor} /etc/chef/node.json")
    return      
  else
    puts "Type or paste your node.json (type EOF then press <enter> to finish):"
    $/ = "EOF"
    json = STDIN.gets
  end
  Dir.mkdir('/etc/chef') unless File.exists?('/etc/chef')
  File.open(file, "w") {|f| f.write json.gsub('EOF', '')}
end

#setup_solo_rb(file, auto = @setup_defaults) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/config_helper.rb', line 109

def setup_solo_rb(file, auto=@setup_defaults)
  @l.log "Setting up #{file}."
  if auto
    default_solo = 'y'
  else
    puts '  Use default solo.rb or edit existing [y/n/e] <enter> ?'
    default_solo = 'n'
    default_solo = gets.chomp
  end
  if default_solo == 'y'
    File.open(file, "w") {|f| f.write 'file_cache_path "/var/chef-solo"'+"\n"+'cookbook_path [ "/usr/src/chef-cookbooks/default" ]'+"\n"'json_attribs "/etc/chef/node.json"'+"\n"}
    system("mkdir -p /usr/src/chef-cookbooks/default")
    File.new('/usr/src/chef-cookbooks/default/chefignore', "w").close
    return
  elsif default_solo == 'e'
    editor = `which nano`.strip   # fallback
    editor = ENV['EDITOR'] if ENV['EDITOR']
    puts "    DEBUG: Using #{editor}"
    system("#{editor} /etc/chef/solo.rb")
    return
  end
  puts "  Type or paste your solo.rb (type EOF then press <enter> to finish):"
  $/ = "EOF"
  stdin = STDIN.gets
  File.open(file, "w") {|f| f.write stdin }
end

#setup_solo_rb_sandbox(file, sandbox_version = 5.8) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/config_helper.rb', line 96

def setup_solo_rb_sandbox(file, sandbox_version=5.8)
  raise "RightScale cookbooks cache, /var/cache/rightscale/cookbooks not found!" unless File.exists?('/var/cache/rightscale/cookbooks')
  @l.log "Setting up #{file}."
  if sandbox_version.to_s == '5.8'
    cookbooks_cache = '/var/cache/rightscale/cookbooks/default'
  else
    cookbooks_cache = '/var/cache/rightscale/cookbooks'
  end
  system('mkdir -p /etc/chef')
  cookbooks_path = Dir.glob("#{cookbooks_cache}/*").map {|element| "\"#{element}/cookbooks\"" }.join(', ')
  File.open(file, "w") {|f| f.write 'file_cache_path "/var/chef-solo"'+"\n"+'cookbook_path [ '+"#{cookbooks_path}"+' ]'+"\n"'json_attribs "/etc/chef/node.json"'+"\n"}
end

#show(solo_file, json_file) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/config_helper.rb', line 39

def show(solo_file, json_file)
  puts
  puts '* Chef Solo Setup *'
  puts 
  puts "#{solo_file}:"
  puts '--'
  puts File.open(solo_file, "r").read
  puts '--'
  puts
  puts "#{json_file}:"
  puts '--'
  puts File.open(json_file, "r").read
  puts '--'
  puts
end

#test_setupObject



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/config_helper.rb', line 183

def test_setup
  begin
    @l.log 'Testing require of chef.'
    require 'chef'
  rescue
    @l.log 'Failed to require Chef RubyGem!'
    exit 1
  end
  @l.log 'Test passed.'
  exit
end