Class: Warg::Host
- Inherits:
-
Object
- Object
- Warg::Host
- Defined in:
- lib/warg.rb
Defined Under Namespace
Modules: Parser Classes: CommandOutcome
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #create_directory(directory) ⇒ Object
- #create_file_from(content, path:, mode: 0644) ⇒ Object
- #download(path, to: nil) ⇒ Object
- #hash ⇒ Object
-
#initialize(user: nil, address:, port: nil, properties: {}) ⇒ Host
constructor
A new instance of Host.
- #inspect ⇒ Object
- #matches?(filters) ⇒ Boolean
- #run_command(command, &setup) ⇒ Object
- #run_script(script, &setup) ⇒ Object
- #to_s ⇒ Object
- #upload(file, to:) ⇒ Object
Constructor Details
#initialize(user: nil, address:, port: nil, properties: {}) ⇒ Host
Returns a new instance of Host.
763 764 765 766 767 768 769 770 |
# File 'lib/warg.rb', line 763 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
#address ⇒ Object (readonly)
Returns the value of attribute address.
757 758 759 |
# File 'lib/warg.rb', line 757 def address @address end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
758 759 760 |
# File 'lib/warg.rb', line 758 def id @id end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
759 760 761 |
# File 'lib/warg.rb', line 759 def port @port end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
760 761 762 |
# File 'lib/warg.rb', line 760 def uri @uri end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
761 762 763 |
# File 'lib/warg.rb', line 761 def user @user end |
Class Method Details
.from(host_data) ⇒ Object
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 751 752 753 754 755 |
# File 'lib/warg.rb', line 719 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?
794 795 796 |
# File 'lib/warg.rb', line 794 def ==(other) self.class == other.class && uri == other.uri end |
#[](name) ⇒ Object
782 783 784 |
# File 'lib/warg.rb', line 782 def [](name) @properties[name.to_s] end |
#[]=(name, value) ⇒ Object
786 787 788 789 790 791 792 |
# File 'lib/warg.rb', line 786 def []=(name, value) @properties[name.to_s] = value build_uri! value end |
#create_directory(directory) ⇒ Object
855 856 857 858 859 860 861 862 863 864 |
# File 'lib/warg.rb', line 855 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
866 867 868 869 870 871 872 873 874 875 876 877 |
# File 'lib/warg.rb', line 866 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
883 884 885 886 887 888 889 890 891 892 893 894 895 896 |
# File 'lib/warg.rb', line 883 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 |
#hash ⇒ Object
800 801 802 |
# File 'lib/warg.rb', line 800 def hash inspect.hash end |
#inspect ⇒ Object
898 899 900 |
# File 'lib/warg.rb', line 898 def inspect %{#<#{self.class.name} uri=#{@uri.to_s}>} end |
#matches?(filters) ⇒ Boolean
772 773 774 775 776 777 778 779 780 |
# File 'lib/warg.rb', line 772 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
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 841 842 843 844 845 |
# File 'lib/warg.rb', line 804 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.}") outcome end |
#run_script(script, &setup) ⇒ Object
847 848 849 850 851 852 853 |
# File 'lib/warg.rb', line 847 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_s ⇒ Object
902 903 904 |
# File 'lib/warg.rb', line 902 def to_s @uri.to_s end |
#upload(file, to:) ⇒ Object
879 880 881 |
# File 'lib/warg.rb', line 879 def upload(file, to:) connection.scp.upload!(file, to) end |