class AptlyAPI::Server
This class represents an Aptly server running the Aptly API
Attributes
Public Class Methods
Creates a new AptlyServer located at url
# File lib/server.rb, line 26 def initialize(url) @server = URI(url) @http = Net::HTTP.new(@server.host, @server.port) @version = hget("/api/version").fetch("Version", "unknown") end
Public Instance Methods
Compare two AptlyServer objects to see if they are identical
# File lib/server.rb, line 128 def ==(r) r.server == server and r.version == version end
Create a new repo called name
. If the specified repo already exists, simply returns the AptlyRepo for the existing repo.
# File lib/server.rb, line 35 def create_repo(name, options = {}) request_body = Hash.new request_body.merge!({"Name" => name}) request_body.merge!(options) return true if hpost('/api/repos', request_body) == 200 return false end
Deletes an aptly repo called name
from the AptlyServer
# File lib/server.rb, line 45 def delete_repo(name) return true if hdelete("/api/repos/#{name}") == 200 return false end
Upload a local file
to the server at directory
# File lib/server.rb, line 82 def file_upload(file, directory) if !File.exist?(file) return false end File.open(file) do |data| request = Net::HTTP::Post::Multipart.new("#{@server.path}/api/files/#{directory}", "file" => UploadIO.new(data, "application/x-debian-package", File.basename(file))) result = Net::HTTP.start(@server.host, @server.port) do |conn| conn.request(request) end if result.code.to_i != 200 return false end end return true end
Get AptlyRepo object for repo called name
# File lib/server.rb, line 63 def get_repo(name) info = hget("/api/repos/#{name}") return Repo.new(@server, info) end
Get an array of all AptlyRepo’s that exist on server.
# File lib/server.rb, line 52 def get_repos repos = Array.new remote_repos = hget("/api/repos") remote_repos.each do |info| repos.push(Repo.new(@server, info)) end return repos end
Publish a repo from sources
(hash of “Component/Name”)
TODO: There are bare minimum for the moment and will change
# File lib/server.rb, line 103 def publish(kind, prefix, sources, distribution, keyid, password) body = Hash.new body.merge!({"SourceKind" => kind}) body.merge!({"Sources" => sources}) body.merge!({"Distribution" => distribution}) body.merge!({"ForceOverwrite" => true}) body.merge!({"Signing" => { "Batch" => true, "GpgKey" => keyid, "Passphrase" => password}}) return true if hpost("/api/publish/#{prefix.to_s}", body).between?(200, 299) return false end
Update a published repo
TODO: There are bare minimum for the moment and will change
# File lib/server.rb, line 118 def publish_update(prefix, distribution, keyid, password) body = Hash.new body.merge!({"ForceOverwrite" => true}) body.merge!({"Signing" => { "Batch" => true, "GpgKey" => keyid, "Passphrase" => password}}) return true if hput("/api/publish/#{prefix.to_s}/#{distribution.to_s}", body).between?(200, 299) return false end
Return true if repo called name
exists. Otherwise false
# File lib/server.rb, line 70 def repo_exist?(name) remote_repos = hget("/api/repos") inventory = Array.new remote_repos.each do |info| inventory.push(info.fetch("Name")) end return true if inventory.include?(name) return false end
Protected Instance Methods
Sends HTTP delete call to path
# File lib/server.rb, line 168 def hdelete(path) request = Net::HTTP::Delete.new("#{@server.path}#{URI.escape(path)}") response = @http.request(request) return response.code.to_i end
Get an hash of JSON data from server path
# File lib/server.rb, line 137 def hget(path) request = Net::HTTP::Get.new("#{@server.path}#{URI.escape(path)}") response = @http.request(request) if !response.code.to_i.between?(200, 299) return response.code.to_i end return JSON.parse(response.body) end
Post data
hash to path
as JSON
# File lib/server.rb, line 148 def hpost(path, data) request = Net::HTTP::Post.new("#{@server.path}#{URI.escape(path)}") request.add_field('Content-Type', 'application/json') request.body = data.to_json response = @http.request(request) return response.code.to_i end
Put data
hash to path
as JSON
# File lib/server.rb, line 158 def hput(path, data) request = Net::HTTP::Put.new("#{@server.path}#{URI.escape(path)}") request.add_field('Content-Type', 'application/json') request.body = data.to_json response = @http.request(request) return response.code.to_i end