ruby-jss 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +29 -22
  3. data/README.md +66 -86
  4. data/bin/jamfHelperBackgrounder +148 -0
  5. data/bin/netseg-update +0 -1
  6. data/lib/jss.rb +20 -9
  7. data/lib/jss/api_connection.rb +369 -295
  8. data/lib/jss/api_object.rb +651 -418
  9. data/lib/jss/api_object/account.rb +69 -77
  10. data/lib/jss/api_object/advanced_search.rb +201 -236
  11. data/lib/jss/api_object/advanced_search/advanced_computer_search.rb +42 -42
  12. data/lib/jss/api_object/advanced_search/advanced_mobile_device_search.rb +33 -43
  13. data/lib/jss/api_object/advanced_search/advanced_user_search.rb +33 -43
  14. data/lib/jss/api_object/building.rb +39 -52
  15. data/lib/jss/api_object/categorizable.rb +221 -0
  16. data/lib/jss/api_object/category.rb +81 -89
  17. data/lib/jss/api_object/computer.rb +486 -525
  18. data/lib/jss/api_object/computer_invitation.rb +73 -86
  19. data/lib/jss/api_object/criteriable.rb +6 -7
  20. data/lib/jss/api_object/ebook.rb +21 -0
  21. data/lib/jss/api_object/extendable.rb +6 -8
  22. data/lib/jss/api_object/group.rb +0 -3
  23. data/lib/jss/api_object/locatable.rb +19 -20
  24. data/lib/jss/api_object/mac_application.rb +21 -0
  25. data/lib/jss/api_object/mobile_device.rb +30 -21
  26. data/lib/jss/api_object/mobile_device_application.rb +447 -0
  27. data/lib/jss/api_object/mobile_device_configuration_profile.rb +21 -0
  28. data/lib/jss/api_object/osx_configuration_profile.rb +0 -3
  29. data/lib/jss/api_object/package.rb +21 -34
  30. data/lib/jss/api_object/peripheral.rb +16 -18
  31. data/lib/jss/api_object/policy.rb +5 -83
  32. data/lib/jss/api_object/purchasable.rb +11 -13
  33. data/lib/jss/api_object/scopable.rb +11 -12
  34. data/lib/jss/api_object/script.rb +3 -17
  35. data/lib/jss/api_object/self_servable.rb +419 -205
  36. data/lib/jss/api_object/self_servable/icon.rb +179 -0
  37. data/lib/jss/api_object/updatable.rb +35 -34
  38. data/lib/jss/api_object/uploadable.rb +72 -70
  39. data/lib/jss/api_object/user.rb +6 -7
  40. data/lib/jss/api_object/vppable.rb +117 -0
  41. data/lib/jss/client.rb +264 -225
  42. data/lib/jss/db_connection.rb +7 -5
  43. data/lib/jss/exceptions.rb +50 -42
  44. data/lib/jss/ruby_extensions.rb +8 -7
  45. data/lib/jss/ruby_extensions/object.rb +19 -0
  46. data/lib/jss/utility.rb +82 -40
  47. data/lib/jss/version.rb +1 -1
  48. metadata +37 -68
  49. data/bin/jss-webhook-server +0 -3
  50. data/lib/jss/webhooks.rb +0 -53
  51. data/lib/jss/webhooks/README.md +0 -269
  52. data/lib/jss/webhooks/configuration.rb +0 -213
  53. data/lib/jss/webhooks/data/sample_handlers/RestAPIOperation-executable +0 -91
  54. data/lib/jss/webhooks/data/sample_handlers/RestAPIOperation.rb +0 -45
  55. data/lib/jss/webhooks/data/sample_jsons/ComputerAdded.json +0 -27
  56. data/lib/jss/webhooks/data/sample_jsons/ComputerCheckIn.json +0 -27
  57. data/lib/jss/webhooks/data/sample_jsons/ComputerInventoryCompleted.json +0 -27
  58. data/lib/jss/webhooks/data/sample_jsons/ComputerPolicyFinished.json +0 -27
  59. data/lib/jss/webhooks/data/sample_jsons/ComputerPushCapabilityChanged.json +0 -27
  60. data/lib/jss/webhooks/data/sample_jsons/JSSShutdown.json +0 -14
  61. data/lib/jss/webhooks/data/sample_jsons/JSSStartup.json +0 -14
  62. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceCheckIn.json +0 -26
  63. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceCommandCompleted.json +0 -26
  64. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceEnrolled.json +0 -26
  65. data/lib/jss/webhooks/data/sample_jsons/MobileDevicePushSent.json +0 -26
  66. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceUnEnrolled.json +0 -26
  67. data/lib/jss/webhooks/data/sample_jsons/PatchSoftwareTitleUpdated.json +0 -14
  68. data/lib/jss/webhooks/data/sample_jsons/PushSent.json +0 -11
  69. data/lib/jss/webhooks/data/sample_jsons/RestAPIOperation.json +0 -15
  70. data/lib/jss/webhooks/data/sample_jsons/SCEPChallenge.json +0 -10
  71. data/lib/jss/webhooks/data/sample_jsons/SmartGroupComputerMembershipChange.json +0 -13
  72. data/lib/jss/webhooks/data/sample_jsons/SmartGroupMobileDeviceMembershipChange.json +0 -13
  73. data/lib/jss/webhooks/event.rb +0 -139
  74. data/lib/jss/webhooks/event/computer_added.rb +0 -38
  75. data/lib/jss/webhooks/event/computer_check_in.rb +0 -38
  76. data/lib/jss/webhooks/event/computer_inventory_completed.rb +0 -38
  77. data/lib/jss/webhooks/event/computer_policy_finished.rb +0 -38
  78. data/lib/jss/webhooks/event/computer_push_capability_changed.rb +0 -38
  79. data/lib/jss/webhooks/event/handlers.rb +0 -192
  80. data/lib/jss/webhooks/event/jss_shutdown.rb +0 -38
  81. data/lib/jss/webhooks/event/jss_startup.rb +0 -38
  82. data/lib/jss/webhooks/event/mobile_device_check_in.rb +0 -38
  83. data/lib/jss/webhooks/event/mobile_device_command_completed.rb +0 -38
  84. data/lib/jss/webhooks/event/mobile_device_enrolled.rb +0 -38
  85. data/lib/jss/webhooks/event/mobile_device_push_sent.rb +0 -38
  86. data/lib/jss/webhooks/event/mobile_device_unenrolled.rb +0 -38
  87. data/lib/jss/webhooks/event/patch_software_title_updated.rb +0 -38
  88. data/lib/jss/webhooks/event/push_sent.rb +0 -38
  89. data/lib/jss/webhooks/event/rest_api_operation.rb +0 -38
  90. data/lib/jss/webhooks/event/scep_challenge.rb +0 -38
  91. data/lib/jss/webhooks/event/smart_group_computer_membership_change.rb +0 -38
  92. data/lib/jss/webhooks/event/smart_group_mobile_device_membership_change.rb +0 -38
  93. data/lib/jss/webhooks/event/webhook.rb +0 -40
  94. data/lib/jss/webhooks/event_objects.rb +0 -112
  95. data/lib/jss/webhooks/event_objects/computer.rb +0 -49
  96. data/lib/jss/webhooks/event_objects/jss.rb +0 -36
  97. data/lib/jss/webhooks/event_objects/mobile_device.rb +0 -48
  98. data/lib/jss/webhooks/event_objects/patch_software_title_update.rb +0 -38
  99. data/lib/jss/webhooks/event_objects/push.rb +0 -33
  100. data/lib/jss/webhooks/event_objects/rest_api_operation.rb +0 -37
  101. data/lib/jss/webhooks/event_objects/scep_challenge.rb +0 -32
  102. data/lib/jss/webhooks/event_objects/smart_group.rb +0 -35
  103. data/lib/jss/webhooks/server_app.rb +0 -37
  104. data/lib/jss/webhooks/server_app/routes.rb +0 -27
  105. data/lib/jss/webhooks/server_app/routes/handle_webhook_event.rb +0 -39
  106. data/lib/jss/webhooks/server_app/routes/home.rb +0 -37
  107. data/lib/jss/webhooks/server_app/self_signed_cert.rb +0 -65
  108. data/lib/jss/webhooks/server_app/server.rb +0 -60
  109. data/lib/jss/webhooks/version.rb +0 -32
@@ -1,3 +0,0 @@
1
- #!/usr/bin/ruby
2
- require 'jss/webhooks/server_app'
3
- JSSWebHooks::Server.run!
@@ -1,53 +0,0 @@
1
- ### Copyright 2017 Pixar
2
-
3
- ###
4
- ### Licensed under the Apache License, Version 2.0 (the "Apache License")
5
- ### with the following modification; you may not use this file except in
6
- ### compliance with the Apache License and the following modification to it:
7
- ### Section 6. Trademarks. is deleted and replaced with:
8
- ###
9
- ### 6. Trademarks. This License does not grant permission to use the trade
10
- ### names, trademarks, service marks, or product names of the Licensor
11
- ### and its affiliates, except as required to comply with Section 4(c) of
12
- ### the License and to reproduce the content of the NOTICE file.
13
- ###
14
- ### You may obtain a copy of the Apache License at
15
- ###
16
- ### http://www.apache.org/licenses/LICENSE-2.0
17
- ###
18
- ### Unless required by applicable law or agreed to in writing, software
19
- ### distributed under the Apache License with the above modification is
20
- ### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21
- ### KIND, either express or implied. See the Apache License for the specific
22
- ### language governing permissions and limitations under the Apache License.
23
- ###
24
- ###
25
-
26
- require 'ruby-jss'
27
- require 'immutable-struct'
28
-
29
-
30
- # The JSSWebHooks module
31
- #
32
- module JSSWebHooks
33
-
34
- # load in some sample JSON files, one per event type
35
- @sample_jsons = {}
36
-
37
- sample_json_dir = Pathname.new(__FILE__).parent + 'webhooks/data/sample_jsons'
38
- sample_json_dir.children.each do |jf|
39
- event = jf.basename.to_s.chomp(jf.extname).to_sym
40
- @sample_jsons[event] = jf.read
41
- end
42
-
43
- def self.sample_jsons
44
- @sample_jsons
45
- end
46
-
47
- end # module
48
-
49
- require 'jss/webhooks/configuration'
50
- require 'jss/webhooks/event_objects'
51
- require 'jss/webhooks/event'
52
-
53
- JSSWebHooks::Event::Handlers.load_handlers
@@ -1,269 +0,0 @@
1
-
2
- # WebHooks for ruby-jss
3
-
4
- - [Introduction](#introduction)
5
- - [The Framework](#the-framework)
6
- - [Event Handlers](#event-handlers)
7
- - [Internal Handlers](#internal-handlers)
8
- - [External Handlers](#external-handlers)
9
- - [Putting it together](#putting-it-together)
10
- - [Events and Event objects](#events-and-event-objects)
11
- - [The Server](#the-server)
12
- - [Installing JSSWebHooks into ruby-jss](#installing-jsswebhooks-into-ruby-jss)
13
- - [TODOs](#todos)
14
-
15
- <!-- TOC depthFrom:2 depthTo:6 withLinks:1 updateOnSave:0 orderedList:0 -->
16
-
17
-
18
- <!-- /TOC -->
19
-
20
- ## Introduction
21
-
22
- JSSWebHooks is a sub-module of ruby-jss which implements both a framework for
23
- working with JSS Webhook events, and a simple http server, based on Sinatra and
24
- Webrick, for handling those events.
25
-
26
- You do not need to be a Ruby programmer to make use of this framework! "Event Handlers"
27
- can be written in any language and used by the web server included with the module.
28
- See _Event Handlers_ and _The Server_ below for more info.
29
-
30
- JSSWebHooks is still in early development. While the basics seem to work,
31
- there's much to do before it can be released in the ruby-jss gem.
32
-
33
- For details about the JSS Webhooks API, and the JSON data it passes, please see
34
- [Bryson Tyrrell's excellent
35
- documentation.](https://unofficial-jss-api-docs.atlassian.net/wiki/display/JRA/Webhooks+API)
36
-
37
- **Note:** when creating WebHooks in your JSS to be handled by the framework, you must
38
- specify JSON in the 'Content Type' section. This framework doesn't support XML
39
- formated WebHook data.
40
-
41
- ## The Framework
42
-
43
- The JSSWebHooks framework abstracts WebHook events and their parts as Ruby
44
- classes. When the JSON payload of a JSS WebHook POST request is passed into the
45
- `JSSWebHooks::Event.parse_event` method, an instance of the appropriate subclass
46
- of `JSSWebHooks::Event` is returned, for example
47
- `JSSWebHooks::ComputerInventoryCompletedEvent`
48
-
49
- Each event instance contains these important attributes:
50
-
51
- * **webhook:** A read-only instance of `JSSWebHook::Event::WebHook`
52
- representing the WebHook stored in the JSS which cause the POST request. This
53
- object has attributes matching those in the "webhook" dict. of the POSTed
54
- JSON.
55
-
56
- * **event_object:** A read-only instance of a `JSSWebHook::EventObject::<Class>`
57
- representing the 'event object' that accompanies the event that triggered the
58
- WebHook. It comes from the 'object' dict of the POSTed JSON, and different
59
- events come with different objects attached. For example, the
60
- ComputerInventoryCompleted event comes with a "computer" object, containing
61
- data about the JSS computer that completed inventory.
62
-
63
- This is not full `JSS::Computer` instance from the REST API, but rather a group
64
- of named attributes about that computer. At the moment the JSSWebHooks
65
- framework makes no attempt to use the event object to create a `JSS::Computer`
66
- instance but the handlers written for the event could easily do so if needed.
67
-
68
- * **event_json:** The JSON content from the POST request, parsed into
69
- a Ruby hash with symbolized keys (meaning the JSON key "deviceName" becomes
70
- the symbol :deviceName)
71
-
72
- * **raw_json:** A String containing the raw JSON from the POST
73
- request.
74
-
75
- * **handlers:** An Array of custom plugins for working with the
76
- event. See _Event Handlers_, below.
77
-
78
-
79
- ### Event Handlers
80
-
81
- A handler is a file containing code to run when a webhook event occurs. These
82
- files are located in a specified directory, /Library/Application
83
- Support/JSSWebHooks/ by default, and are loaded at runtime. It's up to the Casper
84
- administrator to create these handlers to perform desired tasks. Each class of
85
- event can have as many handlers as desired, all will be executed when the event's
86
- `handle` method is called.
87
-
88
- Handler files must begin with the name of the event they handle, e.g.
89
- ComputerAdded, followed by: nothing, a dot, a dash, or an underscore. Hander
90
- filenames are case-insensitive.
91
-
92
- All of these filenames work as handlers for ComputerAdded events:
93
-
94
- - ComputerAdded
95
- - computeradded.sh
96
- - COMPUTERAdded_notify_team
97
- - Computeradded-update-ldap
98
-
99
- There are two kinds of handlers:
100
-
101
- #### Internal Handlers
102
-
103
- These handlers are _non-executable_ files containing Ruby code. The code is
104
- loaded at runtime and executed in the context of the JSSWebHooks Framework when
105
- called by an event.
106
-
107
- Internal handlers must be defined as a [ruby code block](http://rubylearning.com/satishtalim/ruby_blocks.html) passed to the
108
- `JSSWebHooks.event_handler` method. The block must take one parameter, the
109
- JSSWebHooks::Event subclass instance being handled. Here's a simple example of
110
- a handler for a JSSWebHooks::ComputerAddedEvent
111
-
112
- ```ruby
113
- JSSWebHooks.event_handler do |event|
114
- cname = event.event_object.deviceName
115
- uname = event.event_object.realName
116
- puts "Computer '#{cname}' was just added to the JSS for user #{uname}."
117
- end
118
- ```
119
-
120
- In this example, the codeblock takes one parameter, which it expects to be
121
- a JSSWebHooks::ComputerAddedEvent instance, and uses it in the variable "event".
122
- It then extracts the "deviceName" and "realName" values from the event_object
123
- contained in the event, and uses them to send a message to stdout.
124
-
125
- Internal handlers **must not** be executable files. Executability is how the
126
- framework determines if a handler is internal or external.
127
-
128
- #### External Handlers
129
-
130
- External handlers are _executable_ files that are executed when called by an
131
- event. They can be written in any language, but they must accept raw JSON on
132
- their standard input. It's up to them to parse that JSON and react to it as
133
- desired. In this case the JSSWebHooks framework is merely a conduit for passing
134
- the Posted JSON to the executable program.
135
-
136
- Here's a simple example using bash and [jq](https://stedolan.github.io/jq/) to
137
- do the same as the ruby example above:
138
-
139
- ```bash
140
- #!/bin/bash
141
- JQ="/usr/local/bin/jq"
142
- while read line ; do JSON="$JSON $line" ; done
143
- cname=`echo $JSON | "$JQ" -r '.event.deviceName'`
144
- uname=`echo $JSON | "$JQ" -r '.event.realName'`
145
- echo "Computer '${cname}' was just added to the JSS for user ${uname}."
146
- ```
147
-
148
- External handlers **must** be executable files. Executability is how the
149
- framework determines if a handler is internal or external.
150
-
151
- See ruby-jss/lib/jss/webhooks/data/sample_handlers/RestAPIOperation-executable
152
- for a more detailed bash example that handles RestAPIOperation events.
153
-
154
- ### Putting it together
155
-
156
- Here's a commented sample of ruby code that uses the framework to process a
157
- ComputerAdded event:
158
-
159
- ```ruby
160
- # load in the framework
161
- require 'jss/webhooks'
162
-
163
- # The framework comes with sample JSON files for each event type.
164
- # In reality, a webserver would extract this from the data POSTed from the JSS
165
- posted_json = JSSWebHooks.sample_jsons[:ComputerAdded]
166
-
167
- # Create JSSWebHooks::Event::ComputerAddedEvent instance for the event
168
- event = JSSWebHooks::Event.parse_event posted_json
169
-
170
- # Call the events #handle method, which will execute any ComputerAdded
171
- # handlers that were in the Handler directory when the framework was loaded.
172
- event.handle
173
- ```
174
-
175
- Of course, you can use the framework without using the built-in #handle method,
176
- and if you don't have any handlers in the directory, it won't do anything
177
- anyway. Instead you are welcome to use the Event objects as desired in your own
178
- Ruby code.
179
-
180
- ### Events and Event objects
181
-
182
- Here are the Event classes supported by the framework and the EventObject classes
183
- they contain.
184
- For details about the attributes of each EventObject, see [The Unofficial JSS API
185
- Docs](https://unofficial-jss-api-docs.atlassian.net/wiki/display/JRA/Webhooks+API)
186
-
187
- Each Event class is a subclass of `JSSWebHooks::Event`, where all of their
188
- functionality is defined.
189
-
190
- The EventObject classes aren't suclasses, but are dynamically-defined members of
191
- the `JSSWebHooks::EventObjects` module.
192
-
193
- | Event Classes | Event Object Classes |
194
- | -------------- | ------------ |
195
- | JSSWebHooks::ComputerAddedEvent | JSSWebHooks::EventObjects::Computer |
196
- | JSSWebHooks::ComputerCheckInEvent | JSSWebHooks::EventObjects::Computer |
197
- | JSSWebHooks::ComputerInventoryCompletedEvent | JSSWebHooks::EventObjects::Computer |
198
- | JSSWebHooks::ComputerPolicyFinishedEvent | JSSWebHooks::EventObjects::Computer |
199
- | JSSWebHooks::ComputerPushCapabilityChangedEvent | JSSWebHooks::EventObjects::Computer |
200
- | JSSWebHooks::JSSShutdownEvent | JSSWebHooks::EventObjects::JSS |
201
- | JSSWebHooks::JSSStartupEvent | JSSWebHooks::EventObjects::JSS |
202
- | JSSWebHooks::MobilDeviceCheckinEvent | JSSWebHooks::EventObjects::MobileDevice |
203
- | JSSWebHooks::MobilDeviceCommandCompletedEvent | JSSWebHooks::EventObjects::MobileDevice |
204
- | JSSWebHooks::MobilDeviceEnrolledEvent | JSSWebHooks::EventObjects::MobileDevice |
205
- | JSSWebHooks::MobilDevicePushSentEvent | JSSWebHooks::EventObjects::MobileDevice |
206
- | JSSWebHooks::MobilDeviceUnenrolledEvent | JSSWebHooks::EventObjects::MobileDevice |
207
- | JSSWebHooks::PatchSoftwareTitleUpdateEvent | JSSWebHooks::EventObjects::PatchSoftwareTitleUpdate |
208
- | JSSWebHooks::PushSentEvent | JSSWebHooks::EventObjects::Push |
209
- | JSSWebHooks::RestAPIOperationEvent | JSSWebHooks::EventObjects::RestAPIOperation |
210
- | JSSWebHooks::SCEPChallengeEvent | JSSWebHooks::EventObjects::SCEPChallenge |
211
- | JSSWebHooks::SmartGroupComputerMembershipChangeEvent | JSSWebHooks::EventObjects::SmartGroup |
212
- | JSSWebHooks::SmartGroupMobileDeviveMembershipChangeEvent | JSSWebHooks::EventObjects::SmartGroup |
213
-
214
-
215
- ## The Server
216
-
217
- JSSWebHooks comes with a simple http server that uses the JSSWebHooks framework
218
- to handle all incoming webhook POST requests from the JSS via a single URL.
219
-
220
- To use it you'll need to install the [sinatra](http://www.sinatrarb.com/
221
- ) ruby gem (`sudo gem install sinatra`).
222
-
223
- After that, just run the `jss-webhook-server` command located in the bin
224
- directory for ruby-jss and then point your WebHooks at:
225
- http://_my_hostname_/handle_webhook_event
226
-
227
- It will then process all incoming webhook POST requests using whatever handlers
228
- you have installed.
229
-
230
- To automate it on a dedicated machine, just make a LaunchDaemon plist to run
231
- that command and keep it running.
232
-
233
- ## Installing JSSWebHooks into ruby-jss
234
-
235
- Until JSSWebHooks is officially released as part of ruby-jss, here's how to get
236
- it up and running:
237
-
238
- 0. Write a handler or two (see _Handlers_ above) and put them into
239
- /Library/Application Support/JSSWebHooks/
240
- 0. Clone ruby-jss from github into some path like /path/to/github/clone/
241
- 1. If you don't already have it, install the ruby-jss gem `sudo gem install ruby-jss`
242
- 2. Install sinata: `sudo gem install sinatra`
243
- 3. Install immutable-struct: `sudo gem install immutable-struct`
244
- 4. From /path/to/github/clone/lib/jss/ copy the webhooks folder **and** webhooks.rb
245
- and into /Library/Ruby/Gems/2.0.0/gems/ruby-jss-_version_/lib/jss/ or
246
- where-ever your gems are installed.
247
- 5. From /path/to/github/clone/bin/ copy 'jss-webhook-server' into
248
- /Library/Ruby/Gems/2.0.0/gems/ruby-jss-_version_/bin/
249
-
250
- Then fire up `irb` and `require jss/webhooks` to start playing around. (remember
251
- the sample JSON strings available in `JSSWebHooks.sample_jsons`)
252
-
253
- OR
254
-
255
- run /Library/Ruby/Gems/2.0.0/gems/ruby-jss-_version_/bin/jss-webhook-server and
256
- point some WebHooks at your machine.
257
-
258
-
259
- ## TODOs
260
-
261
- - Add SSL support to the server
262
- - Better (any!) thread management for handlers
263
- - Logging and Debug options
264
- - handler reloading for individual, or all, Event subclasses
265
- - Generate the YARD docs
266
- - better namespace protection for internal handlers
267
- - Use and improve the configuration stuff.
268
- - write proper documentation beyond this README
269
- - I'm sure there's more to do...
@@ -1,213 +0,0 @@
1
- ### Copyright 2017 Pixar
2
-
3
- ###
4
- ### Licensed under the Apache License, Version 2.0 (the "Apache License")
5
- ### with the following modification; you may not use this file except in
6
- ### compliance with the Apache License and the following modification to it:
7
- ### Section 6. Trademarks. is deleted and replaced with:
8
- ###
9
- ### 6. Trademarks. This License does not grant permission to use the trade
10
- ### names, trademarks, service marks, or product names of the Licensor
11
- ### and its affiliates, except as required to comply with Section 4(c) of
12
- ### the License and to reproduce the content of the NOTICE file.
13
- ###
14
- ### You may obtain a copy of the Apache License at
15
- ###
16
- ### http://www.apache.org/licenses/LICENSE-2.0
17
- ###
18
- ### Unless required by applicable law or agreed to in writing, software
19
- ### distributed under the Apache License with the above modification is
20
- ### distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21
- ### KIND, either express or implied. See the Apache License for the specific
22
- ### language governing permissions and limitations under the Apache License.
23
- ###
24
- ###
25
-
26
- module JSSWebHooks
27
-
28
- # The configuration object
29
- class Configuration
30
-
31
- include Singleton
32
-
33
- # The location of the default config file
34
- DEFAULT_CONF_FILE = Pathname.new '/etc/jsswebhooks.conf'
35
-
36
- ### The attribute keys we maintain, and the type they should be stored as
37
- CONF_KEYS = {
38
- server_port: :to_i,
39
- server_engine: :to_sym,
40
- handler_dir: :to_s,
41
- handler_computer_added: :to_s,
42
- handler_computer_check_in: :to_s,
43
- handler_computer_inventory_completed: :to_s,
44
- handler_computer_policy_finished: :to_s,
45
- handler_computer_push_capability_changed: :to_s,
46
- handler_jss_shutdown: :to_s,
47
- handler_jss_startup: :to_s,
48
- handler_mobile_device_check_in: :to_s,
49
- handler_mobile_device_command_complete: :to_s,
50
- handler_mobile_device_enrolled: :to_s,
51
- handler_mobile_device_push_sent: :to_s,
52
- handler_mobile_device_unenrolled: :to_s,
53
- handler_patch_software_title_updated: :to_s,
54
- handler_push_sent: :to_s,
55
- handler_rest_api_operation: :to_s,
56
- handler_scep_challenge: :to_s,
57
- handler_smart_group_computer_membership_change: :to_s,
58
- handler_smart_group_mobile_device_membership_change: :to_s
59
- }.freeze
60
-
61
- #####################################
62
- ### Class Variables
63
- #####################################
64
-
65
- #####################################
66
- ### Class Methods
67
- #####################################
68
-
69
- #####################################
70
- ### Attributes
71
- #####################################
72
-
73
- # automatically create accessors for all the CONF_KEYS
74
- CONF_KEYS.keys.each { |k| attr_accessor k }
75
-
76
- #####################################
77
- ### Constructor
78
- #####################################
79
-
80
- ###
81
- ### Initialize!
82
- ###
83
- def initialize
84
- read_global
85
- end
86
-
87
- #####################################
88
- ### Public Instance Methods
89
- #####################################
90
-
91
- ###
92
- ### Clear all values
93
- ###
94
- ### @return [void]
95
- ###
96
- def clear_all
97
- CONF_KEYS.keys.each { |k| send "#{k}=".to_sym, nil }
98
- end
99
-
100
- ###
101
- ### (Re)read the global prefs, if it exists.
102
- ###
103
- ### @return [Boolean] was the file loaded?
104
- ###
105
- def read_global
106
- return false unless DEFAULT_CONF_FILE.file? && DEFAULT_CONF_FILE.readable?
107
- read DEFAULT_CONF_FILE
108
- end
109
-
110
- ###
111
- ### Clear the settings and reload the prefs file, or another file if provided
112
- ###
113
- ### @param file[String,Pathname] a non-standard prefs file to load
114
- ###
115
- ### @return [Boolean] was the file reloaded?
116
- ###
117
- def reload(file = DEFAULT_CONF_FILE)
118
- file = Pathname.new file
119
- return false unless file.file? and file.readable?
120
- clear_all
121
- read file
122
- end
123
-
124
- ###
125
- ### Save the prefs into a file
126
- ###
127
- ### @param file[Symbol,String,Pathname] either :user, :global, or an arbitrary file to save.
128
- ###
129
- ### @return [void]
130
- ###
131
- def save(file)
132
- path = Pathname.new(file)
133
-
134
- # file already exists? read it in and update the values.
135
- # Don't overwrite it, since the user might have comments
136
- # in there.
137
- if path.readable?
138
- data = path.read
139
-
140
- # go thru the known attributes/keys
141
- CONF_KEYS.keys.sort.each do |k|
142
- # if the key exists, update it.
143
- if data =~ /^#{k}:/
144
- data.sub!(/^#{k}:.*$/, "#{k}: #{send k}")
145
-
146
- # if not, add it to the end unless it's nil
147
- else
148
- data += "\n#{k}: #{send k}" unless send(k).nil?
149
- end # if data =~ /^#{k}:/
150
- end # each do |k|
151
-
152
- else # not readable, make a new file
153
- data = ''
154
- CONF_KEYS.keys.sort.each do |k|
155
- data << "#{k}: #{send k}\n" unless send(k).nil?
156
- end
157
- end # if path readable
158
-
159
- # make sure we end with a newline, the save it.
160
- data << "\n" unless data.end_with?("\n")
161
- path.jss_save data
162
- end # save file
163
-
164
- ###
165
- ### Print out the current settings to stdout
166
- ###
167
- ### @return [void]
168
- ###
169
- def print
170
- CONF_KEYS.keys.sort.each { |k| puts "#{k}: #{send k}" }
171
- end
172
-
173
- #####################################
174
- ### Private Instance Methods
175
- #####################################
176
- private
177
-
178
- ###
179
- ### Read in any prefs file
180
- ###
181
- ### @param file[String,Pathname] the file to read
182
- ###
183
- ### @return [Boolean] was the file read?
184
- ###
185
- def read(file)
186
- available_conf_keys = CONF_KEYS.keys
187
- Pathname.new(file).read.each_line do |line|
188
- # skip blank lines and those starting with #
189
- next if line =~ /^\s*(#|$)/
190
-
191
- line.strip =~ /^(\w+?):\s*(\S.*)$/
192
- key = Regexp.last_match(1)
193
- next unless key
194
- attr = key.to_sym
195
- setter = "#{key}=".to_sym
196
- value = Regexp.last_match(2).strip
197
-
198
- next unless available_conf_keys.include? attr
199
-
200
- # convert the value to the correct class
201
- value = value.send(CONF_KEYS[attr]) if value
202
-
203
- send(setter, value)
204
- end # do line
205
- true
206
- end # read file
207
-
208
- end # class Configuration
209
-
210
- # The single instance of Configuration
211
- CONFIG = JSSWebHooks::Configuration.instance
212
-
213
- end # module