Class: DPL::Provider::Packagecloud

Inherits:
DPL::Provider show all
Defined in:
lib/dpl/provider/packagecloud.rb

Instance Method Summary collapse

Instance Method Details

#check_authObject



6
7
8
9
10
11
12
13
# File 'lib/dpl/provider/packagecloud.rb', line 6

def check_auth
  setup_auth
  begin
    @client = ::Packagecloud::Client.new(@creds, "travis-ci")
  rescue ::Packagecloud::UnauthenticatedException
    error "Could not authenticate to https://packagecloud.io, please check credentials"
  end
end

#dist_required?(filename) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/dpl/provider/packagecloud.rb', line 46

def dist_required?(filename)
  ext = File.extname(filename).gsub!('.','')
  if ext.nil?
    error "filename: #{filename} has no extension!"
  end
  ["rpm", "deb", "dsc", "whl", "egg", "egg-info", "gz", "zip", "tar", "bz2", "z", "tgz"].include?(ext.downcase)
end

#error_if_dist_required(filename) ⇒ Object



54
55
56
57
58
# File 'lib/dpl/provider/packagecloud.rb', line 54

def error_if_dist_required(filename)
  if dist_required?(filename) && @dist.nil?
    error "Distribution needed for rpm, deb, python, and dsc packages, example --dist='ubuntu/breezy'"
  end
end

#get_distro(query) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dpl/provider/packagecloud.rb', line 28

def get_distro(query)
  distro = nil
  begin
    distro = @client.find_distribution_id(query)
  rescue ArgumentError => exception
    error "Error: #{exception.message}"
  end
  if distro.nil?
    error "Could not find distribution named #{query}"
  end
  distro
end

#get_source_files_for(orig_filename) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dpl/provider/packagecloud.rb', line 65

def get_source_files_for(orig_filename)
  source_files = {}
  glob_args = ["**/*"]
  package = ::Packagecloud::Package.new(:file => orig_filename)
  result = @client.package_contents(@repo, package, get_distro(@dist))
  if result.succeeded
    package_contents_files = result.response["files"].map { |x| x["filename"] }
    Dir.chdir(options.fetch(:local_dir, Dir.pwd)) do
      Dir.glob(*glob_args) do |filename|
        unless File.directory?(filename)
          basename = File.basename(filename)
          if package_contents_files.include?(basename)
            log "Found source fragment: #{basename} for #{orig_filename}"
            source_files = source_files.merge({basename => open(filename)})
          end
        end
      end
    end
  else
    error "Error: #{result.response}"
  end
  source_files
end

#is_source_package?(filename) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/dpl/provider/packagecloud.rb', line 60

def is_source_package?(filename)
  ext = File.extname(filename).gsub!('.','')
  ext == 'dsc'
end

#is_supported_package?(filename) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/dpl/provider/packagecloud.rb', line 41

def is_supported_package?(filename)
  ext = File.extname(filename).gsub!('.','')
  ::Packagecloud::SUPPORTED_EXTENSIONS.include?(ext)
end

#needs_key?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/dpl/provider/packagecloud.rb', line 15

def needs_key?
  false
end

#push_appObject



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
133
134
135
136
137
138
# File 'lib/dpl/provider/packagecloud.rb', line 89

def push_app
  forced = options.fetch(:force, nil)
  packages = []
  glob_args = Array(options.fetch(:package_glob, '**/*'))
  Dir.chdir(options.fetch(:local_dir, Dir.pwd)) do
    Dir.glob(*glob_args) do |filename|
      unless File.directory?(filename)
        if is_supported_package?(filename)
          log "Detected supported package: #{filename}"
          error_if_dist_required(filename)
          if is_source_package?(filename)
            log "Processing source package: #{filename}"
            source_files = get_source_files_for(filename)
            packages << ::Packagecloud::Package.new(:file => filename, :source_files => source_files)
          else
            packages << ::Packagecloud::Package.new(:file => filename)
          end
        end
      end
    end
  end

  packages.each do |package|
    if forced
      log "Deleting package: #{package.filename}"
      distro, distro_release = @dist.split("/")
      result = @client.delete_package(@repo, distro, distro_release, package.filename)
      if result.succeeded
        log "Successfully deleted #{package.filename} on #{@dist}"
      else
        error "Error #{result.response}"
      end
    end
    log "Pushing package: #{package.filename}"
    if dist_required?(package.filename)
      result = @client.put_package(@repo, package, get_distro(@dist))
    else
      result = @client.put_package(@repo, package)
    end

    if result.succeeded
      log "Successfully pushed #{package.filename} to #{@username}/#{@repo}"
    else
      error "Error #{result.response}"
    end
  end
  if packages.empty?
    error "Error: No supported packages found! Perhaps try skip_cleanup: true"
  end
end

#setup_authObject



19
20
21
22
23
24
25
26
# File 'lib/dpl/provider/packagecloud.rb', line 19

def setup_auth
  @username = option(:username)
  @token = option(:token)
  @repo = option(:repository)
  @dist = option(:dist) if options[:dist]
  @creds = ::Packagecloud::Credentials.new(@username, @token)
  log "Logging into https://packagecloud.io with #{@username}:#{@token[-4..-1].rjust(20, '*')}"
end