<commit_before>#
# Cookbook:: runit
# Provider:: runit_service
#
# Author:: <NAME> <<EMAIL>>
# Author:: <NAME> <<EMAIL>>
# Copyright:: 2011-2019, Chef Software, Inc.
#
# 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.
#

class Chef
  class Provider
    class RunitService < Chef::Provider::LWRPBase
      provides :runit_service

      unless defined?(VALID_SIGNALS)
        # Mapping of valid signals with optional friendly name
        VALID_SIGNALS = Mash.new(
          :down => nil,
          :hup => nil,
          :int => nil,
          :term => nil,
          :kill => nil,
          :quit => nil,
          :up => nil,
          :once => nil,
          :cont => nil,
          1 => :usr1,
          2 => :usr2
        )
      end

      # Mix in helpers from libraries/helpers.rb
      include RunitCookbook::Helpers

      # actions
      action :create do
        declare_service_resource

        ruby_block 'restart_service' do
          block do
            previously_enabled = enabled?
            action_enable

            # Only restart the service if it was previously enabled. If the service was disabled
            # or not running, then the enable action will start the service, and it's unnecessary
            # to restart the service again.
            restart_service if previously_enabled
          end
          action :nothing
          only_if { new_resource.restart_on_update && !new_resource.start_down }
        end

        ruby_block 'restart_log_service' do
          block do
            action_enable
            restart_log_service
          end
          action :nothing
          only_if { new_resource.restart_on_update && !new_resource.start_down }
        end

        # sv_templates
        if new_resource.sv_templates

          directory sv_dir_name do
            owner new_resource.owner unless new_resource.owner.nil?
            group new_resource.group unless new_resource.group.nil?
            mode '0755'
            recursive true
            action :create
          end

          template ::File.join(sv_dir_name, 'run') do
            owner new_resource.owner unless new_resource.owner.nil?
            group new_resource.group unless new_resource.group.nil?
            source "sv-#{new_resource.run_template_name}-run.erb"
            cookbook template_cookbook
            mode '0755'
            variables(options: new_resource.options)
            action :create
            notifies :run, 'ruby_block[restart_service]', :delayed
          end

          # log stuff
          if new_resource.log
            directory ::File.join(sv_dir_name, 'log') do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              recursive true
              action :create
            end

            directory ::File.join(sv_dir_name, 'log', 'main') do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              mode '0755'
              recursive true
              action :create
            end

            directory new_resource.log_dir do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              mode '0755'
              recursive true
              action :create
            end

            template ::File.join(sv_dir_name, 'log', 'config') do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              mode '0644'
              cookbook 'runit'
              source 'log-config.erb'
              variables(config: new_resource)
              notifies :run, 'ruby_block[restart_log_service]', :delayed
              action :create
            end

            link ::File.join(new_resource.log_dir, 'config') do
              to ::File.join(sv_dir_name, 'log', 'config')
            end

            if new_resource.default_logger
              template ::File.join(sv_dir_name, 'log', 'run') do
                owner new_resource.owner unless new_resource.owner.nil?
                group new_resource.group unless new_resource.group.nil?
                mode '0755'
                cookbook 'runit'
                source 'log-run.erb'
                variables(config: new_resource)
                notifies :run, 'ruby_block[restart_log_service]', :delayed
                action :create
              end
            else
              template ::File.join(sv_dir_name, 'log', 'run') do
                owner new_resource.owner unless new_resource.owner.nil?
                group new_resource.group unless new_resource.group.nil?
                mode '0755'
                source "sv-#{new_resource.log_template_name}-log-run.erb"
                cookbook template_cookbook
                variables(options: new_resource.options)
                action :create
                notifies :run, 'ruby_block[restart_log_service]', :delayed
              end
            end

          end

          # environment stuff
          directory ::File.join(sv_dir_name, 'env') do
            owner new_resource.owner unless new_resource.owner.nil?
            group new_resource.group unless new_resource.group.nil?
            mode '0755'
            action :create
          end

          new_resource.env.map do |var, value|
            file ::File.join(sv_dir_name, 'env', var) do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              content value
              sensitive true if Chef::Resource.instance_methods(false).include?(:sensitive)
              mode '0640'
              action :create
              notifies :run, 'ruby_block[restart_service]', :delayed
            end
          end

          ruby_block "Delete unmanaged env files for #{new_resource.name} service" do
            block { delete_extra_env_files }
            only_if { extra_env_files? }
            not_if { new_resource.env.empty? }
            action :run
            notifies :run, 'ruby_block[restart_service]', :delayed
          end

          template ::File.join(sv_dir_name, 'check') do
            owner new_resource.owner unless new_resource.owner.nil?<commit_msg>Remove the backwards compat check for the sensitive property<commit_after>#
# Cookbook:: runit
# Provider:: runit_service
#
# Author:: <NAME> <<EMAIL>>
# Author:: <NAME> <<EMAIL>>
# Copyright:: 2011-2019, Chef Software, Inc.
#
# 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.
#

class Chef
  class Provider
    class RunitService < Chef::Provider::LWRPBase
      provides :runit_service

      unless defined?(VALID_SIGNALS)
        # Mapping of valid signals with optional friendly name
        VALID_SIGNALS = Mash.new(
          :down => nil,
          :hup => nil,
          :int => nil,
          :term => nil,
          :kill => nil,
          :quit => nil,
          :up => nil,
          :once => nil,
          :cont => nil,
          1 => :usr1,
          2 => :usr2
        )
      end

      # Mix in helpers from libraries/helpers.rb
      include RunitCookbook::Helpers

      # actions
      action :create do
        declare_service_resource

        ruby_block 'restart_service' do
          block do
            previously_enabled = enabled?
            action_enable

            # Only restart the service if it was previously enabled. If the service was disabled
            # or not running, then the enable action will start the service, and it's unnecessary
            # to restart the service again.
            restart_service if previously_enabled
          end
          action :nothing
          only_if { new_resource.restart_on_update && !new_resource.start_down }
        end

        ruby_block 'restart_log_service' do
          block do
            action_enable
            restart_log_service
          end
          action :nothing
          only_if { new_resource.restart_on_update && !new_resource.start_down }
        end

        # sv_templates
        if new_resource.sv_templates

          directory sv_dir_name do
            owner new_resource.owner unless new_resource.owner.nil?
            group new_resource.group unless new_resource.group.nil?
            mode '0755'
            recursive true
            action :create
          end

          template ::File.join(sv_dir_name, 'run') do
            owner new_resource.owner unless new_resource.owner.nil?
            group new_resource.group unless new_resource.group.nil?
            source "sv-#{new_resource.run_template_name}-run.erb"
            cookbook template_cookbook
            mode '0755'
            variables(options: new_resource.options)
            action :create
            notifies :run, 'ruby_block[restart_service]', :delayed
          end

          # log stuff
          if new_resource.log
            directory ::File.join(sv_dir_name, 'log') do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              recursive true
              action :create
            end

            directory ::File.join(sv_dir_name, 'log', 'main') do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              mode '0755'
              recursive true
              action :create
            end

            directory new_resource.log_dir do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              mode '0755'
              recursive true
              action :create
            end

            template ::File.join(sv_dir_name, 'log', 'config') do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              mode '0644'
              cookbook 'runit'
              source 'log-config.erb'
              variables(config: new_resource)
              notifies :run, 'ruby_block[restart_log_service]', :delayed
              action :create
            end

            link ::File.join(new_resource.log_dir, 'config') do
              to ::File.join(sv_dir_name, 'log', 'config')
            end

            if new_resource.default_logger
              template ::File.join(sv_dir_name, 'log', 'run') do
                owner new_resource.owner unless new_resource.owner.nil?
                group new_resource.group unless new_resource.group.nil?
                mode '0755'
                cookbook 'runit'
                source 'log-run.erb'
                variables(config: new_resource)
                notifies :run, 'ruby_block[restart_log_service]', :delayed
                action :create
              end
            else
              template ::File.join(sv_dir_name, 'log', 'run') do
                owner new_resource.owner unless new_resource.owner.nil?
                group new_resource.group unless new_resource.group.nil?
                mode '0755'
                source "sv-#{new_resource.log_template_name}-log-run.erb"
                cookbook template_cookbook
                variables(options: new_resource.options)
                action :create
                notifies :run, 'ruby_block[restart_log_service]', :delayed
              end
            end

          end

          # environment stuff
          directory ::File.join(sv_dir_name, 'env') do
            owner new_resource.owner unless new_resource.owner.nil?
            group new_resource.group unless new_resource.group.nil?
            mode '0755'
            action :create
          end

          new_resource.env.map do |var, value|
            file ::File.join(sv_dir_name, 'env', var) do
              owner new_resource.owner unless new_resource.owner.nil?
              group new_resource.group unless new_resource.group.nil?
              content value
              sensitive true
              mode '0640'
              action :create
              notifies :run, 'ruby_block[restart_service]', :delayed
            end
          end

          ruby_block "Delete unmanaged env files for #{new_resource.name} service" do
            block { delete_extra_env_files }
            only_if { extra_env_files? }
            not_if { new_resource.env.empty? }
            action :run
            notifies :run, 'ruby_block[restart_service]', :delayed
          end

          template ::File.join(sv_dir_name, 'check') do
            owner new_resource.owner unless new_resource.owner.nil?
{'fraction_non_alphanumeric': 0.07412242529736002, 'fraction_numerical': 0.007542790832608065, 'mean_word_length': 1.8032120349664567, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3850385', 'n_tokens_mistral': 3707, 'n_tokens_neox': 3558, 'n_words': 1009}