Module: Fhcap::KnifeHelper

Included in:
Tasks::Chef::ChefServerTask, Tasks::Cluster::CreateDNSRecords, Tasks::TaskBase
Defined in:
lib/fhcap/knife_helper.rb

Instance Method Summary collapse

Instance Method Details

#chef_server_config_for(name) ⇒ Object



29
30
31
# File 'lib/fhcap/knife_helper.rb', line 29

def chef_server_config_for(name)
  ::Chef::Config.from_file(knife_config_file_for(name))
end

#chef_server_config_hash_for(name) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/fhcap/knife_helper.rb', line 33

def chef_server_config_hash_for(name)
  chef_server_config_for(name)
  chef_server_config = {
      chef_server_url: ::Chef::Config[:chef_server_url],
      node_name: ::Chef::Config[:node_name],
      client_key: ::Chef::Config[:client_key],
  }
end

#chef_server_namesObject



23
24
25
26
27
# File 'lib/fhcap/knife_helper.rb', line 23

def chef_server_names
  knife_config.collect do |server_name, cfg|
    server_name.to_s
  end
end

#delete_chef_object(klass, server, *args) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fhcap/knife_helper.rb', line 133

def delete_chef_object(klass, server, *args)
  begin
    suppress_stdout(!options[:verbose]) do
      klass.load_deps
      knife_command = klass.new(args)
      knife_command.config[:config_file] = knife_config_file_for(server)
      knife_command.config[:yes] = true
      knife_command.configure_chef
      knife_command.run
    end
  rescue Net::HTTPServerException => e
    response = e.response
    case response
      when Net::HTTPNotFound
        puts "The object you are looking for could not be found, assuming this ok and continuing ..."
      else
        raise e
    end
  end
end

#knife_configObject



11
12
13
# File 'lib/fhcap/knife_helper.rb', line 11

def knife_config
  config.exists? && config[:knife] ? config[:knife] : {}
end

#knife_config_dirObject



15
16
17
# File 'lib/fhcap/knife_helper.rb', line 15

def knife_config_dir
  config[:knife_dir] || File.join(config.default_dir, '.chef')
end

#knife_config_file_for(name) ⇒ Object



19
20
21
# File 'lib/fhcap/knife_helper.rb', line 19

def knife_config_file_for(name)
  File.join(knife_config_dir, "knife-#{name}.rb")
end

#knife_data_bag_delete(data_bag, item, server) ⇒ Object



128
129
130
131
# File 'lib/fhcap/knife_helper.rb', line 128

def knife_data_bag_delete(data_bag, item, server)
  thor.say " * deleting data bag '#{data_bag} #{item}' from #{server}"
  delete_chef_object(::Chef::Knife::DataBagDelete, server, data_bag, item)
end

#knife_download(repo, server, pattern) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fhcap/knife_helper.rb', line 107

def knife_download(repo, server, pattern)
  thor.say " * downloading server items to #{repo} - #{pattern} ..."
  suppress_stdout(!options[:verbose]) do
    Dir.chdir(repo) do
      ::Chef::Knife::Download.load_deps
      knife_command = ::Chef::Knife::Download.new(pattern)
      knife_command.config[:config_file] = knife_config_file_for(server)
      knife_command.config[:chef_repo_path] = repo
      knife_command.configure_chef
      knife_command.config[:recurse] = true
      knife_command.config[:purge] = true
      knife_command.run
    end
  end
end

#knife_environment_delete(env, server) ⇒ Object



123
124
125
126
# File 'lib/fhcap/knife_helper.rb', line 123

def knife_environment_delete(env, server)
  thor.say " * deleting environment '#{env}' from #{server}"
  delete_chef_object(::Chef::Knife::EnvironmentDelete, server, env)
end

#knife_upload(repo, server, pattern) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fhcap/knife_helper.rb', line 92

def knife_upload(repo, server, pattern)
  thor.say " * uploading local repo items from #{repo} - #{pattern} ..."
  suppress_stdout(!options[:verbose]) do
    Dir.chdir(repo) do
      ::Chef::Knife::Upload.load_deps
      knife_command = ::Chef::Knife::Upload.new(pattern)
      knife_command.config[:config_file] = knife_config_file_for(server)
      knife_command.config[:chef_repo_path] = repo
      knife_command.configure_chef
      knife_command.config[:recurse] = true
      knife_command.run
    end
  end
end

#local_chef_server?(cfg = {}) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/fhcap/knife_helper.rb', line 88

def local_chef_server?(cfg={})
  cfg[:chef_server] && cfg[:chef_server] == 'local'
end

#with_chef_server(cfg, &block) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/fhcap/knife_helper.rb', line 42

def with_chef_server(cfg, &block)
  ::Chef::Config.solo = false
  if local_chef_server? cfg
    with_local_chef_server(repo_dir(cfg[:repo]), &block)
  else
    block.call
  end
end

#with_local_chef_server(local_repo, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fhcap/knife_helper.rb', line 51

def with_local_chef_server(local_repo, &block)
  begin
    ::Chef::Config.from_file(knife_config_file_for('local'))
    uri = URI(::Chef::Config[:chef_server_url])
    host = uri.hostname
    port = uri.port
  rescue
    host = '127.0.0.1'
    port = 7799
  end

  zero_server = Fhcap::Tasks::Chef::ChefZeroServer.new({host: host, port: port})

  begin
    zero_server.start_chef_zero_server
    if zero_server.running?
      thor.say "[Chef Zero Server] - running at #{zero_server.url}"
    else
      thor.say "[Chef Zero Server] - Failed to start server", :red
      exit(-1)
    end
  rescue Errno::EADDRINUSE => e
    thor.say "[Chef Zero Server] - Address in use (#{host}:#{port}), assuming this is ok"
  end

  thor.say "[Chef Zero Server] - Syncing nodes from #{local_repo}"
  knife_upload(local_repo, 'local', ['/nodes'])
  knife_upload(local_repo, 'local', ['/data_bags/aws_*'])
  begin
    block.call
  ensure
    thor.say "[Chef Zero Server] - Syncing nodes to #{local_repo}"
    knife_download(local_repo, 'local', ['/nodes'])
    knife_download(local_repo, 'local', ['/data_bags/aws_*'])
  end
end