Class: Caterer::Provisioner::ChefSolo

Inherits:
Base
  • Object
show all
Includes:
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::Shell

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

Methods inherited from Base

#cleanup

Constructor Details

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

Returns a new instance of ChefSolo.



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

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.



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

def cookbooks_path
  @cookbooks_path
end

#data_bags_pathObject

Returns the value of attribute data_bags_path.



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

def data_bags_path
  @data_bags_path
end

#jsonObject

Returns the value of attribute json.



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

def json
  @json
end

#roles_pathObject

Returns the value of attribute roles_path.



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

def roles_path
  @roles_path
end

#run_listObject (readonly)

Returns the value of attribute run_list.



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

def run_list
  @run_list
end

Instance Method Details

#add_recipe(recipe) ⇒ Object

config DSL



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

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

#add_role(role) ⇒ Object



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

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

#errorsObject



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

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



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

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

  # somewhere mark (on the server) that we installed chef-solo for later uninstall

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

#installed?(server) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/caterer/provisioner/chef_solo.rb', line 81

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

#prepare(server) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
135
136
137
138
139
140
141
# File 'lib/caterer/provisioner/chef_solo.rb', line 86

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(config_data.merge(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



143
144
145
# File 'lib/caterer/provisioner/chef_solo.rb', line 143

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

#uninstall(server) ⇒ Object



147
148
149
150
151
# File 'lib/caterer/provisioner/chef_solo.rb', line 147

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

  # figure out how to uninstall chef_solo IF we installed it
end