Class: Rsh::Box

Inherits:
Object
  • Object
show all
Includes:
Marks
Defined in:
lib/rsh/box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Marks

#clear_marks, #has_mark?, #mark

Constructor Details

#initialize(options = {}) ⇒ Box

Returns a new instance of Box.



7
8
9
10
# File 'lib/rsh/box.rb', line 7

def initialize options = {}
  @options = options
  options[:host] ||= 'localhost'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object (protected)



178
179
180
# File 'lib/rsh/box.rb', line 178

def method_missing m, *a, &b
  driver.send m, *a, &b
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/rsh/box.rb', line 5

def options
  @options
end

Instance Method Details

#bash(cmd, options = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rsh/box.rb', line 146

def bash cmd, options = {}
  ignore_stderr = options.delete :ignore_stderr
  raise "invalid options :#{options.keys.join(', :')}" unless options.empty?
  
  code, stdout, stderr = exec cmd
  unless code == 0
    puts stdout
    puts stderr
    raise "can't execute '#{cmd}'!" 
  end
  unless stderr.empty? or ignore_stderr
    puts stderr
    raise "stderr not empty for '#{cmd}'!"
  end
  stdout + stderr
end

#bulk(&b) ⇒ Object



24
25
26
# File 'lib/rsh/box.rb', line 24

def bulk &b
  driver.bulk &b
end

#create_directory(remote_path, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rsh/box.rb', line 84

def create_directory remote_path, options = {}
  bulk do
    if driver.directory_exist?(remote_path)
      if options[:override]
        driver.remove_directory remote_path
        driver.create_directory remote_path
      elsif options[:silent]
        # do nothing
      else
        raise "directory '#{remote_path}' already exists!"
      end
    else
      driver.create_directory remote_path
    end        
  end
end

#directory_exist?(remote_path) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rsh/box.rb', line 101

def directory_exist? remote_path
  driver.directory_exist? remote_path
end

#download_directory(from_remote_path, to_local_path, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rsh/box.rb', line 119

def download_directory from_remote_path, to_local_path, options = {}
  bulk do
    raise "directory #{from_remote_path} not exists!" unless driver.directory_exist?(from_remote_path)
    if local_driver.directory_exist? to_local_path
      if options[:override]
        local_driver.remove_directory to_local_path
      else
        raise "directory #{to_local_path} already exists!" 
      end
    end
    driver.download_directory from_remote_path, to_local_path
  end
end

#download_file(from_remote_path, to_local_path, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rsh/box.rb', line 42

def download_file from_remote_path, to_local_path, options = {}
  bulk do
    raise "file #{from_remote_path} not exists!" unless driver.file_exist?(from_remote_path)
    if local_driver.file_exist? to_local_path
      if options[:override]
        local_driver.remove_file to_local_path
      else
        raise "file #{to_local_path} already exists!" 
      end
    end
    driver.download_file from_remote_path, to_local_path
  end
end

#driverObject



12
13
14
15
16
17
18
# File 'lib/rsh/box.rb', line 12

def driver
  unless @driver
    klass = options[:host] == 'localhost' ? Drivers::Local : Drivers::Ssh
    @driver = klass.new options
  end
  @driver
end

#exec(cmd) ⇒ Object



163
164
165
# File 'lib/rsh/box.rb', line 163

def exec cmd
  driver.exec cmd
end

#exist?(remote_path) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rsh/box.rb', line 66

def exist? remote_path
  driver.exist? remote_path
end

#file_exist?(remote_file_path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rsh/box.rb', line 70

def file_exist? remote_file_path
  driver.file_exist? remote_file_path
end

#home(path = nil) ⇒ Object



167
168
169
170
# File 'lib/rsh/box.rb', line 167

def home path = nil
  @home ||= bash('cd ~; pwd').gsub("\n", '')    
  "#{@home}#{path}"
end

#inspectObject Also known as: to_s



172
173
174
# File 'lib/rsh/box.rb', line 172

def inspect
  "<Box: #{options[:host]}>"
end

#local_driverObject



20
21
22
# File 'lib/rsh/box.rb', line 20

def local_driver
  @local_driver ||= Drivers::Local.new
end

#remove_directory(remote_directory_path, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/rsh/box.rb', line 74

def remove_directory remote_directory_path, options = {}
  bulk do
    if driver.directory_exist? remote_directory_path
      driver.remove_directory remote_directory_path
    else
      raise "directory #{remote_directory_path} not exists!" unless options[:silent]
    end
  end
end

#remove_file(remote_file_path, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/rsh/box.rb', line 56

def remove_file remote_file_path, options = {}
  bulk do
    if driver.file_exist? remote_file_path
      driver.remove_file remote_file_path
    else
      raise "file #{remote_file_path} not exists!" unless options[:silent]
    end        
  end
end

#upload_directory(from_local_path, to_remote_path, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rsh/box.rb', line 105

def upload_directory from_local_path, to_remote_path, options = {}
  bulk do
    raise "directory '#{from_local_path}' not exists!" unless local_driver.directory_exist? from_local_path
    if driver.directory_exist?(to_remote_path)
      if options[:override]
        driver.remove_directory to_remote_path
      else
        raise "directory '#{to_remote_path}' already exists!"
      end
    end        
    driver.upload_directory from_local_path, to_remote_path
  end
end

#upload_file(from_local_path, to_remote_path, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rsh/box.rb', line 28

def upload_file from_local_path, to_remote_path, options = {}
  bulk do
    raise "file '#{from_local_path}' not exists!" unless local_driver.file_exist? from_local_path
    if driver.file_exist?(to_remote_path)
      if options[:override]
        driver.remove_file to_remote_path
      else
        raise "file '#{to_remote_path}' already exists!"
      end
    end        
    driver.upload_file from_local_path, to_remote_path
  end
end

#with_tmp_dir(&block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rsh/box.rb', line 133

def with_tmp_dir &block
  bulk do
    tmp_dir = driver.generate_tmp_dir_name
    begin   
      remove_directory tmp_dir if directory_exist? tmp_dir
      create_directory tmp_dir
      block.call
    ensure
      remove_directory tmp_dir if directory_exist? tmp_dir
    end
  end
end