-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
187 lines (159 loc) · 5.04 KB
/
app.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
# encoding: utf-8
ENV['RACK_ENV'] = "development" unless ENV['RACK_ENV'] != nil
require "rubygems"
require "bundler/setup"
require "fileutils"
require_relative ENV['RACK_ENV']=='production' ? 'lib/remote_syslog' : 'lib/log'
# get all the gems in
Bundler.require(:default)
class MercuryAuthentication < Sinatra::Base
before do
pass if request.path_info == '401' || api_auth(env)
throw :halt, [401, "Unauthorized / Authentication failed"]
end
protected
def api_auth(the_env)
token = the_env['HTTP_TOKEN'] || the_env['TOKEN']
username = the_env['HTTP_USERNAME'] || the_env['USERNAME']
return false unless (token != nil) && (username != nil)
return false if (Settings.api.token != token) || (Settings.api.username != username)
return true
end
end
use MercuryAuthentication
class Mercury < Sinatra::Application
RailsConfig.load_and_set_settings("./config/settings.yml", "./config/settings/#{settings.environment.to_s}.yml")
enable :logging
configure :production do
LOGGER = RemoteSyslog.new(Settings.remote_log_host,Settings.remote_log_port)
use MercuryAuthentication
use Rack::CommonLogger, LOGGER
end
configure :development do
use MercuryAuthentication
LOGGER = Logger.new("log/#{settings.environment.to_s}.log")
end
helpers do
def logger
LOGGER
end
end
get "/alive" do
status 200
st_answ = {"status" => "running"}.to_json
body st_answ
end
post '/repositories/create/?' do
data = params
if not init_store(data["path"])
status 409
answ = {"result" => false, "error" => "can't create repository"}.to_json
body answ
end
if repo_init(data["path"])
status 200
answ = {"result" => true}.to_json
body answ
else
status 409
answ = {"result" => false, "error" => "can't create repository"}.to_json
body answ
end
end
post '/repositories/destroy/?' do
data = params
if File.exist?(data["path"])
FileUtils.mv(data["path"], Settings.root + "/.trash/#{Time.now.strftime("%d%m%Y-%H%M%S")}.old")
if not File.exist?(data["path"])
status 200
body "Repository deleted"
return
else
status 500
body "Could not destroy repository"
return
end
else
status 200
body "Repository doesn't exist"
return
end
end
post "/repositories/status" do
data = params
answer = {"status" => "loose"}.to_json
answer = {"status" => "created"}.to_json if File.exist?(data["path"])
status 200
body answer
end
post "/keys" do
logger.info("data in")
if not params[:data]
status 400
body "Missing data"
return
end
data = JSON.parse(params[:data])
File.open("/tmp/authorized_keys", "w") { |file_out| file_out.write(data["authfile"]) }
status 200
body "exported"
end
private
def init_store(repository_path)
if File.exist?(repository_path)
return false
end
begin
FileUtils.mkdir_p(repository_path)
rescue Errno::EACCES
logger.info("permission denied #{repository_path}")
return false
end
return true
end
def repo_init(repository)
app_dir = File.expand_path(File.dirname(__FILE__))
File.umask(0007)
dirs = {
"hooks" => nil,
"info" => nil,
"objects" => { "info" => nil, "pack" => nil },
"refs" => { "heads" => nil, "tags" => nil }}
files = ["HEAD", "config", "description"]
hooks = ["post-receive"]
dot_git = Pathname(repository)
# Create the dirs
l_mkdirs(dot_git, dirs)
# Create base files
bare = true
files.each do |l_file|
if !File.exist?("#{dot_git}/#{l_file}")
File.open("#{dot_git}/#{l_file}", "a") do |file_out|
IO.foreach("#{app_dir}/config/templates/#{l_file}") { |w| file_out.puts(w) }
end
end
end
# create post receive hook to send git front last rev data
# curl -s -H "TOKEN:tokenstring" -H "USERNAME:shell_user" -X POST http://localhost:8080/api/git/push?repository=$REPOSITORY_BASENAME&rev=$newrev
curl_string = "curl -s -H \"TOKEN:#{Settings.egg_api.token}\" -H \"USERNAME:#{Settings.egg_api.username}\" -X POST \"http://#{Settings.egg_api.hostname}:#{Settings.egg_api.port}/api/git/push?repository=$REPOSITORY_BASENAME&rev=$newrev\""
post_recv_hook = IO.read("#{app_dir}/config/templates/post-receive")
post_recv_hook.gsub!("REQUEST_TO_FRONT",curl_string)
File.open("#{dot_git}/hooks/post-receive", "a") do |file_out|
file_out.puts(post_recv_hook)
end
FileUtils.chmod(770, "#{dot_git}/hooks/post-receive")
return true
end # def git_repo_init
# l_mkdirs method creates dirs using a hash
#
# root is a pathname object
# dir_hash is a hash containing dirs to create in root
# if subdirs need to be created in those then the key points to another hash
def l_mkdirs(root, dir_hash)
dir_hash.each_key do |s_dir|
File.umask(0007)
Dir.mkdir(root + s_dir) if !(root + s_dir).exist?
l_mkdirs(root + s_dir, dir_hash[s_dir]) if dir_hash[s_dir]
end
end
end