Class: PackageCloud::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/package_cloud/repository.rb

Instance Method Summary collapse

Methods inherited from Object

#method_missing, #respond_to?

Constructor Details

#initialize(attrs, config) ⇒ Repository

Returns a new instance of Repository.



5
6
7
8
# File 'lib/package_cloud/repository.rb', line 5

def initialize(attrs, config)
  @attrs = attrs
  @config = config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PackageCloud::Object

Instance Method Details

#create_gpg_key(file_path) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/package_cloud/repository.rb', line 111

def create_gpg_key(file_path)
  file_data = File.new(file_path, 'rb')
  base_url = @config.base_url
  url = PackageCloud::Util.compute_url(@config.base_url, paths["gpg_keys"])
  params = { keydata: file_data }

  print "Attempting to upload key file #{file_path}... "

  begin
    RestClient::Request.execute(:method => 'post',
                                :url => url,
                                :timeout => nil,
                                :payload => { :gpg_key => params })
  rescue RestClient::UnprocessableEntity => e
    print "error: ".color(:red)
    json = JSON.parse(e.response)
    puts json["error"]
    puts ""
    exit(1)
  end

  print "success!\n".color(:green)
end

#create_master_token(name) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/package_cloud/repository.rb', line 147

def create_master_token(name)
  url = PackageCloud::Util.compute_url(@config.base_url, paths["create_master_token"])
  begin
    resp = RestClient.post(url, :master_token => {:name => name})
    resp = JSON.parse(resp)
  rescue RestClient::UnprocessableEntity => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
  resp
end

#create_package(file_path, file_url, dist_id, files = nil, urls = nil, filetype = nil, coordinates = nil) ⇒ Object



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
# File 'lib/package_cloud/repository.rb', line 53

def create_package(file_path, file_url, dist_id, files=nil, urls=nil, filetype=nil, coordinates=nil)
  base_url = @config.base_url
  url = PackageCloud::Util.compute_url(base_url, paths["create_package"])
  params = {
    :distro_version_id => dist_id,
  }

  if file_url
    params[:url] = file_url
  else
    params[:package_file] = File.new(file_path, 'rb')
  end

  if coordinates
    params[:coordinates] = coordinates
  end

  if filetype == "dsc"
    if urls
      params[:source_urls] = urls
    else
      params[:source_files] = files.map do |f|
        File.new(f, 'rb')
      end
    end
  end

  RestClient::Request.execute(
    :method => 'post',
    :url => url,
    :timeout => nil,
    :payload => { :package =>  params },
  )

  print "success!\n".color(:green)
end

#create_uploadObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/package_cloud/repository.rb', line 34

def create_upload
  base_url = @config.base_url
  url = PackageCloud::Util.compute_url(base_url, paths["create_upload"])
  begin
    JSON.parse(RestClient.post(url, {}))
  rescue RestClient::UnprocessableEntity => e
    print "error!\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts
    exit(1)
  rescue RestClient::ResourceNotFound => e
    print "failed!\n".color(:red)
    exit(127)
  end
end

#gpg_keysObject



135
136
137
138
139
# File 'lib/package_cloud/repository.rb', line 135

def gpg_keys
  url = PackageCloud::Util.compute_url(@config.base_url, paths["gpg_keys"])
  attrs = JSON.parse(RestClient.get(url))
  attrs["gpg_keys"].map { |a| GpgKey.new(a, @config) }
end

#install_script(type) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/package_cloud/repository.rb', line 90

def install_script(type)
  url = urls["install_script"].gsub(/:package_type/, type)

  # the URL we've obtained above already contains the correct tokens
  # because the install script URL uses a master token for access, not
  # your API token, so if we pass @config.base_url in to compute_url,
  # we'll end up generating a URL like: https://token:@https://token:@...
  # because @config.base_url has the url with the API token in it and url
  # has the url (lol) with the master token in it.
  #
  # so just pass url in here.
  url = PackageCloud::Util.compute_url(url, '')
  script = RestClient.get(url)

  # persist the script to a tempfile to make it easier to execute
  file = Tempfile.new('foo')
  file.write(script)
  file.close
  file
end

#master_tokensObject



141
142
143
144
145
# File 'lib/package_cloud/repository.rb', line 141

def master_tokens
  url = PackageCloud::Util.compute_url(@config.base_url, paths["master_tokens"])
  attrs = JSON.parse(RestClient.get(url))
  attrs.map { |a| MasterToken.new(a, @config) }
end

#parse_dsc(dsc_path, dist_id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/package_cloud/repository.rb', line 10

def parse_dsc(dsc_path, dist_id)
  file_data = File.new(dsc_path, 'rb')
  base_url = @config.base_url
  url = PackageCloud::Util.compute_url(base_url, paths["package_contents"])
  begin
    resp = RestClient::Request.execute(:method => 'post',
                                       :url => url,
                                       :timeout => nil,
                                       :payload => { :package => {:package_file      => file_data,
                                                                  :distro_version_id => dist_id}})
    resp = JSON.parse(resp)
    print "success!\n"
    resp["files"]
  rescue RestClient::UnprocessableEntity => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
end

#private_humanObject



203
204
205
# File 'lib/package_cloud/repository.rb', line 203

def private_human
  send(:private) ? "private".color(:red) : "public".color(:green)
end

#promote(dist, package_name, dest_repo_name, scope = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/package_cloud/repository.rb', line 164

def promote(dist, package_name, dest_repo_name, scope=nil)
  begin
    url = PackageCloud::Util.compute_url(@config.base_url, paths["self"] + "/" + [dist, package_name, "promote.json"].compact.join("/"))
    resp = if scope
             RestClient.post(url, destination: dest_repo_name, scope: scope)
           else
             RestClient.post(url, destination: dest_repo_name)
           end
    resp = JSON.parse(resp)
  rescue RestClient::UnprocessableEntity, RestClient::ResourceNotFound => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
end

#yank(dist, package_name, scope = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/package_cloud/repository.rb', line 184

def yank(dist, package_name, scope=nil)
  begin
    url = PackageCloud::Util.compute_url(@config.base_url, paths["self"] + "/" + [dist, package_name].compact.join("/"))
    if scope
      RestClient.delete(url, params: { scope: scope })
    else
      RestClient.delete(url)
    end
  rescue RestClient::ResourceNotFound => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
end