Class: Orca::FileSync

Inherits:
Object
  • Object
show all
Defined in:
lib/orca/extensions/file_sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, config, &blk) ⇒ FileSync

Returns a new instance of FileSync.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/orca/extensions/file_sync.rb', line 10

def initialize(parent, config, &blk)
  @parent = parent
  @config = config
  @after_apply = blk
  raise ArgumentError.new('A file :source  or template must be provided') unless @config[:source] or @config[:template]
  raise ArgumentError.new('A file :destination must be provided') unless @config[:destination]
end

Instance Method Details

#add_content_packageObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/orca/extensions/file_sync.rb', line 70

def add_content_package
  fs = self
  add_package('content') do |package|
    package.command :apply do
      if fs.create_dir(self)
        mk_dir = fs.create_dir(self) == true ? File.dirname(fs.remote_path(self)) : fs.create_dir(self)
        sudo("mkdir -p #{mk_dir}")
        sudo("chown #{fs.user(self)}:#{fs.group(self) || fs.user(self)} #{mk_dir}") if fs.user(self)
      end
      local_file = local(fs.local_path_for_node(self))
      tmp_path = "orca-upload-#{local_file.hash}"
      local_file.copy_to(remote(tmp_path))
      sudo("mv #{tmp_path} #{fs.remote_path(self)}")
      fs.run_after_apply(self)
    end

    package.command :remove do
      remote(fs.remote_path(self)).delete!
    end

    package.command :validate do
      local(fs.local_path_for_node(self)).matches?(remote(fs.remote_path(self)))
    end
  end
end

#add_package(suffix) {|package| ... } ⇒ Object

Yields:

  • (package)


115
116
117
118
119
120
# File 'lib/orca/extensions/file_sync.rb', line 115

def add_package(suffix)
  package = Orca.add_package(package_name(suffix))
  yield(package)
  @parent.triggers(package.name)
  package
end

#add_permissions_packageObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/orca/extensions/file_sync.rb', line 96

def add_permissions_package
  fs = self
  add_package('permissions') do |package|
    package.command :apply do
      remote(fs.remote_path(self)).set_owner(fs.user(self), fs.group(self)) unless fs.user(self).nil? and fs.group(self).nil?
      remote(fs.remote_path(self)).set_permissions(fs.permissions(self)) unless fs.permissions(self).nil?
      fs.run_after_apply(self)
    end

    package.command :validate do
      r_file = remote(fs.remote_path(self))
      valid = r_file.permissions == fs.permissions(self)
      valid = valid && r_file.user == fs.user(self) if fs.user(self)
      valid = valid && r_file.group == fs.group(self) if fs.group(self)
      valid
    end
  end
end

#configureObject



60
61
62
63
64
# File 'lib/orca/extensions/file_sync.rb', line 60

def configure
  fs = self
  add_content_package
  add_permissions_package unless @config[:permissions].nil? and @config[:user].nil? and @config[:group].nil?
end

#create_dir(context) ⇒ Object



47
48
49
# File 'lib/orca/extensions/file_sync.rb', line 47

def create_dir(context)
  value_for context, (@config[:create_dir] || @config[:create_dirs])
end

#group(context) ⇒ Object



43
44
45
# File 'lib/orca/extensions/file_sync.rb', line 43

def group(context)
  value_for context, @config[:group]
end

#local_path(context) ⇒ Object



18
19
20
# File 'lib/orca/extensions/file_sync.rb', line 18

def local_path(context)
  value_for context, @config[:source]
end

#local_path_for_node(context) ⇒ Object



26
27
28
29
# File 'lib/orca/extensions/file_sync.rb', line 26

def local_path_for_node(context)
  return local_path(context) if @config[:source]
  Orca::Template.new(context.node, template_path(context)).render_to_tempfile
end

#package_name(suffix) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/orca/extensions/file_sync.rb', line 51

def package_name(suffix)
  name = @config[:name]
  if name.nil? && !@config[:destination].is_a?(String)
    raise ArgumentError.new("You must provide a :name option unless :destination is a String")
  end
  name ||= @config[:destination]
  "file-#{suffix}[#{name}]"
end

#permissions(context) ⇒ Object



35
36
37
# File 'lib/orca/extensions/file_sync.rb', line 35

def permissions(context)
  value_for context, @config[:permissions]
end

#remote_path(context) ⇒ Object



31
32
33
# File 'lib/orca/extensions/file_sync.rb', line 31

def remote_path(context)
  value_for context, @config[:destination]
end

#run_after_apply(context) ⇒ Object



66
67
68
# File 'lib/orca/extensions/file_sync.rb', line 66

def run_after_apply(context)
  context.instance_eval(&@after_apply) if @after_apply
end

#template_path(context) ⇒ Object



22
23
24
# File 'lib/orca/extensions/file_sync.rb', line 22

def template_path(context)
  value_for context, @config[:template]
end

#user(context) ⇒ Object



39
40
41
# File 'lib/orca/extensions/file_sync.rb', line 39

def user(context)
  value_for context, @config[:user]
end

#value_for(context, option) ⇒ Object



122
123
124
125
126
# File 'lib/orca/extensions/file_sync.rb', line 122

def value_for(context, option)
  return nil if option.nil?
  return context.instance_exec(&option) if option.respond_to?(:call)
  option
end