s3repo 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0641c73158e5144c4a988efb266b2fa57b2cde5d
4
- data.tar.gz: 5ef0a969a378659cbc9f2aeb5f16b1a962890fc2
3
+ metadata.gz: 7e1bc9926cf762452f2d9a97ce3285aff708d2fd
4
+ data.tar.gz: 4b8c64c255cf57d2d42f542a3af02d0b42cfb320
5
5
  SHA512:
6
- metadata.gz: 047abc0e5e6448278c265082985f14d7a7da59026720658f42899ecd5547b2f9be88770ea5091e35ce6bb97ba66488ac9b516c09d7290066d198f52c9db433a4
7
- data.tar.gz: e6272c8574e44c6c830d87ba15718c70c0c10f0ba619d3e5ee2855c61f3830122514a2514cd933ffbf0fe86ea0f7ce5a28b35c655a5381782d3e0bc45e5a69a3
6
+ metadata.gz: f9f8031ab8c1ccdd6cfe815028595b7cceb82084f1cfe7c8ef70bf4bfdf6d7cd97ebb986d87b923bc6b383929df9738529407399b03a9b59905280ed1047f855
7
+ data.tar.gz: 7f75e8489ecbc9dd87ea43717ae0dec64c034602731a472e45a80a6ac890ab0161f29e6134223c8bcfaa0ef875f7a9a4650c8cdf22294b6b697df4d54acae90f
data/bin/s3repo CHANGED
@@ -2,3 +2,27 @@
2
2
 
3
3
  require 's3repo'
4
4
  require 'mercenary'
5
+
6
+ Mercenary.program(:s3repo) do |p|
7
+ p.version S3Repo::VERSION
8
+ p.description 'Package management tool for Archlinux repos'
9
+ p.syntax 's3repo <subcommand> [args]'
10
+
11
+ p.command(:build) do |c|
12
+ c.syntax 'build'
13
+ c.description 'Build package files from PKGBUILDs'
14
+
15
+ c.action do
16
+ S3Repo.new.build_packages Dir.glob('*/PKGBUILD')
17
+ end
18
+ end
19
+
20
+ p.command(:upload) do |c|
21
+ c.syntax 'upload'
22
+ c.description 'Upload packages to repo'
23
+
24
+ c.action do
25
+ S3Repo.new.add_packages Dir.glob('*/*.pkg.tar.xz')
26
+ end
27
+ end
28
+ end
data/lib/s3repo.rb CHANGED
@@ -11,7 +11,9 @@ module S3Repo
11
11
  end
12
12
  end
13
13
 
14
+ require 's3repo/version'
14
15
  require 's3repo/base'
16
+ require 's3repo/metadata'
15
17
  require 's3repo/cache'
16
18
  require 's3repo/client'
17
19
  require 's3repo/repo'
data/lib/s3repo/base.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'English'
2
+
1
3
  module S3Repo
2
4
  ##
3
5
  # Base object, used to provide common attributes
@@ -8,6 +10,12 @@ module S3Repo
8
10
 
9
11
  private
10
12
 
13
+ def run(cmd)
14
+ results = `#{cmd} 2>&1`
15
+ return results if $CHILD_STATUS.success?
16
+ fail "Failed running #{cmd}:\n#{results}"
17
+ end
18
+
11
19
  def bucket
12
20
  @bucket ||= @options[:bucket] || ENV['S3_BUCKET']
13
21
  return @bucket if @bucket
@@ -17,5 +25,11 @@ module S3Repo
17
25
  def client
18
26
  @client ||= @options[:client] || Client.new(bucket: bucket)
19
27
  end
28
+
29
+ def file_cache
30
+ @file_cache ||= @options[:file_cache] || Cache.new(
31
+ client: client, tmpdir: @options[:tmpdir]
32
+ )
33
+ end
20
34
  end
21
35
  end
data/lib/s3repo/cache.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  require 'fileutils'
2
+ require 'tmpdir'
2
3
  require 'tempfile'
3
4
 
4
5
  module S3Repo
5
6
  ##
6
7
  # Cache object, stores S3 objects on disk
7
8
  class Cache < Base
8
- TMPDIRS = [ENV['S3REPO_TMPDIR'], ENV['TMPDIR'], '/tmp/s3repo']
9
+ TMPDIRS = [ENV['S3REPO_TMPDIR'], ENV['TMPDIR'], Dir.tmpdir, '/tmp/s3repo']
9
10
 
10
11
  def initialize(params = {})
11
12
  super
@@ -13,15 +14,19 @@ module S3Repo
13
14
  end
14
15
 
15
16
  def serve(key, refresh = true)
16
- path = expand_path key
17
- download(key, path) if refresh || !cached?(path)
18
- File.open(path) { |fh| fh.read }
17
+ File.open(download(key, refresh)) { |fh| fh.read }
19
18
  rescue Aws::S3::Errors::NoSuchKey
20
19
  nil
21
20
  end
22
21
 
23
22
  private
24
23
 
24
+ def download(key, refresh = true)
25
+ path = expand_path key
26
+ get_object(key, path) if refresh || !cached?(path)
27
+ path
28
+ end
29
+
25
30
  def expand_path(key)
26
31
  File.absolute_path(key, cachedir)
27
32
  end
@@ -30,17 +35,22 @@ module S3Repo
30
35
  File.exist? path
31
36
  end
32
37
 
33
- def download(key, path)
38
+ def get_object(key, path)
34
39
  FileUtils.mkdir_p File.dirname(path)
40
+ object = atomic_get_object(key, path)
41
+ etags[key] = object.etag
42
+ rescue Aws::S3::Errors::NotModified
43
+ return
44
+ end
45
+
46
+ def atomic_get_object(key, path)
35
47
  tmpfile = Tempfile.create(key, partialdir)
36
48
  object = client.get_object(
37
49
  key: key, if_none_match: etags[key], response_target: tmpfile
38
50
  )
39
51
  tmpfile.close
40
52
  File.rename tmpfile.path, path
41
- etags[key] = object.etag
42
- rescue Aws::S3::Errors::NotModified
43
- return
53
+ object
44
54
  end
45
55
 
46
56
  def etags
data/lib/s3repo/client.rb CHANGED
@@ -13,6 +13,11 @@ module S3Repo
13
13
  @api.respond_to?(method, include_all) || super
14
14
  end
15
15
 
16
+ def upload!(key, path)
17
+ puts "Uploading #{key}"
18
+ put_object key: key, body: File.open(path) { |fh| fh.read }
19
+ end
20
+
16
21
  private
17
22
 
18
23
  def method_missing(method, *args, &block)
@@ -0,0 +1,41 @@
1
+ require 'fileutils'
2
+ require 'tempfile'
3
+
4
+ module S3Repo
5
+ ##
6
+ # Metadata object, represents repo's DB file
7
+ class Metadata < Base
8
+ def initialize(params = {})
9
+ super
10
+ FileUtils.mkdir_p db_dir
11
+ end
12
+
13
+ def add_packages(paths)
14
+ @db_path = nil
15
+ paths.each do |path|
16
+ puts "Adding #{File.basename(path)} to repo.db"
17
+ run("repo-add #{db_path} #{path}")
18
+ end
19
+ client.upload!('repo.db', db_path)
20
+ end
21
+
22
+ private
23
+
24
+ def db_path
25
+ @db_path ||= download_db
26
+ end
27
+
28
+ def download_db
29
+ tmpfile = Tempfile.create(['repo', '.db.tar.xz'], db_dir)
30
+ tmpfile << file_cache.serve('repo.db')
31
+ tmpfile.close
32
+ tmpfile.path
33
+ end
34
+
35
+ def db_dir
36
+ @db_dir ||= File.absolute_path(
37
+ @options[:tmpdir] || Cache::TMPDIRS.compact.first
38
+ )
39
+ end
40
+ end
41
+ end
data/lib/s3repo/repo.rb CHANGED
@@ -8,12 +8,25 @@ module S3Repo
8
8
  super
9
9
  end
10
10
 
11
- def add_package(file)
12
- upload!(file)
11
+ def build_packages(paths)
12
+ paths.each do |path|
13
+ dir = File.dirname(path)
14
+ puts "Building #{File.basename(dir)}"
15
+ Dir.chdir(dir) { run 'makepkg -f' }
16
+ end
17
+ end
18
+
19
+ def add_packages(paths)
20
+ paths.each do |path|
21
+ key = File.basename(path)
22
+ next if include? key
23
+ client.upload!(key, path)
24
+ end
25
+ metadata.add_packages(paths)
13
26
  end
14
27
 
15
28
  def packages
16
- meta_cache.cache { parse_packages }
29
+ package_cache.cache { parse_packages }
17
30
  end
18
31
 
19
32
  def include?(key)
@@ -27,20 +40,12 @@ module S3Repo
27
40
 
28
41
  private
29
42
 
30
- def file_cache
31
- @file_cache ||= Cache.new(client: client, tmpdir: @options[:tmpdir])
32
- end
33
-
34
- def meta_cache
35
- @meta_cache ||= BasicCache::TimeCache.new lifetime: 600
43
+ def metadata
44
+ @metadata ||= Metadata.new(client: client, file_cache: file_cache)
36
45
  end
37
46
 
38
- def upload!(file)
39
- client.put_object(
40
- bucket: bucket,
41
- key: file,
42
- body: File.open(file) { |fh| fh.read }
43
- )
47
+ def package_cache
48
+ @package_cache ||= BasicCache::TimeCache.new lifetime: 600
44
49
  end
45
50
 
46
51
  def parse_packages
@@ -0,0 +1,5 @@
1
+ ##
2
+ # Define version
3
+ module S3Repo
4
+ VERSION = '0.1.0'
5
+ end
data/s3repo.gemspec CHANGED
@@ -1,6 +1,9 @@
1
+ $:.unshift File.expand_path('../lib/', __FILE__)
2
+ require 's3repo/version'
3
+
1
4
  Gem::Specification.new do |s|
2
5
  s.name = 's3repo'
3
- s.version = '0.0.3'
6
+ s.version = S3Repo::VERSION
4
7
  s.date = Time.now.strftime('%Y-%m-%d')
5
8
 
6
9
  s.summary = 'S3 Archlinux repo library'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3repo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Les Aker
@@ -142,7 +142,9 @@ files:
142
142
  - lib/s3repo/base.rb
143
143
  - lib/s3repo/cache.rb
144
144
  - lib/s3repo/client.rb
145
+ - lib/s3repo/metadata.rb
145
146
  - lib/s3repo/repo.rb
147
+ - lib/s3repo/version.rb
146
148
  - s3repo.gemspec
147
149
  - spec/s3repo_spec.rb
148
150
  - spec/spec_helper.rb