<commit_before>#
# Author:: <NAME> <<EMAIL>>
# Copyright:: Copyright (c) 2010 <NAME>
# 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 'chef/provider/service'
require 'chef/provider/service/simple'
require 'chef/mixin/command'
require 'chef/util/file_edit'

class Chef
  class Provider
    class Service
      class Upstart < Chef::Provider::Service::Simple
        UPSTART_STATE_FORMAT = /\w+ \(?(\w+)\)?[\/ ](\w+)/
       
        # Upstart does more than start or stop a service, creating multiple 'states' [1] that a service can be in.
        # In chef, when we ask a service to start, we expect it to have started before performing the next step
        # since we have top down dependencies. Which is to say we may follow witha resource next that requires
        # that service to be running. According to [2] we can trust that sending a 'goal' such as start will not
        # return until that 'goal' is reached, or some error has occured.
        #
        # [1] http://upstart.ubuntu.com/wiki/JobStates
        # [2] http://www.netsplit.com/2008/04/27/upstart-05-events/

        def initialize(new_resource, run_context)
          # TODO: re-evaluate if this is needed after integrating cookbook fix
          raise ArgumentError, "run_context cannot be nil" unless run_context
          super
          
          run_context.node
          
          @job = @new_resource.service_name
          
          if @new_resource.parameters
            @new_resource.parameters.each do |key, value|
              @job << " #{key}=#{value}"
            end
          end
          
          platform, version = Chef::Platform.find_platform_and_version(run_context.node)
          if platform == "ubuntu" && (8.04..9.04).include?(version.to_f)
            @upstart_job_dir = "/etc/event.d"
            @upstart_conf_suffix = ""
          else
            @upstart_job_dir = "/etc/init"
            @upstart_conf_suffix = ".conf"
          end

          @command_success = true # new_resource.status_command= false, means upstart used 
          @config_file_found = true 
          @upstart_command_success = true
        end

        def define_resource_requirements 
          super

          requirements.assert(:all_actions) do |a| 
            if !@command_success
              whyrun_msg = @new_resource.status_command ? "Provided status command #{@new_resource.status_command} failed." : 
                "Could not determine upstart state for service"
            end
            a.assertion { @command_success }
            # no failure here, just document the assumptions made. 
            a.whyrun "#{whyrun_msg} Assuming service installed and not running." 
          end

          requirements.assert(:all_actions) do |a| 
            a.assertion  { @config_file_found } 
            # no failure here, just document the assumptions made. 
            a.whyrun "Could not find #{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}. Assuming service is disabled." 
          end
        end

        def load_current_resource
          @current_resource = Chef::Resource::Service.new(@new_resource.name)
          @current_resource.service_name(@new_resource.service_name)

          # Get running/stopped state
          # We do not support searching for a service via ps when using upstart since status is a native
          # upstart function. We will however support status_command in case someone wants to do something special.
          if @new_resource.status_command
            Chef::Log.debug("#{@new_resource} you have specified a status command, running..")

            begin<commit_msg>[CHEF-3410] Remove call to super to avoid erroneous error messages.<commit_after>#
# Author:: <NAME> <<EMAIL>>
# Copyright:: Copyright (c) 2010 <NAME>
# 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 'chef/provider/service'
require 'chef/provider/service/simple'
require 'chef/mixin/command'
require 'chef/util/file_edit'

class Chef
  class Provider
    class Service
      class Upstart < Chef::Provider::Service::Simple
        UPSTART_STATE_FORMAT = /\w+ \(?(\w+)\)?[\/ ](\w+)/
       
        # Upstart does more than start or stop a service, creating multiple 'states' [1] that a service can be in.
        # In chef, when we ask a service to start, we expect it to have started before performing the next step
        # since we have top down dependencies. Which is to say we may follow witha resource next that requires
        # that service to be running. According to [2] we can trust that sending a 'goal' such as start will not
        # return until that 'goal' is reached, or some error has occured.
        #
        # [1] http://upstart.ubuntu.com/wiki/JobStates
        # [2] http://www.netsplit.com/2008/04/27/upstart-05-events/

        def initialize(new_resource, run_context)
          # TODO: re-evaluate if this is needed after integrating cookbook fix
          raise ArgumentError, "run_context cannot be nil" unless run_context
          super
          
          run_context.node
          
          @job = @new_resource.service_name
          
          if @new_resource.parameters
            @new_resource.parameters.each do |key, value|
              @job << " #{key}=#{value}"
            end
          end
          
          platform, version = Chef::Platform.find_platform_and_version(run_context.node)
          if platform == "ubuntu" && (8.04..9.04).include?(version.to_f)
            @upstart_job_dir = "/etc/event.d"
            @upstart_conf_suffix = ""
          else
            @upstart_job_dir = "/etc/init"
            @upstart_conf_suffix = ".conf"
          end

          @command_success = true # new_resource.status_command= false, means upstart used 
          @config_file_found = true 
          @upstart_command_success = true
        end

        def define_resource_requirements
          # Do not call super, only call shared requirements
          shared_resource_requirements
          requirements.assert(:all_actions) do |a|
            if !@command_success
              whyrun_msg = @new_resource.status_command ? "Provided status command #{@new_resource.status_command} failed." : 
                "Could not determine upstart state for service"
            end
            a.assertion { @command_success }
            # no failure here, just document the assumptions made. 
            a.whyrun "#{whyrun_msg} Assuming service installed and not running." 
          end

          requirements.assert(:all_actions) do |a| 
            a.assertion  { @config_file_found } 
            # no failure here, just document the assumptions made. 
            a.whyrun "Could not find #{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}. Assuming service is disabled." 
          end
        end

        def load_current_resource
          @current_resource = Chef::Resource::Service.new(@new_resource.name)
          @current_resource.service_name(@new_resource.service_name)

          # Get running/stopped state
          # We do not support searching for a service via ps when using upstart since status is a native
          # upstart function. We will however support status_command in case someone wants to do something special.
          if @new_resource.status_command
            Chef::Log.debug("#{@new_resource} you have specified a status command, running..")

            begin
{'fraction_non_alphanumeric': 0.08455666470933647, 'fraction_numerical': 0.007516147974163241, 'mean_word_length': 2.428341384863124, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3857473', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2229, 'n_words': 790}