Class: Bolt::Project
- Inherits:
-
Object
- Object
- Bolt::Project
- Defined in:
- lib/bolt/project.rb
Constant Summary collapse
- BOLTDIR_NAME =
'Boltdir'
- CONFIG_NAME =
'bolt-project.yaml'
Instance Attribute Summary collapse
-
#backup_dir ⇒ Object
readonly
Returns the value of attribute backup_dir.
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#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.
-
#managed_moduledir ⇒ Object
readonly
Returns the value of attribute managed_moduledir.
-
#modulepath ⇒ Object
readonly
Returns the value of attribute modulepath.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#plan_cache_file ⇒ Object
readonly
Returns the value of attribute plan_cache_file.
-
#plans_path ⇒ Object
readonly
Returns the value of attribute plans_path.
-
#plugin_cache_file ⇒ Object
readonly
Returns the value of attribute plugin_cache_file.
-
#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') ⇒ Object
- .default_project ⇒ Object
-
.find_boltdir(dir) ⇒ Object
Search recursively up the directory hierarchy for the Project.
-
.schema ⇒ Object
Builds the schema for bolt-project.yaml used by the validator.
Instance Method Summary collapse
- #check_deprecated_file ⇒ Object
- #disable_warnings ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(raw_data, path, type = 'option') ⇒ Project
constructor
A new instance of Project.
- #load_as_module? ⇒ Boolean
- #module_install ⇒ Object
- #modules ⇒ Object
- #name ⇒ Object
- #plans ⇒ Object
- #plugin_cache ⇒ 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') ⇒ Project
Returns a new instance of Project.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bolt/project.rb', line 113 def initialize(raw_data, path, type = 'option') @path = Pathname.new(path). @project_file = @path + CONFIG_NAME if (@path + 'bolt.yaml').file? && project_file? Bolt::Logger.deprecate( "bolt_yaml", "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." ) end @inventory_file = @path + 'inventory.yaml' @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' @managed_moduledir = @path + '.modules' @backup_dir = @path + '.bolt-bak' @plugin_cache_file = @path + '.plugin_cache.json' @plan_cache_file = @path + '.plan_cache.json' if (tc = Bolt::Config::INVENTORY_OPTIONS.keys & raw_data.keys).any? Bolt::Logger.warn( "project_transport_config", "Transport configuration isn't supported in bolt-project.yaml. Ignoring keys #{tc}." ) end @data = raw_data.reject { |k, _| Bolt::Config::INVENTORY_OPTIONS.include?(k) } # If the 'modules' key is present in the project configuration file, # use the new, shorter modulepath. @modulepath = if @data.key?('modules') [(@path + 'modules').to_s] else [(@path + 'modules').to_s, (@path + 'site-modules').to_s, (@path + 'site').to_s] end # 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? Bolt::Logger.warn( "project_config_conflict", "bolt-project.yaml contains valid config keys, bolt.yaml will be ignored" ) end @project_file else @path + 'bolt.yaml' end validate if project_file? end |
Instance Attribute Details
#backup_dir ⇒ Object (readonly)
Returns the value of attribute backup_dir.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def backup_dir @backup_dir end |
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def config_file @config_file end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def data @data end |
#downloads ⇒ Object (readonly)
Returns the value of attribute downloads.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def downloads @downloads end |
#hiera_config ⇒ Object (readonly)
Returns the value of attribute hiera_config.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def hiera_config @hiera_config end |
#inventory_file ⇒ Object (readonly)
Returns the value of attribute inventory_file.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def inventory_file @inventory_file end |
#managed_moduledir ⇒ Object (readonly)
Returns the value of attribute managed_moduledir.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def managed_moduledir @managed_moduledir end |
#modulepath ⇒ Object (readonly)
Returns the value of attribute modulepath.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def modulepath @modulepath end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def path @path end |
#plan_cache_file ⇒ Object (readonly)
Returns the value of attribute plan_cache_file.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def plan_cache_file @plan_cache_file end |
#plans_path ⇒ Object (readonly)
Returns the value of attribute plans_path.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def plans_path @plans_path end |
#plugin_cache_file ⇒ Object (readonly)
Returns the value of attribute plugin_cache_file.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def plugin_cache_file @plugin_cache_file end |
#project_file ⇒ Object (readonly)
Returns the value of attribute project_file.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def project_file @project_file end |
#puppetfile ⇒ Object (readonly)
Returns the value of attribute puppetfile.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def puppetfile @puppetfile end |
#rerunfile ⇒ Object (readonly)
Returns the value of attribute rerunfile.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def rerunfile @rerunfile end |
#resource_types ⇒ Object (readonly)
Returns the value of attribute resource_types.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def resource_types @resource_types end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/bolt/project.rb', line 14 def type @type end |
Class Method Details
.create_project(path, type = 'option') ⇒ Object
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bolt/project.rb', line 57 def self.create_project(path, type = 'option') fullpath = Pathname.new(path). if type == 'user' begin # This is already expanded if the type is user FileUtils.mkdir_p(path) rescue StandardError Bolt::Logger.warn( "non_writeable_project", "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, CONFIG_NAME) data = Bolt::Util.read_optional_yaml_hash(File.(project_file), 'project') default = type =~ /user|system/ ? 'default ' : '' exist = File.exist?(File.(project_file)) if exist Bolt::Logger.info("Loaded #{default}project from '#{fullpath}'") end Bolt::Validator.new.tap do |validator| validator.validate(data, schema, project_file) validator.warnings.each { |warning| Bolt::Logger.warn(warning[:id], warning[:msg]) } validator.deprecations.each { |dep| Bolt::Logger.deprecate(dep[:id], dep[:msg]) } end new(data, path, type) end |
.default_project ⇒ Object
19 20 21 22 23 24 |
# File 'lib/bolt/project.rb', line 19 def self.default_project create_project(File.(File.join('~', '.puppetlabs', 'bolt')), 'user') # If homedir isn't defined use the system config path rescue ArgumentError create_project(Bolt::Config.system_path, 'system') 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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bolt/project.rb', line 30 def self.find_boltdir(dir) dir = Pathname.new(dir) if (dir + BOLTDIR_NAME).directory? create_project(dir + BOLTDIR_NAME, 'embedded') elsif (dir + 'bolt.yaml').file? command = Bolt::Util.powershell? ? 'Update-BoltProject' : 'bolt project migrate' Bolt::Logger.deprecate( "bolt_yaml", "Configuration file #{dir + 'bolt.yaml'} is deprecated and will be "\ "removed in Bolt 3.0.\nUpdate your Bolt project to the latest Bolt practices "\ "using #{command}." ) create_project(dir, 'local') elsif (dir + CONFIG_NAME).file? create_project(dir, 'local') elsif dir.root? default_project else Bolt::Logger.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) end end |
.schema ⇒ Object
Builds the schema for bolt-project.yaml used by the validator.
105 106 107 108 109 110 111 |
# File 'lib/bolt/project.rb', line 105 def self.schema { type: Hash, properties: Bolt::Config::BOLT_PROJECT_OPTIONS.map { |opt| [opt, _ref: opt] }.to_h, definitions: Bolt::Config::OPTIONS } end |
Instance Method Details
#check_deprecated_file ⇒ Object
252 253 254 255 256 257 |
# File 'lib/bolt/project.rb', line 252 def check_deprecated_file if (@path + 'project.yaml').file? msg = "Project configuration file 'project.yaml' is deprecated; use 'bolt-project.yaml' instead." Bolt::Logger.warn("project_yaml", msg) end end |
#disable_warnings ⇒ Object
217 218 219 |
# File 'lib/bolt/project.rb', line 217 def disable_warnings @data['disable-warnings'] || [] end |
#eql?(other) ⇒ Boolean Also known as: ==
184 185 186 |
# File 'lib/bolt/project.rb', line 184 def eql?(other) path == other.path end |
#load_as_module? ⇒ Boolean
193 194 195 |
# File 'lib/bolt/project.rb', line 193 def load_as_module? !name.nil? end |
#module_install ⇒ Object
213 214 215 |
# File 'lib/bolt/project.rb', line 213 def module_install @data['module-install'] end |
#modules ⇒ Object
221 222 223 224 225 226 227 228 229 |
# File 'lib/bolt/project.rb', line 221 def modules @modules ||= @data['modules']&.map do |mod| if mod.is_a?(String) { 'name' => mod } else mod end end end |
#name ⇒ Object
197 198 199 |
# File 'lib/bolt/project.rb', line 197 def name @data['name'] end |
#plans ⇒ Object
205 206 207 |
# File 'lib/bolt/project.rb', line 205 def plans @data['plans'] end |
#plugin_cache ⇒ Object
209 210 211 |
# File 'lib/bolt/project.rb', line 209 def plugin_cache @data['plugin-cache'] end |
#project_file? ⇒ Boolean
189 190 191 |
# File 'lib/bolt/project.rb', line 189 def project_file? @project_file.file? end |
#tasks ⇒ Object
201 202 203 |
# File 'lib/bolt/project.rb', line 201 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
178 179 180 181 182 |
# File 'lib/bolt/project.rb', line 178 def to_h { path: @path.to_s, name: name, load_as_module?: load_as_module? } end |
#to_s ⇒ Object
172 173 174 |
# File 'lib/bolt/project.rb', line 172 def to_s @path.to_s end |
#validate ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/bolt/project.rb', line 231 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::Config::Modulepath::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 elsif name.nil? && (File.directory?(plans_path) || File.directory?(@path + 'tasks') || File.directory?(@path + 'files')) = "No project name is specified in bolt-project.yaml. Project-level content will not be available." Bolt::Logger.warn("missing_project_name", ) end end |