ruby-jss 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ruby-jss might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.yardopts +7 -0
- data/CHANGES.md +112 -0
- data/LICENSE.txt +174 -0
- data/README.md +426 -0
- data/THANKS.md +6 -0
- data/bin/cgrouper +485 -0
- data/bin/subnet-update +400 -0
- data/lib/jss-api.rb +2 -0
- data/lib/jss.rb +190 -0
- data/lib/jss/api_connection.rb +410 -0
- data/lib/jss/api_object.rb +616 -0
- data/lib/jss/api_object/advanced_search.rb +389 -0
- data/lib/jss/api_object/advanced_search/advanced_computer_search.rb +95 -0
- data/lib/jss/api_object/advanced_search/advanced_mobile_device_search.rb +96 -0
- data/lib/jss/api_object/advanced_search/advanced_user_search.rb +95 -0
- data/lib/jss/api_object/building.rb +92 -0
- data/lib/jss/api_object/category.rb +147 -0
- data/lib/jss/api_object/computer.rb +852 -0
- data/lib/jss/api_object/creatable.rb +98 -0
- data/lib/jss/api_object/criteriable.rb +189 -0
- data/lib/jss/api_object/criteriable/criteria.rb +231 -0
- data/lib/jss/api_object/criteriable/criterion.rb +228 -0
- data/lib/jss/api_object/department.rb +93 -0
- data/lib/jss/api_object/distribution_point.rb +560 -0
- data/lib/jss/api_object/extendable.rb +221 -0
- data/lib/jss/api_object/extension_attribute.rb +466 -0
- data/lib/jss/api_object/extension_attribute/computer_extension_attribute.rb +362 -0
- data/lib/jss/api_object/extension_attribute/mobile_device_extension_attribute.rb +189 -0
- data/lib/jss/api_object/extension_attribute/user_extension_attribute.rb +117 -0
- data/lib/jss/api_object/group.rb +380 -0
- data/lib/jss/api_object/group/computer_group.rb +124 -0
- data/lib/jss/api_object/group/mobile_device_group.rb +139 -0
- data/lib/jss/api_object/group/user_group.rb +139 -0
- data/lib/jss/api_object/ldap_server.rb +535 -0
- data/lib/jss/api_object/locatable.rb +286 -0
- data/lib/jss/api_object/matchable.rb +97 -0
- data/lib/jss/api_object/mobile_device.rb +556 -0
- data/lib/jss/api_object/netboot_server.rb +148 -0
- data/lib/jss/api_object/network_segment.rb +414 -0
- data/lib/jss/api_object/osx_configuration_profile.rb +262 -0
- data/lib/jss/api_object/package.rb +839 -0
- data/lib/jss/api_object/peripheral.rb +335 -0
- data/lib/jss/api_object/peripheral_type.rb +295 -0
- data/lib/jss/api_object/policy.rb +898 -0
- data/lib/jss/api_object/purchasable.rb +316 -0
- data/lib/jss/api_object/removable_macaddr.rb +98 -0
- data/lib/jss/api_object/scopable.rb +136 -0
- data/lib/jss/api_object/scopable/scope.rb +621 -0
- data/lib/jss/api_object/script.rb +631 -0
- data/lib/jss/api_object/self_servable.rb +356 -0
- data/lib/jss/api_object/site.rb +93 -0
- data/lib/jss/api_object/software_update_server.rb +109 -0
- data/lib/jss/api_object/updatable.rb +117 -0
- data/lib/jss/api_object/uploadable.rb +138 -0
- data/lib/jss/api_object/user.rb +272 -0
- data/lib/jss/client.rb +504 -0
- data/lib/jss/compatibility.rb +66 -0
- data/lib/jss/composer.rb +185 -0
- data/lib/jss/configuration.rb +306 -0
- data/lib/jss/db_connection.rb +298 -0
- data/lib/jss/exceptions.rb +95 -0
- data/lib/jss/ruby_extensions.rb +35 -0
- data/lib/jss/ruby_extensions/filetest.rb +43 -0
- data/lib/jss/ruby_extensions/hash.rb +79 -0
- data/lib/jss/ruby_extensions/ipaddr.rb +91 -0
- data/lib/jss/ruby_extensions/pathname.rb +77 -0
- data/lib/jss/ruby_extensions/string.rb +59 -0
- data/lib/jss/ruby_extensions/time.rb +63 -0
- data/lib/jss/server.rb +108 -0
- data/lib/jss/utility.rb +478 -0
- data/lib/jss/version.rb +31 -0
- metadata +187 -0
@@ -0,0 +1,66 @@
|
|
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/composer.rb
ADDED
@@ -0,0 +1,185 @@
|
|
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
|
69
|
+
### the root file system of the target install drive
|
70
|
+
###
|
71
|
+
### @param opts[Hash] the options for building the .pkg
|
72
|
+
###
|
73
|
+
### @options opts :pkg_id[String] the full package if for the new pkg.
|
74
|
+
### e.g. 'com.mycompany.myapp'
|
75
|
+
###
|
76
|
+
### @option opts :bundle_id_prefix[String] the pkg bundle identifier prefix.
|
77
|
+
### If no :pkg_id is provided, one is made using this prefix and
|
78
|
+
### the name provided. e.g. 'com.mycompany'
|
79
|
+
### Defaults to '{PKG_BUNDLE_ID_PFX}'. See 'man pkgbuild' for more info
|
80
|
+
###
|
81
|
+
### @option opts :out_dir[String,Pathname] he folder in which the .pkg will be
|
82
|
+
### created. Defaults to {DEFAULT_OUT_DIR}
|
83
|
+
###
|
84
|
+
### @option opts :preserve_ownership[Boolean] If true, the owner/group of the
|
85
|
+
### rootpath are preserved.
|
86
|
+
### Default is false: they become the pkgbuild/installer "recommended"
|
87
|
+
### (root/wheel or root/admin)
|
88
|
+
###
|
89
|
+
### @return [Pathname] the local path to the new .pkg
|
90
|
+
###
|
91
|
+
def self.mk_pkg(name, version, root, opts = {})
|
92
|
+
raise NoSuchItemError, "Missing pkgbuild tool. Please make sure you're running 10.8 or later." unless PKGBUILD.executable?
|
93
|
+
|
94
|
+
opts[:out_dir] ||= DEFAULT_OUT_DIR
|
95
|
+
opts[:bundle_id_prefix] ||= PKG_BUNDLE_ID_PFX
|
96
|
+
|
97
|
+
pkg_filename = name.end_with?(".pkg") ? name : name+".pkg"
|
98
|
+
pkg_id = opts[:pkg_id]
|
99
|
+
pkg_id ||= opts[:bundle_id_prefix] + "." + name
|
100
|
+
pkg_out = "#{opts[:out_dir]}/#{pkg_filename}"
|
101
|
+
pkg_ownership = opts[:preserve_ownership] ? "preserve" : "recommended"
|
102
|
+
|
103
|
+
|
104
|
+
### first, run 'analyze' to get a 'component plist' in which we can change some settings
|
105
|
+
### for any bundles in the root (bundles like .apps, frameworks, plugins, etc..)
|
106
|
+
###
|
107
|
+
### we edit the settings thus:
|
108
|
+
### BundleOverwriteAction = upgrade, totally replace any version current on disk
|
109
|
+
### BundleIsVersionChecked = false, allow us to install regardless of what version is currently installed
|
110
|
+
### BundleIsRelocatable = false, if there's a version of this in some other location, Do Not move this one there after installation
|
111
|
+
### BundleHasStrictIdentifier = false, don't care if there's something at the install path with a different bundle id.
|
112
|
+
###
|
113
|
+
### In other words, just install the thing!
|
114
|
+
### (see 'man pkgbuild' for more info)
|
115
|
+
###
|
116
|
+
###
|
117
|
+
comp_plist_out = Pathname.new "/tmp/#{PKG_BUNDLE_ID_PFX}-#{pkg_filename}.plist"
|
118
|
+
system "#{PKGBUILD} --analyze --root '#{root}' '#{comp_plist_out}'"
|
119
|
+
comp_plist = Plist.parse_xml comp_plist_out.read
|
120
|
+
|
121
|
+
### if the plist is empty, there are no bundles in the pkg
|
122
|
+
if comp_plist[0].nil?
|
123
|
+
comp_plist_arg = ''
|
124
|
+
else
|
125
|
+
### otherwise, edit the bundle dictionaries
|
126
|
+
comp_plist.each do |bndl|
|
127
|
+
bndl.delete "ChildBundles" if bndl["ChildBundles"]
|
128
|
+
bndl["BundleOverwriteAction"] = "upgrade"
|
129
|
+
bndl["BundleIsVersionChecked"] = false
|
130
|
+
bndl["BundleIsRelocatable"] = false
|
131
|
+
bndl["BundleHasStrictIdentifier"] = false
|
132
|
+
end
|
133
|
+
### write out the edits
|
134
|
+
comp_plist_out.open('w'){|f| f.write comp_plist.to_plist}
|
135
|
+
comp_plist_arg = "--component-plist '#{comp_plist_out}'"
|
136
|
+
end
|
137
|
+
|
138
|
+
### now build the pkg
|
139
|
+
begin
|
140
|
+
system "#{PKGBUILD} --identifier '#{pkg_id}' --version '#{version}' --ownership #{pkg_ownership} --install-location / --root '#{root}' #{comp_plist_arg} '#{pkg_out}' "
|
141
|
+
|
142
|
+
raise RuntimeError, "There was an error building the .pkg" unless $?.exitstatus == 0
|
143
|
+
ensure
|
144
|
+
comp_plist_out.delete if comp_plist_out.exist?
|
145
|
+
end
|
146
|
+
|
147
|
+
return Pathname.new pkg_out
|
148
|
+
end # mk_dot_pkg
|
149
|
+
|
150
|
+
|
151
|
+
###
|
152
|
+
### Make a casper-happy .dmg out of a root folder, permissions are assumed to be correct.
|
153
|
+
###
|
154
|
+
### @param name[String] The name of the .dmg, the suffix will be added if needed
|
155
|
+
###
|
156
|
+
### @param root[String, Pathname] the path to the "root folder" representing the root file system of the target install drive
|
157
|
+
###
|
158
|
+
### @param out_dir[String, Pathname] the folder in which the .pkg will be created. Defaults to {DEFAULT_OUT_DIR}
|
159
|
+
###
|
160
|
+
### @return [Pathname] the local path to the new .dmg
|
161
|
+
###
|
162
|
+
###
|
163
|
+
def self.mk_dmg(name, root, out_dir = DEFAULT_OUT_DIR)
|
164
|
+
|
165
|
+
dmg_filename = "#{name}.dmg"
|
166
|
+
dmg_vol = name
|
167
|
+
dmg_out = Pathname.new "#{out_dir}/#{dmg_filename}"
|
168
|
+
if dmg_out.exist?
|
169
|
+
mv_to = dmg_out.dirname + "#{dmg_out.basename}.#{Time.now.strftime('%Y%m%d%H%M%S')}"
|
170
|
+
dmg_out.rename mv_to
|
171
|
+
end # if dmg out exist
|
172
|
+
|
173
|
+
### TODO - this may need to be sudo'd to handle proper internal permissions.
|
174
|
+
system "#{HDI_UTIL} create -volname '#{dmg_vol}' -scrub -srcfolder '#{root}' '#{dmg_out}'"
|
175
|
+
|
176
|
+
raise RuntimeError, "There was an error building the .dmg" unless $?.exitstatus == 0
|
177
|
+
return Pathname.new dmg_out
|
178
|
+
|
179
|
+
end # mk_dmg
|
180
|
+
|
181
|
+
|
182
|
+
end # module Composer
|
183
|
+
end # module JSS
|
184
|
+
|
185
|
+
|
@@ -0,0 +1,306 @@
|
|
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
|