Class: ChefSpec::Server

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chefspec/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Create a new instance of the ChefSpec::Server singleton. This method also starts the Chef Zero server in the background.



146
147
148
149
150
# File 'lib/chefspec/server.rb', line 146

def initialize
  @server = ChefZero::Server.new(
    log_level:  RSpec.configuration.log_level || :warn,
  )
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



140
141
142
# File 'lib/chefspec/server.rb', line 140

def server
  @server
end

Class Method Details

.entity(method, klass, key) ⇒ Object



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
# File 'lib/chefspec/server.rb', line 99

def self.entity(method, klass, key)
  class_eval "    def create_\#{method}(name, data = {})\n      unless '\#{key}' == 'data'\n        # Automatically set the \"name\" if no explicit one was given\n        data[:name] ||= name\n\n        # Convert it to JSON\n        data = JSON.fast_generate(data)\n      end\n\n      @server.load_data('\#{key}' => { name => data })\n    end\n\n    def \#{method}(name)\n      data = @server.data_store.get(['\#{key}', name])\n      json = JSON.parse(data)\n\n      if \#{klass}.respond_to?(:json_create)\n        \#{klass}.json_create(json)\n      else\n        \#{klass}.new(json)\n      end\n    rescue ChefZero::DataStore::DataNotFoundError\n      nil\n    end\n\n    def \#{key}\n      @server.data_store.list(['\#{key}'])\n    end\n\n    def has_\#{method}?(name)\n      [email protected]_store.get(['\#{key}', name]).nil?\n    rescue ChefZero::DataStore::DataNotFoundError\n      false\n    end\n  EOH\nend\n", __FILE__, __LINE__ + 1

.method_missing(m, *args, &block) ⇒ Object

Delegate all methods to the singleton instance.



60
61
62
# File 'lib/chefspec/server.rb', line 60

def self.method_missing(m, *args, &block)
  instance.send(m, *args, &block)
end

Instance Method Details

#client(name) ⇒ Chef::Client?

Find a client at the given name

Parameters:

  • name (String)

    the name of the client

Returns:



152
# File 'lib/chefspec/server.rb', line 152

entity :client,      Chef::Client, 'clients'

#client_keyString

The path to the insecure Chef Zero private key on disk. Because Chef requires the path to a file instead of the contents of the key (why), this method dynamically writes the ChefZero::PRIVATE_KEY to disk and then returns that path.

Returns:

  • (String)

    the path to the client key on disk



167
168
169
170
171
172
173
# File 'lib/chefspec/server.rb', line 167

def client_key
  @client_key ||= begin
    path = File.join(cache_dir, 'client.pem')
    File.open(path, 'w') { |f| f.write(ChefZero::PRIVATE_KEY) }
    path
  end
end

#clientsArray<Hash>

The list of client on the Chef Server

Returns:

  • (Array<Hash>)

    all the client on the Chef Server



152
# File 'lib/chefspec/server.rb', line 152

entity :client,      Chef::Client, 'clients'

#create_client(name, data = {}) ⇒ Object

Create a new client on the Chef Server

Parameters:

  • name (String)

    the name of the client

  • data (Hash) (defaults to: {})

    the list of data to load



152
# File 'lib/chefspec/server.rb', line 152

entity :client,      Chef::Client, 'clients'

#create_data_bag(name, data = {}) ⇒ Object

Create a new data_bag on the Chef Server

Parameters:

  • name (String)

    the name of the data_bag

  • data (Hash) (defaults to: {})

    the list of data to load



153
# File 'lib/chefspec/server.rb', line 153

entity :data_bag,    Chef::DataBag, 'data'

#create_environment(name, data = {}) ⇒ Object

Create a new environment on the Chef Server

Parameters:

  • name (String)

    the name of the environment

  • data (Hash) (defaults to: {})

    the list of data to load



154
# File 'lib/chefspec/server.rb', line 154

entity :environment, Chef::Environment, 'environments'

#create_node(name, data = {}) ⇒ Object

Create a new node on the Chef Server

Parameters:

  • name (String)

    the name of the node

  • data (Hash) (defaults to: {})

    the list of data to load



155
# File 'lib/chefspec/server.rb', line 155

entity :node,        Chef::Node, 'nodes'

#create_role(name, data = {}) ⇒ Object

Create a new role on the Chef Server

Parameters:

  • name (String)

    the name of the role

  • data (Hash) (defaults to: {})

    the list of data to load



156
# File 'lib/chefspec/server.rb', line 156

entity :role,        Chef::Role, 'roles'

#dataArray<Hash>

The list of data_bag on the Chef Server

Returns:

  • (Array<Hash>)

    all the data_bag on the Chef Server



153
# File 'lib/chefspec/server.rb', line 153

entity :data_bag,    Chef::DataBag, 'data'

#data_bag(name) ⇒ Chef::DataBag?

Find a data_bag at the given name

Parameters:

  • name (String)

    the name of the data_bag

Returns:

  • (Chef::DataBag, nil)


153
# File 'lib/chefspec/server.rb', line 153

entity :data_bag,    Chef::DataBag, 'data'

#environment(name) ⇒ Chef::Environment?

Find a environment at the given name

Parameters:

  • name (String)

    the name of the environment

Returns:

  • (Chef::Environment, nil)


154
# File 'lib/chefspec/server.rb', line 154

entity :environment, Chef::Environment, 'environments'

#environmentsArray<Hash>

The list of environment on the Chef Server

Returns:

  • (Array<Hash>)

    all the environment on the Chef Server



154
# File 'lib/chefspec/server.rb', line 154

entity :environment, Chef::Environment, 'environments'

#has_client?(name) ⇒ Boolean

Determine if the Chef Server has the given client

Parameters:

  • name (String)

    the name of the client to find

Returns:

  • (Boolean)


152
# File 'lib/chefspec/server.rb', line 152

entity :client,      Chef::Client, 'clients'

#has_data_bag?(name) ⇒ Boolean

Determine if the Chef Server has the given data_bag

Parameters:

  • name (String)

    the name of the data_bag to find

Returns:

  • (Boolean)


153
# File 'lib/chefspec/server.rb', line 153

entity :data_bag,    Chef::DataBag, 'data'

#has_environment?(name) ⇒ Boolean

Determine if the Chef Server has the given environment

Parameters:

  • name (String)

    the name of the environment to find

Returns:

  • (Boolean)


154
# File 'lib/chefspec/server.rb', line 154

entity :environment, Chef::Environment, 'environments'

#has_node?(name) ⇒ Boolean

Determine if the Chef Server has the given node

Parameters:

  • name (String)

    the name of the node to find

Returns:

  • (Boolean)


155
# File 'lib/chefspec/server.rb', line 155

entity :node,        Chef::Node, 'nodes'

#has_role?(name) ⇒ Boolean

Determine if the Chef Server has the given role

Parameters:

  • name (String)

    the name of the role to find

Returns:

  • (Boolean)


156
# File 'lib/chefspec/server.rb', line 156

entity :role,        Chef::Role, 'roles'

#node(name) ⇒ Chef::Node?

Find a node at the given name

Parameters:

  • name (String)

    the name of the node

Returns:

  • (Chef::Node, nil)


155
# File 'lib/chefspec/server.rb', line 155

entity :node,        Chef::Node, 'nodes'

#nodesArray<Hash>

The list of node on the Chef Server

Returns:

  • (Array<Hash>)

    all the node on the Chef Server



155
# File 'lib/chefspec/server.rb', line 155

entity :node,        Chef::Node, 'nodes'

#reset!Object

Clear the contents of the server (used between examples)



189
190
191
# File 'lib/chefspec/server.rb', line 189

def reset!
  @server.clear_data
end

#role(name) ⇒ Chef::Role?

Find a role at the given name

Parameters:

  • name (String)

    the name of the role

Returns:

  • (Chef::Role, nil)


156
# File 'lib/chefspec/server.rb', line 156

entity :role,        Chef::Role, 'roles'

#rolesArray<Hash>

The list of role on the Chef Server

Returns:

  • (Array<Hash>)

    all the role on the Chef Server



156
# File 'lib/chefspec/server.rb', line 156

entity :role,        Chef::Role, 'roles'

#start!Object

Start the Chef Zero server in the background, updating the Chef::Config with the proper chef_server_url.



179
180
181
182
183
184
# File 'lib/chefspec/server.rb', line 179

def start!
  unless @server.running?
    @server.start_background
    Chef::Config[:chef_server_url] = @server.url
  end
end

#stop!Object

Stop the Chef Zero server, if it is running. This method also runs any cleanup hooks, such as clearing the cache directories.



197
198
199
200
# File 'lib/chefspec/server.rb', line 197

def stop!
  @server.stop if @server.running?
  FileUtils.rm_rf(cache_dir)
end