Class: Bookbinder::Archive

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

Defined Under Namespace

Classes: FileDoesNotExist, NoNamespaceGiven

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, time_fetcher: TimeFetcher.new, key: '', secret: '') ⇒ Archive

Returns a new instance of Archive.



12
13
14
15
16
17
# File 'lib/bookbinder/archive.rb', line 12

def initialize(logger: nil, time_fetcher: TimeFetcher.new, key: '', secret: '')
  @logger = logger
  @time_fetcher = time_fetcher
  @aws_key = key
  @aws_secret_key = secret
end

Instance Method Details

#create_and_upload_tarball(build_number: nil, app_dir: 'final_app', bucket: '', namespace: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/bookbinder/archive.rb', line 19

def create_and_upload_tarball(build_number: nil, app_dir: 'final_app', bucket: '', namespace: nil)
  build_number ||= time_fetcher.current_time
  raise 'You must provide a namespace to push an identifiable build.' unless namespace

  tarball_filename, tarball_path = create_tarball(app_dir, build_number, namespace)

  upload_file(bucket, tarball_filename, tarball_path)
  @logger.log "Green build ##{build_number.to_s.green} has been uploaded to S3 for #{namespace.to_s.cyan}"
end

#download(download_dir: nil, bucket: nil, build_number: nil, namespace: nil) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bookbinder/archive.rb', line 36

def download(download_dir: nil, bucket: nil, build_number: nil, namespace: nil)
  raise NoNamespaceGiven, 'One must specify a namespace to find files in this bucket' unless namespace

  directory = connection.directories.get bucket
  build_number ||= latest_build_number_for_namespace(directory, namespace)
  filename = ArtifactNamer.new(namespace, build_number, 'tgz').filename

  s3_file = directory.files.get(filename)
  raise FileDoesNotExist, "Unable to find tarball on AWS for book '#{namespace}', build number: #{build_number}" unless s3_file

  downloaded_file = File.join(Dir.mktmpdir, 'downloaded.tgz')
  File.open(downloaded_file, 'wb') { |f| f.write(s3_file.body) }
  Dir.chdir(download_dir) { `tar xzf #{downloaded_file}` }

  @logger.log "Green build ##{build_number.to_s.green} has been downloaded from S3 and untarred into #{download_dir.to_s.cyan}"
end

#tarball_name_regex(namespace) ⇒ Object



53
54
55
# File 'lib/bookbinder/archive.rb', line 53

def tarball_name_regex(namespace)
  /^#{namespace}-(\d+_?\d*)\.tgz/
end

#upload_file(bucket, name, source_path) ⇒ Object



29
30
31
32
33
34
# File 'lib/bookbinder/archive.rb', line 29

def upload_file(bucket, name, source_path)
  find_or_create_directory(bucket).
    files.create(key: name,
                 body: File.read(source_path),
                 public: true)
end