Class: Autoproj::Sync::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/autoproj/sync/remote.rb

Defined Under Namespace

Classes: FailedRemoteCommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, name: uri, enabled: true) ⇒ Remote

Returns a new instance of Remote.



18
19
20
21
22
# File 'lib/autoproj/sync/remote.rb', line 18

def initialize(uri, name: uri, enabled: true)
    @uri  = uri
    @enabled = enabled
    @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/autoproj/sync/remote.rb', line 9

def name
  @name
end

#uriObject (readonly)

Returns the value of attribute uri.



8
9
10
# File 'lib/autoproj/sync/remote.rb', line 8

def uri
  @uri
end

Class Method Details

.from_uri(uri, name: uri, enabled: true) ⇒ Object



11
12
13
14
15
16
# File 'lib/autoproj/sync/remote.rb', line 11

def self.from_uri(uri, name: uri, enabled: true)
    if uri.scheme != "ssh"
        raise ArgumentError, "unsupported protocol #{uri.scheme}"
    end
    Remote.new(uri, name: name, enabled: enabled)
end

Instance Method Details

#autoproj_annex_files(ws) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/autoproj/sync/remote.rb', line 105

def autoproj_annex_files(ws)
    user_files = %w[env.sh].
        map do |file|
            File.join(ws.root_dir, file)
        end
    autoproj_files = %w[env.yml installation-manifest].
        map do |file|
            File.join(ws.root_dir, '.autoproj', file)
        end
    bundler_files = %w[gems/Gemfile gems/Gemfile.lock].
        map do |file|
            File.join(ws.prefix_dir, file)
        end
    [*user_files, *ws.env.source_before, *ws.env.source_after,
     *autoproj_files, *bundler_files].
       find_all { |file| File.exist?(file) }
end

#bootstrap_or_update_autoproj(sftp, ws) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/autoproj/sync/remote.rb', line 313

def bootstrap_or_update_autoproj(sftp, ws)
    gemfile_lock_path = File.join(ws.root_dir, ".autoproj/Gemfile.lock")
    if remote_file_exist?(sftp, gemfile_lock_path)
        remote_gemfile_lock = remote_file_get(sftp, gemfile_lock_path)
        local_gemfile_lock  = local_file_get(gemfile_lock_path)
        if remote_gemfile_lock == local_gemfile_lock
            info "remote Autoproj install up-to-date"
            info "sync: Autoproj install up-to-date on #{name}"
            return
        end

        info "sync: updating the Autoproj install on #{name}"

        remote_file_put(sftp, gemfile_lock_path, local_gemfile_lock)
        remote_file_transfer(
            sftp, File.join(ws.root_dir, ".autoproj/Gemfile"))
        remote_file_transfer(
            sftp, File.join(ws.root_dir, ".autoproj/config.yml"))
        result = remote_autoproj(
            sftp, ws.root_dir, "update", "--autoproj")
        unless result[:exit_code] == 0
            raise FailedRemoteCommand, "failed to update Autoproj:\n"\
                "autoproj update --autoproj finished with exit status "\
                "#{result[:exit_code]}\n"\
                "#{result}"
        end
    else
        info "sync: installing Autoproj on #{name}"

        autoproj_spec = Bundler.definition.specs.
            find { |spec| spec.name == "autoproj" }
        autoproj_dir = autoproj_spec.full_gem_path
        install_script = File.join(autoproj_dir, "bin", "autoproj_install")
        remote_mkdir_p(sftp, ws.root_dir)
        sftp.upload!(install_script,
            remote_path(File.join(ws.root_dir, "autoproj_install")))
        remote_file_transfer(sftp, File.join(ws.root_dir, ".autoproj/config.yml"),
            target: remote_path(File.join(ws.root_dir, 'bootstrap-config.yml')))
        remote_file_transfer(sftp, File.join(ws.root_dir, ".autoproj/Gemfile"),
            target: remote_path(File.join(ws.root_dir, 'bootstrap-Gemfile')))
        result = sftp.session.exec!("cd '#{remote_path(ws.root_dir)}' && "\
            "#{ws.config.ruby_executable} autoproj_install "\
            "--gemfile bootstrap-Gemfile "\
            "--seed-config bootstrap-config.yml")
        if result.exitstatus != 0
            raise RuntimeError, "failed to install autoproj: #{result}"
        end
    end
end

#create_package_directories(sftp, pkg) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/autoproj/sync/remote.rb', line 189

def create_package_directories(sftp, pkg)
    Autobuild.progress_start pkg, "sync: preparing #{pkg.name}@#{name}",
        done_message: "sync: prepared #{pkg.name}@#{name}" do

        remote_mkdir_p(sftp, pkg.autobuild.prefix)
        remote_mkdir_p(sftp, File.dirname(pkg.autobuild.installstamp))
    end
end

#each_outdated_package(sftp, ws, packages) {|sftp, an| ... } ⇒ Object

Enumerate the packages that are outdated on the remote

Yield Parameters:

  • sftp (Net::SFTP::Session)

    the opened SFTP session, that can be used to do further operations on the remote

  • an (Autoproj::PackageDescription)

    outdated package



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/autoproj/sync/remote.rb', line 45

def each_outdated_package(sftp, ws, packages)
    return enum_for(__method__, sftp, ws, packages) unless block_given?

    stat = packages.map do |package|
        autobuild    = package.autobuild
        installstamp = autobuild.installstamp
        next unless File.exist?(installstamp)

        local_stat  = File.stat(installstamp)
        remote_path = File.join(uri.path, installstamp)
        begin
            remote_stat = sftp.file.open(remote_path) do |f|
                f.stat
            end
            [package, local_stat, remote_stat, remote_path]
        rescue Net::SFTP::StatusException => e
            if e.code != Net::SFTP::Constants::StatusCodes::FX_NO_SUCH_FILE
                raise
            end
            package
        end
    end

    stat.compact.map do |package, local_stat, remote_stat, remote_path|
        yield(package) if !local_stat ||
            changed_stat?(local_stat, remote_stat)
    end.compact
end

#enabled?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/autoproj/sync/remote.rb', line 24

def enabled?
    @enabled
end

#info(message) ⇒ Object



309
310
311
# File 'lib/autoproj/sync/remote.rb', line 309

def info(message)
    Autoproj.message "  #{message}", force: true
end

#install_osdep_packages(sftp, ws, manager_name, packages) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/autoproj/sync/remote.rb', line 170

def install_osdep_packages(sftp, ws, manager_name, packages)
    Autobuild.progress_start "sync-#{name}-osdeps-#{manager_name}",
        "sync: handling #{packages.size} osdeps #{manager_name} packages "\
            "on #{name}",
        done_message: "sync: handled #{packages.size} "\
            "osdeps #{manager_name} packages on #{name}" do

        result = remote_autoproj(sftp, ws.root_dir,
            "sync", "install-osdeps",
            manager_name, *packages)
        if result[:exit_code] != 0
            raise RuntimeError,
                "remote autoproj command failed\n"\
                "autoproj exited with status "\
                "#{result[:exit_code]}\n#{result}"
        end
    end
end

#local_file_get(local_path) ⇒ Object



305
306
307
# File 'lib/autoproj/sync/remote.rb', line 305

def local_file_get(local_path)
    File.read(local_path)
end

#osdeps(sftp, ws, osdep_packages) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/autoproj/sync/remote.rb', line 145

def osdeps(sftp, ws, osdep_packages)
    installer = ws.os_package_installer

    installer.setup_package_managers
    all = ws.all_os_packages
    partitioned_packages = installer.
        resolve_and_partition_osdep_packages(osdep_packages, all)

    os_packages = partitioned_packages.delete(installer.os_package_manager)
    if os_packages
        partitioned_packages = [[installer.os_package_manager, os_packages]].
            concat(partitioned_packages.to_a)
    end

    partitioned_packages = partitioned_packages.map do |manager, packages|
        manager_name, _ = installer.package_managers.
            find { |key, obj| manager == obj }
        [manager_name, packages]
    end

    partitioned_packages.each do |manager_name, packages|
        install_osdep_packages(sftp, ws, manager_name, packages)
    end
end

#remote_autoproj(sftp, root_dir, *command, chdir: nil, interactive: false) ⇒ Object



238
239
240
241
242
# File 'lib/autoproj/sync/remote.rb', line 238

def remote_autoproj(sftp, root_dir, *command, chdir: nil, interactive: false)
    remote_exec(sftp,
        remote_path(File.join(root_dir, ".autoproj/bin/autoproj")),
        *command, chdir: chdir, interactive: interactive)
end

#remote_exec(sftp, *command, chdir: nil, interactive: false) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/autoproj/sync/remote.rb', line 244

def remote_exec(sftp, *command, chdir: nil, interactive: false)
    if interactive
        remote_interactive_exec(sftp, *command, chdir: chdir)
    else
        status = Hash.new
        ios = Hash[:stdout => STDOUT, :stderr => STDOUT]
        target_dir = @uri.path
        target_dir = File.join(target_dir, chdir) if chdir
        pid = nil
        command = "cd '#{target_dir}' && "\
            "echo \"AUTOPROJ_SYNC_PID=$$\" && "\
            "exec '" + command.join("' '") + "'"
        ch = sftp.session.exec(command, status: status) do |channel, stream, data|
            if !pid && (m = /^AUTOPROJ_SYNC_PID=(\d+)/.match(data))
                pid = Integer(m[1])
            else
                ios[stream].print(data)
                ios[stream].flush
            end
        end

        begin
            ch.wait
            status
        rescue Interrupt
            sftp.session.exec!("kill #{pid}") if pid
            ch.wait
            raise
        end
    end
end

#remote_file_exist?(sftp, path) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
223
224
# File 'lib/autoproj/sync/remote.rb', line 215

def remote_file_exist?(sftp, path)
    sftp.stat!(remote_path(path))
    true
rescue Net::SFTP::StatusException => e
    if e.code == Net::SFTP::Constants::StatusCodes::FX_NO_SUCH_FILE
        false
    else
        raise
    end
end

#remote_file_get(sftp, local_path) ⇒ Object



226
227
228
# File 'lib/autoproj/sync/remote.rb', line 226

def remote_file_get(sftp, local_path)
    sftp.download!(remote_path(local_path))
end

#remote_file_put(sftp, local_path, content) ⇒ Object



230
231
232
# File 'lib/autoproj/sync/remote.rb', line 230

def remote_file_put(sftp, local_path, content)
    sftp.upload!(StringIO.new(content), remote_path(local_path))
end

#remote_file_transfer(sftp, local_path, target: remote_path(local_path)) ⇒ Object



234
235
236
# File 'lib/autoproj/sync/remote.rb', line 234

def remote_file_transfer(sftp, local_path, target: remote_path(local_path))
    sftp.upload!(local_path, target)
end

#remote_interactive_exec(sftp, *command, chdir: nil) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/autoproj/sync/remote.rb', line 276

def remote_interactive_exec(sftp, *command, chdir: nil)
    channel = sftp.session.open_channel do |ch|
        ch.on_data do |ch, data|
            STDOUT.print data
            STDOUT.flush
        end
        ch.on_extended_data do |ch, type, data|
            STDERR.print data
        end

        ch.request_pty
        ch.exec("cd '#{chdir}' && '" + command.join("' '") + "'")
    end

    ssh = sftp.session
    while channel.active?
        ssh.process(0.1)
        begin
            while true
                data = STDIN.read_nonblock(4096)
                channel.send_data(data)
            end
        rescue IO::WaitReadable
        end
    end
rescue EOFError
    channel.close
end

#remote_path(local_path) ⇒ Object



28
29
30
# File 'lib/autoproj/sync/remote.rb', line 28

def remote_path
    @uri.path
end

#rsync_dir(sftp, local_dir) ⇒ Object



133
134
135
136
137
# File 'lib/autoproj/sync/remote.rb', line 133

def rsync_dir(sftp, local_dir)
    remote_dir = remote_path(local_dir)
    ["rsync", "-a", "--delete-after", "#{local_dir}/",
        "#{rsync_target}:#{remote_dir}/"]
end

#rsync_file(sftp, local_file) ⇒ Object



139
140
141
142
143
# File 'lib/autoproj/sync/remote.rb', line 139

def rsync_file(sftp, local_file)
    remote_file = remote_path(local_file)
    ["rsync", "-a", local_file,
        "#{rsync_target}:#{remote_file}"]
end

#rsync_package(sftp, pkg) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/autoproj/sync/remote.rb', line 198

def rsync_package(sftp, pkg)
    Autobuild.progress_start pkg, "sync: updating #{pkg.name}@#{name}",
        done_message: "sync: updated #{pkg.name}@#{name}" do
        ops = [rsync_dir(sftp, pkg.autobuild.prefix),
            rsync_file(sftp, pkg.autobuild.installstamp)]
        ops.each do |op|
            if !system(*op)
                raise "update of #{pkg.name} failed"
            end
        end
    end
end

#rsync_targetObject



123
124
125
126
127
128
129
130
131
# File 'lib/autoproj/sync/remote.rb', line 123

def rsync_target
    if @uri.user && @uri.password
        "#{@uri.user}:#{@uri.password}@#{@uri.host}"
    elsif @uri.user
        "#{@uri.user}@#{@uri.host}"
    else
        @uri.host
    end
end

#startObject



32
33
34
35
36
37
38
# File 'lib/autoproj/sync/remote.rb', line 32

def start
    result = nil
    Net::SFTP.start(@uri.host, @uri.user, password: @uri.password) do |sftp|
        result = yield(sftp)
    end
    result
end

#update(sftp, ws, packages) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/autoproj/sync/remote.rb', line 363

def update(sftp, ws, packages)
    # First check if autoproj is bootstrapped on the target already
    bootstrap_or_update_autoproj(sftp, ws)

    packages = each_outdated_package(sftp, ws, packages).to_a

    info "sync: #{packages.size} outdated packages on #{name}"

    executor = Concurrent::FixedThreadPool.new(6)
    futures = packages.map do |pkg|
        create_package_directories(sftp, pkg)
        Concurrent::Future.execute(executor: executor) do
            rsync_package(sftp, pkg)
        end
    end

    # Copy some autoproj installation-manifest files
    Autobuild.progress_start "sync-#{name}-autoproj",
        "sync: updating Autoproj configuration files on #{name}",
        done_message: "sync: updated Autoproj configuration files on #{name}" do
        autoproj_annex_files(ws).each do |file|
            missing = []
            dir = File.join(@uri.path, File.dirname(file))
            while dir != '/'
                begin
                    sftp.stat!(dir)
                    break
                rescue Net::SFTP::StatusException
                    missing.unshift dir
                    dir = File.dirname(dir)
                end
            end

            missing.each do |dir|
                sftp.mkdir!(dir)
            end

            sftp.upload!(file, File.join(@uri.path, file))
        end
    end

    futures.each_with_index do |f, i|
        f.value!
    end

ensure
    if executor
        executor.shutdown
        executor.wait_for_termination
    end
end