jss-api 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/jss-api.rb +1 -191
- metadata +16 -146
- data/.yardopts +0 -7
- data/CHANGES.md +0 -88
- data/LICENSE.txt +0 -174
- data/README.md +0 -396
- data/THANKS.md +0 -6
- data/bin/cgrouper +0 -485
- data/bin/subnet-update +0 -400
- data/lib/jss-api/api_connection.rb +0 -400
- data/lib/jss-api/api_object.rb +0 -616
- data/lib/jss-api/api_object/advanced_search.rb +0 -389
- data/lib/jss-api/api_object/advanced_search/advanced_computer_search.rb +0 -95
- data/lib/jss-api/api_object/advanced_search/advanced_mobile_device_search.rb +0 -96
- data/lib/jss-api/api_object/advanced_search/advanced_user_search.rb +0 -95
- data/lib/jss-api/api_object/building.rb +0 -92
- data/lib/jss-api/api_object/category.rb +0 -147
- data/lib/jss-api/api_object/computer.rb +0 -852
- data/lib/jss-api/api_object/creatable.rb +0 -98
- data/lib/jss-api/api_object/criteriable.rb +0 -189
- data/lib/jss-api/api_object/criteriable/criteria.rb +0 -231
- data/lib/jss-api/api_object/criteriable/criterion.rb +0 -228
- data/lib/jss-api/api_object/department.rb +0 -93
- data/lib/jss-api/api_object/distribution_point.rb +0 -560
- data/lib/jss-api/api_object/extendable.rb +0 -221
- data/lib/jss-api/api_object/extension_attribute.rb +0 -457
- data/lib/jss-api/api_object/extension_attribute/computer_extension_attribute.rb +0 -362
- data/lib/jss-api/api_object/extension_attribute/mobile_device_extension_attribute.rb +0 -189
- data/lib/jss-api/api_object/extension_attribute/user_extension_attribute.rb +0 -117
- data/lib/jss-api/api_object/group.rb +0 -380
- data/lib/jss-api/api_object/group/computer_group.rb +0 -124
- data/lib/jss-api/api_object/group/mobile_device_group.rb +0 -139
- data/lib/jss-api/api_object/group/user_group.rb +0 -139
- data/lib/jss-api/api_object/ldap_server.rb +0 -535
- data/lib/jss-api/api_object/locatable.rb +0 -286
- data/lib/jss-api/api_object/matchable.rb +0 -97
- data/lib/jss-api/api_object/mobile_device.rb +0 -556
- data/lib/jss-api/api_object/netboot_server.rb +0 -148
- data/lib/jss-api/api_object/network_segment.rb +0 -414
- data/lib/jss-api/api_object/osx_configuration_profile.rb +0 -261
- data/lib/jss-api/api_object/package.rb +0 -812
- data/lib/jss-api/api_object/peripheral.rb +0 -335
- data/lib/jss-api/api_object/peripheral_type.rb +0 -295
- data/lib/jss-api/api_object/policy.rb +0 -898
- data/lib/jss-api/api_object/purchasable.rb +0 -316
- data/lib/jss-api/api_object/removable_macaddr.rb +0 -98
- data/lib/jss-api/api_object/scopable.rb +0 -136
- data/lib/jss-api/api_object/scopable/scope.rb +0 -621
- data/lib/jss-api/api_object/script.rb +0 -631
- data/lib/jss-api/api_object/self_servable.rb +0 -355
- data/lib/jss-api/api_object/site.rb +0 -93
- data/lib/jss-api/api_object/software_update_server.rb +0 -109
- data/lib/jss-api/api_object/updatable.rb +0 -117
- data/lib/jss-api/api_object/uploadable.rb +0 -138
- data/lib/jss-api/api_object/user.rb +0 -272
- data/lib/jss-api/client.rb +0 -504
- data/lib/jss-api/compatibility.rb +0 -66
- data/lib/jss-api/composer.rb +0 -171
- data/lib/jss-api/configuration.rb +0 -306
- data/lib/jss-api/db_connection.rb +0 -298
- data/lib/jss-api/exceptions.rb +0 -95
- data/lib/jss-api/ruby_extensions.rb +0 -35
- data/lib/jss-api/ruby_extensions/filetest.rb +0 -43
- data/lib/jss-api/ruby_extensions/hash.rb +0 -79
- data/lib/jss-api/ruby_extensions/ipaddr.rb +0 -91
- data/lib/jss-api/ruby_extensions/pathname.rb +0 -77
- data/lib/jss-api/ruby_extensions/string.rb +0 -59
- data/lib/jss-api/ruby_extensions/time.rb +0 -63
- data/lib/jss-api/server.rb +0 -108
- data/lib/jss-api/utility.rb +0 -416
- data/lib/jss-api/version.rb +0 -31
@@ -1,66 +0,0 @@
|
|
1
|
-
### Copyright 2016 Pixar
|
2
|
-
###
|
3
|
-
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
-
### with the following modification; you may not use this file except in
|
5
|
-
### compliance with the Apache License and the following modification to it:
|
6
|
-
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
-
###
|
8
|
-
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
-
### names, trademarks, service marks, or product names of the Licensor
|
10
|
-
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
-
### the License and to reproduce the content of the NOTICE file.
|
12
|
-
###
|
13
|
-
### You may obtain a copy of the Apache License at
|
14
|
-
###
|
15
|
-
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
-
###
|
17
|
-
### Unless required by applicable law or agreed to in writing, software
|
18
|
-
### distributed under the Apache License with the above modification is
|
19
|
-
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
-
### KIND, either express or implied. See the Apache License for the specific
|
21
|
-
### language governing permissions and limitations under the Apache License.
|
22
|
-
###
|
23
|
-
###
|
24
|
-
|
25
|
-
|
26
|
-
###
|
27
|
-
### Augmentation to make this code compatible with ruby 1.8.7
|
28
|
-
###
|
29
|
-
|
30
|
-
#############################################
|
31
|
-
### Hash.key
|
32
|
-
###
|
33
|
-
### #key exists in ruby 1.9+, in 1.8 its called #index
|
34
|
-
### it returns the hash key for a given value, if the value exists
|
35
|
-
### in the hash
|
36
|
-
###
|
37
|
-
unless {}.respond_to? :key
|
38
|
-
class Hash
|
39
|
-
alias_method :key, :index
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
#############################################
|
44
|
-
### Array.sample
|
45
|
-
###
|
46
|
-
### #sample exists in ruby 1.9+, in 1.8 its called #choice
|
47
|
-
### it returns a randomly chosen element of the given array
|
48
|
-
### eg: [1, 2, 3].sample returns either 1, 2, or 3
|
49
|
-
###
|
50
|
-
unless [].respond_to? :sample
|
51
|
-
class Array
|
52
|
-
alias_method :sample, :choice
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
|
57
|
-
#############################################
|
58
|
-
### String.force_encoding
|
59
|
-
###
|
60
|
-
### Ruby 1.8 doesn't do encodings...
|
61
|
-
###
|
62
|
-
unless "".respond_to? :force_encoding
|
63
|
-
class String
|
64
|
-
def force_encoding(args = nil); self; end
|
65
|
-
end
|
66
|
-
end
|
data/lib/jss-api/composer.rb
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
### Copyright 2016 Pixar
|
2
|
-
###
|
3
|
-
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
-
### with the following modification; you may not use this file except in
|
5
|
-
### compliance with the Apache License and the following modification to it:
|
6
|
-
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
-
###
|
8
|
-
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
-
### names, trademarks, service marks, or product names of the Licensor
|
10
|
-
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
-
### the License and to reproduce the content of the NOTICE file.
|
12
|
-
###
|
13
|
-
### You may obtain a copy of the Apache License at
|
14
|
-
###
|
15
|
-
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
-
###
|
17
|
-
### Unless required by applicable law or agreed to in writing, software
|
18
|
-
### distributed under the Apache License with the above modification is
|
19
|
-
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
-
### KIND, either express or implied. See the Apache License for the specific
|
21
|
-
### language governing permissions and limitations under the Apache License.
|
22
|
-
###
|
23
|
-
###
|
24
|
-
|
25
|
-
###
|
26
|
-
module JSS
|
27
|
-
|
28
|
-
###
|
29
|
-
### This module provides two methods for building very simple Casper-happy .pkg and .dmg packages for deployment.
|
30
|
-
###
|
31
|
-
### Unlike Composer.app from JAMF, this module currently doesn't offer a way to do a before/after disk scan
|
32
|
-
### and use the differences to build the root folder from which the package is built. Nor does the module support
|
33
|
-
### editing the pre/post install scripts in .pkgs.
|
34
|
-
###
|
35
|
-
### The 'root folder', a folder representing the root filesystem of the target machine where the package will be installed,
|
36
|
-
### must already exist and be fully populated and with correct permissions.
|
37
|
-
###
|
38
|
-
module Composer
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
#####################################
|
43
|
-
### Constants
|
44
|
-
#####################################
|
45
|
-
|
46
|
-
### the apple pkgutil tool
|
47
|
-
PKG_UTIL = Pathname.new "/usr/sbin/pkgutil"
|
48
|
-
|
49
|
-
### The location of the cli tool for making .pkgs
|
50
|
-
PKGBUILD = Pathname.new "/usr/bin/pkgbuild"
|
51
|
-
|
52
|
-
### the default bundle identifier prefix for pkgs
|
53
|
-
PKG_BUNDLE_ID_PFX = 'jss_gem_composer'
|
54
|
-
|
55
|
-
### Apple's hdiutil for making dmgs
|
56
|
-
HDI_UTIL= '/usr/bin/hdiutil'
|
57
|
-
|
58
|
-
### Where to save the output ?
|
59
|
-
DEFAULT_OUT_DIR = Pathname.new "/Users/Shared"
|
60
|
-
|
61
|
-
###
|
62
|
-
### Make a casper-happy .pkg out of a root folder, permissions are assumed to be correct.
|
63
|
-
###
|
64
|
-
### @param name[String] the name of the .pkg. The .pkg suffix will be added if not present
|
65
|
-
###
|
66
|
-
### @param version[String] the version of the .pkg, needed for building the .pkg
|
67
|
-
###
|
68
|
-
### @param root[String, Pathname] the path to the "root folder" representing the root file system of the target install drive
|
69
|
-
###
|
70
|
-
### @param opts[Hash] the options for building the .pkg
|
71
|
-
###
|
72
|
-
### @option opts :bundle_id_prefix[String] the pkg bundle identifier prefix.
|
73
|
-
### e.g. 'org.some.organization' or 'com.pixar.d3'.
|
74
|
-
### Defaults to '{PKG_BUNDLE_ID_PFX}'. See 'man pkgbuild' for more info
|
75
|
-
###
|
76
|
-
### @option opts :out_dir[String,Pathname] he folder in which the .pkg will be created. Defaults to {DEFAULT_OUT_DIR}
|
77
|
-
###
|
78
|
-
### @option opts :preserve_ownership[Boolean] If true, the owner/group of the rootpath are preserved.
|
79
|
-
### Default is false: they become the pkgbuild/installer "recommended" (root/wheel or root/admin)
|
80
|
-
###
|
81
|
-
### @return [Pathname] the local path to the new .pkg
|
82
|
-
###
|
83
|
-
def self.mk_pkg(name, version, root, opts = {})
|
84
|
-
raise NoSuchItemError, "Missing pkgbuild tool. Please make sure you're running 10.8 or later." unless PKGBUILD.executable?
|
85
|
-
|
86
|
-
opts[:out_dir] ||= DEFAULT_OUT_DIR
|
87
|
-
opts[:bundle_id_prefix] ||= PKG_BUNDLE_ID_PFX
|
88
|
-
|
89
|
-
pkg_filename = name.end_with?(".pkg") ? name : name+".pkg"
|
90
|
-
pkg_id = opts[:bundle_id_prefix] + "." + name
|
91
|
-
pkg_out = "#{opts[:out_dir]}/#{pkg_filename}"
|
92
|
-
pkg_ownership = opts[:preserve_ownership] ? "preserve" : "recommended"
|
93
|
-
|
94
|
-
|
95
|
-
### first, run 'analyze' to get a 'component plist' in which we can change some settings
|
96
|
-
### for any bundles in the root (bundles like .apps, frameworks, plugins, etc..)
|
97
|
-
###
|
98
|
-
### we edit the settings thus:
|
99
|
-
### BundleOverwriteAction = upgrade, totally replace any version current on disk
|
100
|
-
### BundleIsVersionChecked = false, allow us to install regardless of what version is currently installed
|
101
|
-
### BundleIsRelocatable = false, if there's a version of this in some other location, Do Not move this one there after installation
|
102
|
-
### BundleHasStrictIdentifier = false, don't care if there's something at the install path with a different bundle id.
|
103
|
-
###
|
104
|
-
### In other words, just install the thing!
|
105
|
-
### (see 'man pkgbuild' for more info)
|
106
|
-
###
|
107
|
-
###
|
108
|
-
comp_plist_out = Pathname.new "/tmp/#{PKG_BUNDLE_ID_PFX}-#{pkg_filename}.plist"
|
109
|
-
system "#{PKGBUILD} --analyze --root '#{root}' '#{comp_plist_out}'"
|
110
|
-
comp_plist = Plist.parse_xml comp_plist_out.read
|
111
|
-
|
112
|
-
### if the plist is empty, there are no bundles in the pkg
|
113
|
-
if comp_plist[0].nil?
|
114
|
-
comp_plist_arg = ''
|
115
|
-
else
|
116
|
-
### otherwise, edit the bundle dictionaries
|
117
|
-
comp_plist.each do |bndl|
|
118
|
-
bndl.delete "ChildBundles" if bndl["ChildBundles"]
|
119
|
-
bndl["BundleOverwriteAction"] = "upgrade"
|
120
|
-
bndl["BundleIsVersionChecked"] = false
|
121
|
-
bndl["BundleIsRelocatable"] = false
|
122
|
-
bndl["BundleHasStrictIdentifier"] = false
|
123
|
-
end
|
124
|
-
### write out the edits
|
125
|
-
comp_plist_out.open('w'){|f| f.write comp_plist.to_plist}
|
126
|
-
comp_plist_arg = "--component-plist '#{comp_plist_out}'"
|
127
|
-
end
|
128
|
-
|
129
|
-
### now build the pkg
|
130
|
-
begin
|
131
|
-
system "#{PKGBUILD} --identifier '#{pkg_id}' --version '#{version}' --ownership #{pkg_ownership} --install-location / --root '#{root}' #{comp_plist_arg} '#{pkg_out}' "
|
132
|
-
|
133
|
-
raise RuntimeError, "There was an error building the .pkg" unless $?.exitstatus == 0
|
134
|
-
ensure
|
135
|
-
comp_plist_out.delete if comp_plist_out.exist?
|
136
|
-
end
|
137
|
-
|
138
|
-
return Pathname.new pkg_out
|
139
|
-
end # mk_dot_pkg
|
140
|
-
|
141
|
-
|
142
|
-
###
|
143
|
-
### Make a casper-happy .dmg out of a root folder, permissions are assumed to be correct.
|
144
|
-
###
|
145
|
-
### @param name[String] The name of the .dmg, the suffix will be added if needed
|
146
|
-
###
|
147
|
-
### @param root[String, Pathname] the path to the "root folder" representing the root file system of the target install drive
|
148
|
-
###
|
149
|
-
### @param out_dir[String, Pathname] the folder in which the .pkg will be created. Defaults to {DEFAULT_OUT_DIR}
|
150
|
-
###
|
151
|
-
### @return [Pathname] the local path to the new .dmg
|
152
|
-
###
|
153
|
-
###
|
154
|
-
def self.mk_dmg(name, root, out_dir = DEFAULT_OUT_DIR)
|
155
|
-
|
156
|
-
dmg_filename = "#{name}.dmg"
|
157
|
-
dmg_vol = name
|
158
|
-
dmg_out = "#{out_dir}/#{dmg_filename}"
|
159
|
-
|
160
|
-
system "#{HDI_UTIL} create -volname '#{dmg_vol}' -srcfolder '#{root}' '#{dmg_out}'"
|
161
|
-
|
162
|
-
raise RuntimeError, "There was an error building the .dmg" unless $?.exitstatus == 0
|
163
|
-
return Pathname.new dmg_out
|
164
|
-
|
165
|
-
end # mk_dmg
|
166
|
-
|
167
|
-
|
168
|
-
end # module Composer
|
169
|
-
end # module JSS
|
170
|
-
|
171
|
-
|
@@ -1,306 +0,0 @@
|
|
1
|
-
### Copyright 2016 Pixar
|
2
|
-
###
|
3
|
-
### Licensed under the Apache License, Version 2.0 (the "Apache License")
|
4
|
-
### with the following modification; you may not use this file except in
|
5
|
-
### compliance with the Apache License and the following modification to it:
|
6
|
-
### Section 6. Trademarks. is deleted and replaced with:
|
7
|
-
###
|
8
|
-
### 6. Trademarks. This License does not grant permission to use the trade
|
9
|
-
### names, trademarks, service marks, or product names of the Licensor
|
10
|
-
### and its affiliates, except as required to comply with Section 4(c) of
|
11
|
-
### the License and to reproduce the content of the NOTICE file.
|
12
|
-
###
|
13
|
-
### You may obtain a copy of the Apache License at
|
14
|
-
###
|
15
|
-
### http://www.apache.org/licenses/LICENSE-2.0
|
16
|
-
###
|
17
|
-
### Unless required by applicable law or agreed to in writing, software
|
18
|
-
### distributed under the Apache License with the above modification is
|
19
|
-
### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
20
|
-
### KIND, either express or implied. See the Apache License for the specific
|
21
|
-
### language governing permissions and limitations under the Apache License.
|
22
|
-
###
|
23
|
-
###
|
24
|
-
|
25
|
-
###
|
26
|
-
module JSS
|
27
|
-
|
28
|
-
|
29
|
-
#####################################
|
30
|
-
### Module Variables
|
31
|
-
#####################################
|
32
|
-
|
33
|
-
#####################################
|
34
|
-
### Module Methods
|
35
|
-
#####################################
|
36
|
-
|
37
|
-
#####################################
|
38
|
-
### Classes
|
39
|
-
#####################################
|
40
|
-
|
41
|
-
###
|
42
|
-
### A class for working with pre-defined settings & preferences for the JSS Gem
|
43
|
-
###
|
44
|
-
### This is a singleton class, only one instance can exist at a time.
|
45
|
-
###
|
46
|
-
### When the JSS module loads, that instance is created and stored in the constant {JSS::CONFIG},
|
47
|
-
### which can then be used in applications to avoid always having to pass in server names, API user names
|
48
|
-
### and so on.
|
49
|
-
###
|
50
|
-
### When the JSS::Configuration instance is created, the {GLOBAL_CONF} file (/etc/jss_gem.conf) is examined if it exists, and
|
51
|
-
### the items in it are loaded into the attributes.
|
52
|
-
###
|
53
|
-
### Then the user-specific {USER_CONF} file (~/.jss_gem.conf) is examined if it exists, and any attributes defined there will
|
54
|
-
### override those values from the {GLOBAL_CONF}.
|
55
|
-
###
|
56
|
-
### The file format is one attribute per line, thus:
|
57
|
-
### attr_name: value
|
58
|
-
###
|
59
|
-
### Lines that don't start with a known attribute name followed by a colon are ignored. If an attribute is defined
|
60
|
-
### more than once, the last one wins.
|
61
|
-
###
|
62
|
-
### The known attributes are:
|
63
|
-
### - api_server_name [String] the hostname of the JSS API server
|
64
|
-
### - api_server_port [Integer] the port number for the API connection
|
65
|
-
### - api_ssl_version [String] the SSL version (from the open_ssl module) to use for the connection.
|
66
|
-
### - api_verify_cert [Boolean] if SSL is used, should the SSL certificate be verified (usually false for a self-signed cert)
|
67
|
-
### - api_username [String] the JSS username for connecting to the API
|
68
|
-
### - api_timeout_open [Integer] the number of seconds for the open-connection timeout
|
69
|
-
### - api_timeout [Integer] the number of seconds for the response timeout
|
70
|
-
###
|
71
|
-
### The {APIConnection#connect} method will first use any values given as method arguments. For any not given, it will
|
72
|
-
### look at the Preferences instance for any values there, and if still none, will use default values or raise an exception.
|
73
|
-
###
|
74
|
-
### At any point, the attributes can read or changed using standard Ruby getter/setter methods matching the name of the attribute,
|
75
|
-
### e.g.
|
76
|
-
### JSS::CONFIG.api_server_name # => 'myjss.mycompany.com'
|
77
|
-
### JSS::CONFIG.api_server_name = 'otherjss.mycompany.com' # sets the api_server_name to a new value
|
78
|
-
###
|
79
|
-
###
|
80
|
-
### The current settings may be saved to the GLOBAL_CONF file, the USER_CONF file, or an arbitrary file using {#save}.
|
81
|
-
### The argument to {#save} should be either :user, :global, or a String or Pathname file path.
|
82
|
-
### NOTE: This overwrites any existing file.
|
83
|
-
###
|
84
|
-
### To re-load the settings use {#reload}. This clears the current settings, and re-reads both the global and user files.
|
85
|
-
### If a pathname is provided, e.g.
|
86
|
-
### JSS::CONFIG.reload '/path/to/other/file'
|
87
|
-
### the current settings are cleared and reloaded from that other file.
|
88
|
-
###
|
89
|
-
### To view the current settings, use {#print}.
|
90
|
-
###
|
91
|
-
### @note Passwords are not saved in prefs files. Your application will have to acquire them using the :prompt or :stdin options to
|
92
|
-
### {APIConnection#connect}, or by custom means.
|
93
|
-
###
|
94
|
-
class Configuration
|
95
|
-
include Singleton
|
96
|
-
|
97
|
-
#####################################
|
98
|
-
### Class Constants
|
99
|
-
#####################################
|
100
|
-
|
101
|
-
### The filename for storing the prefs, globally or user-level
|
102
|
-
CONF_FILE = "jss_gem.conf"
|
103
|
-
|
104
|
-
### The Pathname to the machine-wide preferences plist
|
105
|
-
GLOBAL_CONF = Pathname.new "/etc/#{CONF_FILE}"
|
106
|
-
|
107
|
-
### The Pathname to the user-specific preferences plist
|
108
|
-
USER_CONF = ENV["HOME"] ? Pathname.new("~/.#{CONF_FILE}").expand_path : nil
|
109
|
-
|
110
|
-
### The attribute keys we maintain, and the type they should be stored as
|
111
|
-
CONF_KEYS = {
|
112
|
-
:api_server_name => :to_s,
|
113
|
-
:api_server_port => :to_i,
|
114
|
-
:api_ssl_version => :to_s,
|
115
|
-
:api_verify_cert => :jss_to_bool,
|
116
|
-
:api_username => :to_s,
|
117
|
-
:api_timeout_open => :to_i,
|
118
|
-
:api_timeout => :to_i,
|
119
|
-
:db_server_name => :to_s,
|
120
|
-
:db_server_port => :to_i,
|
121
|
-
:db_server_socket => :to_s,
|
122
|
-
:db_username => :to_s,
|
123
|
-
:db_name => :to_s,
|
124
|
-
:db_connect_timeout => :to_i,
|
125
|
-
:db_read_timeout => :to_i,
|
126
|
-
:db_write_timeout => :to_i
|
127
|
-
}
|
128
|
-
|
129
|
-
#####################################
|
130
|
-
### Class Variables
|
131
|
-
#####################################
|
132
|
-
|
133
|
-
#####################################
|
134
|
-
### Class Methods
|
135
|
-
#####################################
|
136
|
-
|
137
|
-
#####################################
|
138
|
-
### Attributes
|
139
|
-
#####################################
|
140
|
-
|
141
|
-
# automatically create accessors for all the CONF_KEYS
|
142
|
-
CONF_KEYS.keys.each {|k| attr_accessor k}
|
143
|
-
|
144
|
-
|
145
|
-
#####################################
|
146
|
-
### Constructor
|
147
|
-
#####################################
|
148
|
-
|
149
|
-
###
|
150
|
-
### Initialize!
|
151
|
-
###
|
152
|
-
def initialize
|
153
|
-
|
154
|
-
read_global
|
155
|
-
read_user
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
#####################################
|
160
|
-
### Public Instance Methods
|
161
|
-
#####################################
|
162
|
-
|
163
|
-
###
|
164
|
-
### Clear all values
|
165
|
-
###
|
166
|
-
### @return [void]
|
167
|
-
###
|
168
|
-
def clear_all
|
169
|
-
CONF_KEYS.keys.each {|k| self.send "#{k}=".to_sym, nil}
|
170
|
-
end
|
171
|
-
|
172
|
-
###
|
173
|
-
### (Re)read the global prefs, if it exists.
|
174
|
-
###
|
175
|
-
### @return [void]
|
176
|
-
###
|
177
|
-
def read_global
|
178
|
-
read GLOBAL_CONF if GLOBAL_CONF.file? and GLOBAL_CONF.readable?
|
179
|
-
end
|
180
|
-
|
181
|
-
###
|
182
|
-
### (Re)read the user prefs, if it exists.
|
183
|
-
###
|
184
|
-
### @return [void]
|
185
|
-
###
|
186
|
-
def read_user
|
187
|
-
return unless USER_CONF
|
188
|
-
read USER_CONF if USER_CONF.file? and USER_CONF.readable?
|
189
|
-
end
|
190
|
-
|
191
|
-
|
192
|
-
###
|
193
|
-
### Clear the settings and reload the prefs files, or another file if provided
|
194
|
-
###
|
195
|
-
### @param file[String,Pathname] a non-standard prefs file to load
|
196
|
-
###
|
197
|
-
### @return [void]
|
198
|
-
###
|
199
|
-
def reload(file = nil)
|
200
|
-
clear_all
|
201
|
-
if file
|
202
|
-
read file
|
203
|
-
return true
|
204
|
-
end
|
205
|
-
read_global
|
206
|
-
read_user
|
207
|
-
return true
|
208
|
-
end
|
209
|
-
|
210
|
-
|
211
|
-
###
|
212
|
-
### Save the prefs into a file
|
213
|
-
###
|
214
|
-
### @param file[Symbol,String,Pathname] either :user, :global, or an arbitrary file to save.
|
215
|
-
###
|
216
|
-
### @return [void]
|
217
|
-
###
|
218
|
-
def save(file)
|
219
|
-
path = case file
|
220
|
-
when :global then GLOBAL_CONF
|
221
|
-
when :user then USER_CONF
|
222
|
-
else Pathname.new(file)
|
223
|
-
end
|
224
|
-
|
225
|
-
raise JSS::MissingDataError, "No HOME environment variable, can't write to user conf file." if path.nil?
|
226
|
-
|
227
|
-
# file already exists? read it in and update the values.
|
228
|
-
if path.readable?
|
229
|
-
data = path.read
|
230
|
-
|
231
|
-
# go thru the known attributes/keys
|
232
|
-
CONF_KEYS.keys.sort.each do |k|
|
233
|
-
|
234
|
-
# if the key exists, update it.
|
235
|
-
if data =~ /^#{k}:/
|
236
|
-
data.sub!(/^#{k}:.*$/, "#{k}: #{self.send k}")
|
237
|
-
|
238
|
-
# if not, add it to the end unless it's nil
|
239
|
-
else
|
240
|
-
data += "\n#{k}: #{self.send k}" unless self.send(k).nil?
|
241
|
-
end # if data =~ /^#{k}:/
|
242
|
-
end #each do |k|
|
243
|
-
|
244
|
-
else # not readable, make a new file
|
245
|
-
data = ""
|
246
|
-
CONF_KEYS.keys.sort.each do |k|
|
247
|
-
data << "#{k}: #{self.send k}\n" unless self.send(k).nil?
|
248
|
-
end
|
249
|
-
end # if path readable
|
250
|
-
|
251
|
-
# make sure we end with a newline, the save it.
|
252
|
-
data << "\n" unless data.end_with?("\n")
|
253
|
-
path.jss_save data
|
254
|
-
end # read file
|
255
|
-
|
256
|
-
|
257
|
-
###
|
258
|
-
### Print out the current settings to stdout
|
259
|
-
###
|
260
|
-
### @return [void]
|
261
|
-
###
|
262
|
-
def print
|
263
|
-
CONF_KEYS.keys.sort.each{|k| puts "#{k}: #{self.send k}"}
|
264
|
-
end
|
265
|
-
|
266
|
-
#####################################
|
267
|
-
### Private Instance Methods
|
268
|
-
#####################################
|
269
|
-
private
|
270
|
-
|
271
|
-
###
|
272
|
-
### Read in any prefs file
|
273
|
-
###
|
274
|
-
### @param file[String,Pathname] the file to read
|
275
|
-
###
|
276
|
-
### @return [void]
|
277
|
-
###
|
278
|
-
def read(file)
|
279
|
-
|
280
|
-
Pathname.new(file).read.each_line do |line|
|
281
|
-
# skip blank lines and those starting with #
|
282
|
-
next if line =~ /^\s*(#|$)/
|
283
|
-
|
284
|
-
line.strip =~ /^(\w+?):\s*(\S.*)$/
|
285
|
-
next unless $1
|
286
|
-
attr = $1.to_sym
|
287
|
-
setter = "#{attr}=".to_sym
|
288
|
-
value = $2.strip
|
289
|
-
|
290
|
-
if CONF_KEYS.keys.include? attr
|
291
|
-
if value
|
292
|
-
# convert the value to the correct class
|
293
|
-
value = value.send(CONF_KEYS[attr])
|
294
|
-
end
|
295
|
-
self.send(setter, value)
|
296
|
-
end # if
|
297
|
-
end # do line
|
298
|
-
|
299
|
-
end # read file
|
300
|
-
|
301
|
-
end # class Preferences
|
302
|
-
|
303
|
-
# The single instance of Configuration
|
304
|
-
CONFIG = JSS::Configuration.instance
|
305
|
-
|
306
|
-
end # module
|