Class: Itamae::Resource::File

Inherits:
Base
  • Object
show all
Defined in:
lib/itamae/resource/file.rb

Direct Known Subclasses

RemoteFile

Instance Attribute Summary

Attributes inherited from Base

#attributes, #current_attributes, #notifications, #recipe, #resource_name, #subscriptions, #updated

Instance Method Summary collapse

Methods inherited from Base

#action_nothing, define_attribute, inherited, #initialize, #resource_type, #run

Constructor Details

This class inherits a constructor from Itamae::Resource::Base

Instance Method Details

#action_create(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/itamae/resource/file.rb', line 61

def action_create(options)
  if attributes.mode
    run_specinfra(:change_file_mode, @temppath, attributes.mode)
  end
  if attributes.owner || attributes.group
    run_specinfra(:change_file_owner, @temppath, attributes.owner, attributes.group)
  end

  if run_specinfra(:check_file_is_file, attributes.path)
    unless check_command(["diff", "-q", @temppath, attributes.path])
      # the file is modified
      updated!
    end
  else
    # new file
    updated!
  end

  run_specinfra(:move_file, @temppath, attributes.path)
end

#action_delete(options) ⇒ Object



82
83
84
85
86
# File 'lib/itamae/resource/file.rb', line 82

def action_delete(options)
  if run_specinfra(:check_file_is_file, attributes.path)
    run_specinfra(:remove_file, attributes.path)
  end
end

#content_fileObject

will be overridden



111
112
113
# File 'lib/itamae/resource/file.rb', line 111

def content_file
  nil
end

#pre_actionObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/itamae/resource/file.rb', line 13

def pre_action
  begin
    src = if content_file
            content_file
          else
            f = Tempfile.open('itamae')
            f.write(attributes.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

#set_current_attributesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/itamae/resource/file.rb', line 36

def set_current_attributes
  current.exist = run_specinfra(:check_file_is_file, attributes.path)

  if current.exist
    current.mode = run_specinfra(:get_file_mode, attributes.path).stdout.chomp
    current.owner = run_specinfra(:get_file_owner_user, attributes.path).stdout.chomp
    current.group = run_specinfra(:get_file_owner_group, attributes.path).stdout.chomp
  else
    current.mode = nil
    current.owner = nil
    current.group = nil
  end
end

#show_differencesObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/itamae/resource/file.rb', line 50

def show_differences
  current.mode    = current.mode.rjust(4, '0') if current.mode
  attributes.mode = attributes.mode.rjust(4, '0') if attributes.mode

  super

  if current.exist
    show_file_diff
  end
end

#show_file_diffObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/itamae/resource/file.rb', line 88

def show_file_diff
  diff = run_command(["diff", "-u", attributes.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|
      color = if line.start_with?('+')
                :green
              elsif line.start_with?('-')
                :red
              else
                :clear
              end
      Logger.formatter.color(color) do
        Logger.info line.chomp
      end
    end
  end
end