Class: VagrantPlugins::Unify::UnifyHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-unify/helper.rb

Class Method Summary collapse

Class Method Details

.init_excludes(opts) ⇒ Object



53
54
55
56
57
58
# File 'lib/vagrant-unify/helper.rb', line 53

def self.init_excludes(opts)
  excludes = ['.vagrant/', '.DS_Store']
  excludes += Array(opts[:exclude]).map(&:to_s) if opts[:exclude]
  excludes.uniq!
  return excludes
end

.init_ops(machine, local_path, remote_path, opts) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/vagrant-unify/helper.rb', line 8

def self.init_ops(machine, local_path, remote_path, opts)
  opts[:hostpath] ||= local_path
  opts[:guestpath] ||= remote_path
  opts[:owner] ||= machine.ssh_info[:username]
  opts[:group] ||= machine.ssh_info[:username]
  return opts
end

.init_paths(machine, local_path, remote_path, cygwinify = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-unify/helper.rb', line 16

def self.init_paths(machine, local_path, remote_path, cygwinify = false)
  guestpath = remote_path
  hostpath  = File.expand_path(local_path, machine.env.root_path)
  hostpath  = Vagrant::Util::Platform.fs_real_path(hostpath).to_s

  if cygwinify and Vagrant::Util::Platform.windows?
    hostpath = Vagrant::Util::Platform.cygwin_path(hostpath)
  end

  if !guestpath.end_with?("/")
    guestpath += "/"
  end

  if !hostpath.end_with?("/")
    hostpath += "/"
  end

  return [hostpath, guestpath]
end

.init_ssh_args(machine) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant-unify/helper.rb', line 36

def self.init_ssh_args(machine)
  ssh_info = machine.ssh_info
  proxy_command = ""

  if ssh_info[:proxy_command]
    proxy_command = "-o ProxyCommand='#{ssh_info[:proxy_command]}' "
  end

  return [
    "-p #{ssh_info[:port]} " +
    proxy_command +
    "-o StrictHostKeyChecking=no " +
    "-o UserKnownHostsFile=/dev/null",
    ssh_info[:private_key_path].map { |p| "-i '#{p}'" },
  ].flatten.join(" ")
end

.post_sync(machine, opts) ⇒ Object



71
72
73
74
75
# File 'lib/vagrant-unify/helper.rb', line 71

def self.post_sync(machine, opts)
  if machine.guest.capability?(:rsync_post)
    machine.guest.capability(:rsync_post, opts)
  end
end

.pre_sync(machine, hostpath, guestpath, opts) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant-unify/helper.rb', line 60

def self.pre_sync(machine, hostpath, guestpath, opts)
  if machine.guest.capability?(:rsync_pre)
    machine.guest.capability(:rsync_pre, opts)
  else
    machine.communicate.sudo("mkdir -p '#{guestpath}'")
    machine.communicate.sudo("chown #{machine.ssh_info[:username]} '#{guestpath}'")
  end

  FileUtils.mkdir_p(hostpath)
end

.rsync_single(direction, machine, local_path, remote_path, opts) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/vagrant-unify/helper.rb', line 124

def self.rsync_single(direction, machine, local_path, remote_path, opts)
  opts = init_ops(machine, local_path, remote_path, opts)
  hostpath, guestpath = init_paths(machine, local_path, remote_path, true)
  ssh_args = init_ssh_args(machine)
  excludes = init_excludes(opts)

  args = nil
  args = Array(opts[:args]).dup if opts[:args]
  args ||= ["--verbose", "--archive", "--delete", "-z", "--copy-links"]

  if Vagrant::Util::Platform.windows? && !args.any? { |arg| arg.start_with?("--chmod=") }
    args << "--chmod=ugo=rwX"

    args << "--no-perms" if args.include?("--archive") || args.include?("-a")
  end

  args << "--no-owner" unless args.include?("--owner") || args.include?("-o")
  args << "--no-group" unless args.include?("--group") || args.include?("-g")

  if machine.guest.capability?(:rsync_command)
    args << "--rsync-path"<< machine.guest.capability(:rsync_command)
  end

  guest_spec = "#{machine.ssh_info[:username]}@#{machine.ssh_info[:host]}:#{guestpath}"
  from_path = hostpath
  to_path = guest_spec

  if :pull.equal?(direction)
    from_path = guest_spec
    to_path = hostpath
  end

  command = [
    "rsync",
    args,
    "-e", "ssh " + ssh_args,
    excludes.map { |e| ["--exclude", e] },
    from_path,
    to_path,
  ].flatten

  command_opts = {}
  command_opts[:workdir] = machine.env.root_path.to_s

  command = command + [command_opts]

  if :pull.equal?(direction)
    machine.ui.info(I18n.t(
      "vagrant_unify.pull.message", guestpath: guestpath, hostpath: hostpath))
  else
    machine.ui.info(I18n.t(
      "vagrant_unify.push.message", guestpath: guestpath, hostpath: hostpath))
  end
  if excludes.length > 1
    machine.ui.info(I18n.t(
      "vagrant_unify.excludes", excludes: excludes.inspect))
  end

  pre_sync(machine, hostpath, guestpath, opts)

  r = Vagrant::Util::Subprocess.execute(*command)
  if r.exit_code != 0
    raise Vagrant::Errors::UnifyError,
      command: command.inspect,
      guestpath: guestpath,
      hostpath: hostpath,
      stderr: r.stderr
  end

  post_sync(machine, opts)
end

.unison_single(machine, local_path, remote_path, opts) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vagrant-unify/helper.rb', line 77

def self.unison_single(machine, local_path, remote_path, opts)
  opts = init_ops(machine, local_path, remote_path, opts)
  hostpath, guestpath = init_paths(machine, local_path, remote_path)
  ssh_args = init_ssh_args(machine)
  excludes = init_excludes(opts)

  command = [
    "unison", "-batch",
    "-ignore=Name {" + excludes.join(",")  + "}",
    "-sshargs", ssh_args,
    hostpath,
    "ssh://#{machine.ssh_info[:username]}@#{machine.ssh_info[:host]}/#{guestpath}"
  ]

  command_opts = {}
  command_opts[:workdir] = machine.env.root_path.to_s

  command = command + [command_opts]

  machine.ui.info(I18n.t(
    "vagrant_unify.sync.message", guestpath: guestpath, hostpath: hostpath))
  if excludes.length > 1
    machine.ui.info(I18n.t(
      "vagrant_unify.excludes", excludes: excludes.inspect))
  end

  pre_sync(machine, hostpath, guestpath, opts)

  r = Vagrant::Util::Subprocess.execute(*command)
  case r.exit_code
  when 0
    machine.ui.info(I18n.t("vagrant_unify.unison.success_0"))
  when 1
    machine.ui.info(I18n.t("vagrant_unify.unison.success_1"))
  when 2
    machine.ui.info(I18n.t("vagrant_unify.unison.success_2"))
  else
    raise Vagrant::Errors::UnifyError,
      :command => command.inspect,
      :guestpath => guestpath,
      :hostpath => hostpath,
      :stderr => r.stderr
  end

  post_sync(machine, opts)
end