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

def dist_required?(filename)
  ext = File.extname(filename).gsub!('.','')
  ["rpm", "deb", "dsc"].include?(ext)
end

#error_if_dist_required(filename) ⇒ Object



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

def error_if_dist_required(filename)
  ext = File.extname(filename).gsub!('.','')
  if dist_required?(ext) && @dist.nil?
    error "Distribution needed for rpm, deb, 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



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

def get_source_files_for(orig_filename)
  source_files = {}
  glob_args = ["**/*"]
  package = ::Packagecloud::Package.new(open(orig_filename))
  result = @client.package_contents(@repo, package)
  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)


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

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



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

def push_app
  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)
          error_if_dist_required(filename)
          log "Detected supported package: #{filename}"
          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(open(filename), get_distro(@dist), source_files, filename)
            else
              packages << ::Packagecloud::Package.new(open(filename), get_distro(@dist), {}, filename)
            end
          else
            packages << ::Packagecloud::Package.new(open(filename), nil, {}, filename)
          end
        end
      end
    end
  end

  packages.each do |package|
    result = @client.put_package(@repo, package)
    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