summaryrefslogtreecommitdiff
path: root/lib/chef/resource/habitat_service.rb
blob: 59b7b877e10a770691d7d5baea6c3daa6710afc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# Copyright:: Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require_relative "../resource"
require "chef-utils/dist" unless defined?(ChefUtils::Dist)

class Chef
  class Resource
    class HabitatService < Chef::Resource
      unified_mode true
      provides :habitat_service

      description "Use the **habitat_service** resource to manage Chef Habitat services. This requires that `core/hab-sup` be running as a service. See the `habitat_sup` resource documentation for more information. Note: Applications may run as a specific user. Often with Habitat, the default is `hab`, or `root`. If the application requires another user, then it should be created with Chef's `user` resource."
      introduced "17.3"
      examples <<~DOC
        **Install and load nginx**

        ```ruby
        habitat_package 'core/nginx'
        habitat_service 'core/nginx'

        habitat_service 'core/nginx unload' do
          service_name 'core/nginx'
          action :unload
        end
        ```

        **Pass the `strategy` and `topology` options to hab service commands**

        ```ruby
        habitat_service 'core/redis' do
          strategy 'rolling'
          topology 'standalone'
        end
        ```

        **Using update_condition**

        ```ruby
        habitat_service 'core/redis' do
          strategy 'rolling'
          update_condition 'track-channel'
          topology 'standalone'
        end
        ```

        **If the service has it's own user specified that is not the `hab` user, don't create the `hab` user on install, and instead create the application user with Chef's `user` resource**

        ```ruby
        habitat_install 'install habitat' do
          create_user false
        end

        user 'acme-apps' do
          system true
        end

        habitat_service 'acme/apps'
        ```
      DOC

      property :service_name, String, name_property: true,
        description: "The name of the service, must be in the form of `origin/name`"

      property :loaded, [true, false], default: false, skip_docs: true,
        description: "state property indicating whether the service is loaded in the supervisor"

      property :running, [true, false], default: false, skip_docs: true,
        description: "state property indicating whether the service is running in the supervisor"

      # hab svc options which get included based on the action of the resource
      property :strategy, [Symbol, String], equal_to: [:none, "none", :'at-once', "at-once", :rolling, "rolling"], default: :none, coerce: proc { |s| s.is_a?(String) ? s.to_sym : s },
        description: "Passes `--strategy` with the specified update strategy to the hab command. Defaults to `:none`. Other options are `:'at-once'` and `:rolling`"

      property :topology, [Symbol, String], equal_to: [:standalone, "standalone", :leader, "leader"], default: :standalone, coerce: proc { |s| s.is_a?(String) ? s.to_sym : s },
        description: "Passes `--topology` with the specified service topology to the hab command"

      property :bldr_url, String, default: "https://bldr.habitat.sh/",
        description: "Passes `--url` with the specified Habitat Builder URL to the hab command. Depending on the type of Habitat Builder you are connecting to, this URL will look different, here are the **3** current types:
        - Public Habitat Builder (default) - `https://bldr.habitat.sh`
        - On-Prem Habitat Builder installed using the [Source Install Method](https://github.com/habitat-sh/on-prem-builder) - `https://your.bldr.url`
        - On-Prem Habitat Builder installed using the [Automate Installer](https://automate.chef.io/docs/on-prem-builder/) - `https://your.bldr.url/bldr/v1`"

      property :channel, [Symbol, String], default: :stable, coerce: proc { |s| s.is_a?(String) ? s.to_sym : s },
        description: "Passes `--channel` with the specified channel to the hab command"

      property :bind, [String, Array], coerce: proc { |b| b.is_a?(String) ? [b] : b }, default: [],
        description: "Passes `--bind` with the specified services to bind to the hab command. If an array of multiple service binds are specified then a `--bind` flag is added for each."

      property :binding_mode, [Symbol, String], equal_to: [:strict, "strict", :relaxed, "relaxed"], default: :strict, coerce: proc { |s| s.is_a?(String) ? s.to_sym : s },
        description: "Passes `--binding-mode` with the specified binding mode. Defaults to `:strict`. Options are `:strict` or `:relaxed`"

      property :service_group, String, default: "default",
        description: " Passes `--group` with the specified service group to the hab command"

      property :shutdown_timeout, Integer, default: 8,
        description: "The timeout in seconds allowed during shutdown."

      property :health_check_interval, Integer, default: 30,
        description: "The interval (seconds) on which to run health checks."

      property :remote_sup, String, default: "127.0.0.1:9632", desired_state: false,
        description: "Address to a remote Supervisor's Control Gateway"

      # Http port needed for querying/comparing current config value
      property :remote_sup_http, String, default: "127.0.0.1:9631", desired_state: false,
        description: "IP address and port used to communicate with the remote supervisor. If this value is invalid, the resource will update the supervisor configuration each time #{ChefUtils::Dist::Server::PRODUCT} runs."

      property :gateway_auth_token, String, desired_state: false,
        description: "Auth token for accessing the remote supervisor's http port."

      property :update_condition, [Symbol, String], equal_to: [:latest, "latest", :'track-channel', "track-channel"], default: :latest, coerce: proc { |s| s.is_a?(String) ? s.to_sym : s },
        description: "Passes `--update-condition` dictating when this service should updated. Defaults to `latest`. Options are `latest` or `track-channel` **_Note: This requires a minimum habitat version of 1.5.71_**
        - `latest`: Runs the latest package that can be found in the configured channel and local packages.
        - `track-channel`: Always run the package at the head of a given channel. This enables service rollback, where demoting a package from a channel will cause the package to rollback to an older version of the package. A ramification of enabling this condition is that packages that are newer than the package at the head of the channel are also uninstalled during a service rollback."

      load_current_value do
        service_details = get_service_details(service_name)

        running service_up?(service_details)
        loaded service_loaded?(service_details)

        if loaded
          service_name get_spec_identifier(service_details)
          strategy get_update_strategy(service_details)
          update_condition get_update_condition(service_details)
          topology get_topology(service_details)
          bldr_url get_builder_url(service_details)
          channel get_channel(service_details)
          bind get_binds(service_details)
          binding_mode get_binding_mode(service_details)
          service_group get_service_group(service_details)
          shutdown_timeout get_shutdown_timeout(service_details)
          health_check_interval get_health_check_interval(service_details)
        end

        Chef::Log.debug("service #{service_name} service name: #{service_name}")
        Chef::Log.debug("service #{service_name} running state: #{running}")
        Chef::Log.debug("service #{service_name} loaded state: #{loaded}")
        Chef::Log.debug("service #{service_name} strategy: #{strategy}")
        Chef::Log.debug("service #{service_name} update condition: #{update_condition}")
        Chef::Log.debug("service #{service_name} topology: #{topology}")
        Chef::Log.debug("service #{service_name} builder url: #{bldr_url}")
        Chef::Log.debug("service #{service_name} channel: #{channel}")
        Chef::Log.debug("service #{service_name} binds: #{bind}")
        Chef::Log.debug("service #{service_name} binding mode: #{binding_mode}")
        Chef::Log.debug("service #{service_name} service group: #{service_group}")
        Chef::Log.debug("service #{service_name} shutdown timeout: #{shutdown_timeout}")
        Chef::Log.debug("service #{service_name} health check interval: #{health_check_interval}")
      end

      # This method is defined here otherwise it isn't usable in the
      # `load_current_value` method.
      #
      # It performs a check with TCPSocket to ensure that the HTTP API is
      # available first. If it cannot connect, it assumes that the service
      # is not running. It then attempts to reach the `/services` path of
      # the API to get a list of services. If this fails for some reason,
      # then it assumes the service is not running.
      #
      # Finally, it walks the services returned by the API to look for the
      # service we're configuring. If it is "Up", then we know the service
      # is running and fully operational according to Habitat. This is
      # wrapped in a begin/rescue block because if the service isn't
      # present and `sup_for_service_name` will be nil and we will get a
      # NoMethodError.
      #
      def get_service_details(svc_name)
        http_uri = "http://#{remote_sup_http}"

        begin
          TCPSocket.new(URI(http_uri).host, URI(http_uri).port).close
        rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
          Chef::Log.debug("Could not connect to #{http_uri} to retrieve status for #{service_name}")
          return false
        end

        begin
          headers = {}
          headers["Authorization"] = "Bearer #{gateway_auth_token}" if property_is_set?(:gateway_auth_token)
          svcs = Chef::HTTP::SimpleJSON.new(http_uri).get("/services", headers)
        rescue
          Chef::Log.debug("Could not connect to #{http_uri}/services to retrieve status for #{service_name}")
          return false
        end

        origin, name, _version, _release = svc_name.split("/")
        svcs.find do |s|
          s["pkg"]["origin"] == origin && s["pkg"]["name"] == name
        end
      end

      def service_up?(service_details)
        service_details["process"]["state"] == "up"
      rescue
        Chef::Log.debug("#{service_name} not found on the Habitat supervisor")
        false
      end

      def service_loaded?(service_details)
        if service_details
          true
        else
          false
        end
      end

      def get_spec_identifier(service_details)
        service_details["spec_ident"]["spec_identifier"]
      rescue
        Chef::Log.debug("#{service_name} not found on the Habitat supervisor")
        nil
      end

      def get_update_strategy(service_details)
        service_details["update_strategy"].to_sym
      rescue
        Chef::Log.debug("Update Strategy for #{service_name} not found on Supervisor API")
        "none"
      end

      def get_update_condition(service_details)
        service_details["update_condition"].to_sym
      rescue
        Chef::Log.debug("Update condition #{service_name} not found on Supervisor API")
        "latest"
      end

      def get_topology(service_details)
        service_details["topology"].to_sym
      rescue
        Chef::Log.debug("Topology for #{service_name} not found on Supervisor API")
        "standalone"
      end

      def get_builder_url(service_details)
        service_details["bldr_url"]
      rescue
        Chef::Log.debug("Habitat Builder URL for #{service_name} not found on Supervisor API")
        "https://bldr.habitat.sh"
      end

      def get_channel(service_details)
        service_details["channel"].to_sym
      rescue
        Chef::Log.debug("Channel for #{service_name} not found on Supervisor API")
        "stable"
      end

      def get_binds(service_details)
        service_details["binds"]
      rescue
        Chef::Log.debug("Update Strategy for #{service_name} not found on Supervisor API")
        []
      end

      def get_binding_mode(service_details)
        service_details["binding_mode"].to_sym
      rescue
        Chef::Log.debug("Binding mode for #{service_name} not found on Supervisor API")
        "strict"
      end

      def get_service_group(service_details)
        service_details["service_group"].split(".").last
      rescue
        Chef::Log.debug("Service Group for #{service_name} not found on Supervisor API")
        "default"
      end

      def get_shutdown_timeout(service_details)
        service_details["pkg"]["shutdown_timeout"]
      rescue
        Chef::Log.debug("Shutdown Timeout for #{service_name} not found on Supervisor API")
        8
      end

      def get_health_check_interval(service_details)
        service_details["health_check_interval"]["secs"]
      rescue
        Chef::Log.debug("Health Check Interval for #{service_name} not found on Supervisor API")
        30
      end

      action :load, description: "(default action) runs `hab service load` to load and start the specified application service" do
        modified = false
        converge_if_changed :service_name do
          modified = true
        end
        converge_if_changed :strategy do
          modified = true
        end
        converge_if_changed :update_condition do
          modified = true
        end
        converge_if_changed :topology do
          modified = true
        end
        converge_if_changed :bldr_url do
          modified = true
        end
        converge_if_changed :channel do
          modified = true
        end
        converge_if_changed :bind do
          modified = true
        end
        converge_if_changed :binding_mode do
          modified = true
        end
        converge_if_changed :service_group do
          modified = true
        end
        converge_if_changed :shutdown_timeout do
          modified = true
        end
        converge_if_changed :health_check_interval do
          modified = true
        end

        options = svc_options
        if current_resource.loaded && modified
          Chef::Log.debug("Reloading #{current_resource.service_name} using --force due to parameter change")
          options << "--force"
        end

        unless current_resource.loaded && !modified
          execute "test" do
            command "hab svc load #{new_resource.service_name} #{options.join(" ")}"
            retry_delay 10
            retries 5
          end
        end
      end

      action :unload, description: "runs `hab service unload` to unload and stop the specified application service" do
        if current_resource.loaded
          execute "hab svc unload #{new_resource.service_name} #{svc_options.join(" ")}"
          wait_for_service_unloaded
        end
      end

      action :start, description: "runs `hab service start` to start the specified application service" do
        unless current_resource.loaded
          Chef::Log.fatal("No service named #{new_resource.service_name} is loaded on the Habitat supervisor")
          raise "No service named #{new_resource.service_name} is loaded on the Habitat supervisor"
        end

        execute "hab svc start #{new_resource.service_name} #{svc_options.join(" ")}" unless current_resource.running
      end

      action :stop, description: "runs `hab service stop` to stop the specified application service" do
        unless current_resource.loaded
          Chef::Log.fatal("No service named #{new_resource.service_name} is loaded on the Habitat supervisor")
          raise "No service named #{new_resource.service_name} is loaded on the Habitat supervisor"
        end

        if current_resource.running
          execute "hab svc stop #{new_resource.service_name} #{svc_options.join(" ")}"
          wait_for_service_stopped
        end
      end

      action :restart, description: "runs the `:stop` and then `:start` actions" do
        action_stop
        action_start
      end

      action :reload, description: "runs the `:unload` and then `:load` actions" do
        action_unload
        action_load
      end

      action_class do
        def svc_options
          opts = []

          # certain options are only valid for specific `hab svc` subcommands.
          case action
          when :load
            opts.push(*new_resource.bind.map { |b| "--bind #{b}" }) if new_resource.bind
            opts << "--binding-mode #{new_resource.binding_mode}"
            opts << "--url #{new_resource.bldr_url}" if new_resource.bldr_url
            opts << "--channel #{new_resource.channel}" if new_resource.channel
            opts << "--group #{new_resource.service_group}" if new_resource.service_group
            opts << "--strategy #{new_resource.strategy}" if new_resource.strategy
            opts << "--update-condition #{new_resource.update_condition}" if new_resource.update_condition
            opts << "--topology #{new_resource.topology}" if new_resource.topology
            opts << "--health-check-interval #{new_resource.health_check_interval}" if new_resource.health_check_interval
            opts << "--shutdown-timeout #{new_resource.shutdown_timeout}" if new_resource.shutdown_timeout
          when :unload, :stop
            opts << "--shutdown-timeout #{new_resource.shutdown_timeout}" if new_resource.shutdown_timeout
          end

          opts << "--remote-sup #{new_resource.remote_sup}" if new_resource.remote_sup

          opts.map(&:split).flatten.compact
        end

        def wait_for_service_unloaded
          ruby_block "wait-for-service-unloaded" do
            block do
              raise "#{new_resource.service_name} still loaded" if service_loaded?(get_service_details(new_resource.service_name))
            end
            retries get_shutdown_timeout(new_resource.service_name) + 1
            retry_delay 1
          end

          ruby_block "update current_resource" do
            block do
              current_resource.loaded = service_loaded?(get_service_details(new_resource.service_name))
            end
            action :nothing
            subscribes :run, "ruby_block[wait-for-service-unloaded]", :immediately
          end
        end

        def wait_for_service_stopped
          ruby_block "wait-for-service-stopped" do
            block do
              raise "#{new_resource.service_name} still running" if service_up?(get_service_details(new_resource.service_name))
            end
            retries get_shutdown_timeout(new_resource.service_name) + 1
            retry_delay 1

            ruby_block "update current_resource" do
              block do
                current_resource.running = service_up?(get_service_details(new_resource.service_name))
              end
              action :nothing
              subscribes :run, "ruby_block[wait-for-service-stopped]", :immediately
            end
          end
        end
      end
    end
  end
end