Class: Indocker::Artifacts::Services::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/indocker/artifacts/services/synchronizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger:, progress:) ⇒ Synchronizer

Returns a new instance of Synchronizer.



2
3
4
5
# File 'lib/indocker/artifacts/services/synchronizer.rb', line 2

def initialize(logger:, progress:)
  @logger   = logger
  @progress = progress
end

Instance Method Details

#call(clonner, artifact_servers) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/indocker/artifacts/services/synchronizer.rb', line 7

def call(clonner, artifact_servers)
  @logger.info("Syncing git artifacts")

  remote_operations = []

  artifact_servers.each do |artifact, servers|
    remote_operations += servers.map do |server|
      @progress.start_syncing_artifact(server, artifact)

      thread = Thread.new do
        server.synchronize do
          session = Indocker::SshSession.new(
            host: server.host,
            user: server.user,
            port: server.port,
            logger: @logger
          )

          if artifact.is_git?
            @logger.info("Pulling git artifact  #{artifact.name.to_s.green} for #{server.user}@#{server.host}")
            result = clonner.clone(session, artifact.repository)

            if result.exit_code != 0
              @logger.error("Artifact repository :#{artifact.repository.name} was not clonned")
              @logger.error(result.stderr_data)
              exit 1
            end
          end

          artifact.files.each do |artifact_item|
            source_path = artifact.build_source_path(artifact_item.source_path)

            result = session.exec!("mkdir -p #{artifact_item.target_path}")
            result = session.exec!("cp #{source_path} #{artifact_item.target_path}")

            if !result.success?
              @logger.error(result.stdout_data)
              @logger.error(result.stderr_data)
              exit 1
            end
          end

          @progress.finish_syncing_artifact(server, artifact)
        end
      end

      Indocker::Launchers::DTO::RemoteOperationDTO.new(thread, server, :artifact_sync)
    end
  end

  remote_operations
end