Class: ReleasedCookbook

Inherits:
Cookbook show all
Defined in:
lib/mofa/released_cookbook.rb

Instance Attribute Summary

Attributes inherited from Cookbook

#cookbooks_url, #mofa_yml, #mofa_yml_local, #name, #override_mofa_secrets, #pkg_dir, #pkg_name, #pkg_uri, #source_uri, #token, #type, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cookbook

#autodetect_type, create, #error, #ok, #run, #say

Constructor Details

#initialize(cookbook_name_or_path, override_mofa_secrets = nil) ⇒ ReleasedCookbook

Returns a new instance of ReleasedCookbook.



17
18
19
20
21
22
23
# File 'lib/mofa/released_cookbook.rb', line 17

def initialize(cookbook_name_or_path, override_mofa_secrets = nil)
  super()
  nv = ReleasedCookbook.get_name_and_version(cookbook_name_or_path)
  @name = nv['name']
  @version = nv['version']
  @override_mofa_secrets = override_mofa_secrets
end

Class Method Details

.exists?(cookbook_name_or_path) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/mofa/released_cookbook.rb', line 10

def self.exists?(cookbook_name_or_path)
  nv = get_name_and_version(cookbook_name_or_path)
  url = "#{Mofa::CLI::binrepo_base_url}/#{nv['name']}/#{nv['version']}/#{nv['name']}_#{nv['version']}-full.tar.gz"
  puts "Checking if cookbook exists: #{url}"
  RestClient.head(url)
end

.get_name_and_version(cookbook_name_or_path) ⇒ Object



3
4
5
6
7
8
# File 'lib/mofa/released_cookbook.rb', line 3

def self.get_name_and_version(cookbook_name_or_path)
  # TODO: this needs proper vaidation!
  name = cookbook_name_or_path.split(/@/).first
  version = cookbook_name_or_path.split(/@/).last
  { 'name' => name, 'version' => version }
end

Instance Method Details

#cleanupObject



37
38
39
40
41
# File 'lib/mofa/released_cookbook.rb', line 37

def cleanup
  say "Removing folder #{pkg_dir}...#{nl}"
  run "rm -rf #{pkg_dir}"
  ok
end

#cleanup!Object



116
117
118
119
120
121
122
123
# File 'lib/mofa/released_cookbook.rb', line 116

def cleanup!
  unless (Dir.entries("#{Mofa::CLI::option_tmp_dir}/.mofa") - %w{ . .. }).empty?
    say "Removing content of folder #{Mofa::CLI::option_tmp_dir}/.mofa"
    run "rm -r #{Mofa::CLI::option_tmp_dir}/.mofa/*"
  else
    say "Folder #{Mofa::CLI::option_tmp_dir}/.mofa is (already) clean."
  end
end

#executeObject



33
34
35
# File 'lib/mofa/released_cookbook.rb', line 33

def execute
  package
end

#load_mofa_ymlObject



103
104
105
# File 'lib/mofa/released_cookbook.rb', line 103

def load_mofa_yml
  @mofa_yml = MofaYml.load_from_file("#{pkg_dir}/.mofa.yml", self)
end

#load_mofa_yml_localObject



107
108
109
# File 'lib/mofa/released_cookbook.rb', line 107

def load_mofa_yml_local
  @mofa_yml_local = MofaYml.load_from_file("#{pkg_dir}/.mofa.local.yml", self)
end

#packageObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mofa/released_cookbook.rb', line 43

def package
  tar_verbose = (Mofa::CLI::option_debug) ? 'v' : ''
  mkdir_p @pkg_dir
  say "Downloading released cookbook from: #{cookbooks_url} to #{pkg_dir}/#{pkg_name}..."
  File.open("#{pkg_dir}/#{pkg_name}", "wb") do |saved_file|
    # the following "open" is provided by open-uri
    open(cookbooks_url, "rb") do |read_file|
      saved_file.write(read_file.read)
    end
  end
  mkdir_p "#{pkg_dir}/tmp"
  run "tar x#{tar_verbose}fz #{pkg_dir}/#{pkg_name} -C #{pkg_dir}/tmp/"

  # copy out data_bags if exists
  if File.directory?("#{pkg_dir}/tmp/cookbooks/#{name}/data_bags")
    FileUtils.cp_r "#{pkg_dir}/tmp/cookbooks/#{name}/data_bags", pkg_dir
  end

  # copy out recipes
  if File.directory?("#{pkg_dir}/tmp/cookbooks/#{name}/recipes")
    FileUtils.cp_r "#{pkg_dir}/tmp/cookbooks/#{name}/recipes", pkg_dir
  end

  # Sync in mofa_secrets
  if override_mofa_secrets
    if File.directory?("#{override_mofa_secrets}/#{name}")
      if File.directory?("#{override_mofa_secrets}/#{name}/cookbooks")
        run "rsync -vr #{override_mofa_secrets}/#{name}/cookbooks/ #{pkg_dir}/tmp/cookbooks/"
        if File.file?("#{override_mofa_secrets}/#{name}/.mofa.local.yml")
          FileUtils.cp "#{override_mofa_secrets}/#{name}/.mofa.local.yml", "#{pkg_dir}/tmp/cookbooks/#{name}/"
        end
        if File.file?("#{override_mofa_secrets}/#{name}/.mofa.yml")
          FileUtils.cp "#{override_mofa_secrets}/#{name}/.mofa.yml", "#{pkg_dir}/tmp/cookbooks/#{name}/"
        end
      else
        run "rsync -vr #{override_mofa_secrets}/#{name}/ #{pkg_dir}/tmp/cookbooks/#{name}/"
      end
    else
      say "Skipping non-existant mofa-secrets folder #{override_mofa_secrets}/#{name}"
    end
  end

  if File.file?("#{pkg_dir}/tmp/cookbooks/#{name}/.mofa.yml")
    FileUtils.cp "#{pkg_dir}/tmp/cookbooks/#{name}/.mofa.yml", pkg_dir
  end

  if File.file?("#{pkg_dir}/tmp/cookbooks/#{name}/.mofa.local.yml")
    FileUtils.cp "#{pkg_dir}/tmp/cookbooks/#{name}/.mofa.local.yml", pkg_dir
  end

  # reload mofa yml
  load_mofa_yml
  load_mofa_yml_local

  run "cd #{pkg_dir}/tmp/;tar c#{tar_verbose}fz #{pkg_dir}/#{pkg_name}.new ."
  run "rm #{pkg_dir}/#{pkg_name}"
  run "mv #{pkg_dir}/#{pkg_name}.new #{pkg_dir}/#{pkg_name}"
  run "rm -rf #{pkg_dir}/tmp/"
end

#prepareObject

————- Interface Methods



27
28
29
30
31
# File 'lib/mofa/released_cookbook.rb', line 27

def prepare
  @pkg_name ||= "#{name}_#{version}-full.tar.gz"
  @pkg_dir = "#{Mofa::CLI::option_tmp_dir}/.mofa/#{token}"
  set_cookbooks_url
end

#recipesObject



131
132
133
134
135
# File 'lib/mofa/released_cookbook.rb', line 131

def recipes
  raise 'Cookbook not unpacked yet or no recipes found.' unless (Dir.exists?("#{pkg_dir}/recipes"))
  recipes = Dir.entries("#{pkg_dir}/recipes").select { |f| f.match(/.rb$/) }
  recipes.map! { |f| f.gsub(/\.rb/, '') }
end

#set_cookbooks_urlObject



125
126
127
128
129
# File 'lib/mofa/released_cookbook.rb', line 125

def set_cookbooks_url
  say 'Using remote URI as cookbooks_url: '
  @cookbooks_url = "#{Mofa::CLI::binrepo_base_url}/#{@name}/#{@version}/#{@name}_#{@version}-full.tar.gz"
  say "#{@cookbooks_url}"
end

#source_dirObject

————- /Interface Methods



112
113
114
# File 'lib/mofa/released_cookbook.rb', line 112

def source_dir
  "#{pkg_dir}"
end