Class: PackageCloud::CLI::Entry

Inherits:
Base
  • Object
show all
Defined in:
lib/package_cloud/cli/entry.rb

Instance Method Summary collapse

Instance Method Details

#promote(source_repo_desc, package_name, dest_repo_desc) ⇒ Object



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
59
60
61
62
63
# File 'lib/package_cloud/cli/entry.rb', line 31

def promote(source_repo_desc, package_name, dest_repo_desc)
  repo_name = source_repo_desc.split("/")[0..1].join("/")
  dist = expand_dist_shortcut(source_repo_desc.split("/")[2..3].join("/"))

  dest_repo_name = expand_dist_shortcut(dest_repo_desc.split("/")[0..1].join("/"))

  if dist == "" && package_name =~ /\.gem$/
    dist = "gems"
  end

  print "Looking for source repository at #{repo_name}... "
  src_repo = client.repository(source_repo_desc)
  print "success!\n"

  print "Looking for destination repository at #{dest_repo_name}... "
  repo = client.repository(dest_repo_desc)
  print "success!\n"

  if package_name.include?('@')
    scope, unscoped_package_name = package_scope(package_name)

    print "Attempting to promote scoped package #{scope}/#{unscoped_package_name} from #{repo_name}/#{dist} to #{dest_repo_name}..."
    packages = src_repo.promote(dist, unscoped_package_name, dest_repo_name, scope)
  else
    print "Attempting to promote #{repo_name}/#{dist}/#{package_name} to #{dest_repo_name}..."
    packages = src_repo.promote(dist, package_name, dest_repo_name)
  end
  puts "done!".color(:green)

  if dist == "node/1"
    puts "WARNING: This Node.js package will NOT be downloadable by clients until a dist tag is created. Read more: https://packagecloud.io/docs/#node_promote".color(:yellow)
  end
end

#push(repo, package_file, *package_files) ⇒ Object



120
121
122
123
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/package_cloud/cli/entry.rb', line 120

def push(repo, package_file, *package_files)
  total_time = Benchmark.measure do
    ARGV.clear # otherwise gets explodes
    package_files << package_file

    exts = package_files.map { |f| f.split(".").last }.uniq

    if package_files.length > 1 && exts.length > 1
      abort("You can't push multiple packages of different types at the same time.\nFor example, use *.deb to push all your debs at once.".color(:red))
    end

    invalid_packages = package_files.select do |f|
      !SUPPORTED_EXTS.include?(f.split(".").last.downcase)
    end

    if JAVA_EXTS.include?(exts.first.downcase) && options.has_key?("coordinates")
      puts "Using coordinates #{options["coordinates"].color(:yellow)}"
    end

    if !options.has_key?("skip-file-ext-validation") && invalid_packages.any?
      message = "I don't know how to push these packages:\n\n".color(:red)
      invalid_packages.each do |p|
        message << "  #{p}\n"
      end
      message << "\npackage_cloud only supports node.js, deb, gem, java, python, "\
                 "rpm, alpine, helm, anyfile (.zip, .asc) packages".color(:red)
      abort(message)
    end

    if !options.has_key?("yes") && exts.first == "gem" && package_files.length > 1
      answer = get_valid("Are you sure you want to push #{package_files.length} packages? (y/n)") do |s|
        s == "y" || s == "n"
      end

      if answer != "y"
        abort("Aborting...".color(:red))
      end
    end

    validator = Validator.new(client)
    if PY_EXTS.include?(exts.first.downcase)
      dist_id   = validator.distribution_id(repo, package_files, 'py')
    elsif ANYFILE_EXTS.include?(exts.first.downcase)
      dist_id   = validator.distribution_id(repo, package_files, 'anyfile')
    elsif NODE_EXTS.include?(exts.first.downcase)
      dist_id   = validator.distribution_id(repo, package_files, 'node')
    elsif JAVA_EXTS.include?(exts.first.downcase)
      abort_if_snapshot!(package_files)
      dist_id   = validator.distribution_id(repo, package_files, 'jar')
    elsif AMBIGUOUS_EXTS.include?(exts.first.downcase)
      dist_id   = validator.distribution_id_from_repo_url(repo, package_files)
    else
      dist_id   = validator.distribution_id(repo, package_files, exts.first)
    end

    # strip os/dist
    split_repo = repo.split("/")[0..1].join("/")

    print "Looking for repository at #{split_repo}... "
    client_repo = nil
    measurement = Benchmark.measure do
      client_repo = client.repository(split_repo)
    end
    print "success!\n"
    $logger.debug("repository lookup request timing: #{measurement}")

    package_files.each do |f|
      url = nil
      files = nil
      urls = nil
      ext = f.split(".").last

      if ext == "dsc"
        print "Checking source package #{f}... "
        files = parse_and_verify_dsc(client_repo, f, dist_id)
      end

      if options["upload-before-push"]
        print "Uploading #{f}... "
        upload = client_repo.create_upload
        begin
          response = RestClient::Request.execute(
            :method => upload.fetch("form").fetch("method", "POST"),
            :url => upload.fetch("form").fetch("url"),
            :timeout => nil,
            :payload => upload.fetch("form").fetch("data", {}).merge(
              upload.fetch("form").fetch("file_input") => File.open(f, "r"),
            )
          )
          url = response.headers.fetch(:location)

          # Amazon S3 uploads return urlencoded keys in the Location
          # header, and we want to return the decoded object key for
          # API operations.
          uri = URI.parse(url)
          if uri.host.end_with?(".amazonaws.com")
            uri.path = CGI.unescape(uri.path)
            url = uri.to_s
          end

          if files
            urls = files.map do |file|
              response = RestClient::Request.execute(
                :method => upload.fetch("form").fetch("method", "POST"),
                :url => upload.fetch("form").fetch("url"),
                :timeout => nil,
                :payload => upload.fetch("form").fetch("data", {}).merge(
                  upload.fetch("form").fetch("file_input") => File.open(file, "r"),
                )
              )
              source_url = response.headers.fetch(:location)
              source_uri = URI.parse(source_url)
              if source_uri.host.end_with?(".amazonaws.com")
                source_uri.path = CGI.unescape(source_uri.path)
                source_url = source_uri.to_s
              end
              source_url
            end
          end
        rescue
          print "error!\n".color(:red)
          puts $!, *$!.backtrace, $!.response, $!.response.body
          exit(1)
        end
      end

      print "Pushing #{f}... "
      measurement = Benchmark.measure do
        if options.has_key?("skip-errors")
          create_package_skip_errors(client_repo, f, url, dist_id, files, urls, ext, options["coordinates"])
        elsif options.has_key?("skip-duplicates")
          create_package_skip_duplicates(client_repo, f, url, dist_id, files, urls, ext, options["coordinates"])
        else
          create_package(client_repo, f, url, dist_id, files, urls, ext, options["coordinates"])
        end
      end
      $logger.debug("create package request timing: #{measurement}")
    end
  end
  $logger.debug("push command total timing: #{total_time}")
end

#versionObject



264
265
266
# File 'lib/package_cloud/cli/entry.rb', line 264

def version
  puts "package_cloud CLI #{VERSION}\nSee https://packagecloud.io/docs#cli for more details."
end

#yank(repo_desc, package_name) ⇒ Object



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
# File 'lib/package_cloud/cli/entry.rb', line 67

def yank(repo_desc, package_name)
  ARGV.clear # otherwise gets explodes

  # strip os/dist
  repo_name = repo_desc.split("/")[0..1].join("/")
  dist = expand_dist_shortcut(repo_desc.split("/")[2..3].join("/"))

  if dist == "" && package_name =~ /\.gem$/
    dist = "gems"
  end

  print "Looking for repository at #{repo_name}... "
  repo = client.repository(repo_desc)
  print "success!\n"

  if package_name.include?('@')
    scope, unscoped_package_name = package_scope(package_name)
    print "Attempting to yank scoped package at #{repo_name}/#{dist} #{package_name}..."
    repo.yank(dist, unscoped_package_name, scope)
  else
    print "Attempting to yank package at #{repo_name}/#{dist}/#{package_name}..."
    repo.yank(dist, package_name)
  end
  puts "done!".color(:green)

  if dist == "node/1"
    puts "WARNING: Deleting Node.js packages can have unexpected side effects with dist tags. Read more: https://packagecloud.io/docs/#node_delete".color(:yellow)
  end
end