Class: PDK::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/config.rb,
lib/pdk/config/json.rb,
lib/pdk/config/yaml.rb,
lib/pdk/config/errors.rb,
lib/pdk/config/setting.rb,
lib/pdk/config/namespace.rb,
lib/pdk/config/validator.rb,
lib/pdk/config/json_with_schema.rb,
lib/pdk/config/yaml_with_schema.rb,
lib/pdk/config/json_schema_setting.rb,
lib/pdk/config/json_schema_namespace.rb

Defined Under Namespace

Modules: Validator Classes: JSON, JSONSchemaNamespace, JSONSchemaSetting, JSONWithSchema, LoadError, Namespace, Setting, YAML, YAMLWithSchema

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.analytics_config_exist?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/pdk/config.rb', line 77

def self.analytics_config_exist?
  PDK::Util::Filesystem.file?(analytics_config_path)
end

.analytics_config_interview!Object



81
82
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
# File 'lib/pdk/config.rb', line 81

def self.analytics_config_interview!
  require 'pdk/cli/util'

  return unless PDK::CLI::Util.interactive?

  pre_message = _(
    'PDK collects anonymous usage information to help us understand how ' \
    'it is being used and make decisions on how to improve it. You can ' \
    'find out more about what data we collect and how it is used in the ' \
    "PDK documentation at %{url}.\n",
  ) % { url: 'https://puppet.com/docs/pdk/latest/pdk_install.html' }
  post_message = _(
    'You can opt in or out of the usage data collection at any time by ' \
    'editing the analytics configuration file at %{path} and changing ' \
    "the '%{key}' value.",
  ) % {
    path: PDK::Config.analytics_config_path,
    key:  'disabled',
  }

  questions = [
    {
      name:     'enabled',
      question: _('Do you consent to the collection of anonymous PDK usage information?'),
      type:     :yes,
    },
  ]

  require 'pdk/cli/util/interview'

  PDK.logger.info(text: pre_message, wrap: true)
  prompt = TTY::Prompt.new(help_color: :cyan)
  interview = PDK::CLI::Util::Interview.new(prompt)
  interview.add_questions(questions)
  answers = interview.run

  if answers.nil?
    PDK.logger.info _('No answer given, opting out of analytics collection.')
    PDK.config.user['analytics']['disabled'] = true
  else
    PDK.config.user['analytics']['disabled'] = !answers['enabled']
  end

  PDK.logger.info(text: post_message, wrap: true)
end

.analytics_config_pathObject



60
61
62
# File 'lib/pdk/config.rb', line 60

def self.analytics_config_path
  PDK::Util::Env['PDK_ANALYTICS_CONFIG'] || File.join(File.dirname(PDK::Util.configdir), 'puppet', 'analytics.yml')
end

.bolt_analytics_configObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/pdk/config.rb', line 49

def self.bolt_analytics_config
  file = File.expand_path('~/.puppetlabs/bolt/analytics.yaml')
  PDK::Config::YAML.new(file: file)
rescue PDK::Config::LoadError => e
  PDK.logger.debug _('Unable to load %{file}: %{message}') % {
    file:    file,
    message: e.message,
  }
  PDK::Config::YAML.new
end

.json_schema(name) ⇒ Object

return nil if not exist



73
74
75
# File 'lib/pdk/config.rb', line 73

def self.json_schema(name)
  File.join(json_schemas_path, name + '_schema.json')
end

.json_schemas_pathObject



68
69
70
# File 'lib/pdk/config.rb', line 68

def self.json_schemas_path
  File.join(__dir__, 'config')
end

.user_config_pathObject



64
65
66
# File 'lib/pdk/config.rb', line 64

def self.user_config_path
  File.join(PDK::Util.configdir, 'user_config.json')
end

Instance Method Details

#resolve(filter = nil) ⇒ Hash{String => Object}

Resolves all filtered settings from all namespaces

Parameters:

  • filter (String) (defaults to: nil)

    Only resolve setting names which match the filter. See PDK::Config::Namespace.be_resolved? for matching rules

Returns:

  • (Hash{String => Object})

    All resolved settings for example => ‘johndoe’



45
46
47
# File 'lib/pdk/config.rb', line 45

def resolve(filter = nil)
  user.resolve(filter)
end

#userObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pdk/config.rb', line 14

def user
  @user ||= PDK::Config::JSON.new('user', file: PDK::Config.user_config_path) do
    mount :module_defaults, PDK::Config::JSON.new(file: PDK.answers.answer_file_path)

    # Due to the json-schema gem having issues with Windows based paths, and only supporting Draft 05 (or less) do
    # not use JSON validation yet.  Once PDK drops support for EOL rubies, we will be able to use the json_schemer gem
    # Which has much more modern support
    # Reference - https://github.com/puppetlabs/pdk/pull/777
    # Reference - https://tickets.puppetlabs.com/browse/PDK-1526
    mount :analytics, PDK::Config::YAML.new(file: PDK::Config.analytics_config_path, persistent_defaults: true) do
      setting :disabled do
        validate PDK::Config::Validator.boolean
        default_to { PDK::Config.bolt_analytics_config.fetch('disabled', true) }
      end

      setting 'user-id' do
        validate PDK::Config::Validator.uuid
        default_to do
          require 'securerandom'

          PDK::Config.bolt_analytics_config.fetch('user-id', SecureRandom.uuid)
        end
      end
    end
  end
end