-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgitosis.rb
More file actions
186 lines (155 loc) · 4.08 KB
/
Copy pathgitosis.rb
File metadata and controls
186 lines (155 loc) · 4.08 KB
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
#
# Cookbook Name:: gitosis
# Recipe:: default
#
# Copyright 2013, deis
#
# All rights reserved - Do Not Redistribute
#
gitosis_dir = node.deis.gitosis.dir
gitosis_checkout = "#{gitosis_dir}/checkout"
gitosis_admin_repo = "#{gitosis_dir}/repositories/gitosis-admin.git"
gitosis_admin_checkout = "#{gitosis_dir}/gitosis-admin"
gitosis_key_dir = "#{gitosis_admin_checkout}/keydir"
# setup git core
package 'git'
# create git user
user 'git' do
system true
uid 325 # "reserved" for git
shell '/bin/sh'
comment 'git version control'
home gitosis_dir
supports :manage_home => true
action :create
end
# allow git user to trigger build-release-run script during
# git push build hook
sudo 'git' do
user 'git'
runas node.deis.username
nopasswd true
commands [ node.deis.controller.dir + '/bin/build-release-run' ]
end
# synchronize the gitosis repository
git gitosis_checkout do
repository 'git://github.com/opdemand/gitosis.git'
action :sync
end
# install gitosis
bash "gitosis-install" do
cwd gitosis_checkout
code "python setup.py install"
action :nothing
subscribes :run, "git[#{gitosis_checkout}]", :immediately
end
# initialize gitosis
bash 'gitosis-generate-ssh-key' do
user 'git'
group 'git'
code "ssh-keygen -t rsa -b 2048 -N '' -f #{gitosis_dir}/.ssh/gitosis-admin -C gitosis-admin"
not_if "test -e #{gitosis_dir}/.ssh/gitosis-admin"
end
bash 'gitosis-init' do
code "sudo -H -u git gitosis-init < #{gitosis_dir}/.ssh/gitosis-admin.pub"
not_if "test -e #{gitosis_dir}/gitosis-admin"
end
bash 'git-clone-gitosis-admin' do
user 'git'
group 'git'
cwd gitosis_dir
code "git clone #{gitosis_admin_repo}"
not_if "test -e #{gitosis_admin_checkout}"
end
# try to load the gitosis data bag item
gitosis = data_bag_item('deis-build', 'gitosis')
# create ssh keys
gitosis['ssh_keys'].each do |key_name, key_material|
file "#{gitosis_key_dir}/#{key_name}.pub" do
owner 'git'
group 'git'
content key_material
notifies :run, 'bash[git-add-gitosis-admin]'
end
end
# purge old ssh keys
Dir.glob("#{gitosis_key_dir}/*.pub").each do |f|
next if f.sub("#{gitosis_key_dir}/", '') == 'gitosis-admin.pub'
if !gitosis['ssh_keys'].has_key? f.sub(gitosis_key_dir+'/', '').sub('.pub', '')
file f do
action :delete
notifies :run, 'bash[git-add-gitosis-admin]'
end
end
end
# configure gitosis
template "#{gitosis_admin_checkout}/gitosis.conf" do
user 'git'
group 'git'
source 'gitosis.conf.erb'
variables({
:admins => ['gitosis-admin'],
:formations => gitosis['formations'],
})
notifies :run, 'bash[git-add-gitosis-admin]'
end
bash 'git-add-gitosis-admin' do
user 'git'
group 'git'
cwd gitosis_admin_checkout
code 'git add .'
action :nothing
notifies :run, 'bash[git-commit-gitosis-admin]'
end
bash 'git-commit-gitosis-admin' do
user 'git'
group 'git'
cwd gitosis_admin_checkout
code 'git commit -m "auto-update via chef"'
action :nothing
notifies :run, 'bash[git-push-gitosis-admin]'
end
bash 'git-push-gitosis-admin' do
user 'git'
group 'git'
cwd gitosis_admin_checkout
code 'git push'
action :nothing
notifies :run, 'bash[gitosis-update-hook]'
end
# TODO: figure out why this needs to be run manually
# after pushing to the gitosis-admin repo
bash 'gitosis-update-hook' do
user 'git'
group 'git'
cwd gitosis_admin_repo
code 'gitosis-run-hook post-update'
environment 'GIT_DIR' => gitosis_admin_repo, 'HOME' => gitosis_dir
action :nothing
end
# create application repositories
gitosis['formations'].each_pair do |name, ssh_keys|
dir = "#{gitosis_dir}/repositories/#{name}.git"
directory dir do
user 'git'
group 'git'
mode 0750
end
bash 'gitosis-init-bare-app' do
user 'git'
group 'git'
cwd dir
code 'git init --bare'
not_if "test -e #{dir}/HEAD"
end
end
# purge old repository directories
Dir.glob("#{gitosis_dir}/repositories/*.git").each do |d|
next if d.include? 'gitosis-admin.git'
if !gitosis['formations'].has_key? d.sub("#{gitosis_dir}/repositories/", '').sub('.git', '')
directory d do
action :delete
recursive true
end
end
end