Class: ChefGen::FlavorBase

Inherits:
Object
  • Object
show all
Includes:
CopyHelpers, ResourceHelpers, Hooks
Defined in:
lib/chef_gen/flavor_base.rb,
lib/chef_gen/flavor_base/copy_helpers.rb,
lib/chef_gen/flavor_base/resource_helpers.rb

Overview

a base for ChefDK Template Flavors

Defined Under Namespace

Modules: CopyHelpers, ResourceHelpers

Constant Summary collapse

VERSION =

the version of the gem

'0.9.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResourceHelpers

#add_directories, #add_files, #add_templates

Constructor Details

#initialize(temp_path) ⇒ self #initialize(type, recipe) ⇒ self

Returns a new instance of FlavorBase.

Overloads:

  • #initialize(temp_path) ⇒ self

    initializes the flavor in setup mode

    Parameters:

    • temp_path (String)

      the temporary directory provided by chef-gen-flavors

  • #initialize(type, recipe) ⇒ self

    initializes the flavor in generate mode

    Parameters:

    • type (String)

      the type of object being generated when constructed by ChefDK

    • recipe (Chef::Recipe)

      the recipe into which to inject resources when constructed by ChefDK



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef_gen/flavor_base.rb', line 79

def initialize(temp_path: nil, type: nil, recipe: nil)
  # run before hook
  run_hook :before_initialize

  # set up instance variables
  @recipe = recipe

  # start with an empty list of snippets
  @snippets = []
  @enabled_snippets = {}

  if setup_mode?
    @temp_path = temp_path
    # in setup mode, start with an empty list of things to copy
    @tocopy = []
  else
    @type = type
    # in generate mode, start empty dir, file and template lists
    @directories = []
    @files = []
    @files_if_missing = []
    @templates = []
    @templates_if_missing = []
  end

  # run after hook
  run_hook :after_initialize
end

Instance Attribute Details

#directoriesArray<String> (readonly)

a list of directory resources

Returns:

  • (Array<String>)

    target path relative to cookbook root



49
50
51
# File 'lib/chef_gen/flavor_base.rb', line 49

def directories
  @directories
end

#filesArray<String> (readonly)

a list of file resources with action :create

Returns:

  • (Array<String>)

    target path relative to cookbook root



53
54
55
# File 'lib/chef_gen/flavor_base.rb', line 53

def files
  @files
end

#files_if_missingArray<String> (readonly)

a list of file resources with action :create_if_missing

Returns:

  • (Array<String>)

    target path relative to cookbook root



57
58
59
# File 'lib/chef_gen/flavor_base.rb', line 57

def files_if_missing
  @files_if_missing
end

#snippetsArray<Class> (readonly)

a list of snippet classes to mix in

Returns:

  • (Array<Class>)

    list of snippet classes



40
41
42
# File 'lib/chef_gen/flavor_base.rb', line 40

def snippets
  @snippets
end

#templatesArray<String> (readonly)

a list of template resources with action :create

Returns:

  • (Array<String>)

    target path relative to cookbook root



61
62
63
# File 'lib/chef_gen/flavor_base.rb', line 61

def templates
  @templates
end

#templates_if_missingArray<String> (readonly)

a list of template resources with action :create_if_missing

Returns:

  • (Array<String>)

    target path relative to cookbook root



65
66
67
# File 'lib/chef_gen/flavor_base.rb', line 65

def templates_if_missing
  @templates_if_missing
end

#tocopyArray<Array> (readonly)

a list of pairs of src/dst files or directories to copy into the temporary directory.

Returns:

  • (Array<Array>)

    list of [src, dst] pairs



45
46
47
# File 'lib/chef_gen/flavor_base.rb', line 45

def tocopy
  @tocopy
end

#typeString (readonly)

the type of object being generated (cookbook, repo, etc.)

Returns:

  • (String)

    the type of object being generated



36
37
38
# File 'lib/chef_gen/flavor_base.rb', line 36

def type
  @type
end

Instance Method Details

#add_contentvoid

This method returns an undefined value.

constructs and calls the #add_content of each snippet, then calls #copy_content to copy the files to the temporary directory



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef_gen/flavor_base.rb', line 125

def add_content
  # construct each snippet in setup mode
  run_hook :before_snippet_construct
  snippets.map! { |s| s.new(flavor: self) }
  mark_enabled_snippets
  run_hook :after_snippet_construct

  # call hooks to add content
  run_hook :before_add_content
  run_hook :do_add_content
  run_hook :after_add_content

  # copy queued content
  run_hook :before_copy_content
  copy_content
  run_hook :after_copy_content
end

#declare_resourcesvoid

This method returns an undefined value.

constructs and calls the #declare_resources method of each snippet, then calls #add_resources to add the resources to the recipe



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/chef_gen/flavor_base.rb', line 146

def declare_resources
  # declare the target directory
  @recipe.send(:directory, destination_path('.'))

  # construct each snippet in generate mode
  run_hook :before_snippet_construct
  snippets.map! { |s| s.new(flavor: self, recipe: @recipe) }
  mark_enabled_snippets
  run_hook :after_snippet_construct

  # call hooks to add content
  run_hook :before_declare_resources
  run_hook :do_declare_resources
  run_hook :after_declare_resources

  # add queued resources to the recipe
  run_hook :before_add_resources
  add_resources
  run_hook :after_add_resources
end

#generate_mode?Boolean

determines if the flavor is in generate mode

Returns:

  • (Boolean)

    true if the flavor is in generate mode, used by ChefDK to create an object



118
119
120
# File 'lib/chef_gen/flavor_base.rb', line 118

def generate_mode?
  !setup_mode?
end

#setup_mode?Boolean

determines if the flavor is in setup mode

Returns:

  • (Boolean)

    true if the flavor is in setup mode, creating the temporary directory



111
112
113
# File 'lib/chef_gen/flavor_base.rb', line 111

def setup_mode?
  @recipe.nil?
end

#snippet?(name) ⇒ Boolean

a predicate to determine if a snippet has been included in a flavor

Parameters:

  • name (String)

    the NAME constant of the snippet

Returns:

  • (Boolean)

    true if the snippet has been enabled



170
171
172
# File 'lib/chef_gen/flavor_base.rb', line 170

def snippet?(name)
  @enabled_snippets.key?(name)
end