Class: Xolo::Admin::Title

Inherits:
Core::BaseClasses::Title show all
Defined in:
lib/xolo/admin/title.rb

Overview

A title used by xadm.

These are instantiated with data from the server (for existing Titles) or from the xadm CLI opts or walkthru process.

This class also defines how xadm communicates title data to and from the server.

Constant Summary collapse

SERVER_ROUTE =

This is the server path for dealing with titles POST to add a new one GET to get a list of titles GET …/<title> to get the data for a single title PUT …/<title> to update a title with new data DELETE …/<title> to delete a title and its version

'/titles'
UPLOAD_ICON_ROUTE =
'ssvc-icon'
TARGET_TITLE_PLACEHOLDER =
'TARGET_TITLE_PH'

Constants inherited from Core::BaseClasses::Title

Core::BaseClasses::Title::ATTRIBUTES, Core::BaseClasses::Title::MIN_TITLE_DESC_LENGTH

Instance Attribute Summary collapse

Attributes inherited from Core::BaseClasses::Title

#contact_email, #created_by, #creation_date, #expire_paths, #jamf_patch_title_id, #modification_date, #modified_by, #released_version, #self_service, #self_service_category, #self_service_icon, #ted_id_number, #version_order

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::BaseClasses::Title

#autopkg_enabled?, #latest_version, #managed?

Methods inherited from Core::BaseClasses::ServerObject

#to_h, #to_json

Methods included from Core::JSONWrappers

extended, included, #parse_json

Constructor Details

#initialize(data_hash) ⇒ Title

Read in the contents of any version script given



134
135
136
137
138
139
140
141
142
# File 'lib/xolo/admin/title.rb', line 134

def initialize(data_hash)
  super
  # @self_service_icon = nil if @self_service_icon == Xolo::ITEM_UPLOADED

  return unless version_script
  return if version_script == Xolo::ITEM_UPLOADED

  @version_script = version_script.read if version_script.respond_to?(:read)
end

Instance Attribute Details

#cnxObject

Attributes

the server connection used to fetch this version



127
128
129
# File 'lib/xolo/admin/title.rb', line 127

def cnx
  @cnx
end

Class Method Details

.all_title_objects(cnx) ⇒ Array<Xolo::Admin::Title>

Returns The currently known titles on the server.

Returns:



66
67
68
69
# File 'lib/xolo/admin/title.rb', line 66

def self.all_title_objects(cnx)
  resp = cnx.get SERVER_ROUTE
  resp.body.map { |td| Xolo::Admin::Title.new td }
end

.all_titles(cnx) ⇒ Array<String>

Returns The currently known titles names on the server.

Returns:

  • (Array<String>)

    The currently known titles names on the server



59
60
61
62
# File 'lib/xolo/admin/title.rb', line 59

def self.all_titles(cnx)
  resp = cnx.get SERVER_ROUTE
  resp.body.map { |t| t[:title] }
end

.cli_optsHash{Symbol: Hash}

Returns The ATTRIBUTES that are available as CLI & walkthru options.

Returns:

  • (Hash{Symbol: Hash})

    The ATTRIBUTES that are available as CLI & walkthru options



47
48
49
# File 'lib/xolo/admin/title.rb', line 47

def self.cli_opts
  @cli_opts ||= ATTRIBUTES.select { |_k, v| v[:cli] }
end

.delete(title, cnx) ⇒ Hash

Delete a title from the server

Parameters:

  • title (String)

    the title to delete

  • cnx (Faraday::Connection)

    The connection to use, must be logged in already

Returns:

  • (Hash)

    the response data



99
100
101
102
# File 'lib/xolo/admin/title.rb', line 99

def self.delete(title, cnx)
  resp = cnx.delete "#{SERVER_ROUTE}/#{title}"
  resp.body
end

.exist?(title, cnx) ⇒ Boolean

Does a title exist on the server?

Parameters:

  • title (String)

    the title

  • cnx (Faraday::Connection)

    The connection to use, must be logged in already

Returns:

  • (Boolean)


76
77
78
# File 'lib/xolo/admin/title.rb', line 76

def self.exist?(title, cnx)
  all_titles(cnx).include? title
end

.fetch(title, cnx) ⇒ Xolo::Admin::Title

Fetch a title from the server

Parameters:

  • title (String)

    the title to fetch

  • cnx (Faraday::Connection)

    The connection to use, must be logged in already

Returns:



85
86
87
88
89
90
91
92
# File 'lib/xolo/admin/title.rb', line 85

def self.fetch(title, cnx)
  resp = cnx.get "#{SERVER_ROUTE}/#{title}"
  title_obj = new resp.body
  title_obj.cnx = cnx
  title_obj
rescue Faraday::ResourceNotFound
  raise Xolo::NoSuchItemError, "No such title '#{title}'"
end

.latest_version(title, cnx) ⇒ void

This method returns an undefined value.

the latest version of a title in Xolo

Parameters:

  • title (String)

    the title we care about

  • cnx (Faraday::Connection)

    The connection to use, must be logged in already



109
110
111
112
# File 'lib/xolo/admin/title.rb', line 109

def self.latest_version(title, cnx)
  resp = cnx.get "#{SERVER_ROUTE}/#{title}"
  resp.body[:version_order].first
end

.release_to_all_allowed?(cnx) ⇒ Boolean

Is the current admin allowed to set a title’s release groups to ‘all’?

Parameters:

  • cnx (Faraday::Connection)

    The connection to use, must be logged in already

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/xolo/admin/title.rb', line 118

def self.release_to_all_allowed?(cnx)
  resp = cnx.get '/auth/release_to_all_allowed'
  resp.body
end

Instance Method Details

#add(cnx = self.cnx) ⇒ Hash

Add this title to the server

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Hash)

    the response body from the server



152
153
154
155
# File 'lib/xolo/admin/title.rb', line 152

def add(cnx = self.cnx)
  resp = cnx.post SERVER_ROUTE, to_h
  resp.body
end

#changelog(cnx = self.cnx) ⇒ Array<Hash>

The change log is a list of hashes, each with keys: :time, :admin, :ipaddr, :version (may be nil), :action

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Array<Hash>)

    The change log for this title



240
241
242
243
# File 'lib/xolo/admin/title.rb', line 240

def changelog(cnx = self.cnx)
  resp = cnx.get "#{SERVER_ROUTE}/#{title}/changelog"
  resp.body
end

#delete(cnx = self.cnx) ⇒ Hash

Delete this title from the server

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Hash)

    the response data



190
191
192
# File 'lib/xolo/admin/title.rb', line 190

def delete(cnx = self.cnx)
  self.class.delete title, cnx
end

#freeze(targets, users = false, cnx = self.cnx) ⇒ Hash

Freeze the one or more computers for this title

Parameters:

  • computers (Array<String>)

    the computers to freeze

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Hash)

    the response data



199
200
201
202
203
# File 'lib/xolo/admin/title.rb', line 199

def freeze(targets, users = false, cnx = self.cnx)
  data = { targets: targets, users: users }
  resp = cnx.put "#{SERVER_ROUTE}/#{title}/freeze", data
  resp.body
end

#frozen(cnx = self.cnx) ⇒ Hash{String => String}

Fetch the frozen computers for this title

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Hash{String => String})

    computer name => user name



220
221
222
223
# File 'lib/xolo/admin/title.rb', line 220

def frozen(cnx = self.cnx)
  resp = cnx.get "#{SERVER_ROUTE}/#{title}/frozen"
  resp.body
end

#gui_urls(cnx = self.cnx) ⇒ Hash{String => String}

Fetch a hash of URLs for the GUI pages for this title

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Hash{String => String})

    page_name => url



229
230
231
232
# File 'lib/xolo/admin/title.rb', line 229

def gui_urls(cnx = self.cnx)
  resp = cnx.get "#{SERVER_ROUTE}/#{title}/urls"
  resp.body
end

#patch_report_data(cnx = self.cnx) ⇒ Array<Hash>

Get the patch report data for this title It’s the JPAPI report data with each hash having a frozen: key added

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Array<Hash>)

    Data for each computer with any version of this title installed



279
280
281
282
# File 'lib/xolo/admin/title.rb', line 279

def patch_report_data(cnx = self.cnx)
  resp = cnx.get "#{SERVER_ROUTE}/#{title}/patch_report"
  resp.body
end

#release(cnx = self.cnx, version:) ⇒ Hash

Release a version of this title.

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

  • version (String)

    the version to release

Returns:

  • (Hash)

    the response body from the server



171
172
173
174
# File 'lib/xolo/admin/title.rb', line 171

def release(cnx = self.cnx, version:)
  resp = cnx.patch "#{SERVER_ROUTE}/#{title}/release/#{version}", {}
  resp.body
end

#repair(cnx = self.cnx, versions: false) ⇒ Hash

Repair this title, and optionally all its versions

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

  • versions (Boolean) (defaults to: false)

    if true, repair all versions of this title

Returns:

  • (Hash)

    the response body from the server



181
182
183
184
# File 'lib/xolo/admin/title.rb', line 181

def repair(cnx = self.cnx, versions: false)
  resp = cnx.post "#{SERVER_ROUTE}/#{title}/repair", { repair_versions: versions }
  resp.body
end

#thaw(targets, users = false, cnx = self.cnx) ⇒ Hash

Thaw the one or more computers for this title

Parameters:

  • computers (Array<String>)

    the computers to freeze

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Hash)

    the response data



210
211
212
213
214
# File 'lib/xolo/admin/title.rb', line 210

def thaw(targets, users = false, cnx = self.cnx)
  data = { targets: targets, users: users }
  resp = cnx.put "#{SERVER_ROUTE}/#{title}/thaw", data
  resp.body
end

#update(cnx = self.cnx) ⇒ Hash

Update this title to the server

Parameters:

  • cnx (Faraday::Connection) (defaults to: self.cnx)

    The connection to use, must be logged in already

Returns:

  • (Hash)

    the response body from the server



161
162
163
164
# File 'lib/xolo/admin/title.rb', line 161

def update(cnx = self.cnx)
  resp = cnx.put "#{SERVER_ROUTE}/#{title}", to_h
  resp.body
end

#upload_self_service_icon(upload_cnx) ⇒ Faraday::Response

Upload an icon for self service. At this point, the self_service_icon attribute should contain the local file path.

Parameters:

Returns:

  • (Faraday::Response)

    The server response



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/xolo/admin/title.rb', line 253

def upload_self_service_icon(upload_cnx)
  return unless self_service_icon.is_a? Pathname

  unless self_service_icon.readable?
    raise Xolo::NoSuchItemError,
          "Can't upload self service icon '#{self_service_icon}': file doesn't exist or is not readable"
  end

  mimetype = `/usr/bin/file --brief --mime-type #{Shellwords.escape self_service_icon.expand_path.to_s}`.chomp
  upfile = Faraday::Multipart::FilePart.new(self_service_icon.expand_path.to_s, mimetype)
  content = { file: upfile }

  route = "#{SERVER_ROUTE}/#{title}/#{UPLOAD_ICON_ROUTE}"
  upload_cnx.post(route) { |req| req.body = content }
rescue Xolo::NoSuchItemError
  raise
rescue StandardError => e
  raise Xolo::ConnectionError, "#{e.class}: #{e.message}"
end