<commit_before>require 'setup_job'
require 'backup_job'

class RunJob
  
  # Testing if our IP address matches what we can resolve the hostname to.
  # If hostname and IP address are set in the object, resolve the hostname and
  # see if we can find an IP address that matches. If IP address is not matched
  # we'll fill it in with the first IP address we resolve.
  def self.check_hostname(hostname, ip_address=nil)
    begin
      addrinfo = Socket.getaddrinfo(hostname, 22)
    rescue SocketError
      return false
    end
    addrinfo.each do |addr|
      if ip_address && ip_address == addr[3]
        return addr[3]
      elsif ip_address
        next
      else
        return addr[3]
      end
    end
    return false
  end
  
  def self.run(jobs)
    jobs.each do |job|
      if job.aasm_events_for_current_state.include? :finish
        
        unless job.aasm_current_state == :running
          job.run
          job.save!
        end
        
        if job.data['hostname'][:value] && job.data['ip_address'][:value]
          unless self.check_hostname(job.data['hostname'][:value], job.data['ip_address'][:value])
            DaemonKit.logger.warn "Hostname #{job.data['hostname'][:value]} does not match IP address given. Tested by resolving hostname. SKIPPING!"
            next
          end
        end
        
        if job.operation == 'setup'
          setupJob = SetupJob.new(:ip_address => job.data['ip_address'][:value],
            :hostname => job.data['hostname'][:value],
            :size => job.data['quota'][:value]
          )
          
          rstatus = setupJob.create_zfs_fs!
          path = setupJob.path
          if rstatus[0] == 0 && path[0] == 0
            job.finish
            job.finished_at = Time.now
            job.data['backup_dir'] = { :value => path[1] }
            job.save!
            DaemonKit.logger.info "Successfully setup host #{job.data['ip_address'][:value]}"
          else
            job.error
            job.data['error'] = {'exit_code' => rstatus[0], 'message' => rstatus[1]}
            job.save!
            DaemonKit.logger.warn "Error while trying to setup host, #{rstatus[1]}"
          end
        
        elsif job.operation == 'maintenance'
          nil
          
        elsif job.operation == 'backup'
          
          # Make sure we have a backup dir for this job's schedule before we go on.
          backup_dirs = YAML::load(job.data['backup_dir'][:value])
          unless backup_dirs && backup_dirs[job.schedule_id]
            DaemonKit.logger.warn "Could not find a backup_dir for host #{job.data['hostname'][:value]}, schedule id #{job.schedule_id}, SKIPPING!"
            next
          end
          
          backupJob = BackupJob.new(:ip_address => job.data['ip_address'][:value],
            :hostname => job.data['hostname'][:value],
            :host_type => job.data['host_type'][:value],
            :local_backup_dir => backup_dirs[job.schedule_id],
            :exclusions => job.data['exclusions'][:value],
            :directories => job.data['backup_directories'][:value]
          )
          rstatus = backupJob.run(job.data)
          
          
        elsif job.operation == 'restore'
          nil
        end
      end # End if
    end # End each
    
  end # End run method
  
end # End Class<commit_msg>Testing.<commit_after>require 'setup_job'
require 'backup_job'

class RunJob
  
  # Testing if our IP address matches what we can resolve the hostname to.
  # If hostname and IP address are set in the object, resolve the hostname and
  # see if we can find an IP address that matches. If IP address is not matched
  # we'll fill it in with the first IP address we resolve.
  def self.check_hostname(hostname, ip_address=nil)
    begin
      addrinfo = Socket.getaddrinfo(hostname, 22)
    rescue SocketError
      return false
    end
    addrinfo.each do |addr|
      if ip_address && ip_address == addr[3]
        return addr[3]
      elsif ip_address
        next
      else
        return addr[3]
      end
    end
    return false
  end
  
  def self.run(jobs)
    jobs.each do |job|
      if job.aasm_events_for_current_state.include? :finish
        
        unless job.aasm_current_state == :running
          job.run
          job.save!
        end
        
        if job.data['hostname'][:value] && job.data['ip_address'][:value]
          unless self.check_hostname(job.data['hostname'][:value], job.data['ip_address'][:value])
            DaemonKit.logger.warn "Hostname #{job.data['hostname'][:value]} does not match IP address given. Tested by resolving hostname. SKIPPING!"
            next
          end
        end
        
        if job.operation == 'setup'
          setupJob = SetupJob.new(:ip_address => job.data['ip_address'][:value],
            :hostname => job.data['hostname'][:value],
            :size => job.data['quota'][:value]
          )
          
          rstatus = setupJob.create_zfs_fs!
          path = setupJob.path
          if rstatus[0] == 0 && path[0] == 0
            job.finish
            job.finished_at = Time.now
            job.data['backup_dir'] = { :value => path[1] }
            job.save!
            DaemonKit.logger.info "Successfully setup host #{job.data['ip_address'][:value]}"
          else
            job.error
            job.data['error'] = {'exit_code' => rstatus[0], 'message' => rstatus[1]}
            job.save!
            DaemonKit.logger.warn "Error while trying to setup host, #{rstatus[1]}"
          end
        
        elsif job.operation == 'maintenance'
          nil
          
        elsif job.operation == 'backup'
          
          # Make sure we have a backup dir for this job's schedule before we go on.
          backup_dirs = YAML::load(job.data['backup_dir'][:value])
          unless backup_dirs && backup_dirs[job.schedule_id]
            DaemonKit.logger.warn "Could not find a backup_dir for host #{job.data['hostname'][:value]}, schedule id #{job.schedule_id}, SKIPPING!"
            next
          end
          
          backupJob = BackupJob.new(:ip_address => job.data['ip_address'][:value],
            :hostname => job.data['hostname'][:value],
            :host_type => job.data['host_type'][:value],
            :local_backup_dir => backup_dirs[job.schedule_id],
            :exclusions => job.data['exclusions'][:value],
            :directories => job.data['backup_directories'][:value]
          )
          return backupJob
          rstatus = backupJob.run(job.data)
          
          
        elsif job.operation == 'restore'
          nil
        end
      end # End if
    end # End each
    
  end # End run method
  
end # End Class
{'fraction_non_alphanumeric': 0.11258971291866028, 'fraction_numerical': 0.00388755980861244, 'mean_word_length': 1.9968637992831542, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3074119', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1957, 'n_words': 505}