Class: Itamae::Resource::File

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

Direct Known Subclasses

HttpRequest, RemoteFile

Class Attribute Summary collapse

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, #do_not_run_because_of_not_if?, #do_not_run_because_of_only_if?, inherited, #initialize, #resource_type, #run

Constructor Details

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

Class Attribute Details

.sha256sum_availableObject

Returns the value of attribute sha256sum_available.



14
15
16
# File 'lib/itamae/resource/file.rb', line 14

def sha256sum_available
  @sha256sum_available
end

Instance Method Details

#action_create(options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/itamae/resource/file.rb', line 68

def action_create(options)
  if !current.exist && !@temppath
    run_command(["touch", attributes.path])
  end

  change_target = attributes.modified ? @temppath : attributes.path

  if attributes.owner || attributes.group
    run_specinfra(:change_file_owner, change_target, attributes.owner, attributes.group)
  end

  if attributes.mode
    run_specinfra(:change_file_mode, change_target, attributes.mode)
  end

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

#action_delete(options) ⇒ Object



88
89
90
91
92
# File 'lib/itamae/resource/file.rb', line 88

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

#action_edit(options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/itamae/resource/file.rb', line 94

def action_edit(options)
  change_target = attributes.modified ? @temppath : attributes.path

  if attributes.owner || attributes.group || attributes.modified
    owner = attributes.owner || run_specinfra(:get_file_owner_user, attributes.path).stdout.chomp
    group = attributes.group || run_specinfra(:get_file_owner_group, attributes.path).stdout.chomp
    run_specinfra(:change_file_owner, change_target, owner, group)
  end

  if attributes.mode || attributes.modified
    mode = attributes.mode || run_specinfra(:get_file_mode, attributes.path).stdout.chomp
    run_specinfra(:change_file_mode, change_target, mode)
  end

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

#pre_actionObject



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
# File 'lib/itamae/resource/file.rb', line 17

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

  case @current_action
  when :create
    attributes.exist = true
  when :delete
    attributes.exist = false
  when :edit
    attributes.exist = true

    if !runner.dry_run? || current.exist
      content = backend.receive_file(attributes.path)
      attributes.block.call(content)
      attributes.content = content
    end
  end

  if exists_and_not_modified?
    attributes.modified = false
    return
  end

  send_tempfile
  compare_file
end

#set_current_attributesObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/itamae/resource/file.rb', line 44

def set_current_attributes
  current.modified = false
  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



57
58
59
60
61
62
63
64
65
66
# File 'lib/itamae/resource/file.rb', line 57

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 @temppath && @current_action != :delete
    show_content_diff
  end
end