Class: Warg::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/warg.rb

Defined Under Namespace

Modules: Parser Classes: CommandOutcome

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user: nil, address:, port: nil, properties: {}) ⇒ Host

Returns a new instance of Host.



736
737
738
739
740
741
742
743
# File 'lib/warg.rb', line 736

def initialize(user: nil, address:, port: nil, properties: {})
  @user = user
  @address = address
  @port = port
  @properties = properties.transform_keys(&:to_s)

  build_uri!
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



730
731
732
# File 'lib/warg.rb', line 730

def address
  @address
end

#idObject (readonly)

Returns the value of attribute id.



731
732
733
# File 'lib/warg.rb', line 731

def id
  @id
end

#portObject (readonly)

Returns the value of attribute port.



732
733
734
# File 'lib/warg.rb', line 732

def port
  @port
end

#uriObject (readonly)

Returns the value of attribute uri.



733
734
735
# File 'lib/warg.rb', line 733

def uri
  @uri
end

#userObject (readonly)

Returns the value of attribute user.



734
735
736
# File 'lib/warg.rb', line 734

def user
  @user
end

Class Method Details

.from(host_data) ⇒ Object



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/warg.rb', line 692

def self.from(host_data)
  case host_data
  when Host
    host_data
  when Hash
    attributes = host_data.transform_keys(&:to_sym)

    new(**attributes)
  when Array
    if host_data.length == 1 && Hash === host_data[0]
      from(host_data[0])
    elsif String === host_data[0]
      last_item_index = -1
      attributes = Parser.(host_data[0])

      if Hash === host_data[-1]
        last_item_index = -2

        more_properties = host_data[-1].transform_keys(&:to_sym)
        attributes[:properties].merge!(more_properties)
      end

      host_data[1..last_item_index].each do |property|
        name, value = property.to_s.split("=", 2)
        attributes[:properties][name.to_sym] = value
      end

      new(**attributes)
    else
      raise InvalidHostDataError.new(host_data)
    end
  when String
    new(**Parser.(host_data))
  else
    raise InvalidHostDataError.new(host_data)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



767
768
769
# File 'lib/warg.rb', line 767

def ==(other)
  self.class == other.class && uri == other.uri
end

#[](name) ⇒ Object



755
756
757
# File 'lib/warg.rb', line 755

def [](name)
  @properties[name.to_s]
end

#[]=(name, value) ⇒ Object



759
760
761
762
763
764
765
# File 'lib/warg.rb', line 759

def []=(name, value)
  @properties[name.to_s] = value

  build_uri!

  value
end

#create_directory(directory) ⇒ Object



828
829
830
831
832
833
834
835
836
837
# File 'lib/warg.rb', line 828

def create_directory(directory)
  command = "mkdir -p #{directory}"

  connection.open_channel do |channel|
    channel.exec(command)
    channel.wait
  end

  connection.loop
end

#create_file_from(content, path:, mode: 0644) ⇒ Object



839
840
841
842
843
844
845
846
847
848
849
850
# File 'lib/warg.rb', line 839

def create_file_from(content, path:, mode: 0644)
  filename = "#{id}-#{File.basename(path)}"

  tempfile = Tempfile.new(filename)
  tempfile.chmod(mode)
  tempfile.write(content)
  tempfile.rewind

  upload tempfile, to: path

  tempfile.unlink
end

#download(path, to: nil) ⇒ Object



856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'lib/warg.rb', line 856

def download(path, to: nil)
  content = connection.scp.download!(path)

  if to
    file = File.new(to, "w+b")
  else
    file = Tempfile.new(path)
  end

  file.write(content)
  file.rewind

  file
end

#hashObject



773
774
775
# File 'lib/warg.rb', line 773

def hash
  inspect.hash
end

#inspectObject



871
872
873
# File 'lib/warg.rb', line 871

def inspect
  %{#<#{self.class.name} uri=#{@uri.to_s}>}
end

#matches?(**filters) ⇒ Boolean

Returns:

  • (Boolean)


745
746
747
748
749
750
751
752
753
# File 'lib/warg.rb', line 745

def matches?(**filters)
  filters.all? do |name, value|
    if respond_to?(name)
      send(name) == value
    else
      @properties[name.to_s] == value
    end
  end
end

#run_command(command, &setup) ⇒ Object



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/warg.rb', line 777

def run_command(command, &setup)
  outcome = CommandOutcome.new(self, command, &setup)

  connection.open_channel do |channel|
    channel.exec(command) do |_, success|
      outcome.command_started!

      channel.on_data do |_, data|
        outcome.collect_stdout(data)
      end

      channel.on_extended_data do |_, __, data|
        outcome.collect_stderr(data)
      end

      channel.on_request("exit-status") do |_, data|
        outcome.exit_status = data.read_long
      end

      channel.on_request("exit-signal") do |_, data|
        outcome.exit_signal = data.read_string
      end

      channel.on_open_failed do |_, code, reason|
        outcome.connection_failed(code, reason)
      end

      channel.on_close do |_|
        outcome.command_finished!
      end
    end

    channel.wait
  end

  connection.loop

  outcome
rescue SocketError, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => error
  outcome.connection_failed(-1, "#{error.class}: #{error.message}")
  outcome
end

#run_script(script, &setup) ⇒ Object



820
821
822
823
824
825
826
# File 'lib/warg.rb', line 820

def run_script(script, &setup)
  create_directory script.install_directory

  create_file_from script.content, path: script.install_path, mode: 0755

  run_command(script.remote_path, &setup)
end

#to_sObject



875
876
877
# File 'lib/warg.rb', line 875

def to_s
  @uri.to_s
end

#upload(file, to:) ⇒ Object



852
853
854
# File 'lib/warg.rb', line 852

def upload(file, to:)
  connection.scp.upload!(file, to)
end