Class: Machinery::WorkloadMapper

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

Overview

Copyright © 2013-2016 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Instance Method Summary collapse

Instance Method Details

#compose_service(workload, config) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/workload_mapper.rb', line 48

def compose_service(workload, config)
  name = config["service"]
  service = {
    name => compact(load_compose_template(workload))
  }
  fill_in_template(service[name], config["parameters"])
  service
end

#extract(system_description, workloads, path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/workload_mapper.rb', line 95

def extract(system_description, workloads, path)
  Dir.mktmpdir do |dir|
    unless workloads.select { |_, w| w["data"] }.empty?
      system_description.unmanaged_files.export_files_as_tarballs(dir)
      workloads.each do |workload, config|
        config.fetch("data", {}).each do |origin, destination|
          file = system_description.unmanaged_files.find { |f| f.name == origin }
          file ||= system_description.changed_config_files.find { |f| f.name == origin }
          if file && file.directory?
            tgz_file = File.join(dir, "trees", "#{origin.chop}.tgz")
            output_path = File.join(path, workload, destination)
            FileUtils.mkdir_p(output_path)
            strip_number = origin.split("/").size - 1
            Cheetah.run("tar", "zxf", tgz_file, "-C", output_path, "--strip=#{strip_number}")
            copy_workload_changed_config_files(workload, output_path)
          end
          if file && file.file?
            output_path = File.join(path, workload, destination, origin)
            FileUtils.mkdir_p(File.dirname(output_path))
            File.write(output_path, file.content)
          end
        end
      end
    end
  end
end

#fill_in_template(service, parameters) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/workload_mapper.rb', line 85

def fill_in_template(service, parameters)
  service.each do |key, value|
    if value.is_a?(Hash)
      fill_in_template(value, parameters)
    elsif value.is_a?(Symbol)
      service[key] = parameters[value.to_s]
    end
  end
end

#identify_workloads(system_description) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/workload_mapper.rb', line 64

def identify_workloads(system_description)
  system_description.assert_scopes("services", "unmanaged_files", "changed_config_files")

  ["unmanaged_files", "changed_config_files"].each do |scope|
    next if system_description.scope_extracted?(scope)

    raise Machinery::Errors::SystemDescriptionError.new(
      "Required scope: '#{scope}' was not extracted. Can't continue."
    )
  end

  workloads = {}

  Dir["#{File.expand_path(workload_mapper_path)}/*"].each do |workload_dir|
    mapper = Machinery::WorkloadMapperDSL.new(system_description)
    workload = mapper.check_clue(File.read(File.join(workload_dir, "clue.rb")))
    workloads.merge!(workload.to_h)
  end
  workloads
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/workload_mapper.rb', line 32

def link_compose_services(services)
  services.each do |service, config|
    config.fetch("links", {}).each do |linked_service|
      services[service]["environment"] ||= {}
      if services[linked_service]
        vars = services[linked_service].fetch("environment", {})
        services[service]["environment"].merge!(vars)
      else
        raise Machinery::Errors::ComposeServiceLink.new(
          "Could not detect '#{linked_service}', which is referenced by '#{service}'."\
        )
      end
    end
  end
end

#load_compose_template(workload) ⇒ Object



57
58
59
60
61
62
# File 'lib/workload_mapper.rb', line 57

def load_compose_template(workload)
  template = YAML::load(
    File.read(File.join(workload_mapper_path, workload, "compose-template.yml"))
  )
  template[workload]
end

#save(workloads, path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/workload_mapper.rb', line 18

def save(workloads, path)
  workloads.each do |workload, config|
    FileUtils.mkdir_p(File.join(path, workload))
    FileUtils.cp_r(
      File.join(workload_mapper_path, workload, "container", "."),
      File.join(path, workload)
    )
  end
  compose_services = compose_services(workloads)
  linked_services = link_compose_services(compose_services)
  File.write(File.join(path, "docker-compose.yml"), linked_services.to_yaml)
  linked_services
end