Class: Puppet::X::Jenkins::Provider::Cli

Inherits:
Provider
  • Object
show all
Defined in:
lib/puppet/x/jenkins/provider/cli.rb

Defined Under Namespace

Classes: AuthError, NetError, UnknownError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cli(command, options = {}, cli_pre_cmd = []) ⇒ Object



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
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 119

def self.cli(command, options = {}, cli_pre_cmd = [])
  if options.nil? || (!options.key?(:stdinjson) && !options.key?(:stdin))
    return execute_with_retry(command, options, cli_pre_cmd)
  end

  if options.key?(:stdinjson)
    data = options.delete(:stdinjson)
    input = JSON.pretty_generate(data)
  end

  input = options.delete(:stdin) if options.key?(:stdin)

  tmpfile_as_param = if options.key?(:tmpfile_as_param)
                       options[:tmpfile_as_param]
                     else
                       false
                     end

  Puppet.debug("#{sname} stdin:\n#{input}")

  # a tempfile block arg is not used to simplify mock testing :/
  tmp = Tempfile.open(sname)
  tmp.write input
  tmp.flush
  options[:stdinfile] = tmp.path
  begin
    Etc.getpwnam('jenkins')
    FileUtils.chown 'jenkins', 'jenkins', tmp.path if tmpfile_as_param && File.exist?(tmp.path)
  rescue StandardError
    FileUtils.chmod 0o644, tmp.path if tmpfile_as_param && File.exist?(tmp.path)
  end
  result = execute_with_retry(command, options, cli_pre_cmd)
  tmp.close
  tmp.unlink

  result
end

.clihelper(command, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 103

def self.clihelper(command, options = {})
  catalog = options.key?(:catalog) ? options[:catalog] : nil
  config = Puppet::X::Jenkins::Config.new(catalog)

  puppet_helper = config[:puppet_helper]

  cli_pre_cmd = ['/bin/cat', puppet_helper, '|']
  cli_cmd = ['groovy', '='] + [command]
  options[:tmpfile_as_param] = true

  cli_pre_cmd.flatten!
  cli_cmd.flatten!

  cli(cli_cmd, options, cli_pre_cmd)
end

.inherited(subclass) ⇒ Object

push a shallow copy of the class confines into the subclass this includes the confine(s) needed for commands

The subclass seems to function with an empty @commands and any value we try to push in will get reselt by ::initvars.



27
28
29
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 27

def self.inherited(subclass) # rubocop:todo Lint/MissingSuper
  subclass.instance_variable_set(:@confine_collection, @confine_collection.dup)
end

.prefetch(resources) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 49

def self.prefetch(resources)
  Puppet.debug("#{sname} prefetch: #{resources.each_key.collect.to_a}")

  catalog = resources.first[1].catalog

  instances(catalog).each do |prov|
    if (resource = resources[prov.name])
      resource.provider = prov
    end
  end
end

.snameObject

shorter class name



45
46
47
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 45

def self.sname
  to_s[%r{.+::(Jenkins.+)}, 1]
end

Instance Method Details

#cli(command, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 91

def cli(command, options = {})
  if resource&.catalog
    options[:catalog] ||= resource.catalog
  end

  if options.empty? # Ruby < 2.7 hack
    self.class.cli(command)
  else
    self.class.cli(command, **options)
  end
end

#clihelper(command, options = {}) ⇒ Object

if the provider instance has a resource (which it should outside of testing), add :catalog to the options hash so the caller doesn’t have to



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 79

def clihelper(command, options = {})
  if resource&.catalog
    options[:catalog] ||= resource.catalog
  end

  if options.empty? # Ruby < 2.7 hack
    self.class.clihelper(command)
  else
    self.class.clihelper(command, **options)
  end
end

#createObject



61
62
63
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 61

def create
  @property_hash[:ensure] = :present
end

#destroyObject



69
70
71
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 69

def destroy
  @property_hash[:ensure] = :absent
end

#exists?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 65

def exists?
  @property_hash[:ensure] == :present
end

#flushObject



73
74
75
# File 'lib/puppet/x/jenkins/provider/cli.rb', line 73

def flush
  @property_hash.clear
end