Class: Aptly::Repository

Inherits:
Representation show all
Includes:
Publishable
Defined in:
lib/aptly/repository.rb

Overview

Aptly repository representation.

Instance Attribute Summary

Attributes inherited from Representation

#connection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Publishable

#published?, #published_in

Methods inherited from Representation

#initialize

Constructor Details

This class inherits a constructor from Aptly::Representation

Class Method Details

.create(name, connection = Connection.new, **kwords) ⇒ Repository

Creates a new Aptly::Repository

Parameters:

  • name (String)

    name fo the repository

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:



134
135
136
137
138
139
140
# File 'lib/aptly/repository.rb', line 134

def create(name, connection = Connection.new, **kwords)
  options = kwords.merge(name: name)
  options = options.map { |k, v| [k.to_s.capitalize, v] }.to_h
  response = connection.send(:post, '/repos',
                             body: JSON.generate(options))
  new(connection, JSON.parse(response.body))
end

.exist?(name, connection = Connection.new, **kwords) ⇒ Boolean

Check if a repository exists.

Parameters:

  • name (String)

    the name of the repository which might exist

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:

  • (Boolean)

    whether or not the repository exists



146
147
148
149
150
151
# File 'lib/aptly/repository.rb', line 146

def exist?(name, connection = Connection.new, **kwords)
  get(name, connection, **kwords)
  true
rescue Aptly::Errors::NotFoundError
  false
end

.get(name, connection = Connection.new, **kwords) ⇒ Repository

Get a Aptly::Repository instance if the repository already exists.

Parameters:

  • name (String)

    name of the repository

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:



123
124
125
126
127
128
# File 'lib/aptly/repository.rb', line 123

def get(name, connection = Connection.new, **kwords)
  kwords = kwords.map { |k, v| [k.to_s.capitalize, v] }.to_h
  response = connection.send(:get, "/repos/#{name}",
                             query: kwords)
  new(connection, JSON.parse(response.body))
end

.list(connection = Connection.new, **kwords) ⇒ Array<Repository>

List all known repositories.

Parameters:

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:



156
157
158
159
# File 'lib/aptly/repository.rb', line 156

def list(connection = Connection.new, **kwords)
  response = connection.send(:get, '/repos', query: kwords)
  JSON.parse(response.body).collect { |r| new(connection, r) }
end

Instance Method Details

#add_file(path, **kwords) ⇒ Hash

Add a previously uploaded file to the Repository. FIXME: this should be called file

Returns:

  • (Hash)

    report data as specified in the API.



27
28
29
30
31
32
33
34
# File 'lib/aptly/repository.rb', line 27

def add_file(path, **kwords)
  response = connection.send(:post, "/repos/#{self.Name}/file/#{path}",
                             query: kwords)
  hash = JSON.parse(response.body)
  error = Errors::RepositoryFileError.from_hash(hash)
  raise error if error
  hash['Report']['Added']
end

#add_package(packages, **kwords) ⇒ Object Also known as: add_packages

Add a package (by key) to the repository.

Parameters:

  • packages (Array<String>, String)

    a list of package keys or a single package key to add to the repository. The package key(s) must already be in the aptly database.



60
61
62
63
64
65
# File 'lib/aptly/repository.rb', line 60

def add_package(packages, **kwords)
  connection.send(:post, "/repos/#{self.Name}/packages",
                  query: kwords,
                  body: JSON.generate(PackageRefs: [*packages]))
  self
end

#delete!(**kwords) ⇒ nil Also known as: delete

Delete this repository.

Returns:

  • (nil)

    always returns nil



17
18
19
20
# File 'lib/aptly/repository.rb', line 17

def delete!(**kwords)
  connection.send(:delete, "/repos/#{self.Name}", query: kwords)
  nil
end

#delete_package(packages, **kwords) ⇒ Object Also known as: delete_packages

Deletes a package (by key) from the repository.

Parameters:

  • packages (Array<String>, String)

    a list of package keys or a single package key to add to the repository. The package key(s) must already be in the aptly database.



72
73
74
75
76
77
# File 'lib/aptly/repository.rb', line 72

def delete_package(packages, **kwords)
  connection.send(:delete, "/repos/#{self.Name}/packages",
                  query: kwords,
                  body: JSON.generate(PackageRefs: [*packages]))
  self
end

#edit!(**kwords) ⇒ self?

Note:

this possibly mutates the attributes depending on the HTTP response

Edit this repository’s attributes as per the parameters.

Returns:

  • (self)

    if the instance data was mutated

  • (nil)

    if the instance data was not mutated



92
93
94
95
96
97
98
99
100
# File 'lib/aptly/repository.rb', line 92

def edit!(**kwords)
  response = connection.send(:put,
                             "/repos/#{self.Name}",
                             body: JSON.generate(kwords))
  hash = JSON.parse(response.body, symbolize_names: true)
  return nil if hash == marshal_dump
  marshal_load(hash)
  self
end

#packages(**kwords) ⇒ Array<String>

List all packages in the repository

Returns:

  • (Array<String>)

    list of packages in the repository



49
50
51
52
53
54
# File 'lib/aptly/repository.rb', line 49

def packages(**kwords)
  response = connection.send(:get, "/repos/#{self.Name}/packages",
                             query: kwords,
                             query_mangle: false)
  JSON.parse(response.body)
end

#publish(prefix, **kwords) ⇒ PublishedRepository

Convenience wrapper around Aptly.publish, publishing this repository locally and as only source of prefix.

Parameters:

  • prefix (String)

    prefix to publish under (i.e. published repo name)

Returns:



84
85
86
# File 'lib/aptly/repository.rb', line 84

def publish(prefix, **kwords)
  Aptly.publish([{ Name: self.Name }], prefix, 'local', kwords)
end

#snapshot(name = nil, **kwords) ⇒ Snapshot

Creates a new Snapshot

Parameters:

  • name (String) (defaults to: nil)

    name of snapshot

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aptly/repository.rb', line 105

def snapshot(name = nil, **kwords)
  # TODO: 1.0
  if name.nil? && !kwords.key?(:Name)
    # backwards compatible handling allows name to be passed though
    # kwords or the argument. Argument is preferred.
    raise ArgumentError, 'wrong number of arguments (given 0, expected 1)'
  end
  kwords[:Name] = name unless name.nil?
  response = connection.send(:post, "/repos/#{self.Name}/snapshots",
                             body: JSON.generate(kwords))
  Aptly::Snapshot.new(::Aptly::Connection.new, JSON.parse(response.body))
end

#upload(files) ⇒ Object

FIXME: needs to support single files Convenience wrapper around Files.upload, #add_file and Files.delete



38
39
40
41
42
43
44
45
# File 'lib/aptly/repository.rb', line 38

def upload(files)
  prefix = "#{self.class.to_s.tr(':', '_')}-#{Socket.gethostname}-"
  directory = Dir::Tmpname.make_tmpname(prefix, nil)
  Files.upload(files, directory, connection)
  add_file(directory)
ensure
  Files.delete(directory, connection)
end