Class: VagrantPlugins::DSC::Config

Inherits:
Object
  • Object
show all
Extended by:
Vagrant::Util::Counter
Defined in:
lib/vagrant-dsc/config.rb

Overview

The “Configuration” represents a configuration of how the DSC provisioner should behave: data directories, working directory, DSC Manifests etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant-dsc/config.rb', line 62

def initialize
  super

  @configuration_file     = UNSET_VALUE
  @manifests_path         = UNSET_VALUE
  @configuration_name     = UNSET_VALUE
  @mof_path               = UNSET_VALUE
  @module_path            = UNSET_VALUE
  @configuration_params   = {}
  @synced_folder_type     = UNSET_VALUE
  @temp_dir               = UNSET_VALUE
  @logger = Log4r::Logger.new("vagrant::vagrant_dsc")
end

Instance Attribute Details

#configuration_fileObject

Relative path to the DSC Configuration file.

Path is relative to the folder containing the Vagrantfile.



25
26
27
# File 'lib/vagrant-dsc/config.rb', line 25

def configuration_file
  @configuration_file
end

#configuration_nameObject

The name of the Configuration module

Defaults to the basename of the “configuration_file” e.g. “Foo.ps1” becomes “Foo”



37
38
39
# File 'lib/vagrant-dsc/config.rb', line 37

def configuration_name
  @configuration_name
end

#configuration_paramsHash

Set of Parameters to pass to the DSC Configuration.

Returns:

  • (Hash)

    Set of k/v parameters to pass to DSC.



15
16
17
# File 'lib/vagrant-dsc/config.rb', line 15

def configuration_params
  @configuration_params
end

#expanded_configuration_fileObject

Fully qualified path to the configuration file.

Do not override this.



60
61
62
# File 'lib/vagrant-dsc/config.rb', line 60

def expanded_configuration_file
  @expanded_configuration_file
end

#manifests_pathObject

Relative path to the folder containing the root Configuration manifest file. Defaults to ‘manifests’.

Path is relative to the folder containing the Vagrantfile.



31
32
33
# File 'lib/vagrant-dsc/config.rb', line 31

def manifests_path
  @manifests_path
end

#module_pathArray

Set of module paths relative to the Vagrantfile dir.

These paths are added to the DSC Configuration running environment to enable local modules to be addressed.

Returns:

  • (Array)

    Set of relative module paths.



45
46
47
# File 'lib/vagrant-dsc/config.rb', line 45

def module_path
  @module_path
end

#mof_pathObject

Relative path to a folder, containing the pre-generated MOF file.

Path is relative to the folder containing the Vagrantfile.



20
21
22
# File 'lib/vagrant-dsc/config.rb', line 20

def mof_path
  @mof_path
end

#synced_folder_typeObject

The type of synced folders to use when sharing the data required for the provisioner to work properly.

By default this will use the default synced folder type. For example, you can set this to “nfs” to use NFS synced folders.



52
53
54
# File 'lib/vagrant-dsc/config.rb', line 52

def synced_folder_type
  @synced_folder_type
end

#temp_dirObject

Temporary working directory on the guest machine.



55
56
57
# File 'lib/vagrant-dsc/config.rb', line 55

def temp_dir
  @temp_dir
end

Instance Method Details

#expanded_module_paths(root_path) ⇒ Array

Returns the module paths as an array of paths expanded relative to the root path.

Parameters:

  • root_path (String|Array)

    The relative path to expand module paths against.

Returns:

  • (Array)

    Set of fully qualified paths to the modules directories.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vagrant-dsc/config.rb', line 110

def expanded_module_paths(root_path)
  return [] if !module_path

  # Get all the paths and expand them relative to the root path, returning
  # the array of expanded paths
  paths = module_path
  paths = [paths] if !paths.is_a?(Array)
  paths.map do |path|
    Pathname.new(path).expand_path(root_path).to_s
  end
end

#finalize!Object

Final step of the Configuration lifecyle prior to validation.

Ensures all attributes are set to defaults if not provided.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant-dsc/config.rb', line 80

def finalize!
  super

  # Null checks
  @configuration_file = "default.ps1" if @configuration_file == UNSET_VALUE
  @module_path        = nil if @module_path == UNSET_VALUE
  @synced_folder_type = nil if @synced_folder_type == UNSET_VALUE
  @temp_dir           = nil if @temp_dir == UNSET_VALUE
  @mof_path           = nil if @mof_path == UNSET_VALUE
  @configuration_name = File.basename(@configuration_file, File.extname(@configuration_file)) if @configuration_name == UNSET_VALUE
  @manifests_path     = File.dirname(@configuration_file) if @manifests_path == UNSET_VALUE

  # Can't supply them both!
  if (@configuration_file != nil && @mof_path != nil)
    raise DSCError, :manifest_and_mof_provided
  end

  # Set a default temp dir that has an increasing counter so
  # that multiple DSC definitions won't overwrite each other
  if !@temp_dir
    counter   = self.class.get_and_update_counter(:dsc_config)
    @temp_dir = "/tmp/vagrant-dsc-#{counter}"
  end
end

#validate(machine) ⇒ Hash

Validate configuration and return a hash of errors.

Does not check that DSC itself is properly configured, which is performed at run-time.

Parameters:

  • The (Machine)

    current Machine

Returns:

  • (Hash)

    Any errors or {} if no errors found



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
# File 'lib/vagrant-dsc/config.rb', line 129

def validate(machine)
  @logger.info("==> Configurin' DSC man!")
  errors = _detected_errors

  # Calculate the manifests and module paths based on env
  this_expanded_module_paths = expanded_module_paths(machine.env.root_path)

  # Manifest file validation
  this_expanded_module_paths.each do |path|
    errors << I18n.t("vagrant_dsc.errors.module_path_missing", path: path) if !Pathname.new(path).expand_path(machine.env.root_path).directory?
  end

  expanded_path = Pathname.new(manifests_path).
      expand_path(machine.env.root_path)

  if !expanded_path.directory?
    errors << I18n.t("vagrant_dsc.errors.manifests_path_missing",
                     path: expanded_path.to_s)
  end

  @expanded_configuration_file = expanded_path.join(File.basename(configuration_file))
  if !expanded_configuration_file.file? && !expanded_configuration_file.directory?
    errors << I18n.t("vagrant_dsc.errors.manifest_missing",
                     manifest: expanded_configuration_file.to_s)
  end

  { "dsc provisioner" => errors }
end