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.
764 765 766 767 768 769 770 771 |
# File 'lib/warg.rb', line 764 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.
758 759 760 |
# File 'lib/warg.rb', line 758 def address @address end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
759 760 761 |
# File 'lib/warg.rb', line 759 def id @id end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
760 761 762 |
# File 'lib/warg.rb', line 760 def port @port end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
761 762 763 |
# File 'lib/warg.rb', line 761 def uri @uri end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
762 763 764 |
# File 'lib/warg.rb', line 762 def user @user end |
Class Method Details
.from(host_data) ⇒ Object
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 756 |
# File 'lib/warg.rb', line 720 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?
795 796 797 |
# File 'lib/warg.rb', line 795 def ==(other) self.class == other.class && uri == other.uri end |
#[](name) ⇒ Object
783 784 785 |
# File 'lib/warg.rb', line 783 def [](name) @properties[name.to_s] end |
#[]=(name, value) ⇒ Object
787 788 789 790 791 792 793 |
# File 'lib/warg.rb', line 787 def []=(name, value) @properties[name.to_s] = value build_uri! value end |
#create_directory(directory) ⇒ Object
856 857 858 859 860 861 862 863 864 865 |
# File 'lib/warg.rb', line 856 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
867 868 869 870 871 872 873 874 875 876 877 878 |
# File 'lib/warg.rb', line 867 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
884 885 886 887 888 889 890 891 892 893 894 895 896 897 |
# File 'lib/warg.rb', line 884 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
801 802 803 |
# File 'lib/warg.rb', line 801 def hash inspect.hash end |
#inspect ⇒ Object
899 900 901 |
# File 'lib/warg.rb', line 899 def inspect %{#<#{self.class.name} uri=#{@uri.to_s}>} end |
#matches?(filters) ⇒ Boolean
773 774 775 776 777 778 779 780 781 |
# File 'lib/warg.rb', line 773 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
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 846 |
# File 'lib/warg.rb', line 805 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
848 849 850 851 852 853 854 |
# File 'lib/warg.rb', line 848 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
903 904 905 |
# File 'lib/warg.rb', line 903 def to_s @uri.to_s end |
#upload(file, to:) ⇒ Object
880 881 882 |
# File 'lib/warg.rb', line 880 def upload(file, to:) connection.scp.upload!(file, to) end |