Class: DPL::Provider::Packagecloud

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

Instance Attribute Summary

Attributes inherited from DPL::Provider

#context, #options

Instance Method Summary collapse

Methods inherited from DPL::Provider

apt_get, #check_app, #cleanup, #commit_msg, context, #create_key, #default_text_charset, #default_text_charset?, #deploy, deprecated, #detect_encoding?, #encoding_for, #error, experimental, #initialize, #log, new, npm_g, #option, pip, requires, #run, #setup_git_credentials, #setup_git_ssh, #sha, shell, #uncleanup, #user_agent, #warn

Constructor Details

This class inherits a constructor from DPL::Provider

Instance Method Details

#check_authObject



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

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)


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

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"].include?(ext.downcase)
end

#error_if_dist_required(filename) ⇒ Object



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

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



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

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



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

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)


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

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

#is_supported_package?(filename) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#needs_key?Boolean

Returns:

  • (Boolean)


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

def needs_key?
  false
end

#push_appObject



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
139
# File 'lib/dpl/provider/packagecloud.rb', line 90

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



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

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