Class: Chef::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/cheffish/recipe_dsl.rb

Instance Method Summary collapse

Instance Method Details

#with_chef_data_bag(name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cheffish/recipe_dsl.rb', line 9

def with_chef_data_bag(name)
  old_enclosing_data_bag = Cheffish.enclosing_data_bag
  Cheffish.enclosing_data_bag = name
  if block_given?
    begin
      yield
    ensure
      Cheffish.enclosing_data_bag = old_enclosing_data_bag
    end
  end
end

#with_chef_data_bag_item_encryption(encryption_options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cheffish/recipe_dsl.rb', line 33

def with_chef_data_bag_item_encryption(encryption_options)
  old_enclosing_data_bag_item_encryption = Cheffish.enclosing_data_bag_item_encryption
  Cheffish.enclosing_data_bag_item_encryption = encryption_options
  if block_given?
    begin
      yield
    ensure
      Cheffish.enclosing_data_bag_item_encryption = old_enclosing_data_bag_item_encryption
    end
  end
end

#with_chef_environment(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cheffish/recipe_dsl.rb', line 21

def with_chef_environment(name)
  old_enclosing_environment = Cheffish.enclosing_environment
  Cheffish.enclosing_environment = name
  if block_given?
    begin
      yield
    ensure
      Cheffish.enclosing_environment = old_enclosing_environment
    end
  end
end

#with_chef_local_server(options, &block) ⇒ Object



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
87
88
89
90
91
92
93
94
95
96
# File 'lib/cheffish/recipe_dsl.rb', line 57

def with_chef_local_server(options, &block)
  options[:host] ||= '127.0.0.1'
  options[:log_level] ||= Chef::Log.level
  options[:port] ||= 8900

  # Create the data store chef-zero will use
  options[:data_store] ||= begin
    if !options[:chef_repo_path]
      raise "chef_repo_path must be specified to with_chef_local_server"
    end

    # Ensure all paths are given
    %w(acl client cookbook container data_bag environment group node role).each do |type|
      options["#{type}_path".to_sym] ||= begin
        if options[:chef_repo_path].kind_of?(String)
          Chef::Config.path_join(options[:chef_repo_path], "#{type}s")
        else
          options[:chef_repo_path].map { |path| Chef::Config.path_join(path, "#{type}s")}
        end
      end
      # Work around issue in earlier versions of ChefFS where it expects strings for these
      # instead of symbols
      options["#{type}_path"] = options["#{type}_path".to_sym]
    end

    chef_fs = Chef::ChefFS::Config.new(options).local_fs
    chef_fs.write_pretty_json = true
    Chef::ChefFS::ChefFSDataStore.new(chef_fs)
  end

  # Start the chef-zero server
  Chef::Log.info("Starting chef-zero on port #{options[:port]} with repository at #{options[:data_store].chef_fs.fs_description}")
  chef_zero_server = ChefZero::Server.new(options)
  chef_zero_server.start_background

  @@local_servers ||= []
  @@local_servers << chef_zero_server

  with_chef_server(chef_zero_server.url, &block)
end

#with_chef_server(server_url, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cheffish/recipe_dsl.rb', line 45

def with_chef_server(server_url, options = {})
  old_enclosing_chef_server = Cheffish.enclosing_chef_server
  Cheffish.enclosing_chef_server = { :chef_server_url => server_url, :options => options }
  if block_given?
    begin
      yield
    ensure
      Cheffish.enclosing_chef_server = old_enclosing_chef_server
    end
  end
end