Class: Rsh::Box

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Box

Returns a new instance of Box.



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

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)



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

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/rsh/box.rb', line 3

def options
  @options
end

Instance Method Details

#bash(cmd) ⇒ Object



134
135
136
137
138
139
# File 'lib/rsh/box.rb', line 134

def bash cmd
  code, stdout, stderr = exec cmd
  raise "can't execute '#{cmd}'!" unless code == 0
  raise stderr unless stderr.empty?
  stdout
end

#bulk(&b) ⇒ Object



22
23
24
# File 'lib/rsh/box.rb', line 22

def bulk &b
  driver.bulk &b
end

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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rsh/box.rb', line 76

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

#directory_exist?(remote_path) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/rsh/box.rb', line 89

def directory_exist? remote_path
  driver.directory_exist? remote_path
end

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



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

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



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

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



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

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

#exec(cmd) ⇒ Object



141
142
143
# File 'lib/rsh/box.rb', line 141

def exec cmd
  driver.exec cmd
end

#exist?(remote_path) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rsh/box.rb', line 61

def exist? remote_path
  driver.exist? remote_path
end

#file_exist?(remote_file_path) ⇒ Boolean

Returns:

  • (Boolean)


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

def file_exist? remote_file_path
  driver.file_exist? remote_file_path
end

#has_mark?(key) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/rsh/box.rb', line 155

def has_mark? key
  ensure_mark_requrements!
  file_exist? "#{marks_dir}/#{key}"
end

#home(path = nil) ⇒ Object



145
146
147
148
# File 'lib/rsh/box.rb', line 145

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

#local_driverObject



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

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

#mark(key) ⇒ Object



150
151
152
153
# File 'lib/rsh/box.rb', line 150

def mark key
  ensure_mark_requrements!
  bash "touch #{marks_dir}/#{key}"
end

#remove_directory(remote_directory_path) ⇒ Object



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

def remove_directory remote_directory_path
  bulk do
    raise "directory #{remote_directory_path} not exists!" unless driver.directory_exist? remote_directory_path
    driver.remove_directory remote_directory_path
  end
end

#remove_file(remote_file_path) ⇒ Object



54
55
56
57
58
59
# File 'lib/rsh/box.rb', line 54

def remove_file remote_file_path
  bulk do
    raise "file #{remote_file_path} not exists!" unless driver.file_exist? remote_file_path
    driver.remove_file remote_file_path
  end
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rsh/box.rb', line 93

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



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

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



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

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