Skip to content

Commit

Permalink
Merge pull request #66 from k0kubun/remote_directory-resource
Browse files Browse the repository at this point in the history
Create remote_directory resource
  • Loading branch information
ryotarai committed Dec 24, 2014
2 parents 3670b30 + 4b5f2f1 commit 09a2ca8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/itamae/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,9 @@ def get_command(*args)
def send_file(*args)
Specinfra::Runner.send_file(*args)
end

def send_directory(*args)
Specinfra::Runner.send_directory(*args)
end
end
end
1 change: 1 addition & 0 deletions lib/itamae/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'itamae/resource/base'
require 'itamae/resource/file'
require 'itamae/resource/package'
require 'itamae/resource/remote_directory'
require 'itamae/resource/remote_file'
require 'itamae/resource/directory'
require 'itamae/resource/template'
Expand Down
11 changes: 11 additions & 0 deletions lib/itamae/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ def send_file(src, dst)
backend.send_file(src, dst)
end

def send_directory(src, dst)
Logger.debug "Sending a directory from '#{src}' to '#{dst}'..."
unless ::File.directory?(src)
raise Error, "'#{src}' is not directory."
end
unless ::File.exist?(src)
raise Error, "The directory '#{src}' doesn't exist."
end
backend.send_directory(src, dst)
end

def do_not_run_because_of_only_if?
@only_if_command &&
run_command(@only_if_command, error: false).exit_status != 0
Expand Down
84 changes: 84 additions & 0 deletions lib/itamae/resource/remote_directory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'itamae'

module Itamae
module Resource
class RemoteDirectory < Base
define_attribute :action, default: :create
define_attribute :path, type: String, default_name: true
define_attribute :source, type: String, required: true
define_attribute :mode, type: String
define_attribute :owner, type: String
define_attribute :group, type: String

def pre_action
directory = ::File.expand_path(attributes.source, ::File.dirname(@recipe.path))
src = ::File.expand_path(directory, ::File.dirname(@recipe.path))

@temppath = ::File.join(runner.tmpdir, Time.now.to_f.to_s)
send_directory(src, @temppath)

case @current_action
when :create
attributes.exist = true
end
end

def set_current_attributes
current.exist = run_specinfra(:check_file_is_directory, 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

def show_differences
super

if current.exist
diff = run_command(["diff", "-u", attributes.path, @temppath], error: false)
if diff.exit_status == 0
# no change
Logger.debug "directory 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 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])
updated!
end
else
updated!
end

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

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

0 comments on commit 09a2ca8

Please sign in to comment.