Class: RemoteInfo

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

Constant Summary collapse

FILENAME =
".reminfo"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRemoteInfo

Returns a new instance of RemoteInfo.



20
21
22
23
24
25
26
27
28
# File 'lib/commons.rb', line 20

def initialize
    @netns = nil
    @owner = nil
    
    @port = nil
    @user = nil
    
    @operations = "rw"
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/commons.rb', line 12

def host
  @host
end

#netnsObject

Returns the value of attribute netns.



16
17
18
# File 'lib/commons.rb', line 16

def netns
  @netns
end

#operationsObject

Returns the value of attribute operations.



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

def operations
  @operations
end

#ownerObject

Returns the value of attribute owner.



17
18
19
# File 'lib/commons.rb', line 17

def owner
  @owner
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/commons.rb', line 15

def path
  @path
end

#portObject

Returns the value of attribute port.



13
14
15
# File 'lib/commons.rb', line 13

def port
  @port
end

#userObject

Returns the value of attribute user.



14
15
16
# File 'lib/commons.rb', line 14

def user
  @user
end

Class Method Details

.filename(folder) ⇒ Object



36
37
38
# File 'lib/commons.rb', line 36

def self.filename(folder)
    return File.join(folder, FILENAME)
end

.load(folder = ".") ⇒ Object



30
31
32
33
34
# File 'lib/commons.rb', line 30

def self.load(folder=".")
    rinfo = RemoteInfo.new
    rinfo.read_info(folder)
    return rinfo
end

.lock_all(local, prevent_write = true) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/commons.rb', line 96

def self.lock_all(local, prevent_write=true)
     if (prevent_write)
        FS.each_remote_info(local) do |rinfo|
            OS.run "chmod -w \"#{rinfo}\""
        end
    end
    
    yield
    
    if (prevent_write)
        FS.each_remote_info(local) do |rinfo|
            OS.run "chmod +w \"#{rinfo}\""
        end
    end
end

.unlock_all(local) ⇒ Object



92
93
94
# File 'lib/commons.rb', line 92

def self.unlock_all(local)
    self.lock_all(local, true) {}
end

Instance Method Details

#can_pull?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/commons.rb', line 40

def can_pull?
    return @operations.include? "r"
end

#can_push?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/commons.rb', line 44

def can_push?
    return @operations.include? "w"
end

#read_info(folder) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/commons.rb', line 48

def read_info(folder)
    info = File.read(File.join(folder, FILENAME))
    
    rows = info.split "\n"
    rows.each do |r|
        key, value = *r.split("\t")
        
        key = key.strip
        value = value.strip
        
        case key
        when "host"
            @host = value
        when "port"
            @port = value
        when "user"
            @user = value
        when "path"
            @path = value
        when "netns"
            @netns = value
        when "owner"
            @owner = value
        when "operations"
            @operations = value
        end
    end
end

#save_info(to = ".") ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/commons.rb', line 77

def save_info(to=".")
    content = ""
    content += "host\t#@host\n"
    content += "port\t#@port\n"                 if @port
    content += "user\t#@user\n"                 if @user
    content += "path\t#@path\n"
    content += "netns\t#@netns\n"               if @netns
    content += "owner\t#@owner\n"               if @owner
    content += "operations\t#@operations\n"
    
    dest = to ? File.join(to, FILENAME) : FILENAME
    
    File.write dest, content
end