Class: RightScale::CookState

Inherits:
Object
  • Object
show all
Defined in:
lib/instance/cook/cook_state.rb

Overview

Manages and persists cook process state

Constant Summary collapse

DEV_TAG_NAMESPACE =

Dev tags namespace

'rs_agent_dev:'
COOKBOOK_PATH_TAG =

Cookbook path dev tag namespace and prefix

"#{DEV_TAG_NAMESPACE}cookbooks_path"
BREAKPOINT_TAG =

Breakpoint dev tag namespace and prefix

"#{DEV_TAG_NAMESPACE}break_point"
DOWNLOAD_ONCE_TAG =

Download once dev tag namespace and prefix

"#{DEV_TAG_NAMESPACE}download_cookbooks_once"
LOG_LEVEL_TAG =

Log level dev tag namespace and prefix

"#{DEV_TAG_NAMESPACE}log_level"
STATE_DIR =

Path to JSON file where dev state is serialized

RightScale::AgentConfig.agent_state_dir
STATE_FILE =
File.join(STATE_DIR, 'cook_state.js')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCookState

Reset class state and load persisted state if any

Return

true

Always return true



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/instance/cook/cook_state.rb', line 86

def initialize
  # set some defaults
  @has_downloaded_cookbooks = false
  @reboot                   = false
  @startup_tags             = []
  @log_level                = Logger::INFO
  @log_file                 = nil

  # replace defaults with state on disk
  load_state

  true
end

Instance Attribute Details

#has_downloaded_cookbooks=(value) ⇒ Object (writeonly)

Sets the attribute has_downloaded_cookbooks

Parameters:

  • the value to set the attribute has_downloaded_cookbooks to.



101
102
103
# File 'lib/instance/cook/cook_state.rb', line 101

def has_downloaded_cookbooks=(value)
  @has_downloaded_cookbooks = value
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



100
101
102
# File 'lib/instance/cook/cook_state.rb', line 100

def log_file
  @log_file
end

#log_levelObject (readonly)

Current logger severity

Return

level(Integer)

one of Logger::INFO … Logger::FATAL



192
193
194
# File 'lib/instance/cook/cook_state.rb', line 192

def log_level
  @log_level
end

#startup_tagsObject (readonly)

Returns the value of attribute startup_tags.



100
101
102
# File 'lib/instance/cook/cook_state.rb', line 100

def startup_tags
  @startup_tags
end

Class Method Details

.init(reset = false) ⇒ Object

Reset class state and load persisted state if any

Return

true

Always return true



51
52
53
54
55
56
# File 'lib/instance/cook/cook_state.rb', line 51

def init(reset=false)
  if @state.nil? || reset
    @state = CookState.new
  end
  true
end

Instance Method Details

#breakpointObject

Name of first recipe in run list that should not be run

Return

recipe(String)

Name of recipe to break execution of sequence on



143
144
145
# File 'lib/instance/cook/cook_state.rb', line 143

def breakpoint
  recipe = tag_value(BREAKPOINT_TAG)
end

#cookbooks_pathObject

Path to cookbooks repos directory. Cookbooks are downloaded to this location if and only if it doesn’t exist.

Return

path(Array)

Dev cookbooks repositories path

nil

Use default cookbook download algorithm



134
135
136
137
# File 'lib/instance/cook/cook_state.rb', line 134

def cookbooks_path
  path = tag_value(COOKBOOK_PATH_TAG)
  path.split(/, */) if path
end

#dev_log_levelObject

Determines the developer log level, if any, which forces and supercedes all other log level configurations.

Return

level(Token)

developer log level or nil



120
121
122
123
124
125
126
# File 'lib/instance/cook/cook_state.rb', line 120

def dev_log_level
  if value = tag_value(LOG_LEVEL_TAG)
    value = value.downcase.to_sym
    value = nil unless [:debug, :info, :warn, :error, :fatal].include?(value)
  end
  value
end

#download_cookbooks?Boolean

Whether cookbooks should be downloaded False if either a dev cookbooks path is used or the download once flag is set and cookbooks have already been downloaded Note: we know a cookbook is downloaded when this method is called.

Make sure this stays true.

Return

true

If cookbooks should be downloaded

false

Otherwise

Returns:



173
174
175
176
177
# File 'lib/instance/cook/cook_state.rb', line 173

def download_cookbooks?
  # always download unless machine is tagged with a valid cookbooks path or
  # machine is tagged with download once and cookbooks have already been downloaded.
  res = !(use_cookbooks_path? || (has_downloaded_cookbooks? && download_once?))
end

#download_once?Boolean

Whether cookbooks should be downloaded only once

Return

true

If cookbooks should be downloaded only once for this instance

false

Otherwise

Returns:



184
185
186
# File 'lib/instance/cook/cook_state.rb', line 184

def download_once?
  tag_value(DOWNLOAD_ONCE_TAG) == 'true'
end

#has_downloaded_cookbooks?Boolean

Returns:



103
104
105
# File 'lib/instance/cook/cook_state.rb', line 103

def has_downloaded_cookbooks?
  !!@has_downloaded_cookbooks
end

#reboot?Boolean

Are we rebooting? (needed for RightScripts)

Return

res(Boolean)

Whether this instance was rebooted

Returns:



111
112
113
# File 'lib/instance/cook/cook_state.rb', line 111

def reboot?
  !!@reboot
end

#update(state_to_merge, overrides = {}) ⇒ Object

Re-initialize then merge given state

Parameters

state_to_merge(RightScale::InstanceState)

InstanceState to be passed on to Cook

overrides(Hash)

Hash keyed by state name that will override state_to_merge

Return

true

Always



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/instance/cook/cook_state.rb', line 209

def update(state_to_merge, overrides = {})
  # only merge state if state to be merged has values
  @startup_tags = state_to_merge.startup_tags if state_to_merge.respond_to?(:startup_tags)
  @reboot       = state_to_merge.reboot?      if state_to_merge.respond_to?(:reboot?)
  @log_level    = state_to_merge.log_level    if state_to_merge.respond_to?(:log_level)
  if state_to_merge.respond_to?(:log_file) && state_to_merge.respond_to?(:value)
    @log_file = state_to_merge.log_file(state_to_merge.value)
  end

  @startup_tags = overrides[:startup_tags] if overrides.has_key?(:startup_tags)
  @reboot       = overrides[:reboot]       if overrides.has_key?(:reboot)
  @log_file     = overrides[:log_file]     if overrides.has_key?(:log_file)

  # check the log level again after the startup_tags have been updated or
  # overridden.
  if overrides.has_key?(:log_level)
    @log_level = overrides[:log_level]
  elsif tagged_log_level = dev_log_level
    @log_level = tagged_log_level
  end

  save_state

  true
end

#use_cookbooks_path?Boolean

Whether dev cookbooks path should be used instead of standard cookbooks repositories location True if in dev mode and all dev cookbooks repos directories are not empty

Return

true

If dev cookbooks repositories path should be used

false

Otherwise

Returns:



154
155
156
157
158
159
160
161
162
# File 'lib/instance/cook/cook_state.rb', line 154

def use_cookbooks_path?
  res = !!(paths = cookbooks_path)
  return false unless res
  paths.each do |path|
    res = path && File.directory?(path) && Dir.entries(path) != ['.', '..']
    break unless res
  end
  res
end