This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stomp-git
executable file
·142 lines (116 loc) · 3.9 KB
/
stomp-git
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
#!/usr/bin/env ruby
require 'rubygems'
require 'yaml'
require 'optparse'
require 'logger'
# You probably don't have the future-stomp gem..
require 'future-stomp'
#require_relative 'lib/future-stomp'
options = {}
version = '3.20'
pidfile = '/var/run/stomp-git.pid'
optparse = OptionParser.new do|opts|
opts.banner = 'Usage: stomp-git.rb [options]'
options[:debug] = false
opts.on('-d', '--debug', 'Much output, do not detach') do
options[:debug] = true
end
options[:configfile] = '/etc/stomp-git/stomp-git.yaml'
opts.on('-c', '--config FILE', 'Config is FILE') do|file|
options[:configfile] = file
end
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
optparse.parse!
yconfig = YAML.load_file(options[:configfile])
sdebug = options[:debug]
puts YAML.dump(yconfig) if sdebug
slog = GLog.new(options[:configfile],sdebug)
if !options[:debug]
pid = Process.fork
else
pid = nil
puts 'DEBUG'
puts "VERSION #{version}"
puts "CONFIGFILE: #{options[:configfile]}"
end
if pid.nil?
Signal.trap('TERM') do
slog.msg('Terminating.')
exit
end
Signal.trap('HUP') do
yconfig = YAML.load_file(options[:configfile])
slog.msg("Re-read #{options[:configfile]}")
end
listen_topic = yconfig['listen-topic']
repos = yconfig['repo-list']
stompconnector = yconfig['stompconnector']
stompconnector[:logger] = slog
client = Stomp::Client.new(stompconnector)
if client
slog.hash('start',{'version' => version, 'topic' => listen_topic})
client.subscribe "/topic/#{listen_topic}" do |message|
dumpmessage(message) if sdebug
msg = Parsemessage.new(message)
begin
check_valid_repo(msg.repo,repos)
repo_dir = repos[msg.repo]['repo']
repo_user = repos[msg.repo]['user']
repo_mode = repos[msg.repo]['mode']
check_valid_user(repo_user)
status = gitfetch(msg,repo_dir,repo_user)
slog.hash('fetch',status)
case repo_mode
when 'branch'
if msg.branch == repos[msg.repo]['branch']
status = git_branch(msg,repo_dir,repo_user,repos[msg.repo]['target'],repos[msg.repo]['branch']) # origin/$branch
slog.hash('branch',status)
else
slog.hash('error',{'error' => 'Not our branch','branch' => branch}) if sdebug
end
when 'trusting'
status = git_branch(msg,repo_dir,repo_user,repos[msg.repo]['target'],'master') # origin/master
slog.hash('trusting',status)
when 'puppetmaster'
status = git_puppetmaster(repo_dir,repo_user,msg,repos[msg.repo]['target'],'/')
slog.hash('puppetmaster',status)
when 'tags'
status = git_tags(repo_dir,repo_user,msg,repos[msg.repo]['target'],'/')
slog.hash('tags',status)
when 'atomic'
status = git_puppetmaster(repo_dir,repo_user,msg,repos[msg.repo]['target'],'_')
slog.hash('atomic',status)
when 'normal'
# Do nothing.
else
slog.hash('error',{'error '=> 'Not a valid repo mode','mode' => repo_mode})
end
rescue ArgumentError
slog.hash('error',{'error' => 'User doesn\'t exist','user' => repo_user})
rescue UserLoginException
slog.hash('error',{'error' => 'User has no login shell','user' => repo_user})
rescue MissingRepoException
slog.hash('error',{'error' => 'Repo in config but not in filesystem','repo' => msg.repo})
rescue NotOurRepoException
slog.hash('error',{'error' => 'Not our repo','repo' => msg.repo}) if sdebug
end
end
client.join
client.close
else
slog.hash('error',{'error' => 'Cannot subscribe to topic','topic' => listen_topic})
end
else
begin
File.open(pidfile, 'w') {|f| f.write(pid) }
rescue Errno::EACCES
slog.hash('error',{'error' => 'Cannot create PID file. Check the permissions and try again.'})
pid = nil
exit
end
Process.detach(pid)
end