Class: CCSH::Hosts

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHosts

Returns a new instance of Hosts.



11
12
13
# File 'lib/ccsh/hosts.rb', line 11

def initialize
    # TODO
end

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



8
9
10
# File 'lib/ccsh/hosts.rb', line 8

def defaults
  @defaults
end

#hostsObject

Returns the value of attribute hosts.



9
10
11
# File 'lib/ccsh/hosts.rb', line 9

def hosts
  @hosts
end

Instance Method Details

#filter_by(targets) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ccsh/hosts.rb', line 58

def filter_by(targets)
    return @hosts if targets.include?('all')

    hosts = []
    @hosts.each do |host|
        targets.each do |target|
            hosts << host if host.groups != nil && host.groups.include?(target)
        end
    end

    return hosts
end

#parser!(filename) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ccsh/hosts.rb', line 15

def parser!(filename)
    begin
        file = Pathname.new(filename)

        raise "The file does not exists: '#{file.realpath}'" if not file.exist?
        raise "The host file is not regular file: '#{file.realpath}'" if not file.file?

        hostfile = YAML.load_file(file.realpath)

        @hosts = []
        @defaults = CCSH::Utils.merge_defaults(hostfile['defaults'])

        hostfile['hosts'].each do |h|
            host = Host.new

            host.name = CCSH::Utils.get_options('name', h, @defaults['name'])
            host.user = CCSH::Utils.get_options('user', h, @defaults['user'])
            host.port = CCSH::Utils.get_options('port', h, @defaults['port'])

            host.hostname = CCSH::Utils.get_options('hostname', h, @defaults['hostname'])
            host.password = CCSH::Utils.get_options('password', h, @defaults['password'])
            host.private_key = CCSH::Utils.get_options('private_key', h, @defaults['private_key'])
            host.ssh_options = CCSH::Utils.get_options('ssh_options', h, @defaults['ssh_options'])
            host.timeout = CCSH::Utils.get_options('timeout', h, @defaults['timeout'])
            host.sudo_enabled = CCSH::Utils.get_options('sudo_enabled', h, @defaults['sudo_enabled'])
            host.sudo_password = CCSH::Utils.get_options('sudo_password', h, @defaults['sudo_password'])

            # define the password if the sudo_password is not set
            host.sudo_password = host.password if host.sudo_password == nil

            host.groups = h['groups']

            @hosts << host
        end
    rescue Exception => e
        puts e.message
        puts e.backtrace
        puts
    end

    return self
end