class AptlyAPI::Server

This class represents an Aptly server running the Aptly API

Attributes

server[R]
version[R]

Public Class Methods

new(url) click to toggle source

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

==(r) click to toggle source

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_repo(name, options = {}) click to toggle source

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
delete_repo(name) click to toggle source

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
file_upload(file, directory) click to toggle source

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_repo(name) click to toggle source

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_repos() click to toggle source

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(kind, prefix, sources, distribution, keyid, password) click to toggle source

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
publish_update(prefix, distribution, keyid, password) click to toggle source

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
repo_exist?(name) click to toggle source

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

hdelete(path) click to toggle source

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
hget(path) click to toggle source

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
hpost(path, data) click to toggle source

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
hput(path, data) click to toggle source

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