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
|
require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) do
desc "Supports Perforce depots"
has_features :filesystem_types, :reference_tracking, :p4config
def create
# create or update client
create_client(client_name)
# if source provided, sync client
source = @resource.value(:source)
if source
revision = @resource.value(:revision)
sync_client(source, revision)
end
update_owner
end
def working_copy_exists?
# Check if the server is there, or raise error
p4(['info'], {:marshal => false})
# Check if workspace is setup
args = ['where']
args.push(@resource.value(:path) + "...")
hash = p4(args, {:raise => false})
return (hash['code'] != "error")
end
def exists?
working_copy_exists?
end
def destroy
args = ['client']
args.push('-d', '-f')
args.push(client_name)
p4(args)
FileUtils.rm_rf(@resource.value(:path))
end
def latest?
rev = self.revision
if rev
(rev >= self.latest)
else
true
end
end
def latest
args = ['changes']
args.push('-m1', @resource.value(:source))
hash = p4(args)
return hash['change'].to_i
end
def revision
args = ['cstat']
args.push(@resource.value(:source))
hash = p4(args, {:marshal => false})
hash = marshal_cstat(hash)
revision = 0
if hash && hash['code'] != 'error'
hash['data'].each do |c|
if c['status'] == 'have'
change = c['change'].to_i
revision = change if change > revision
end
end
end
return revision
end
def revision=(desired)
sync_client(@resource.value(:source), desired)
update_owner
end
private
def update_owner
if @resource.value(:owner) or @resource.value(:group)
set_ownership
end
end
# Sync the client workspace files to head or specified revision.
# Params:
# +source+:: Depot path to sync
# +revision+:: Perforce change list to sync to (optional)
def sync_client(source, revision)
Puppet.debug "Syncing: #{source}"
args = ['sync']
if revision
args.push(source + "@#{revision}")
else
args.push(source)
end
p4(args)
end
# Returns the name of the Perforce client workspace
def client_name
p4config = @resource.value(:p4config)
# default (generated) client name
path = @resource.value(:path)
host = Facter.value('hostname')
default = "puppet-" + Digest::MD5.hexdigest(path + host)
# check config for client name
set_client = nil
if p4config && File.file?(p4config)
open(p4config) do |f|
m = f.grep(/^P4CLIENT=/).pop
p = /^P4CLIENT=(.*)$/
set_client = p.match(m)[1] if m
end
end
return set_client || ENV['P4CLIENT'] || default
end
# Create (or update) a client workspace spec.
# If a client name is not provided then a hash based on the path is used.
# Params:
# +client+:: Name of client workspace
# +path+:: The Root location of the Perforce client workspace
def create_client(client)
Puppet.debug "Creating client: #{client}"
# fetch client spec
hash = parse_client(client)
hash['Root'] = @resource.value(:path)
hash['Description'] = "Generated by Puppet VCSrepo"
# check is source is a Stream
source = @resource.value(:source)
if source
parts = source.split(/\//)
if parts && parts.length >= 4
source = "//" + parts[2] + "/" + parts[3]
streams = p4(['streams', source], {:raise => false})
if streams['code'] == "stat"
hash['Stream'] = streams['Stream']
notice "Streams" + streams['Stream'].inspect
end
end
end
# save client spec
save_client(hash)
end
# Fetches a client workspace spec from Perforce and returns a hash map representation.
# Params:
# +client+:: name of the client workspace
def parse_client(client)
args = ['client']
args.push('-o', client)
hash = p4(args)
return hash
end
# Saves the client workspace spec from the given hash
# Params:
# +hash+:: hash map of client spec
def save_client(hash)
spec = String.new
view = "\nView:\n"
hash.keys.sort.each do |k|
v = hash[k]
next if( k == "code" )
if(k.to_s =~ /View/ )
view += "\t#{v}\n"
else
spec += "#{k.to_s}: #{v.to_s}\n"
end
end
spec += view
args = ['client']
args.push('-i')
p4(args, {:input => spec, :marshal => false})
end
# Sets Perforce Configuration environment.
# P4CLIENT generated, but overwitten if defined in config.
def config
p4config = @resource.value(:p4config)
cfg = Hash.new
cfg.store 'P4CONFIG', p4config if p4config
cfg.store 'P4CLIENT', client_name
return cfg
end
def p4(args, options = {})
# Merge custom options with defaults
opts = {
:raise => true, # Raise errors
:marshal => true, # Marshal output
}.merge(options)
cmd = ['p4']
cmd.push '-R' if opts[:marshal]
cmd.push args
cmd_str = cmd.respond_to?(:join) ? cmd.join(' ') : cmd
Puppet.debug "environment: #{config}"
Puppet.debug "command: #{cmd_str}"
hash = Hash.new
Open3.popen3(config, cmd_str) do |i, o, e, t|
# Send input stream if provided
if(opts[:input])
Puppet.debug "input:\n" + opts[:input]
i.write opts[:input]
i.close
end
if(opts[:marshal])
hash = Marshal.load(o)
else
hash['data'] = o.read
end
# Raise errors, Perforce or Exec
if(opts[:raise] && !e.eof && t.value != 0)
raise Puppet::Error, "\nP4: #{e.read}"
end
if(opts[:raise] && hash['code'] == 'error' && t.value != 0)
raise Puppet::Error, "\nP4: #{hash['data']}"
end
end
Puppet.debug "hash: #{hash}\n"
return hash
end
# helper method as cstat does not Marshal
def marshal_cstat(hash)
data = hash['data']
code = 'error'
list = Array.new
change = Hash.new
data.each_line do |l|
p = /^\.\.\. (.*) (.*)$/
m = p.match(l)
if m
change[m[1]] = m[2]
if m[1] == 'status'
code = 'stat'
list.push change
change = Hash.new
end
end
end
hash = Hash.new
hash.store 'code', code
hash.store 'data', list
return hash
end
end
|