Class: Host::Default

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

Direct Known Subclasses

EC2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'unspecified', info = {}, opts = {}) ⇒ Default

Returns a new instance of Default.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/host/default.rb', line 6

def initialize( host = 'unspecified', info = {}, opts = {} )
  @alias = host

  @host_name = info[ "HostName" ] || nil
  @user = info[ "User" ] || Etc.getlogin
  @ssh_pem = info[ "IdentityFile" ] || nil
  @ssh_port = info[ "Port" ] || nil
  @type = info[ "Type" ] || :default
  @tags = info[ "Tags" ] || {}
  @pem_dirs = opts[ "IdentityLocations" ] || nil
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias.



4
5
6
# File 'lib/host/default.rb', line 4

def alias
  @alias
end

#host_nameObject (readonly)

Returns the value of attribute host_name.



4
5
6
# File 'lib/host/default.rb', line 4

def host_name
  @host_name
end

#ssh_pemObject (readonly)

Returns the value of attribute ssh_pem.



4
5
6
# File 'lib/host/default.rb', line 4

def ssh_pem
  @ssh_pem
end

#tagsObject

Returns the value of attribute tags.



4
5
6
# File 'lib/host/default.rb', line 4

def tags
  @tags
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/host/default.rb', line 4

def user
  @user
end

Instance Method Details

#matches?(tags) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/host/default.rb', line 27

def matches?( tags )
  tags.each do | tag, value |
    is_regex = value.match( /^\/(.*)\/$/ )

    if is_regex
      regex = Regexp.new( is_regex[ 1 ] )

      unless @tags.has_key?( tag ) && @tags[ tag ].match( regex )
        return false
      end
    else
      unless @tags.has_key?( tag ) && @tags[ tag ] == value
        return false
      end
    end
  end

  true
end

#shell!(opts = nil) ⇒ Object

Raises:

  • (IOError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/host/default.rb', line 67

def shell!( opts = nil )
  ssh_cmd = [ "ssh" ]

  raise( IOError, "Error: HostName invalid." ) if @host_name.nil?

  ssh_cmd << [ "-l", @user ]
  ssh_cmd << @host_name

  pem = ssh_pem
  ssh_cmd << [ "-i", pem ] unless pem.nil?

  begin
    exec( ssh_cmd.join(" ") )
  rescue => e
    raise( IOError,  "Could not call ssh." )
  end
end

#shell_exec!(command, opts = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/host/default.rb', line 85

def shell_exec!( command, opts = {} )
  ssh_pem

  options = { :keys => ssh_pem }

  color = Color.random_color

  begin
    Net::SSH.start( host_name, user, options ) do | s |

      channel = s.open_channel do |ch|
        channel.request_pty do |ch, success|
          if success
            puts "pty successfully obtained"
          else
            puts "could not obtain pty"
          end
        end if opts.has_key? :pty

        ch.exec( command ) do | ch, success |
          raise( IOError,
            "#{ host_name } > could not execute command" ) unless
              success

          ch.on_data do | c, data |
            data.split("\n").each do | line |
              puts "#{ Color.print(
                self.alias, [ :bold, color ] ) } > #{ line }"
            end
          end

          ch.on_extended_data do |c, type, data|
            data.split("\n").each do | line |
              puts "#{ Color.print(
                self.alias, [ :bold, color ] ) } > #{ line }"
            end
          end

          ch.on_close do
            puts "#{ Color.print(
              self.alias, [ :bold, color ] ) } > COMMAND finished"
          end

        end
      end
    end
  rescue => e
    puts "Error: #{ self.alias } > #{ e.message }".error
  end
end

#typeObject



18
19
20
# File 'lib/host/default.rb', line 18

def type
  @type.to_sym
end