Module: Mobilize::Git

Defined in:
lib/mobilize-ssh/handlers/git.rb

Class Method Summary collapse

Class Method Details

.configObject



3
4
5
# File 'lib/mobilize-ssh/handlers/git.rb', line 3

def Git.config
  Base.config('git')
end

.default_domainObject



15
16
17
# File 'lib/mobilize-ssh/handlers/git.rb', line 15

def Git.default_domain
  Git.domains.first
end

.domainsObject



11
12
13
# File 'lib/mobilize-ssh/handlers/git.rb', line 11

def Git.domains
  Git.config['domains'].keys
end

.exists?(url) ⇒ Boolean

confirm that git file exists

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mobilize-ssh/handlers/git.rb', line 62

def Git.exists?(url)
  domain,repo,revision,file_path=[]
  url.split("/").ie do |url_nodes|
    domain    = url_nodes[2]
    repo      = url_nodes[3..4].join("/")
    revision  = url_nodes[5]
    file_path = url_nodes[6..-1].join("/")
  end
  repo_dir = Git.pull(domain,repo,revision)
  full_path = "#{repo_dir}/#{file_path}"
  exists = File.exists?(full_path)
  if exists
    FileUtils.rm_r(repo_dir,:force=>true)
    return exists
  else
    raise "Unable to find #{full_path}"
  end
end

.host(domain) ⇒ Object



7
8
9
# File 'lib/mobilize-ssh/handlers/git.rb', line 7

def Git.host(domain)
  Git.config['domains'][domain]['host']
end

.pack(domain, repo, revision = "HEAD") ⇒ Object

return path to tar.gz of git repo



51
52
53
54
55
56
57
58
59
# File 'lib/mobilize-ssh/handlers/git.rb', line 51

def Git.pack(domain,repo,revision="HEAD")
  repo_dir = Git.pull(domain,repo,revision)
  repo_name = repo.split("/").last
  tar_gz_path = "#{repo_dir}/../#{repo_name}.tar.gz"
  pack_cmd = "cd #{repo_dir} && git archive #{revision} --format=tar.gz > #{tar_gz_path}"
  pack_cmd.bash(true)
  FileUtils.rm_r(repo_dir,:force=>true)
  return tar_gz_path
end

.path_to_dst(path, stage_path, gdrive_slot) ⇒ Object

converts a source path or target path to a dst in the context of handler and stage



28
29
30
31
32
# File 'lib/mobilize-ssh/handlers/git.rb', line 28

def Git.path_to_dst(path,stage_path,gdrive_slot)
  red_path = path.split("://").last
  git_url = Git.url_by_path(red_path)
  return Dataset.find_or_create_by_url(git_url)
end

.pull(domain, repo, revision, run_dir = Dir.mktmpdir) ⇒ Object

pulls a git repo and sets it to the specified revision in the specified folder



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mobilize-ssh/handlers/git.rb', line 83

def Git.pull(domain,repo,revision,run_dir=Dir.mktmpdir)
  domain_properties = Git.config['domains'][domain]
  user,host= ['user','host'].map{|k| domain_properties[k]}
  key = Git.repo_key(domain,repo)
  #create folder for repo and command
  run_file_path = run_dir + "/cmd.sh"
  #put together command
  git_prefix = key ? "ssh-add #{Base.root}/#{key};" : ""
  git_suffix = (revision=="HEAD" ? " --depth=1" : "; git checkout -q #{revision}")
  #add keys, clone repo, go to specific revision, execute command
  full_cmd = "cd #{run_dir};#{git_prefix}git clone -q #{user}@#{host}:#{repo}.git#{git_suffix}"
  #put command in file, run ssh-agent bash on it
  File.open(run_file_path,"w") {|f| f.print(full_cmd)}
  run_cmd = "ssh-agent bash #{run_file_path}"
  #run the command, it will return an exception if there are issues
  run_cmd.bash(true)
  repo_name = repo.split("/").last
  repo_dir = "#{run_dir}/#{repo_name}"
  return repo_dir
end

.read_by_dataset_path(dst_path, user_name, *args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mobilize-ssh/handlers/git.rb', line 104

def Git.read_by_dataset_path(dst_path,user_name,*args)
  domain,repo,revision,file_path = []
  dst_path.split("/").ie do |path_nodes|
    domain    = path_nodes[0]
    repo      = path_nodes[1..2].join("/")
    revision  = path_nodes[3]
    file_path = path_nodes[4..-1].join("/")
  end
  #slash in front of path
  repo_dir = Git.pull(domain,repo,revision)
  full_path = "#{repo_dir}/#{file_path}"
  result = "cat #{full_path}".bash(true)
  FileUtils.rm_r(repo_dir,:force=>true)
  return result
end

.repo_key(domain, repo) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/mobilize-ssh/handlers/git.rb', line 19

def Git.repo_key(domain,repo)
  begin
    Git.config['domains'][domain]['repo_keys'][repo]
  rescue
    nil #no key for public repos
  end
end

.url_by_path(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mobilize-ssh/handlers/git.rb', line 34

def Git.url_by_path(path)
  path_nodes = path.split("/")
  domain = path_nodes.first.to_s
  revision = "HEAD"
  if Git.domains.include?(domain)
    repo = path_nodes[1..2].join("/")
    file_path = path_nodes[3..-1].join("/")
  else
    domain = Git.default_domain
    repo = path_nodes[0..1].join("/")
    file_path = path_nodes[2..-1].join("/")
  end
  url = "git://#{domain}/#{repo}/#{revision}/#{file_path}"
  return url
end