aptlyapi 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/repo.rb +60 -0
  3. data/lib/server.rb +133 -0
  4. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed3f855daabc2856ce5e8199bae731906f194fa7
4
- data.tar.gz: 3458f96717934a2a24b7a71f042fbac62295fb70
3
+ metadata.gz: 875a2776d0b83252092056b5bbcf0a3efb1f38e2
4
+ data.tar.gz: fcea7a923d2487225422b32b7feb38bba0d38e33
5
5
  SHA512:
6
- metadata.gz: 7d997c75fd31905eebda2a89d36f80026091cc918d023d8420daf874da06dd910e3bf66b653dcf0d2865bb2cb863cbbfb65f49b38a8a95f0aa34276e5bd64b1c
7
- data.tar.gz: da4db489852f89293db66c7b16db19125f8c35ef68eab4bb3834e750ea8bcc24b0c36d443d9a604202c9c01af304ccc3fd96587e88dfe25bf5125f96d9be05d8
6
+ metadata.gz: 228585cf690d08ba2a202f3054f92d15ca79dd5e614e49dd32c86b529e56cea9b550ab468603174676d8f7203286585e3886190b178259b5e06e622e30c648f0
7
+ data.tar.gz: 0e0f280a9cd07f1b3c1d93c3b722ad7686718be654d6464e29799ab852724dbf96e4434fc2ba0e85986822cf26beeaa7d422645d76b5383e83300419d5f354e0
data/lib/repo.rb ADDED
@@ -0,0 +1,60 @@
1
+
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module AptlyAPI
7
+ ##
8
+ # This class represents an Aptly repo
9
+ class Repo
10
+ ##
11
+ # Creates a new Repo definitation with data +json+ located on +server+
12
+ def initialize(server, json)
13
+ @server = server
14
+ @http = Net::HTTP.new(@server.host, @server.port)
15
+
16
+ @name = json['Name']
17
+ @comment = json['Comment']
18
+ @distrubution = json['DefaultDistribution']
19
+ @component = json['DefaultComponent']
20
+ end
21
+
22
+ ##
23
+ # Import all files uploaded to +directory+
24
+ def import_files(directory, options = {})
25
+ hpost("/api/repos/#{@name}/file/#{directory}", options)
26
+ end
27
+
28
+ attr_reader :name, :comment, :distribution, :component
29
+
30
+ protected
31
+ ##
32
+ # Get an hash of JSON data from server +path+
33
+ def hget(path)
34
+ request = Net::HTTP::Get.new("#{@server.path}#{path}")
35
+ response = @http.request(request)
36
+ if response.code.to_i != 200
37
+ return response.code.to_i
38
+ end
39
+ return JSON.parse(response.body)
40
+ end
41
+
42
+ ##
43
+ # Post +data+ hash to +path+ as JSON
44
+ def hpost(path, data)
45
+ request = Net::HTTP::Post.new("#{@server.path}#{path}")
46
+ request.add_field('Content-Type', 'application/json')
47
+ request.body = data.to_json
48
+ response = @http.request(request)
49
+ return response.code.to_i
50
+ end
51
+
52
+ ##
53
+ # Sends HTTP delete call to +path+
54
+ def hdelete(path)
55
+ request = Net::HTTP::Delete.new("#{@server.path}#{path}")
56
+ response = @http.request(request)
57
+ return response.code.to_i
58
+ end
59
+ end
60
+ end
data/lib/server.rb ADDED
@@ -0,0 +1,133 @@
1
+ #######################################################
2
+ #
3
+ # AptlyAPI gem
4
+ # Copyright 2015, R1Soft.
5
+ # Released under the terms of the GPLv2
6
+ #
7
+ # Wrangle remote Aptly Servers over
8
+ # the Aptly API.
9
+ #
10
+ # Authors:
11
+ # Alexander von Gluck IV <Alex.vonGluck@r1soft.com>
12
+ #
13
+ #######################################################
14
+
15
+ require 'uri'
16
+ require 'net/http'
17
+ require 'net/http/post/multipart'
18
+ require 'json'
19
+
20
+ module AptlyAPI
21
+ ##
22
+ # This class represents an Aptly server running the Aptly API
23
+ class Server
24
+ ##
25
+ # Creates a new AptlyServer located at +url+
26
+ def initialize(url)
27
+ @server = URI(url)
28
+ @http = Net::HTTP.new(@server.host, @server.port)
29
+ @version = hget("/api/version").fetch("Version", "unknown")
30
+ end
31
+
32
+ ##
33
+ # Create a new repo called +name+. If the specified repo already
34
+ # exists, simply returns the AptlyRepo for the existing repo.
35
+ def create_repo(name, options = {})
36
+ request_body = Hash.new
37
+ request_body.merge!({"Name" => name})
38
+ request_body.merge!(options)
39
+ return true if hpost('/api/repos', request_body) == 200
40
+ return false
41
+ end
42
+
43
+ ##
44
+ # Deletes an aptly repo called +name+ from the AptlyServer
45
+ def delete_repo(name)
46
+ return true if hdelete("/api/repos/#{name}") == 200
47
+ return false
48
+ end
49
+
50
+ ##
51
+ # Get an array of all AptlyRepo's that exist on server.
52
+ def get_repos
53
+ repos = Array.new
54
+ remote_repos = hget("/api/repos")
55
+ remote_repos.each do |info|
56
+ repos.push(Repo.new(@server, info))
57
+ end
58
+ return repos
59
+ end
60
+
61
+ ##
62
+ # Get AptlyRepo object for repo called +name+
63
+ def get_repo(name)
64
+ info = hget("/api/repos/#{name}")
65
+ return Repo.new(@server, info)
66
+ end
67
+
68
+ ##
69
+ # Return true if repo called +name+ exists. Otherwise false
70
+ def repo_exist?(name)
71
+ remote_repos = hget("/api/repos")
72
+ inventory = Array.new
73
+ remote_repos.each do |info|
74
+ inventory.push(info.fetch("Name"))
75
+ end
76
+ return true if inventory.include?(name)
77
+ return false
78
+ end
79
+
80
+ ##
81
+ # Upload a local +file+ to the server at +directory+
82
+ def file_upload(file, directory)
83
+ if !File.exist?(file)
84
+ return false
85
+ end
86
+ File.open(file) do |data|
87
+ request = Net::HTTP::Post::Multipart.new("#{@server.path}/api/files/#{directory}",
88
+ "file" => UploadIO.new(data, "application/x-debian-package", File.basename(file)))
89
+ result = Net::HTTP.start(@server.host, @server.port) do |conn|
90
+ conn.request(request)
91
+ end
92
+ end
93
+ end
94
+
95
+ ##
96
+ # Compare two AptlyServer objects to see if they are identical
97
+ def ==(r)
98
+ r.server == server and r.version == version
99
+ end
100
+
101
+ attr_reader :server, :version
102
+
103
+ protected
104
+ ##
105
+ # Get an hash of JSON data from server +path+
106
+ def hget(path)
107
+ request = Net::HTTP::Get.new("#{@server.path}#{path}")
108
+ response = @http.request(request)
109
+ if response.code.to_i != 200
110
+ return response.code.to_i
111
+ end
112
+ return JSON.parse(response.body)
113
+ end
114
+
115
+ ##
116
+ # Post +data+ hash to +path+ as JSON
117
+ def hpost(path, data)
118
+ request = Net::HTTP::Post.new("#{@server.path}#{path}")
119
+ request.add_field('Content-Type', 'application/json')
120
+ request.body = data.to_json
121
+ response = @http.request(request)
122
+ return response.code.to_i
123
+ end
124
+
125
+ ##
126
+ # Sends HTTP delete call to +path+
127
+ def hdelete(path)
128
+ request = Net::HTTP::Delete.new("#{@server.path}#{path}")
129
+ response = @http.request(request)
130
+ return response.code.to_i
131
+ end
132
+ end
133
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aptlyapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander von Gluck IV
@@ -17,6 +17,8 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/aptlyapi.rb
20
+ - lib/repo.rb
21
+ - lib/server.rb
20
22
  homepage: https://github.com/avongluck-r1soft/aptlyapi
21
23
  licenses:
22
24
  - GPLv2