Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create remote_directory resource #66

Merged
merged 1 commit into from
Dec 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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