Class: RightScale::ChefState
Overview
Manages and persists chef state (run list and attributes)
Defined Under Namespace
Classes: ChefStateNotInitialized
Constant Summary collapse
- STATE_FILE =
Path to JSON file where current chef state is serialized
File.join(InstanceState::STATE_DIR, 'chef.js')
- SCRIPTS_FILE =
Path to JSON file where past scripts are serialized
File.join(InstanceState::STATE_DIR, 'past_scripts.js')
Class Method Summary collapse
-
.attributes ⇒ Object
Current node attributes.
-
.attributes=(val) ⇒ Object
Set node attributes Note: can’t set it to nil.
-
.init(agent_id, secret, reset) ⇒ Object
Load chef state from file.
-
.initialized? ⇒ Boolean
Was ‘init’ ever called? Note: Accessing or setting the chef state calls ‘init’.
-
.merge_attributes(attribs) ⇒ Object
Merge given attributes into node attributes.
-
.past_scripts ⇒ Object
Current list of scripts that have already executed.
-
.record_script_execution(nickname) ⇒ Object
Record script execution in scripts file.
-
.save_state ⇒ Object
Save node attributes to file Note: Attributes are saved only when runlist ran on default thread.
Class Method Details
.attributes ⇒ Object
Current node attributes
Return
- attributes(Hash)
-
Current node attributes
71 72 73 74 |
# File 'lib/instance/cook/chef_state.rb', line 71 def attributes ensure_initialized @@attributes end |
.attributes=(val) ⇒ Object
Set node attributes Note: can’t set it to nil. Setting the attributes to nil will cause the attributes to be initialized if they haven’t yet but won’t assign the value nil to them.
Parameters
- attributes(Hash)
-
Node attributes
Return
- true
-
Always return true
95 96 97 98 99 |
# File 'lib/instance/cook/chef_state.rb', line 95 def attributes=(val) ensure_initialized @@attributes = val if val save_state end |
.init(agent_id, secret, reset) ⇒ Object
Load chef state from file
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/instance/cook/chef_state.rb', line 44 def init(agent_id, secret, reset) return true if initialized? && !reset @@encoder = MessageEncoder.for_agent(agent_id, secret) @@attributes = {} @@past_scripts = [] if reset save_state else load_state end true end |
.initialized? ⇒ Boolean
Was ‘init’ ever called? Note: Accessing or setting the chef state calls ‘init’
Return
- true
-
If ‘init’ has been called
- false
-
Otherwise
63 64 65 |
# File 'lib/instance/cook/chef_state.rb', line 63 def initialized? !!defined?(@@attributes) end |
.merge_attributes(attribs) ⇒ Object
Merge given attributes into node attributes
Parameters
- attribs(Hash)
-
Attributes to be merged
Return
- true
-
Always return true
108 109 110 111 |
# File 'lib/instance/cook/chef_state.rb', line 108 def merge_attributes(attribs) self.attributes = ::RightSupport::Data::HashTools.deep_merge!(attributes, attribs) if attribs true end |
.past_scripts ⇒ Object
Current list of scripts that have already executed
Return
(Array) Scripts that have already executed
80 81 82 83 |
# File 'lib/instance/cook/chef_state.rb', line 80 def past_scripts ensure_initialized @@past_scripts end |
.record_script_execution(nickname) ⇒ Object
Record script execution in scripts file
Parameters
- nickname(String)
-
Nickname of RightScript which successfully executed
Return
- true
-
If script was added to past scripts collection
- false
-
If script was already in past scripts collection
121 122 123 124 125 126 127 |
# File 'lib/instance/cook/chef_state.rb', line 121 def record_script_execution(nickname) ensure_initialized new_script = !@@past_scripts.include?(nickname) @@past_scripts << nickname if new_script # note that we only persist state on successful execution of bundle. new_script end |
.save_state ⇒ Object
Save node attributes to file Note: Attributes are saved only when runlist ran on default thread
Return
- true
-
Always return true
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/instance/cook/chef_state.rb', line 134 def save_state if Cook.instance.has_default_thread? begin FileUtils.touch(STATE_FILE) File.chmod(0600, STATE_FILE) write_encoded_data(STATE_FILE, { 'attributes' => @@attributes }) RightScale::JsonUtilities::write_json(SCRIPTS_FILE, @@past_scripts) rescue Exception => e Log.warning("Failed to save node attributes: #{e.}") end end true end |