Class: Packagecloud::Maven::Importer::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/packagecloud/maven/importer/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, repository:, api_token:, scheme:, port:, hostname:, database_path:, maven_repository_path:) ⇒ Main

Returns a new instance of Main.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/packagecloud/maven/importer/main.rb', line 11

def initialize(username:,
               repository:,
               api_token:,
               scheme:,
               port:,
               hostname:,
               database_path:,
               maven_repository_path:)

  self.username = username
  self.scheme = scheme
  self.port = port
  self.hostname = hostname
  self.repository = repository
  self.api_token = api_token
  self.maven_repository_path = maven_repository_path
  self.database = Packagecloud::Maven::Importer::Database.new(path: database_path)
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def api_token
  @api_token
end

#databaseObject

Returns the value of attribute database.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def database
  @database
end

#hostnameObject

Returns the value of attribute hostname.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def hostname
  @hostname
end

#maven_repository_pathObject

Returns the value of attribute maven_repository_path.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def maven_repository_path
  @maven_repository_path
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def port
  @port
end

#repositoryObject

Returns the value of attribute repository.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def repository
  @repository
end

#schemeObject

Returns the value of attribute scheme.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def scheme
  @scheme
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/packagecloud/maven/importer/main.rb', line 9

def username
  @username
end

Instance Method Details

#connectionObject



44
45
46
# File 'lib/packagecloud/maven/importer/main.rb', line 44

def connection
  @connection ||= Excon.new("#{scheme}://#{api_token}:@#{hostname}:#{port}", :persistent => true)
end

#parse_artifact(full_path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/packagecloud/maven/importer/main.rb', line 30

def parse_artifact(full_path)
  base_path = full_path.gsub(maven_repository_path, '')

  sanitized_base_path = ::File.join('/', base_path.gsub(/\\+/, '/'))
  result = nil
  Packagecloud::Maven::Importer::PATTERNS.each do |pattern, artifact_type|
    result = pattern.params(sanitized_base_path)
    if result
      return { full_path: full_path, base_path: sanitized_base_path }
    end
  end
  nil
end

#run!(yes: false, force: false) ⇒ Object



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
73
74
75
76
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
123
124
125
126
127
128
129
130
131
132
# File 'lib/packagecloud/maven/importer/main.rb', line 48

def run!(yes:false, force:false)
  puts "Starting packagecloud-maven-importer v#{Packagecloud::Maven::Importer::VERSION}"

  if force == true
    if yes == false
      print "Delete local artifact database and start over? [y/N]:"
      answer = gets
      if answer.chomp != "y"
        puts 'Aborting!'
        exit 1
      end
    end
    database.reset!
  end

  unknown_files = []
  files_scanned = 0
  artifacts_scanned = 0
  initial_database_count = database.queued_count

  if !File.exists?(maven_repository_path)
    $stderr.puts "#{maven_repository_path} does not exist, aborting!"
    exit 1
  end

  puts "Building database of uploadable artifacts in #{maven_repository_path}..."
  Dir[File.join(maven_repository_path, "/**/*")].each do |possible_artifact|
    next if possible_artifact.end_with?('lastUpdated')
    next if possible_artifact.end_with?('repositories')
    next if possible_artifact.include?('-SNAPSHOT')

    if File.file?(possible_artifact)
      result = parse_artifact(possible_artifact)
      if result
        database.push(result[:full_path], result[:base_path])
        artifacts_scanned += 1
      else
        unknown_files << possible_artifact
      end
      files_scanned += 1
    end
  end

  if initial_database_count == 0
    puts "Found #{artifacts_scanned} total uploadable artifacts out of #{files_scanned} scanned files in #{maven_repository_path}"
  else
    new_artifacts_scanned = initial_database_count - database.queued_count
    puts "Found #{artifacts_scanned} total uploadable artifacts (#{new_artifacts_scanned} previously unseen) out of #{files_scanned} scanned files in #{maven_repository_path}"
  end

  if database.queued_count == 0
    puts "Nothing left to upload"
  else
    puts "#{database.queued_count} artifacts left to upload..."
    if yes == false
      print "Continue? [y/N]:"
      answer = gets
      if answer.chomp != "y"
        puts 'Aborting!'
        exit 1
      end
    end
  end

  while path_pair = database.peek do
    full_path, base_path = path_pair
    print "Uploading #{base_path}..."

    # This will safely ignore any 422's for already existing artifacts and retry on errors (5xx)
    connection.put(path: "/api/v1/repos/#{username}/#{repository}/artifacts.json",
                   body: File.read(full_path),
                   expects: [201, 422],
                   idempotent: true,
                   retry_limit: 5,
                   retry_interval: 5,
                   headers: {'User-Agent' => "packagecloud-maven-importer v#{Packagecloud::Maven::Importer::VERSION} (#{RUBY_PLATFORM})"},
                   query: { key: base_path })

    puts "Done"
    database.finish!(full_path)
  end
  puts "Finished"

  exit 0
end