Class: JSS::Package

Inherits:
APIObject show all
Includes:
Creatable, Updatable
Defined in:
lib/jss-api/api_object/package.rb,
lib/jss-api.rb

Overview

A Package in the JSS

Also the API provides no access to the package’s file list (index), so indexing must be done separately (usually via Casper Admin)

See Also:

Constant Summary collapse

RSRC_BASE =

The base for REST resources of this class

"packages"
RSRC_LIST_KEY =

the hash key used for the JSON list output of all objects in the JSS

:packages
RSRC_OBJECT_KEY =

The hash key used for the JSON object output. It’s also used in various error messages

:package
VALID_DATA_KEYS =

these keys, as well as :id and :name, are present in valid API JSON data for this class

[:fill_existing_users, :fill_user_template, :reboot_required ]
DIST_POINT_PKGS_FOLDER =

The pkg storage folder on the distribution point

"Packages"
CPU_TYPES =

The possible values for cpu_type (required_processor) in a JSS package

["None", "x86", "ppc"]
DEFAULT_CPU_TYPE =

which is default? there must be one to make a new pkg

"None"
PRIORITIES =

the possible priorities

(1..20)
DEFAULT_PRIORITY =

the default priority, since one is needed for making new pkgs

10
DEFAULT_PROCESSOR =

by default, no processor requirement

"None"
DO_NOT_INSTALL =

When we shouldn’t install anything (e.g. switch w/package)

"Do Not Install"

Constants included from Updatable

Updatable::UPDATABLE

Constants included from Creatable

Creatable::CREATABLE

Constants inherited from APIObject

APIObject::DEFAULT_LOOKUP_KEYS, APIObject::REQUIRED_DATA_KEYS

Instance Attribute Summary collapse

Attributes included from Updatable

#need_to_update

Attributes inherited from APIObject

#id, #in_jss, #name, #rest_rsrc

Instance Method Summary collapse

Methods included from Updatable

#name=, #update

Methods included from Creatable

#create

Methods inherited from APIObject

all, all_ids, all_names, #delete, get_name, map_all_ids_to, #save, xml_list

Constructor Details

#initialize(args = {}) ⇒ Package

Returns a new instance of Package.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/jss-api/api_object/package.rb', line 168

def initialize (args = {})

  super

  ### now we have pkg_data with something in it, so fill out the instance vars
  @allow_uninstalled = @init_data[:allow_uninstalled]
  @boot_volume_required = @init_data[:boot_volume_required]
  @category = JSS::APIObject.get_name(@init_data[:category])
  @filename = @init_data[:filename] || @init_data[:name]
  @fill_existing_users = @init_data[:fill_existing_users]
  @fill_user_template = @init_data[:fill_user_template]
  @info = @init_data[:info]
  @install_if_reported_available = @init_data[:install_if_reported_available]
  @notes = @init_data[:notes]
  @os_requirements = @init_data[:os_requirements].split(/\s*,\s*/) if @init_data[:os_requirements]
  @priority = @init_data[:priority] || DEFAULT_PRIORITY
  @reboot_required = @init_data[:reboot_required]
  @required_processor = @init_data[:required_processor] || DEFAULT_CPU_TYPE
  @send_notification = @init_data[:send_notification]
  @switch_with_package = @init_data[:switch_with_package] || DO_NOT_INSTALL
  
  # the receipt is the filename with any .zip extension removed.
  @receipt = @filname ? (JSS::Client::RECEIPTS_FOLDER + @filname.to_s.sub(/.zip$/, '')) : nil
  
end

Instance Attribute Details

#allow_uninstalledBoolean Also known as: removable, removable?

Returns can this item be uninstalled? Some, e.g. OS Updates, can’t.

Returns:

  • (Boolean)

    can this item be uninstalled? Some, e.g. OS Updates, can’t



141
142
143
# File 'lib/jss-api/api_object/package.rb', line 141

def allow_uninstalled
  @allow_uninstalled
end

#boot_volume_requiredBoolean Also known as: boot, boot?

Returns should this pkg be installed on the boot volume during imaging.

Returns:

  • (Boolean)

    should this pkg be installed on the boot volume during imaging



156
157
158
# File 'lib/jss-api/api_object/package.rb', line 156

def boot_volume_required
  @boot_volume_required
end

#categoryString

Returns the category of this pkg, stored in the JSS as the id number from the categories table.

Returns:

  • (String)

    the category of this pkg, stored in the JSS as the id number from the categories table



144
145
146
# File 'lib/jss-api/api_object/package.rb', line 144

def category
  @category
end

#filenameString

Returns the filename of the .pkg, .mpkg, or .dmg on the Casper server.

Returns:

  • (String)

    the filename of the .pkg, .mpkg, or .dmg on the Casper server



117
118
119
# File 'lib/jss-api/api_object/package.rb', line 117

def filename
  @filename
end

#fill_existing_usersBoolean Also known as: feu, feu?

Returns does this item ‘Fill Existing Users’ when jamf installs it?.

Returns:

  • (Boolean)

    does this item ‘Fill Existing Users’ when jamf installs it?



123
124
125
# File 'lib/jss-api/api_object/package.rb', line 123

def fill_existing_users
  @fill_existing_users
end

#fill_user_templateBoolean Also known as: fut, fut?

Returns does this pkg also get install in the OS user homedir template.

Returns:

  • (Boolean)

    does this pkg also get install in the OS user homedir template



126
127
128
# File 'lib/jss-api/api_object/package.rb', line 126

def fill_user_template
  @fill_user_template
end

#infoString

Returns the info field for this pkg - stores d3’s basename & swupdate values.

Returns:

  • (String)

    the info field for this pkg - stores d3’s basename & swupdate values



147
148
149
# File 'lib/jss-api/api_object/package.rb', line 147

def info
  @info
end

#install_if_reported_availableBoolean Also known as: if_in_swupdate, if_in_swupdate?

Returns only install this pkg if it’s available in the commandline softwareupdate.

Returns:

  • (Boolean)

    only install this pkg if it’s available in the commandline softwareupdate.



153
154
155
# File 'lib/jss-api/api_object/package.rb', line 153

def install_if_reported_available
  @install_if_reported_available
end

#notesString

Returns the notes field for this pkg.

Returns:

  • (String)

    the notes field for this pkg



150
151
152
# File 'lib/jss-api/api_object/package.rb', line 150

def notes
  @notes
end

#os_requirementsArray<String> Also known as: oses

Returns the OS versions this can be installed onto. For all minor versions, the format is 10.5.x.

Returns:

  • (Array<String>)

    the OS versions this can be installed onto. For all minor versions, the format is 10.5.x



132
133
134
# File 'lib/jss-api/api_object/package.rb', line 132

def os_requirements
  @os_requirements
end

#priorityInteger

Returns Priority to use for deploying or uninstalling the package.

Returns:

  • (Integer)

    Priority to use for deploying or uninstalling the package



159
160
161
# File 'lib/jss-api/api_object/package.rb', line 159

def priority
  @priority
end

#reboot_requiredBoolean Also known as: reboot, reboot?

Returns does this item require a reboot after installation? If so, it’ll be a puppy-install in d3.

Returns:

  • (Boolean)

    does this item require a reboot after installation? If so, it’ll be a puppy-install in d3



129
130
131
# File 'lib/jss-api/api_object/package.rb', line 129

def reboot_required
  @reboot_required
end

#receiptPathname (readonly)

Returns the local receipt when this pkg is installed.

Returns:

  • (Pathname)

    the local receipt when this pkg is installed



120
121
122
# File 'lib/jss-api/api_object/package.rb', line 120

def receipt
  @receipt
end

#required_processorString Also known as: cpu_type

Returns limit installation to these architectures: ‘x86’, ‘ppc’, ‘None’.

Returns:

  • (String)

    limit installation to these architectures: ‘x86’, ‘ppc’, ‘None’



135
136
137
# File 'lib/jss-api/api_object/package.rb', line 135

def required_processor
  @required_processor
end

#send_notificationBoolean Also known as: notify

Returns does this pkg cause a notification to be sent on self-heal?.

Returns:

  • (Boolean)

    does this pkg cause a notification to be sent on self-heal?



162
163
164
# File 'lib/jss-api/api_object/package.rb', line 162

def send_notification
  @send_notification
end

#switch_with_packageString

Returns the name of a pkg to install (or “Do Not Install”) when this pkg can’t be installed.

Returns:

  • (String)

    the name of a pkg to install (or “Do Not Install”) when this pkg can’t be installed



138
139
140
# File 'lib/jss-api/api_object/package.rb', line 138

def switch_with_package
  @switch_with_package
end

Instance Method Details

#delete_master_file(rw_pw, unmount = true) ⇒ Boolean

Delete the filename from the master distribution point, if it exists.

If you’ll be uploading several files you can specify unmount as false, and do it manually when all are finished.

Parameters:

  • rw_pw (String)

    the password for the read/write account on the master Distribution Point or :prompt, or :stdin# where # is the line of stdin containing the password. See DistributionPoint#mount

  • unmount (Boolean) (defaults to: true)

    whether or not ot unount the distribution point when finished.

Returns:

  • (Boolean)

    was the file deleted?



559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/jss-api/api_object/package.rb', line 559

def delete_master_file (rw_pw, unmount = true)
  mdp = JSS::DistributionPoint.master_distribution_point
  file = mdp.mount(rw_pw, :rw) +"#{DIST_POINT_PKGS_FOLDER}/#{@filename}"
  if file.exist?
    file.delete
    did_it = true
  else
    did_it = false
  end # if exists
  mdp.unmount if unmount
  return did_it
end

#install(args = {}) ⇒ Process::Status

TODO:

deal with cert-based https authentication

Note:

This code must be run as root to install packages

Causes the pkg/dmg to be installed via the jamf binary ‘install’ command from the distribution point for this machine. See DistributionPoint.my_distribution_point

The read-only or http passwd for the dist. point must be provided, except for non-authenticated http downloads)

Parameters:

  • args (Hash) (defaults to: {})

    the arguments for installation

Options Hash (args):

  • :ro_pw (String)

    the read-only or http password for the distribution point for the local machine (http will be used if available, and may not need a pw)

  • :target (String, Pathname)

    The drive on which to install the package, defaults to ‘/’

  • :verbose (Boolean)

    be verbose to stdout, defaults to false

  • :feu (Boolean)

    fill existing users, defaults to false

  • :fut (Boolean)

    fill user template, defaults to false

  • :unmount (Boolean)

    unmount the distribution point when finished?(if we mounted it), defaults to false

  • :no_http (Boolean)

    don’t use http downloads even if they are enabled for the dist. point.

Returns:

  • (Process::Status)

    the final status of the jamf binary command

Raises:



604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/jss-api/api_object/package.rb', line 604

def install (args = {})

  raise JSS::UnsupportedError, "You must have root privileges to install packages" unless JSS.superuser?

  args[:target] ||= '/'

  ro_pw = args[:ro_pw]

  mdp = JSS::DistributionPoint.my_distribution_point

  ### how do we access our dist. point? with http?
  if mdp.http_downloads_enabled and (not args[:no_http])
    using_http = true
    src_path = mdp.http_url
    if mdp.username_password_required
      raise JSS::MissingDataError, "No password provided for http download" unless ro_pw
      raise JSS::InvaldDatatError, "Incorrect password for http access to distribution point." unless mdp.check_pw(:http, ro_pw)
      # insert the name and pw into the uri
      reserved_chars = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}]") # we'll escape all the chars that aren't unreserved
      src_path = src_path.sub(%r{(https?://)(\S)}, "#{$1}#{URI.escape mdp.http_username,reserved_chars}:#{URI.escape ro_pw, reserved_chars}@#{$2}")
    end

  # or with filesharing?
  else
    using_http = false
    src_path = mdp.mount(ro_pw)
  end

  # look at the pkgs folder
  src_path += "/#{DIST_POINT_PKGS_FOLDER}"

  ### are we doing "fill existing users" or "fill user template"?
  do_feu = args[:feu] ? "-feu" : ""
  do_fut = args[:fut] ? "-fut" : ""

  ### the install args for jamf
  command_args = "-package '#{@filename}' -path '#{src_path}'  -target '#{args[:target]}' #{do_feu} #{do_fut} -showProgress -verbose ; echo jamfexit $?"

  ### run it via a client
  install_out = JSS::Client.run_jamf :install, command_args, args[:verbose]

  install_exit = $?

  if (args.include? :unmount)
    mdp.unmount unless using_http
  end

  return install_exit
end

#installed?Boolean

Is this packaged installed on the current machine (via casper)? We just look for the receipt, which is the @filename less any possible .zip extension.

Returns:

  • (Boolean)


477
478
479
# File 'lib/jss-api/api_object/package.rb', line 477

def installed?
  @receipt.file?
end

#uninstall(args = {}) ⇒ Process::Status

Note:

This code must be run as root to uninstall packages

Causes the pkg to be uninstalled via the jamf command.

Parameters:

  • args (Hash) (defaults to: {})

    the arguments for installation

Options Hash (args):

  • :target (String, Pathname)

    The drive from which to uninstall the package, defaults to ‘/’

  • :verbose (Boolean)

    be verbose to stdout, defaults to false

  • :feu (Boolean)

    fill existing users, defaults to false

  • :fut (Boolean)

    fill user template, defaults to false

Returns:

  • (Process::Status)

    the result of the ‘jamf uninstall’ command

Raises:



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/jss-api/api_object/package.rb', line 671

def uninstall (args = {})
  
  raise JSS::UnsupportedError, \
    "This package cannot be uninstalled. Please use CasperAdmin to index it and allow uninstalls" unless removable?
  raise JSS::UnsupportedError, "You must have root privileges to uninstall packages" unless JSS.superuser?
  args[:target] ||= '/'

  ### are we doing "fill existing users" or "fill user template"?
  do_feu = args[:feu] ? "-feu" : ""
  do_fut = args[:fut] ? "-fut" : ""

  ### use jamf binary to uninstall the pkg
  jamf_opts = "-target '#{args[:target]}' -id '#{@id}' #{do_feu} #{do_fut}"

  ### run it via a client
  uninstall_out = JSS::Client.run_jamf "uninstall", jamf_opts, args[:verbose]

  return $?
end

#upload_master_file(local_file_path, rw_pw, unmount = true) ⇒ void

This method returns an undefined value.

Upload a locally-readable file to the master distribution point. If the file is a directory (like a bundle .pk/.mpkg) it will be zipped before uploading and the @filename will be adjusted accordingly

If you’ll be uploading several files you can specify unmount as false, and do it manually when all are finished with JSS::DistributionPoint.master_distribution_point.unmount

Parameters:

  • local_file_path (String, Pathname)

    the local path to the file to be uploaded

  • rw_pw (String, Symbol)

    the password for the read/write account on the master Distribution Point, or :prompt, or :stdin# where # is the line of stdin containing the password See DistributionPoint#mount

  • unmount (Boolean) (defaults to: true)

    whether or not ot unount the distribution point when finished.

Raises:



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/jss-api/api_object/package.rb', line 498

def upload_master_file (local_file_path, rw_pw, unmount = true)

  raise JSS::NoSuchItemError, "Please create this package in the JSS before uploading it." unless @in_jss
  
  mdp = JSS::DistributionPoint.master_distribution_point
  destination = mdp.mount(rw_pw, :rw) +"#{DIST_POINT_PKGS_FOLDER}/#{@filename}"

  local_path = Pathname.new local_file_path
  raise JSS::NoSuchItemError, "Local file '#{@local_file}' doesn't exist" unless local_path.exist?

  ### should we zip it?
  if local_path.directory?
    begin
      zipdir = Pathname.new "/tmp/jssgemtmp-#{Time.new.strftime "%Y%m%d%H%M%S"}-#{$$}"
      zipdir.mkpath
      zipdir.chmod 0700
      zipfile = zipdir + (local_path.basename.to_s + ".zip")

      ### go to the same dir as the local file
      wd = Dir.pwd
      Dir.chdir local_path.parent

      ### the contents of the zip file have to have the same  name as the zip file itself (minus the .zip)
      ### so temporarily rename the source
      local_path.rename(local_path.parent + @filename)
      raise "There was a problem zipping the pkg bundle" unless system "/usr/bin/zip -qr '#{zipfile}' '#{@filename}'"

    ensure
      ### rename the source to the original name
      (local_path.parent + @filename).rename local_path
      ### go back where we started
      Dir.chdir wd
    end # begin

    ### update our info
    local_path = zipfile

    self.filename = zipfile.basename.to_s
    self.update
  end # if directory

  

  FileUtils.copy_entry local_path, destination

  mdp.unmount if unmount
end