forked from redmine-git-hosting/redmine_git_hosting
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsudo.rb
213 lines (165 loc) · 5.08 KB
/
sudo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'digest/md5'
module RedmineGitHosting
module Commands
module Sudo
extend self
##########################
# #
# SUDO Shell Wrapper #
# #
##########################
# Pipe file content via sudo to dest_file.
# Expect file content to end with EOL (\n)
#
def sudo_install_file(content, dest_file, filemode)
stdin = [ 'cat', '<<\EOF', '>' + dest_file, "\n" + content.to_s + "EOF" ].join(' ')
begin
sudo_pipe_data(stdin)
rescue RedmineGitHosting::Error::GitoliteCommandException => e
logger.error(e.output)
return false
else
begin
sudo_chmod(filemode, dest_file)
return true
rescue RedmineGitHosting::Error::GitoliteCommandException => e
logger.error(e.output)
return false
end
end
end
# Test if a file exists with size > 0
#
def sudo_file_exists?(filename)
sudo_test(filename, '-s')
end
# Test if a directory exists
#
def sudo_dir_exists?(dirname)
sudo_test(dirname, '-r')
end
# Test properties of a path from the git user.
#
# e.g., Test if a directory exists: sudo_test('~/somedir', '-d')
#
def sudo_test(path, testarg)
_, _ , code = sudo_shell('test', testarg, path)
return code == 0
rescue RedmineGitHosting::Error::GitoliteCommandException => e
logger.debug("File check for #{path} failed : #{e.message}")
false
end
# Calls mkdir with the given arguments on the git user's side.
#
# e.g., sudo_mkdir('-p', '/some/path')
#
def sudo_mkdir(*args)
sudo_shell('mkdir', *args)
end
# Syntaxic sugar for 'mkdir -p'
#
def sudo_mkdir_p(path)
sudo_mkdir('-p', path)
end
# Calls chmod with the given arguments on the git user's side.
#
# e.g., sudo_chmod('755', '/some/path')
#
def sudo_chmod(mode, file)
sudo_shell('chmod', mode, file)
end
# Removes a directory and all subdirectories below gitolite_user's $HOME.
#
# Assumes a relative path.
#
# If force=true, it will delete using 'rm -rf <path>', otherwise
# it uses rmdir
#
def sudo_rmdir(path, force = false)
if force
sudo_shell('rm', '-rf', path)
else
sudo_shell('rmdir', path)
end
end
# Syntaxic sugar for 'rm -rf' command
#
def sudo_rm_rf(path)
sudo_rmdir(path, true)
end
# Moves a file/directory to a new target.
#
def sudo_move(old_path, new_path)
sudo_shell('mv', old_path, new_path)
end
def sudo_get_dir_size(directory)
sudo_capture('du', '-sh', directory).split(' ')[0] rescue ''
end
def sudo_cat(file)
sudo_capture('cat', file) rescue ''
end
# Test if file content has changed
#
def sudo_file_changed?(source_file, dest_file)
hash_content(content_from_redmine_side(source_file)) != hash_content(content_from_gitolite_side(dest_file))
end
# Return only the output of the shell command.
# Throws an exception if the shell command does not exit with code 0.
#
def sudo_capture(*params)
cmd = sudo.concat(params)
capture(cmd).strip
end
# Execute a command as the gitolite user defined in +GitoliteWrapper.gitolite_user+.
#
# Will shell out to +sudo -n -u <gitolite_user> params+
#
def sudo_shell(*params)
cmd = sudo.concat(params)
execute(cmd)
end
# Write data on stdin and return the output of the shell command.
# Throws an exception if the shell command does not exit with code 0.
#
def sudo_pipe_data(stdin)
cmd = sudo.push('sh')
capture(cmd, { stdin_data: stdin, binmode: true })
end
private
# Return the Sudo command with basic args.
#
def sudo
if RedmineGitHosting::Config.gitolite_use_sudo?
['sudo', *sudo_shell_params]
else
[]
end
end
# Returns the sudo prefix to all sudo_* commands.
#
# These are as follows:
# * (-i) login as `gitolite_user` (setting ENV['HOME')
# * (-n) non-interactive
# * (-u `gitolite_user`) target user
#
def sudo_shell_params
['-n', '-u', RedmineGitHosting::Config.gitolite_user, '-i']
end
# Return a md5 hash of the string passed.
#
def hash_content(content)
Digest::MD5.hexdigest(content)
end
# Return the content of a local (Redmine side) file.
#
def content_from_redmine_side(file)
File.read(file)
end
# Return the content of a file on Gitolite side.
#
def content_from_gitolite_side(destination_path)
sudo_cat(destination_path)
end
end
end
end