/
file.rb
100 lines (86 loc) · 2.73 KB
/
file.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
require 'itamae'
module Itamae
module Resource
class File < Base
define_attribute :action, default: :create
define_attribute :path, type: String, default_name: true
define_attribute :content, type: String, default: ''
define_attribute :content_file, type: String
define_attribute :mode, type: String
define_attribute :owner, type: String
define_attribute :group, type: String
def pre_action
begin
src = if content_file
content_file
else
f = Tempfile.open('itamae')
f.write(content)
f.close
f.path
end
@temppath = ::File.join(runner.tmpdir, Time.now.to_f.to_s)
send_file(src, @temppath)
ensure
f.unlink if f
end
case @current_action
when :create
@attributes[:exist?] = true
end
end
def set_current_attributes
exist = run_specinfra(:check_file_is_file, path)
@current_attributes[:exist?] = exist
if exist
@current_attributes[:mode] = run_specinfra(:get_file_mode, path).stdout.chomp
@current_attributes[:owner] = run_specinfra(:get_file_owner_user, path).stdout.chomp
@current_attributes[:group] = run_specinfra(:get_file_owner_group, path).stdout.chomp
else
@current_attributes[:mode] = nil
@current_attributes[:owner] = nil
@current_attributes[:group] = nil
end
end
def show_differences
super
if @current_attributes[:exist?]
diff = run_command(["diff", "-u", path, @temppath], error: false)
if diff.exit_status == 0
# no change
Logger.debug "file content will not change"
else
Logger.info "diff:"
diff.stdout.each_line do |line|
Logger.info "#{line.strip}"
end
end
end
end
def action_create(options)
if mode
run_specinfra(:change_file_mode, @temppath, mode)
end
if owner || group
run_specinfra(:change_file_owner, @temppath, owner, group)
end
if run_specinfra(:check_file_is_file, path)
run_specinfra(:copy_file, path, "#{path}.bak")
unless check_command(["diff", "-q", @temppath, path])
# the file is modified
updated!
end
else
# new file
updated!
end
run_specinfra(:move_file, @temppath, path)
end
def action_delete(options)
if run_specinfra(:check_file_is_file, path)
run_specinfra(:remove_file, path)
end
end
end
end
end