Class: Fhcap::Tasks::Chef::ChefServerTask

Inherits:
ChefTaskBase show all
Defined in:
lib/fhcap/tasks/chef/chef_server_task.rb

Instance Attribute Summary collapse

Attributes inherited from TaskBase

#config, #options, #thor, #verbose

Instance Method Summary collapse

Methods inherited from ChefTaskBase

#knife_config_dir, #knife_config_file_for, #run_chef_client_cmd, #run_knife_cmd, #run_knife_ssh_cmd

Methods inherited from TaskBase

#ask_config, #color_pad, #exit_with_error, #set_color, #suppress_stdout, #table_header, #table_row, #with_progress

Methods included from KnifeHelper

#chef_server_config_for, #chef_server_config_hash_for, #chef_server_names, #knife_config, #knife_config_dir, #knife_config_file_for

Methods included from ProvidersHelper

#provider_availability_zones, #provider_config, #provider_credentials, #provider_for, #provider_names, #provider_names_for, #provider_regions, #provider_type, #providers_config

Methods included from ReposHelper

#find_cluster, #find_cluster_pwds, #find_cluster_pwds_with_repo, #find_cluster_with_repo, #find_data_bag, #find_data_bag_item, #find_environment, #find_repo_item, #find_role, #get_cookbook_deps, #get_cookbook_meta, #get_cookbook_versions, #get_cookbooks, #get_entry_dependencies, #get_modified_cookbooks, #is_dirty?, #modified?, #repo_cfg, #repo_clusters_dir, #repo_cookbook_paths, #repo_dir, #repo_names, #repo_paths, #repos_config, #repos_dir, #run_inside_repo_dir

Methods included from FhcapHelper

#cluster_template_names

Constructor Details

#initialize(options) ⇒ ChefServerTask

Returns a new instance of ChefServerTask.



16
17
18
19
20
21
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 16

def initialize(options)
  super
  @chef_server = options[:'chef-server']
  @chef_repo = options[:'chef-repo']
  @repos = options[:repos]
end

Instance Attribute Details

#chef_repoObject (readonly)

Returns the value of attribute chef_repo.



14
15
16
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 14

def chef_repo
  @chef_repo
end

#chef_serverObject (readonly)

Returns the value of attribute chef_server.



14
15
16
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 14

def chef_server
  @chef_server
end

#reposObject (readonly)

Returns the value of attribute repos.



14
15
16
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 14

def repos
  @repos
end

Instance Method Details

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 116

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_data_bag_delete(data_bag, item, server) ⇒ Object



111
112
113
114
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 111

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 91

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.run
    end
  end
end

#knife_environment_delete(env, server) ⇒ Object



106
107
108
109
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 106

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 76

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?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 72

def local_chef_server?
  chef_server && chef_server == 'local'
end

#with_chef_server(&block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 23

def with_chef_server(&block)
  ::Chef::Config.solo = false
  if local_chef_server?
    with_local_chef_server(&block)
  else
    block.call
  end
end

#with_local_chef_server(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fhcap/tasks/chef/chef_server_task.rb', line 32

def with_local_chef_server(&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

  local_repo = repo_dir(@chef_repo)
  thor.say "[Chef Zero Server] - Syncing nodes from #{local_repo}"
  knife_upload(local_repo, @chef_server, ['/nodes'])
  knife_upload(local_repo, @chef_server, ['/data_bags/aws_*'])
  begin
    block.call
  ensure
    thor.say "[Chef Zero Server] - Syncing nodes to #{local_repo}"
    FileUtils.rm_rf(File.join(local_repo, 'nodes'))
    FileUtils.rm_rf Dir.glob(File.join(local_repo, 'data_bags/aws_*'))
    knife_download(local_repo, @chef_server, ['/nodes'])
    knife_download(local_repo, @chef_server, ['/data_bags/aws_*'])
  end
end