Class: Bolt::Project
- Inherits:
-
Object
- Object
- Bolt::Project
- 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 show, if they exist in the project."\ "These plans are included in `bolt plan show` output", "tasks" => "An array of task names to show, if they exist in the project."\ "These tasks are included in `bolt task show` output" }.freeze
Instance Attribute Summary collapse
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#deprecations ⇒ Object
readonly
Returns the value of attribute deprecations.
-
#downloads ⇒ Object
readonly
Returns the value of attribute downloads.
-
#hiera_config ⇒ Object
readonly
Returns the value of attribute hiera_config.
-
#inventory_file ⇒ Object
readonly
Returns the value of attribute inventory_file.
-
#logs ⇒ Object
readonly
Returns the value of attribute logs.
-
#modulepath ⇒ Object
readonly
Returns the value of attribute modulepath.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#plans_path ⇒ Object
readonly
Returns the value of attribute plans_path.
-
#project_file ⇒ Object
readonly
Returns the value of attribute project_file.
-
#puppetfile ⇒ Object
readonly
Returns the value of attribute puppetfile.
-
#rerunfile ⇒ Object
readonly
Returns the value of attribute rerunfile.
-
#resource_types ⇒ Object
readonly
Returns the value of attribute resource_types.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .create_project(path, type = 'option', logs = []) ⇒ Object
- .default_project(logs = []) ⇒ Object
-
.find_boltdir(dir, logs = []) ⇒ Object
Search recursively up the directory hierarchy for the Project.
Instance Method Summary collapse
- #check_deprecated_file ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(raw_data, path, type = 'option', logs = []) ⇒ Project
constructor
A new instance of Project.
- #load_as_module? ⇒ Boolean
- #modules ⇒ Object
- #name ⇒ Object
- #plans ⇒ Object
- #project_file? ⇒ Boolean
- #tasks ⇒ Object
-
#to_h ⇒ Object
This API is used to prepend the project as a module to Puppet’s internal module_references list.
- #to_s ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(raw_data, path, type = 'option', logs = []) ⇒ Project
Returns a new instance of Project.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 127 |
# File 'lib/bolt/project.rb', line 83 def initialize(raw_data, path, type = 'option', logs = []) @path = Pathname.new(path). @project_file = @path + 'bolt-project.yaml' @logs = logs @deprecations = [] if (@path + 'bolt.yaml').file? && project_file? msg = "Project-level configuration in bolt.yaml is deprecated if using bolt-project.yaml. "\ "Transport config should be set in inventory.yaml, all other config should be set in "\ "bolt-project.yaml." @deprecations << { type: 'Using bolt.yaml for project configuration', msg: msg } end @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 @downloads = @path + 'downloads' @plans_path = @path + 'plans' tc = Bolt::Config::INVENTORY_OPTIONS.keys & raw_data.keys if tc.any? msg = "Transport configuration isn't supported in bolt-project.yaml. Ignoring keys #{tc}" @logs << { warn: msg } end @data = raw_data.reject { |k, _| Bolt::Config::INVENTORY_OPTIONS.include?(k) } # Once bolt.yaml deprecation is removed, this attribute should be removed # and replaced with .project_file in lib/bolt/config.rb @config_file = if (Bolt::Config::BOLT_OPTIONS & @data.keys).any? if (@path + 'bolt.yaml').file? msg = "bolt-project.yaml contains valid config keys, bolt.yaml will be ignored" @logs << { warn: msg } end @project_file else @path + 'bolt.yaml' end validate if project_file? end |
Instance Attribute Details
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def config_file @config_file end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def data @data end |
#deprecations ⇒ Object (readonly)
Returns the value of attribute deprecations.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def deprecations @deprecations end |
#downloads ⇒ Object (readonly)
Returns the value of attribute downloads.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def downloads @downloads end |
#hiera_config ⇒ Object (readonly)
Returns the value of attribute hiera_config.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def hiera_config @hiera_config end |
#inventory_file ⇒ Object (readonly)
Returns the value of attribute inventory_file.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def inventory_file @inventory_file end |
#logs ⇒ Object (readonly)
Returns the value of attribute logs.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def logs @logs end |
#modulepath ⇒ Object (readonly)
Returns the value of attribute modulepath.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def modulepath @modulepath end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def path @path end |
#plans_path ⇒ Object (readonly)
Returns the value of attribute plans_path.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def plans_path @plans_path end |
#project_file ⇒ Object (readonly)
Returns the value of attribute project_file.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def project_file @project_file end |
#puppetfile ⇒ Object (readonly)
Returns the value of attribute puppetfile.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def puppetfile @puppetfile end |
#rerunfile ⇒ Object (readonly)
Returns the value of attribute rerunfile.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def rerunfile @rerunfile end |
#resource_types ⇒ Object (readonly)
Returns the value of attribute resource_types.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def resource_types @resource_types end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
19 20 21 |
# File 'lib/bolt/project.rb', line 19 def type @type end |
Class Method Details
.create_project(path, type = 'option', logs = []) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bolt/project.rb', line 50 def self.create_project(path, type = 'option', logs = []) fullpath = Pathname.new(path). if type == 'user' begin # This is already expanded if the type is user FileUtils.mkdir_p(path) rescue StandardError logs << { warn: "Could not create default project at #{path}. Continuing without a writeable project. "\ "Log and rerun files will not be written." } end end if type == 'option' && !File.directory?(path) raise Bolt::Error.new("Could not find project at #{path}", "bolt/project-error") end if !Bolt::Util.windows? && type != 'environment' && fullpath.world_writable? raise Bolt::Error.new( "Project directory '#{fullpath}' is world-writable which poses a security risk. Set "\ "BOLT_PROJECT='#{fullpath}' to force the use of this project directory.", "bolt/world-writable-error" ) end project_file = File.join(fullpath, 'bolt-project.yaml') data = Bolt::Util.read_optional_yaml_hash(File.(project_file), 'project') default = type =~ /user|system/ ? 'default ' : '' exist = File.exist?(File.(project_file)) logs << { info: "Loaded #{default}project from '#{fullpath}'" } if exist new(data, path, type, logs) end |
.default_project(logs = []) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/bolt/project.rb', line 23 def self.default_project(logs = []) create_project(File.(File.join('~', '.puppetlabs', 'bolt')), 'user', logs) # If homedir isn't defined use the system config path rescue ArgumentError create_project(Bolt::Config.system_path, 'system', logs) end |
.find_boltdir(dir, logs = []) ⇒ 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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bolt/project.rb', line 34 def self.find_boltdir(dir, logs = []) dir = Pathname.new(dir) if (dir + BOLTDIR_NAME).directory? create_project(dir + BOLTDIR_NAME, 'embedded', logs) elsif (dir + 'bolt.yaml').file? || (dir + 'bolt-project.yaml').file? create_project(dir, 'local', logs) elsif dir.root? default_project(logs) else logs << { debug: "Did not detect Boltdir, bolt.yaml, or bolt-project.yaml at '#{dir}'. "\ "This directory won't be loaded as a project." } find_boltdir(dir.parent, logs) end end |
Instance Method Details
#check_deprecated_file ⇒ Object
209 210 211 212 213 214 |
# File 'lib/bolt/project.rb', line 209 def check_deprecated_file if (@path + 'project.yaml').file? msg = "Project configuration file 'project.yaml' is deprecated; use 'bolt-project.yaml' instead." Bolt::Logger.deprecation_warning('Using project.yaml instead of bolt-project.yaml', msg) end end |
#eql?(other) ⇒ Boolean Also known as: ==
141 142 143 |
# File 'lib/bolt/project.rb', line 141 def eql?(other) path == other.path end |
#load_as_module? ⇒ Boolean
150 151 152 |
# File 'lib/bolt/project.rb', line 150 def load_as_module? !name.nil? end |
#modules ⇒ Object
166 167 168 |
# File 'lib/bolt/project.rb', line 166 def modules @data['modules'] end |
#name ⇒ Object
154 155 156 |
# File 'lib/bolt/project.rb', line 154 def name @data['name'] end |
#plans ⇒ Object
162 163 164 |
# File 'lib/bolt/project.rb', line 162 def plans @data['plans'] end |
#project_file? ⇒ Boolean
146 147 148 |
# File 'lib/bolt/project.rb', line 146 def project_file? @project_file.file? end |
#tasks ⇒ Object
158 159 160 |
# File 'lib/bolt/project.rb', line 158 def tasks @data['tasks'] end |
#to_h ⇒ Object
This API is used to prepend the project as a module to Puppet’s internal module_references list. CHANGE AT YOUR OWN RISK
135 136 137 138 139 |
# File 'lib/bolt/project.rb', line 135 def to_h { path: @path.to_s, name: name, load_as_module?: load_as_module? } end |
#to_s ⇒ Object
129 130 131 |
# File 'lib/bolt/project.rb', line 129 def to_s @path.to_s end |
#validate ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/bolt/project.rb', line 170 def validate if name if name !~ Bolt::Module::MODULE_NAME_REGEX raise Bolt::ValidationError, <<~ERROR_STRING Invalid project name '#{name}' in bolt-project.yaml; project name must begin with a lowercase letter and can include lowercase letters, numbers, and underscores. ERROR_STRING 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 else = "No project name is specified in bolt-project.yaml. Project-level content will not be available." @logs << { warn: } end %w[tasks plans].each do |conf| unless @data.fetch(conf, []).is_a?(Array) raise Bolt::ValidationError, "'#{conf}' in bolt-project.yaml must be an array" end end if @data['modules'] unless @data['modules'].is_a?(Array) raise Bolt::ValidationError, "'modules' in bolt-project.yaml must be an array" end @data['modules'].each do |mod| next if mod.is_a?(Hash) raise Bolt::ValidationError, "Module declaration #{mod.inspect} must be a hash" end unknown_keys = data['modules'].flat_map(&:keys).uniq - %w[name version_requirement] if unknown_keys.any? @logs << { warn: "Ignoring unknown keys in module declarations: #{unknown_keys.join(', ')}." } end end end |