Class: AptlyAPI::Server

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

Overview

This class represents an Aptly server running the Aptly API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Server

Creates a new AptlyServer located at url



26
27
28
29
30
# 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

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



132
133
134
# File 'lib/server.rb', line 132

def server
  @server
end

#versionObject (readonly)

Returns the value of attribute version.



132
133
134
# File 'lib/server.rb', line 132

def version
  @version
end

Instance Method Details

#==(r) ⇒ Object

Compare two AptlyServer objects to see if they are identical



128
129
130
# File 'lib/server.rb', line 128

def ==(r)
  r.server == server and r.version == version
end

#create_repo(name, options = {}) ⇒ Object

Create a new repo called name. If the specified repo already exists, simply returns the AptlyRepo for the existing repo.



35
36
37
38
39
40
41
# 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) ⇒ Object

Deletes an aptly repo called name from the AptlyServer



45
46
47
48
# 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) ⇒ Object

Upload a local file to the server at directory



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

Get AptlyRepo object for repo called name



63
64
65
66
# File 'lib/server.rb', line 63

def get_repo(name)
  info = hget("/api/repos/#{name}")
  return Repo.new(@server, info)
end

#get_reposObject

Get an array of all AptlyRepo’s that exist on server.



52
53
54
55
56
57
58
59
# 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) ⇒ Object

Publish a repo from sources (hash of “Component/Name”)

TODO: There are bare minimum for the moment and will change



103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object

Update a published repo

TODO: There are bare minimum for the moment and will change



118
119
120
121
122
123
124
# 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) ⇒ Boolean

Return true if repo called name exists. Otherwise false

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# 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