<commit_before>#! /usr/bin/env ruby
module Naminori
  class Lb
    class Lvs < Naminori::Lb::Base
      class << self
        def add_member(rip, service)
          transaction("add", lvs_option(rip, service)) if service.healty?(rip)
        end

        def delete_member(rip, service)
          transaction("delete", lvs_option(rip, service))
        end

        def transaction(type, ops)
          ops[:protocols].collect do |protocol|
            merge_option =  ops.merge({ protocol: protocol })
            if self.send("#{type}?", merge_option) && system("ipvsadm #{command_option(type, merge_option)}")
              notifier(type, merge_option)
              true
            end
          end.all? {|res| res }
        end

        def add?(ops)
          vip_exists?(ops) && !delete?(ops)
        end

        def delete?(ops)
          fetch_service(ops).each do |line|
            return true if line.match(/#{ops[:rip]}:/)
          end
          false
        end

        def vip_exists?(ops)
          vips = fetch_service(ops)
          vips.each do |line|
            return true if line.match(/#{ops[:vip]}:/)
          end if vips
          false
        end

        def fetch_service(ops)
          `ipvsadm -Ln --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]}`.split("\n")
        end

        def lvs_option(rip, service)
          { service: service, vip: service.config.vip, rip: rip, protocols: service.config.protocols, port: service.config.port, method: service.config.method }
        end

        def command_option(type, ops)
          case
          when type == "add"
            "--#{type}-server --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]} -r #{ops[:rip]}:#{ops[:port]} #{method_option(ops[:method])}"
          when type == "delete"
            "--#{type}-server --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]} -r #{ops[:rip]}:#{ops[:port]}"
          end
        end

        def method_option(method)
          case
          when method == "gateway"
            "-g"<commit_msg>fail save<commit_after>#! /usr/bin/env ruby
module Naminori
  class Lb
    class Lvs < Naminori::Lb::Base
      class << self
        def add_member(rip, service)
          transaction("add", lvs_option(rip, service)) if service.healty?(rip)
        end

        def delete_member(rip, service)
          transaction("delete", lvs_option(rip, service))
        end

        def transaction(type, ops)
          ops[:protocols].collect do |protocol|
            merge_option =  ops.merge({ protocol: protocol })
            if self.send("#{type}?", merge_option) && system("ipvsadm #{command_option(type, merge_option)}")
              notifier(type, merge_option)
              true
            end
          end.all? {|res| res }
        end

        def add?(ops)
          vip_exists?(ops) && !delete?(ops)
        end

        def delete?(ops)
          fetch_service(ops).each do |line|
            return true if line.match(/#{ops[:rip]}:/)
          end
          false
        end

        def vip_exists?(ops)
          fetch_service(ops).each do |line|
            return true if line.match(/#{ops[:vip]}:/)
          end
          false
        end

        def fetch_service(ops)
          service = `ipvsadm -Ln --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]}`.split("\n")
          raise "fetch errror!" unless service
          service
        end

        def lvs_option(rip, service)
          { service: service, vip: service.config.vip, rip: rip, protocols: service.config.protocols, port: service.config.port, method: service.config.method }
        end

        def command_option(type, ops)
          case
          when type == "add"
            "--#{type}-server --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]} -r #{ops[:rip]}:#{ops[:port]} #{method_option(ops[:method])}"
          when type == "delete"
            "--#{type}-server --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]} -r #{ops[:rip]}:#{ops[:port]}"
          end
        end

        def method_option(method)
          case
          when method == "gateway"
            "-g"
{'fraction_non_alphanumeric': 0.15274751876059064, 'fraction_numerical': 0.0, 'mean_word_length': 1.9493219129193433, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '4786240', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1249, 'n_words': 279}