Sha256: b84e04408f5ac64984af537917751349bff88c3e58c5d348c639e6aae223e654

Contents?: true

Size: 1.48 KB

Versions: 26

Compression:

Stored size: 1.48 KB

Contents

module Specinfra
  module Command
    module Module
      module Ss
        def check_is_listening(port, options={})
          if options[:local_address]
            pattern = inaddr_any_to_asterisk(options[:local_address]).map { |l| " #{l}:#{port} " }
            pattern = pattern.join('|')
          else
            pattern = ":#{port} "
          end
          "ss #{command_options(options[:protocol])} | grep -E -- #{escape(pattern)}"
        end

        private

        # WORKAROUND:
        #   Older ss versions display "*" instead of "0.0.0.0".
        #   But serverspec validates IP address by `valid_ip_address?` method:
        #     https://github.com/serverspec/serverspec/blob/master/lib/serverspec/type/port.rb
        def inaddr_any_to_asterisk(local_address)
          if local_address == '0.0.0.0'
            [ '\*' , '0\.0\.0\.0' ]
          else
            [ fix_format_if_ipv6(local_address) ]
          end
        end

        def fix_format_if_ipv6(local_address)
          if local_address =~ /.*:.*/
            "\\[#{local_address}\\]"
          else
            local_address
          end
        end

        def command_options(protocol)
          case protocol.to_s
          when 'tcp'  then "-tnl4"
          when 'tcp6' then "-tnl6"
          when 'udp'  then "-unl4"
          when 'udp6' then "-unl6"
          when ''     then "-tunl"
          else
            raise ArgumentError, "Unknown protocol [#{protocol}]"
          end
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
specinfra-2.91.0 lib/specinfra/command/module/ss.rb
specinfra-2.90.1 lib/specinfra/command/module/ss.rb
specinfra-2.90.0 lib/specinfra/command/module/ss.rb
specinfra-2.89.0 lib/specinfra/command/module/ss.rb
specinfra-2.88.2 lib/specinfra/command/module/ss.rb
specinfra-2.88.1 lib/specinfra/command/module/ss.rb
specinfra-2.88.0 lib/specinfra/command/module/ss.rb
specinfra-2.87.2 lib/specinfra/command/module/ss.rb
specinfra-2.87.1 lib/specinfra/command/module/ss.rb
specinfra-2.87.0 lib/specinfra/command/module/ss.rb
specinfra-2.86.0 lib/specinfra/command/module/ss.rb
specinfra-2.85.1 lib/specinfra/command/module/ss.rb
specinfra-2.85.0 lib/specinfra/command/module/ss.rb
specinfra-2.84.1 lib/specinfra/command/module/ss.rb
specinfra-2.84.0 lib/specinfra/command/module/ss.rb
specinfra-2.83.4 lib/specinfra/command/module/ss.rb
specinfra-2.83.3 lib/specinfra/command/module/ss.rb
specinfra-2.83.2 lib/specinfra/command/module/ss.rb
specinfra-2.83.1 lib/specinfra/command/module/ss.rb
specinfra-2.83.0 lib/specinfra/command/module/ss.rb