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, #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) ⇒ ReleasedCookbook

Returns a new instance of ReleasedCookbook.



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

def initialize(cookbook_name_or_path)
  super()
  nv = ReleasedCookbook.get_name_and_version(cookbook_name_or_path)
  @name = nv['name']
  @version = nv['version']
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::Config.config['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



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

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

#cleanup!Object



84
85
86
87
88
89
90
91
# File 'lib/mofa/released_cookbook.rb', line 84

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

#executeObject



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

def execute#
  package
end

#load_mofa_ymlObject



71
72
73
# File 'lib/mofa/released_cookbook.rb', line 71

def load_mofa_yml
  @mofa_yml = MofaYml.load_from_file(".mofa.yml", self)
end

#load_mofa_yml_localObject



75
76
77
# File 'lib/mofa/released_cookbook.rb', line 75

def load_mofa_yml_local
  @mofa_yml_local = MofaYml.load_from_file(".mofa.local.yml", self)
end

#packageObject



42
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
# File 'lib/mofa/released_cookbook.rb', line 42

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

  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



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

def prepare
  @pkg_name ||= "#{name}_#{version}-full.tar.gz"
  @pkg_dir = "#{Mofa::Config.config['tmp_dir']}/.mofa/#{token}"
  set_cookbooks_url
end

#recipesObject



99
100
101
102
103
# File 'lib/mofa/released_cookbook.rb', line 99

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



93
94
95
96
97
# File 'lib/mofa/released_cookbook.rb', line 93

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

#source_dirObject

————- /Interface Methods



80
81
82
# File 'lib/mofa/released_cookbook.rb', line 80

def source_dir
  "#{pkg_dir}"
end