Created
October 30, 2014 23:42
-
-
Save coderanger/c389af664649a55b5727 to your computer and use it in GitHub Desktop.
Berks API Lite.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'openssl' | |
require 'tempfile' | |
require 'fog' | |
BUCKET = 'something' | |
ROOT = File.expand_path('..', __FILE__) | |
UNIVERSE = {} | |
# Connection to the storage provider | |
storage = Fog::Storage.new( | |
provider: 'AWS', | |
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], | |
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], | |
) | |
unless directory = storage.directories.get(BUCKET) | |
directory = storage.directories.create(key: BUCKET, public: true) | |
end | |
# Generate all the metadata | |
%x|knife cookbook metadata -a -o "#{ROOT}"| | |
# Find all cookbooks | |
Dir[File.join(ROOT, '*', 'metadata.json')].each do |path| | |
# Load the metadata | |
metadata = JSON.parse(IO.read(path)) | |
name = metadata['name'] | |
raise "NAME MISMATCH" unless path == File.join(ROOT, name, 'metadata.json') | |
# Compute the content hash | |
digest = OpenSSL::Digest::MD5.new | |
Dir[File.join(ROOT, name, '**', '*')].sort.each do |hash_path| | |
digest << hash_path | |
digest << IO.read(hash_path) if File.file?(hash_path) | |
end | |
tarball_name = "#{name}-#{digest.hexdigest}.tar.gz" | |
# Upload the tarball | |
unless file = directory.files.get(tarball_name) | |
tempfile = Tempfile.new([name, '.tar.gz']) | |
%x|tar -czvf #{tempfile.path} -C #{ROOT} #{name}| | |
file = directory.files.create( | |
key: tarball_name, | |
body: tempfile, | |
content_type: 'application/x-gzip', | |
) | |
tempfile.close! | |
end | |
# Create the universe data | |
UNIVERSE[name] = { | |
metadata['version'] => { | |
'endpoint_priority' => 1, | |
'platforms' => {}, | |
'dependencies' => metadata['dependencies'], | |
'location_type' => 'uri', | |
'location_path' => file.url(Time.now.to_i + 60*60*24), | |
} | |
} | |
end | |
# Write out the universe | |
directory.files.create( | |
key: 'universe', | |
body: UNIVERSE.to_json, | |
public: true, | |
content_type: 'application/json', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment