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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-dsc/config.rb', line 74

def initialize
  super

  @configuration_file       = UNSET_VALUE
  @configuration_data_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_data_fileObject

Relative path to the DSC Configuration Data file.

Configuration data is used to parameterise the configuration_file.

Path is relative to the folder containing the Vagrantfile.



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

def configuration_data_file
  @configuration_data_file
end

#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”



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

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_data_fileObject

Fully qualified path to the configuration data file.

Do not override this.



72
73
74
# File 'lib/vagrant-dsc/config.rb', line 72

def expanded_configuration_data_file
  @expanded_configuration_data_file
end

#expanded_configuration_fileObject

Fully qualified path to the configuration file.

Do not override this.



67
68
69
# File 'lib/vagrant-dsc/config.rb', line 67

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.



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

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.



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

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.



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

def synced_folder_type
  @synced_folder_type
end

#temp_dirObject

Temporary working directory on the guest machine.



62
63
64
# File 'lib/vagrant-dsc/config.rb', line 62

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.



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vagrant-dsc/config.rb', line 124

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.



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

def finalize!
  super

  # Null checks
  @configuration_file       = "default.ps1" if @configuration_file == UNSET_VALUE
  @configuration_data_file  = nil if @configuration_data_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



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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/vagrant-dsc/config.rb', line 143

def validate(machine)
  @logger.info("==> Configuring DSC")
  errors = _detected_errors

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

  # Manifest file validation
  local_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

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

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

  # Path to manifest file on the host machine must exist
  host_expanded_configuration_file = host_manifest_path.join(File.basename(configuration_file))
  if !host_expanded_configuration_file.file? && !host_expanded_configuration_file.directory?
    errors << I18n.t("vagrant_dsc.errors.manifest_missing",
                     manifest: host_expanded_configuration_file.to_s)
  end

  # Set absolute path to manifest file on the guest
  @expanded_configuration_file = Pathname.new(File.dirname(configuration_file)).expand_path(temp_dir).join(File.basename(configuration_file))

  # Check path of the configuration data file on host
  if configuration_data_file != nil

    host_expanded_path = Pathname.new(File.dirname(configuration_data_file)).expand_path(machine.env.root_path)
    expanded_host_configuration_data_file = host_expanded_path.join(File.basename(configuration_data_file))

    if !expanded_host_configuration_data_file.file? && !expanded_host_configuration_data_file.directory?
      errors << I18n.t("vagrant_dsc.errors.configuration_data_missing",
                       path: expanded_host_configuration_data_file.to_s)
    end

    @expanded_configuration_data_file = Pathname.new(File.dirname(configuration_data_file)).expand_path(temp_dir).join(File.basename(configuration_data_file))
  end

  { "dsc provisioner" => errors }
end