Class: Bookbinder::Deploy::Archive

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

Defined Under Namespace

Classes: FileDoesNotExist, NoNamespaceGiven

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, key: '', secret: '') ⇒ Archive

Returns a new instance of Archive.



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

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

Instance Method Details

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



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

def create_and_upload_tarball(
  app_dir: 'final_app',
  bucket: '',
  build_number: nil,
  namespace: nil
)
  tarball_filename, tarball_path = create_tarball(app_dir, build_number, namespace)
  upload_file(bucket, tarball_filename, tarball_path)
  Success.new("Green build ##{build_number} has been uploaded to S3 for #{namespace}")
end

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

Raises:



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

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 = Artifact.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



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

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

#upload_file(bucket, name, source_path) ⇒ Object



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

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