Class: Bolt::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/project.rb

Constant Summary collapse

BOLTDIR_NAME =
'Boltdir'
PROJECT_SETTINGS =
{
  "name"  => "The name of the project",
  "plans" => "An array of plan names to whitelist. Whitelisted plans are included in `bolt plan show` output",
  "tasks" => "An array of task names to whitelist. Whitelisted plans are included in `bolt task show` output"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, type = 'option') ⇒ Project

Returns a new instance of Project.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bolt/project.rb', line 39

def initialize(path, type = 'option')
  @path = Pathname.new(path).expand_path
  @config_file = @path + 'bolt.yaml'
  @inventory_file = @path + 'inventory.yaml'
  @modulepath = [(@path + 'modules').to_s, (@path + 'site-modules').to_s, (@path + 'site').to_s]
  @hiera_config = @path + 'hiera.yaml'
  @puppetfile = @path + 'Puppetfile'
  @rerunfile = @path + '.rerun.json'
  @resource_types = @path + '.resource_types'
  @type = type

  @project_file = @path + 'project.yaml'
  @data = Bolt::Util.read_optional_yaml_hash(File.expand_path(@project_file), 'project') || {}
  validate if load_as_module?
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



15
16
17
# File 'lib/bolt/project.rb', line 15

def config_file
  @config_file
end

#hiera_configObject (readonly)

Returns the value of attribute hiera_config.



15
16
17
# File 'lib/bolt/project.rb', line 15

def hiera_config
  @hiera_config
end

#inventory_fileObject (readonly)

Returns the value of attribute inventory_file.



15
16
17
# File 'lib/bolt/project.rb', line 15

def inventory_file
  @inventory_file
end

#modulepathObject (readonly)

Returns the value of attribute modulepath.



15
16
17
# File 'lib/bolt/project.rb', line 15

def modulepath
  @modulepath
end

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/bolt/project.rb', line 15

def path
  @path
end

#puppetfileObject (readonly)

Returns the value of attribute puppetfile.



15
16
17
# File 'lib/bolt/project.rb', line 15

def puppetfile
  @puppetfile
end

#rerunfileObject (readonly)

Returns the value of attribute rerunfile.



15
16
17
# File 'lib/bolt/project.rb', line 15

def rerunfile
  @rerunfile
end

#resource_typesObject (readonly)

Returns the value of attribute resource_types.



15
16
17
# File 'lib/bolt/project.rb', line 15

def resource_types
  @resource_types
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/bolt/project.rb', line 15

def type
  @type
end

Class Method Details

.default_projectObject



18
19
20
# File 'lib/bolt/project.rb', line 18

def self.default_project
  Project.new(File.join('~', '.puppetlabs', 'bolt'), 'user')
end

.find_boltdir(dir) ⇒ Object

Search recursively up the directory hierarchy for the Project. Look for a directory called Boltdir or a file called bolt.yaml (for a control repo type Project). Otherwise, repeat the check on each directory up the hierarchy, falling back to the default if we reach the root.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bolt/project.rb', line 26

def self.find_boltdir(dir)
  dir = Pathname.new(dir)
  if (dir + BOLTDIR_NAME).directory?
    new(dir + BOLTDIR_NAME, 'embedded')
  elsif (dir + 'bolt.yaml').file?
    new(dir, 'local')
  elsif dir.root?
    default_project
  else
    find_boltdir(dir.parent)
  end
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


65
66
67
# File 'lib/bolt/project.rb', line 65

def eql?(other)
  path == other.path
end

#load_as_module?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/bolt/project.rb', line 70

def load_as_module?
  @project_file.file?
end

#nameObject



74
75
76
77
78
79
# File 'lib/bolt/project.rb', line 74

def name
  # If the project is in mymod/Boltdir/project.yaml, use mymod as the project name
  dirname = @path.basename.to_s == 'Boltdir' ? @path.parent.basename.to_s : @path.basename.to_s
  pname = @data['name'] || dirname
  pname.include?('-') ? pname.split('-', 2)[1] : pname
end

#plansObject



85
86
87
# File 'lib/bolt/project.rb', line 85

def plans
  @data['plans']
end

#project_directory_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/bolt/project.rb', line 89

def project_directory_name?(name)
  # it must match an installed project name according to forge validator
  name =~ /^[a-z][a-z0-9_]*$/
end

#project_namespaced_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/bolt/project.rb', line 94

def project_namespaced_name?(name)
  # it must match the full project name according to forge validator
  name =~ /^[a-zA-Z0-9]+[-][a-z][a-z0-9_]*$/
end

#tasksObject



81
82
83
# File 'lib/bolt/project.rb', line 81

def tasks
  @data['tasks']
end

#to_hObject

This API is used to prepend the project as a module to Puppet’s internal module_references list. CHANGE AT YOUR OWN RISK



61
62
63
# File 'lib/bolt/project.rb', line 61

def to_h
  { path: @path, name: name }
end

#to_sObject



55
56
57
# File 'lib/bolt/project.rb', line 55

def to_s
  @path.to_s
end

#validateObject



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

def validate
  n = @data['name']
  if n && !project_directory_name?(n) && !project_namespaced_name?(n)
    raise Bolt::ValidationError, <<~ERROR_STRING
    Invalid project name '#{n}' in project.yaml; project names must match either:
    An installed project name (ex. projectname) matching the expression /^[a-z][a-z0-9_]*$/ -or-
    A namespaced project name (ex. author-projectname) matching the expression /^[a-zA-Z0-9]+[-][a-z][a-z0-9_]*$/
    ERROR_STRING
  elsif !project_directory_name?(name) && !project_namespaced_name?(name)
    raise Bolt::ValidationError, <<~ERROR_STRING
    Invalid project name '#{name}'; project names must match either:
    A project name (ex. projectname) matching the expression /^[a-z][a-z0-9_]*$/ -or-
    A namespaced project name (ex. author-projectname) matching the expression /^[a-zA-Z0-9]+[-][a-z][a-z0-9_]*$/

    Configure project name in <project_dir>/project.yaml
    ERROR_STRING
  # If the project name is the same as one of the built-in modules raise a warning
  elsif Dir.children(Bolt::PAL::BOLTLIB_PATH).include?(name)
    raise Bolt::ValidationError, "The project '#{name}' will not be loaded. The project name conflicts "\
      "with a built-in Bolt module of the same name."
  end

  %w[tasks plans].each do |conf|
    unless @data.fetch(conf, []).is_a?(Array)
      raise Bolt::ValidationError, "'#{conf}' in project.yaml must be an array"
    end
  end
end