Class: Larrow::Runner::Vcs::FileSystem

Inherits:
Base
  • Object
show all
Defined in:
lib/larrow/runner/vcs/file_system.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#larrow_file

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileSystem

Returns a new instance of FileSystem.



6
7
8
9
10
11
12
13
14
# File 'lib/larrow/runner/vcs/file_system.rb', line 6

def initialize path
  if File.file? path
    path  = File.absolute_path path
    self.larrow_file    = File.basename path
    self.project_folder = File.dirname path 
  else # directory
    self.project_folder = File.absolute_path path
  end
end

Instance Attribute Details

#project_folderObject

path: absulute path of LarrowFile



5
6
7
# File 'lib/larrow/runner/vcs/file_system.rb', line 5

def project_folder
  @project_folder
end

Instance Method Details

#formatted_urlObject



16
17
18
# File 'lib/larrow/runner/vcs/file_system.rb', line 16

def formatted_url
  self.project_folder
end

#get(filename) ⇒ Object



20
21
22
23
24
25
# File 'lib/larrow/runner/vcs/file_system.rb', line 20

def get filename
  file_path = "#{project_folder}/#{filename}"
  return nil unless File.exist? file_path

  File.read(file_path)
end

#invoke(command) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/larrow/runner/vcs/file_system.rb', line 49

def invoke command
  RunLogger.level(1).info command
  time = Time.new
  `#{command} 2>&1`.split(/\r?\n/).each do |msg|
    RunLogger.level(2).detail msg
  end
  RunLogger.level(1).detail "invoke time: #{Time.new - time}"
end

#rsync_command(user, host, target_dir) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/larrow/runner/vcs/file_system.rb', line 33

def rsync_command user, host, target_dir
  ssh_path = '%s@%s:%s' % [user, host, target_dir]

  excludes = (get('.gitignore')||'').  # rsync exclude according .gitignore
    split(/[\r\n]/).                   # 
    select{|s| s =~ /^[^#]/}.          # not commented
    compact.                           # not blank
    unshift('.git').                   # .git itself is ignored
    map{|s| "--exclude '#{s}'" }       # build rsync exclude arguments

  rsync_options = "-az -e 'ssh #{ssh_options}' #{excludes.join ' '}"
  rsync_options += ' -v' if RunOption.key? :debug

  "rsync #{rsync_options} #{project_folder}/ '#{ssh_path}'"
end

#ssh_optionsObject



58
59
60
61
62
63
64
65
66
# File 'lib/larrow/runner/vcs/file_system.rb', line 58

def ssh_options
  {
    'GSSAPIAuthentication'  => 'no',
    'StrictHostKeyChecking' => 'no',
    'LogLevel'              => 'ERROR'
  }.map do |k,v|
    "-o #{k}=#{v}" 
  end.join(' ')
end

#update_source(node, target_dir) ⇒ Object



27
28
29
30
31
# File 'lib/larrow/runner/vcs/file_system.rb', line 27

def update_source node, target_dir
  command = rsync_command node.user, node.host,target_dir
  invoke command
  `ssh-keygen -R #{node.host} 2>&1 >/dev/null`
end