Class: Cloudspin::Stack::RemoteDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudspin/stack/remote_definition.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_location) ⇒ RemoteDefinition

Returns a new instance of RemoteDefinition.



8
9
10
# File 'lib/cloudspin/stack/remote_definition.rb', line 8

def initialize(definition_location)
  @definition_location = definition_location
end

Class Method Details

.is_remote?(definition_location) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/cloudspin/stack/remote_definition.rb', line 12

def self.is_remote?(definition_location)
  /^http.*\.zip$/.match definition_location
end

Instance Method Details

#clear_folder(folder_to_clear) ⇒ Object



74
75
76
# File 'lib/cloudspin/stack/remote_definition.rb', line 74

def clear_folder(folder_to_clear)
  FileUtils.remove_entry_secure(folder_to_clear)
end

#download_artefact(artefact_url) ⇒ Object



20
21
22
23
24
# File 'lib/cloudspin/stack/remote_definition.rb', line 20

def download_artefact(artefact_url)
  download_dir = Dir.mktmpdir(['cloudspin-', '-download'])
  zipfile = "#{download_dir}/undetermined-spin-stack-artefact.zip"
  download_file(artefact_url, zipfile)
end

#download_file(remote_file, local_file, tries = 0) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cloudspin/stack/remote_definition.rb', line 26

def download_file(remote_file, local_file, tries = 0)
  raise "Too many redirects (#{remote_file})" if tries > 9
  remote_file_uri = URI(remote_file)

  Net::HTTP.start(
    remote_file_uri.host,
    remote_file_uri.port,
    :use_ssl => remote_file_uri.scheme == 'https'
  ) do |http|
    request = Net::HTTP::Get.new(remote_file_uri)
    http.request(request) do |response|
      case response
        when Net::HTTPSuccess     then write_local_file(response, local_file)
        when Net::HTTPRedirection then download_file(response['Location'], local_file, tries + 1)
        else
          raise "Request to '#{remote_file_uri}' failed: #{response.error} #{response.inspect}"
        end
    end
  end

  # puts "DEBUG: Downloaded file to #{local_file}"
  local_file
end

#fetch(local_folder) ⇒ Object



16
17
18
# File 'lib/cloudspin/stack/remote_definition.rb', line 16

def fetch(local_folder)
  unpack(download_artefact(@definition_location), local_folder)
end

#path_of_configuration_file_in(zipfile_path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cloudspin/stack/remote_definition.rb', line 82

def path_of_configuration_file_in(zipfile_path)
  zipfile = Zip::File.open(zipfile_path)
  list_of_files = begin
    zipfile.entries.select { |entry|
      /^stack-definition.yaml$/.match entry.name or /[\.\/]stack-definition.yaml$/.match entry.name
    }
  ensure
    zipfile.close
  end
  raise MissingStackDefinitionConfigurationFileError, "No configuration file in #{zipfile_path}" if list_of_files.empty?
  list_of_files.first.name
end

#path_of_source_in(zipfile_path) ⇒ Object



78
79
80
# File 'lib/cloudspin/stack/remote_definition.rb', line 78

def path_of_source_in(zipfile_path)
  File.dirname(path_of_configuration_file_in(zipfile_path))
end

#unpack(zipfile, where_to_put_it) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cloudspin/stack/remote_definition.rb', line 58

def unpack(zipfile, where_to_put_it)
  folder_name = path_of_source_in(zipfile)
  # puts "DEBUG: Unzipping #{zipfile} to #{where_to_put_it}"
  clear_folder(where_to_put_it)
  Zip::File.open(zipfile) { |zip_file|
    zip_file.each { |f|
      f_path = File.join(where_to_put_it, f.name)
      FileUtils.mkdir_p(File.dirname(f_path))
      # puts "DEBUG: Extracting #{f} to #{f_path}"
      zip_file.extract(f, f_path) unless File.exist?(f_path)
    }
  }
  raise MissingStackDefinitionConfigurationFileError unless File.exists? "#{where_to_put_it}/#{folder_name}/stack-definition.yaml"
  "#{where_to_put_it}/#{folder_name}"
end

#write_local_file(response, local_file) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/cloudspin/stack/remote_definition.rb', line 50

def write_local_file(response, local_file)
  open(local_file, 'wb') do |io|
    response.read_body do |chunk|
      io.write chunk
    end
  end
end