class ItamaeMitsurin::Resource::File

Public Instance Methods

action_create(options) click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 60
def action_create(options)
  if !current.exist && !@temppath
    run_command(["touch", attributes.path])
  end

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

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

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

  if attributes.modified
    run_specinfra(:move_file, @temppath, attributes.path)
  end
end
action_delete(options) click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 80
def action_delete(options)
  if run_specinfra(:check_file_is_file, attributes.path)
    run_specinfra(:remove_file, attributes.path)
  end
end
action_edit(options) click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 86
def action_edit(options)
  change_target = attributes.modified ? @temppath : attributes.path

  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.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.modified
    run_specinfra(:move_file, @temppath, attributes.path)
  end
end
pre_action() click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 14
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

  send_tempfile
  compare_file
end
set_current_attributes() click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 36
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_differences() click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 49
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

Private Instance Methods

compare_file() click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 115
def compare_file
  attributes.modified = false
  unless @temppath
    return
  end

  case run_command(["diff", "-q", compare_to, @temppath], error: false).exit_status
  when 1
    # diff found
    attributes.modified = true
  when 2
    # error
    raise ItamaeMitsurin::Backend::CommandExecutionError, "diff command exited with 2"
  end
end
compare_to() click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 107
def compare_to
  if current.exist
    attributes.path
  else
    '/dev/null'
  end
end
content_file() click to toggle source

will be overridden

# File lib/itamae-mitsurin/resource/file.rb, line 155
def content_file
  nil
end
send_tempfile() click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 159
def send_tempfile
  if !attributes.content && !content_file
    @temppath = nil
    return
  end

  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)

    run_command(["touch", @temppath])
    run_specinfra(:change_file_mode, @temppath, '0600')
    backend.send_file(src, @temppath)
    run_specinfra(:change_file_mode, @temppath, '0600')
  ensure
    f.unlink if f
  end
end
show_content_diff() click to toggle source
# File lib/itamae-mitsurin/resource/file.rb, line 131
def show_content_diff
  if attributes.modified
    ItamaeMitsurin.logger.info "diff:"
    diff = run_command(["diff", "-u", compare_to, @temppath], error: false)
    diff.stdout.each_line do |line|
      color = if line.start_with?('+')
                :green
              elsif line.start_with?('-')
                :red
              else
                :clear
              end
      ItamaeMitsurin.logger.color(color) do
        ItamaeMitsurin.logger.info line.chomp
      end
    end
    runner.handler.event(:file_content_changed, diff: diff.stdout)
  else
    # no change
    ItamaeMitsurin.logger.debug "file content will not change"
  end
end