forked from auser/glitter
/
glitter.erl
406 lines (358 loc) · 14.9 KB
/
glitter.erl
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
%%%-------------------------------------------------------------------
%%% File : glitter.erl
%%% Author : Ari Lerner
%%% Description :
%%%
%%% Created : Sun Dec 20 13:29:52 PST 2009
%%%-------------------------------------------------------------------
-module (glitter).
-behaviour(gen_server).
%% API
-export ([
list_repos/0,
has_git_repos/1,
add_repos/1,
remove_repos/1,
add_user_to_repos/2,
remove_user_from_repos/2,
add_user/2,
list_groups/0,
set_group/2,
reload/0,
commit/0,
stop/0
]).
-export([start_link/0, start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("glitter.hrl").
-record(state, {
config_file,
config
}).
-define(SERVER, ?MODULE).
%%====================================================================
%% API
%%====================================================================
%% @type repo() = {Name, [user_info()]}.
%% Describes a repository and any users associated with it.
%% @type user_info() = {Username, Permissions},
%% Username = string(),
%% Permissions = string().
%% Describes necessary info for repo permissions. Permissions are
%% generally "RW", "RW+" or "R".
%%
%% @type group() = {Name, [User]},
%% Name = string(),
%% User = string().
%% User groups as defined by gitolite. Name should always begin with ``@''.
%%--------------------------------------------------------------------
%% @doc Returns a list of repo information.
%% @spec list_repos() -> [repo()]
%% @end
%%--------------------------------------------------------------------
list_repos() -> gen_server:call(?SERVER, {list_repos}).
%%--------------------------------------------------------------------
%% @doc Checks if repo is found in gitolite
%% @spec has_git_repos(Name) -> true | false
%% @end
%%--------------------------------------------------------------------
has_git_repos(Name) -> gen_server:call(?SERVER, {has_git_repos, Name}).
%%--------------------------------------------------------------------
%% @doc Adds new repository entry to gitolite.conf. Won't create
%% anything if repository already exists.
%% @spec add_repos(Name) -> ok
%% @end
%%--------------------------------------------------------------------
add_repos(Name) -> gen_server:call(?SERVER, {add_repos, Name}).
%%--------------------------------------------------------------------
%% @doc Removes repository entry to gitolite.conf
%% @spec remove_repos(Name) -> ok
%% @end
%%--------------------------------------------------------------------
remove_repos(Name) -> gen_server:call(?SERVER, {remove_repos, Name}).
%%--------------------------------------------------------------------
%% @doc Adds user and permsissions to specified repo. If repo does not
%% exist, this will also add_repos(Name).
%% @spec add_user_to_repos(UserInfo, Name) -> ok
%% UserInfo = user_info()
%% @end
%%--------------------------------------------------------------------
add_user_to_repos(UserInfo, Name) ->
gen_server:call(?SERVER, {add_user_to_repos, Name, UserInfo}).
%%--------------------------------------------------------------------
%% @doc Removes user_info() from specified repo.
%% @spec remove_user_from_repos(UserName, Name) -> ok
%% @end
%%--------------------------------------------------------------------
remove_user_from_repos(UserName, Name) ->
gen_server:call(?SERVER, {remove_user_from_repos, Name, UserName}).
%%--------------------------------------------------------------------
%% @doc Add user's public ssh key to the gitolite admin repo.
%% @spec add_user(Username, Pubkey) -> ok
%% @end
%%--------------------------------------------------------------------
add_user(Username, Pubkey) ->
gen_server:call(?SERVER, {add_new_user_and_key, Username, Pubkey}).
%%--------------------------------------------------------------------
%% @doc Creates a gitolite user group. Groups are basically ``@group'' =
%% user1 user2. They can then be used as a user by calling
%% add_user_to_repos({``@group'', Permissions}, Name)
%% @spec set_group(Name, Users) -> ok
%% Users = [string()]
%% @end
%%--------------------------------------------------------------------
set_group(Name, Users) ->
gen_server:call(?SERVER, {set_group, Name, Users}).
%%--------------------------------------------------------------------
%% @doc Returns a list of group key/value pairs
%% @spec list_groups() -> [group()]
%% @end
%%--------------------------------------------------------------------
list_groups() ->
gen_server:call(?SERVER, {list_groups}).
%%--------------------------------------------------------------------
%% @doc Reloads repo and user info from the gitolite admin config file.
%% @end
%%--------------------------------------------------------------------
reload() -> gen_server:call(?SERVER, {reload}).
%%--------------------------------------------------------------------
%% @doc Performs a git commit and push to actually publish changes
%% made to the gitolite config by glitter.
%% @end
%%--------------------------------------------------------------------
commit() -> gen_server:cast(?SERVER, {commit}).
stop() ->
gen_server:cast(?SERVER, {stop}).
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
start_link([]).
%%--------------------------------------------------------------------
%% @doc
%% Starts the server. Args are passed to init(Args).
%% @spec start_link(Args) -> {ok,Pid} | ignore | {error,Error}
%% @end
%%-------------------------------------------------------------------
start_link(Args) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, Args, []).
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% @private
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init(Args) ->
case proplists:get_value(config_file, Args) of
undefined ->
{ok, ConfigFile} = application:get_env(glitter, config_file);
ConfigFile -> ConfigFile
end,
Config = conf_reader:parse_file(ConfigFile),
{ok, #state{
config_file = filename:absname(ConfigFile),
config = Config
}}.
%%--------------------------------------------------------------------
%% @private
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call({list_repos}, _From, #state{config = Config} = State) ->
Reply = handle_list_repos(Config),
{reply, Reply, State};
handle_call({add_repos, Name}, _From, #state{config = Config} = State) ->
NewState = handle_add_repos(Name, Config, State),
{reply, ok, NewState};
handle_call({remove_repos, Name}, _From, #state{config = Config} = State) ->
NewState = handle_remove_repos(Name, Config, State),
{reply, ok, NewState};
handle_call({add_user_to_repos, Name, UserInfo}, _From, State) ->
NewState = handle_add_user_to_repos(Name, UserInfo, State),
{reply, ok, NewState};
handle_call({remove_user_from_repos, Name, UserName}, _From, State) ->
NewState = handle_remove_user_from_repos(Name, UserName, State),
{reply, ok, NewState};
handle_call({add_new_user_and_key, UserInfo, Pubkey}, _From, State) ->
NewState = handle_add_new_user_and_key(UserInfo, Pubkey, State),
{reply, ok, NewState};
handle_call({list_groups}, _From, #state{config = Config} = State) ->
Reply = handle_list_groups(Config),
{reply, Reply, State};
handle_call({set_group, Name, Users}, _From, State) ->
NewState = handle_set_group(Name, Users, State),
{reply, ok, NewState};
handle_call({has_git_repos, Name}, _From, #state{config = Config} = State) ->
Reply = lists:any(fun(R) -> Name =:= element(1,R) end,
handle_list_repos(Config)),
{reply, Reply, State};
handle_call({reload}, _From, #state{config_file = ConfigFile} = State) ->
Config = conf_reader:parse_file(ConfigFile),
{reply, ok, State#state{config = Config}};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% @private
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast({stop}, State) ->
{stop, normal, State};
handle_cast({flush}, State) ->
flush(State),
{noreply, State};
handle_cast({commit}, #state{config_file = ConfigFile} = State) ->
handle_commit(ConfigFile),
flush(State),
Config = conf_reader:parse_file(ConfigFile),
{noreply, State#state{config = Config}};
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% @private
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
handle_list_repos(Config) ->
Config#config.repos.
handle_add_repos(undefined, _Config, State) -> State;
handle_add_repos(Name, Config, State) ->
NewConfig =
case lists:any(fun(Repo) ->
Name =:= element(1,Repo)
end, Config#config.repos) of
false ->
%% New config
Config#config{repos = [{Name, []}|Config#config.repos]};
_ ->
Config
end,
NewState = State#state{config = NewConfig},
flush(NewState),
NewState.
handle_remove_repos(undefined, _Config, _State) -> ok;
handle_remove_repos(Name, Config, State) ->
Repos = Config#config.repos,
NewConfig = case proplists:is_defined(Name, Repos) of
true ->
NewRepos = proplists:delete(Name, Repos),
Config#config{repos = NewRepos};
false -> Config
end,
NewState = State#state{config = NewConfig},
flush(NewState),
NewState.
%% UserInfo -> {"name", "permission"}
handle_add_user_to_repos(Name, UserInfo, #state{config = Config} = State) ->
case find_already_defined_repos(Name, Config) of
{error, not_found} ->
NewState = handle_add_repos(Name, Config, State),
handle_add_user_to_repos(Name, UserInfo, NewState);
{ok, Users} ->
NewReposConfig =
case proplists:is_defined(element(1,UserInfo), Users) of
false -> [UserInfo|Users];
true ->
OldUsers = proplists:delete(element(1,UserInfo), Users),
[UserInfo|OldUsers]
end,
OldRepos = proplists:delete(Name, Config#config.repos),
NewRepos = [{Name, NewReposConfig}|OldRepos],
NewState = State#state{config = Config#config{repos = NewRepos}},
flush(NewState),
NewState
end.
handle_remove_user_from_repos(Name, UserName,
#state{config = Config} = State) ->
case find_already_defined_repos(Name, Config) of
{error, not_found} -> ok;
{ok, Users} ->
case proplists:is_defined(UserName, Users) of
false -> ok;
true ->
OtherUsers = proplists:delete(UserName, Users),
UpdatedRepo = {Name, OtherUsers},
OtherRepos = proplists:delete(Name, Config#config.repos),
NewConfig = Config#config{repos = [UpdatedRepo|OtherRepos]},
NewState = State#state{config = NewConfig},
flush(NewState),
NewState
end
end.
handle_add_new_user_and_key(UserName, Pubkey,
#state{config_file = ConfigFile} = State) ->
Dirname = filename:dirname(ConfigFile),
PubKeyfile =
filename:join([Dirname, "..", "keydir", lists:append(UserName, ".pub")]),
{ok, Fd} = file:open(PubKeyfile, [write]),
file:write(Fd, Pubkey),
file:close(Fd),
State.
handle_list_groups(Config) ->
Config#config.groups.
handle_set_group(Name, Users,
#state{config = Config} = State) ->
Groups = Config#config.groups,
NewGroups = proplists:delete(Name, Groups),
UpdatedGroups = [{Name, Users}| NewGroups],
State#state{config = Config#config{groups = UpdatedGroups}}.
find_already_defined_repos(Name, Config) when is_record(Config, config) ->
find_already_defined_repos(Name, Config#config.repos);
find_already_defined_repos(Name, Repos) ->
case proplists:get_value(Name, Repos) of
undefined -> {error, not_found};
Result -> {ok, Result}
end.
flush(#state{config = Config, config_file = ConfigFile}) ->
conf_writer:write(Config, ConfigFile).
handle_commit(ConfigFile) ->
Dirname = filename:dirname(ConfigFile),
Command = lists:append(["cd ", Dirname, " && ",
"git add . && ",
"git commit -a -m 'Updated from glitter'", " && ",
"git push origin master"]),
Out = os:cmd(Command),
io:format("Out: ~p~n", [Out]),
Out.