Class: BoPeep::Host

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

Defined Under Namespace

Modules: Parser Classes: Callbacks, 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.



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

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.



752
753
754
# File 'lib/bopeep.rb', line 752

def address
  @address
end

#idObject (readonly)

Returns the value of attribute id.



753
754
755
# File 'lib/bopeep.rb', line 753

def id
  @id
end

#portObject (readonly)

Returns the value of attribute port.



754
755
756
# File 'lib/bopeep.rb', line 754

def port
  @port
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

#userObject (readonly)

Returns the value of attribute user.



756
757
758
# File 'lib/bopeep.rb', line 756

def user
  @user
end

Class Method Details

.from(host_data) ⇒ Object



714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/bopeep.rb', line 714

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?



789
790
791
# File 'lib/bopeep.rb', line 789

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

#[](name) ⇒ Object



777
778
779
# File 'lib/bopeep.rb', line 777

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

#[]=(name, value) ⇒ Object



781
782
783
784
785
786
787
# File 'lib/bopeep.rb', line 781

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

  build_uri!

  value
end

#create_directory(directory) ⇒ Object



850
851
852
853
854
855
856
857
858
859
# File 'lib/bopeep.rb', line 850

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



861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/bopeep.rb', line 861

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



878
879
880
881
882
883
884
885
886
887
888
889
890
891
# File 'lib/bopeep.rb', line 878

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



795
796
797
# File 'lib/bopeep.rb', line 795

def hash
  inspect.hash
end

#inspectObject



893
894
895
# File 'lib/bopeep.rb', line 893

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

#matches?(**filters) ⇒ Boolean

Returns:

  • (Boolean)


767
768
769
770
771
772
773
774
775
# File 'lib/bopeep.rb', line 767

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, callbacks: nil, log: false, &setup) ⇒ Object



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/bopeep.rb', line 799

def run_command(command, callbacks: nil, log: false, &setup)
  outcome = CommandOutcome.new(self, command, callbacks: callbacks, log: log, &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, callbacks: nil, log: false, &setup) ⇒ Object



842
843
844
845
846
847
848
# File 'lib/bopeep.rb', line 842

def run_script(script, callbacks: nil, log: false, &setup)
  create_directory script.install_directory

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

  run_command(script.remote_path, callbacks: callbacks, log: log, &setup)
end

#to_sObject



897
898
899
# File 'lib/bopeep.rb', line 897

def to_s
  @uri.to_s
end

#upload(file, to:) ⇒ Object



874
875
876
# File 'lib/bopeep.rb', line 874

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