Class: Caterer::Provisioner::ChefSolo

Inherits:
Base
  • Object
show all
Includes:
Util::Hash, Util::Shell
Defined in:
lib/caterer/provisioner/chef_solo.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#image

Instance Method Summary collapse

Methods included from Util::Hash

#deep_merge

Methods included from Util::Shell

#bash, #env, #env_string, #escape, #su

Methods inherited from Base

#cleanup

Constructor Details

#initialize(image, opts = {}) ⇒ ChefSolo

Returns a new instance of ChefSolo.



18
19
20
21
22
23
24
25
# File 'lib/caterer/provisioner/chef_solo.rb', line 18

def initialize(image, opts={})
  @image          = image
  @run_list       = provisioner_config.run_list.dup
  @json           = provisioner_config.json.dup
  @cookbooks_path = provisioner_config.cookbooks_path.dup
  @roles_path     = provisioner_config.roles_path.dup
  @data_bags_path = provisioner_config.data_bags_path.dup
end

Instance Attribute Details

#cookbooks_pathObject

Returns the value of attribute cookbooks_path.



15
16
17
# File 'lib/caterer/provisioner/chef_solo.rb', line 15

def cookbooks_path
  @cookbooks_path
end

#data_bags_pathObject

Returns the value of attribute data_bags_path.



16
17
18
# File 'lib/caterer/provisioner/chef_solo.rb', line 16

def data_bags_path
  @data_bags_path
end

#jsonObject

Returns the value of attribute json.



15
16
17
# File 'lib/caterer/provisioner/chef_solo.rb', line 15

def json
  @json
end

#roles_pathObject

Returns the value of attribute roles_path.



15
16
17
# File 'lib/caterer/provisioner/chef_solo.rb', line 15

def roles_path
  @roles_path
end

#run_listObject (readonly)

Returns the value of attribute run_list.



14
15
16
# File 'lib/caterer/provisioner/chef_solo.rb', line 14

def run_list
  @run_list
end

Instance Method Details

#add_recipe(recipe) ⇒ Object

config DSL



29
30
31
# File 'lib/caterer/provisioner/chef_solo.rb', line 29

def add_recipe(recipe)
  @run_list << "recipe[#{recipe}]"
end

#add_role(role) ⇒ Object



33
34
35
# File 'lib/caterer/provisioner/chef_solo.rb', line 33

def add_role(role)
  @run_list << "role[#{role}]"
end

#errorsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/caterer/provisioner/chef_solo.rb', line 37

def errors
  errors = {}

  if not @run_list.length > 0
    errors[:run_list] = "is empty"
  end

  if not @cookbooks_path.is_a? Array
    errors[:cookbooks_path] = "must be an array"
  end

  if not @roles_path.is_a? Array
    errors[:roles_path] = "must be an array"
  end

  if not @data_bags_path.is_a? Array
    errors[:data_bags_path] = "must be an array"
  end

  if errors.length > 0
    errors
  end
end

#install(server) ⇒ Object

provision engine



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/caterer/provisioner/chef_solo.rb', line 63

def install(server)
  server.ui.info "Preparing installation..."

  installer = install_script(server.platform)

  if not File.exists? installer
    server.ui.error "#{server.platform} doesn't have an install script"
    return
  end

  res = server.ssh.sudo bash(File.read(installer)), :stream => true

  # mark (on the server) that we installed chef-solo for later uninstall
  server.ssh.sudo "touch #{dest_var_dir}/INSTALL"

  unless res == 0
    server.ui.error "install failed with exit code: #{res}"
  end
end

#installed?(server) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/caterer/provisioner/chef_solo.rb', line 83

def installed?(server)
  res = server.ssh.sudo "command -v chef-solo &>/dev/null"
  res == 0 ? true : false
end

#prepare(server) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
161
162
163
164
165
166
# File 'lib/caterer/provisioner/chef_solo.rb', line 111

def prepare(server)
  # create lib dir
  server.ssh.sudo "mkdir -p #{dest_lib_dir}", :stream => true
  server.ssh.sudo "chown -R #{server.username} #{dest_lib_dir}", :stream => true

  # create var dir
  server.ssh.sudo "mkdir -p #{dest_var_dir}", :stream => true
  server.ssh.sudo "chown -R #{server.username} #{dest_var_dir}", :stream => true

  # create cache dir
  server.ssh.sudo "mkdir -p #{dest_cache_dir}", :stream => true
  server.ssh.sudo "chown -R #{server.username} #{dest_cache_dir}", :stream => true

  # create cookbooks directory
  server.ssh.sudo "mkdir -p #{target_cookbooks_path}", :stream => true
  server.ssh.sudo "chown -R #{server.username} #{target_cookbooks_path}", :stream => true

  # sync cookbooks
  server.ui.info "Syncing cookbooks..."
  cookbooks_path.each do |path|
    server.upload_directory path, "#{target_cookbooks_path}/#{Digest::MD5.hexdigest(path)}"
  end

  # create roles directory
  server.ssh.sudo "mkdir -p #{target_roles_path}", :stream => true
  server.ssh.sudo "chown -R #{server.username} #{target_roles_path}", :stream => true

  # sync roles
  server.ui.info "Syncing roles..."
  roles_path.each do |path|
    server.upload_directory path, target_roles_path
  end

  # create data_bags directory
  server.ssh.sudo "mkdir -p #{target_data_bags_path}", :stream => true
  server.ssh.sudo "chown -R #{server.username} #{target_data_bags_path}", :stream => true

  # sync databags
  server.ui.info "Syncing data bags..."
  data_bags_path.each do |path|
    server.upload_directory path, target_data_bags_path
  end

  # create solo.rb
  server.ui.info "Generating solo.rb..."
  server.ssh.upload(StringIO.new(solo_content(server)), target_solo_path)

  # create json
  server.ui.info "Generating json config..."
  server.ssh.upload(StringIO.new(json_config(deep_merge(config_data, server.data))), target_json_config_path)

  # set permissions on everything
  server.ssh.sudo "chown -R #{server.username} #{dest_lib_dir}", :stream => true
  server.ssh.sudo "chown -R #{server.username} #{dest_var_dir}", :stream => true

end

#provision_cmdObject



168
169
170
# File 'lib/caterer/provisioner/chef_solo.rb', line 168

def provision_cmd
  "chef-solo -c #{target_solo_path} -j #{target_json_config_path}"
end

#uninstall(server) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/caterer/provisioner/chef_solo.rb', line 88

def uninstall(server)
  server.ui.info "Uninstalling..."

  # figure out how to uninstall chef_solo IF we installed it
  installed = server.ssh.sudo "[[ -f #{dest_var_dir}/INSTALL ]]"

  if installed == 0
    uninstaller = uninstall_script(server.platform)

    if not File.exists? uninstaller
      server.ui.error "#{server.platform} doesn't have an uninstall script"
      return
    end

    res = server.ssh.sudo bash(File.read(uninstaller)), :stream => true

    unless res == 0
      server.ui.error "install failed with exit code: #{res}"
    end
  end

end