Class: RightScale::RightScriptsCookbook

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

Overview

Generate recipes dynamically for RightScripts Usage is:

1. Call 'recipe_from_right_script' for each RightScript that should be converted to a recipe
2. Call 'save' before running Chef, 'recipe_from_right_script' cannot be called after 'save'
3. Use 'repo_dir' to retrieve the Chef cookbook repo path (can be called at any time)

Constant Summary collapse

COOKBOOK_NAME =

Name of cookbook containing RightScript recipes

'right_scripts_cookbook'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread_name) ⇒ RightScriptsCookbook

Setup temporary directory for cookbook repo containing recipes generated from RightScripts



45
46
47
48
49
50
51
52
53
# File 'lib/instance/right_scripts_cookbook.rb', line 45

def initialize(thread_name)
  @saved        = false
  @recipes      = {}
  @repo_dir     = File.join(AgentConfig.right_scripts_repo_dir, thread_name)
  @cookbook_dir = File.join(@repo_dir, COOKBOOK_NAME)
  @recipes_dir  = File.join(@cookbook_dir, 'recipes')
  FileUtils.rm_rf(@cookbook_dir)
  FileUtils.mkdir_p(@recipes_dir)
end

Instance Attribute Details

#repo_dirObject (readonly)

Path to generated cookbook repo



38
39
40
# File 'lib/instance/right_scripts_cookbook.rb', line 38

def repo_dir
  @repo_dir
end

#savedObject (readonly)

Wheter ‘save’ has been called



41
42
43
# File 'lib/instance/right_scripts_cookbook.rb', line 41

def saved
  @saved
end

Class Method Details

.recipe_title(recipe) ⇒ Object

Human friendly title for given recipe instantiation

Parameters

recipe(String)

Recipe nickname

Return

title(String)

Recipe title to be used in audits



147
148
149
150
# File 'lib/instance/right_scripts_cookbook.rb', line 147

def self.recipe_title(recipe)
  title = right_script?(recipe) ? 'RightScript' : 'recipe'
  title = "#{title} < #{recipe} >"
end

.right_script?(nickname) ⇒ Boolean

Whether given recipe name corresponds to a converted RightScript

Parameters

nickname(String)

Recipe nickname

Return

true

If recipe was created from converting a RightScript

false

Otherwise

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
# File 'lib/instance/right_scripts_cookbook.rb', line 131

def self.right_script?(nickname)
  # the RightScripts deserialized from core appear to not have the
  # RightScript cookbook in their nickname. actual Chef recipes will have
  # their cookbook name, so assume missing cookbook name means RightScript.
  regex = /^(.*)::/
  match = regex.match(nickname)
  return match.nil? || COOKBOOK_NAME == match[1]
end

Instance Method Details

#cache_dir(script) ⇒ Object

Path to cache directory for given script

Parameters

script(Object)

script object of some kind (e.g. RightScale::RightScriptInstantiation)

Return

path(String)

Path to directory used for attachments and source



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/instance/right_scripts_cookbook.rb', line 159

def cache_dir(script)
  # prefix object ID with a text constant to make a legal directory name
  # in case object id is negative (Ubuntu, etc.). this method will be called
  # more than once and must return the same directory each time for a given
  # script instantiation.
  path = File.normalize_path(File.join(AgentConfig.cache_dir, 'right_scripts_content', "rs_attach" + script.object_id.to_s))

  # convert to native format for ease of scripting in Windows, etc. the
  # normalized path is normal for Ruby but not necessarily for native FS.
  return RightScale::Platform.filesystem.pretty_path(path, true)
end

#empty?Boolean

Is there no RightScript recipe in repo?

Return

true

If recipe_from_right_script was never called

false

Otherwise

Returns:

  • (Boolean)


176
177
178
# File 'lib/instance/right_scripts_cookbook.rb', line 176

def empty?
  @recipes.empty?
end

#recipe_from_right_script(script) ⇒ Object

Add RightScript instantiation to cookbook

Parameters

script(RightScale::RightScriptInstantiation)

RightScript to be added

Return

recipe(RightScale::RecipeInstantiation)

Recipe that wraps RightScript

Raise

(RightScale::Exceptions::Application)

If ‘save’ has been called

Raises:

  • (RightScale::Exceptions::Application)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/instance/right_scripts_cookbook.rb', line 65

def recipe_from_right_script(script)
  raise RightScale::Exceptions::Application, 'cannot create recipe after cookbook repo has been saved' if @saved
  path = script_path(script.nickname)
  recipe_name = File.basename(path)
  @recipes[recipe_name] = script.nickname
  recipe_content = "right_script \#{script.nickname.inspect} do\n  parameters(node[\#{script.nickname.inspect}][\"parameters\"])\n  cache_dir  '\#{cache_dir(script)}'\n  source_file '\#{path}'\n  display_version \#{script.display_version.to_s.strip.inspect}\nend\n  EOS\n  File.open(path, 'w') { |f| f.puts script.source }\n  File.chmod(0744, path)\n  recipe_path = \"\#{path}.rb\"\n  File.open(recipe_path, 'w') { |f| f.puts recipe_content }\n\n  recipe = RecipeInstantiation.new(\"\#{COOKBOOK_NAME}::\#{recipe_name}\",\n                                   { script.nickname => { \"parameters\" => script.parameters } },\n                                   script.id, script.ready)\n\nend\n"

#saveObject

Save cookbook repo

Return

true

Always return true



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/instance/right_scripts_cookbook.rb', line 110

def save
  unless empty?
    recipes = @recipes.keys.sort
     = "description \"Automatically generated repo, do not modify\"\n\#{recipes.map { |r| \"recipe \\\"\#{COOKBOOK_NAME}::\#{r}\\\", \\\"RightScript < \#{@recipes[r]} >\\\"\" }.join(\"\\n\")}\n    EOS\n    metadata_path = File.join(@cookbook_dir, 'metadata.rb')\n    File.open(metadata_path, 'w') { |f| f.puts metadata_content }\n  end\n  @saved = true\nend\n"

#script_path(nickname) ⇒ Object

Produce file name for given script nickname

Parameters

nickname(String)

Script nick name

Return

path(String)

Path to corresponding recipe



96
97
98
99
100
101
102
103
104
# File 'lib/instance/right_scripts_cookbook.rb', line 96

def script_path(nickname)
  base_path = nickname.gsub(/[^0-9a-zA-Z_]/,'_')
  base_path = File.join(@recipes_dir, base_path)
  candidate_path = RightScale::Platform.shell.format_script_file_name(base_path)
  i = 1
  path = candidate_path
  path = candidate_path + (i += 1).to_s while File.exists?(path)
  path
end