Class: SourceCookbook

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

Constant Summary collapse

COOKBOOK_IGNORE =
%w(.mofa .idea .kitchen .vagrant .bundle test .git)

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

Instance Method Summary collapse

Methods inherited from Cookbook

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

Constructor Details

#initialize(cookbook_name_or_path) ⇒ SourceCookbook

Returns a new instance of SourceCookbook.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mofa/source_cookbook.rb', line 4

def initialize(cookbook_name_or_path)
  super()
  path = Pathname.new(cookbook_name_or_path)

  say "Looking for Cookbook Sources in Path #{path}..."
  @source_uri = "file://#{path.realpath}"

  say "source_dir=#{source_dir}"

  autodetect_name
  autodetect_version
end

Instance Method Details

#autodetect_nameObject



52
53
54
55
56
# File 'lib/mofa/source_cookbook.rb', line 52

def autodetect_name
  say "Autodetecting Cookbook Name... "
  @name = open("#{source_dir}/metadata.rb").grep(/^name/)[0].gsub(/^name[^a-zA-Z0-9_-]*/, '').gsub(/.$/, '').chomp
  say "#{name}"
end

#autodetect_versionObject



58
59
60
61
62
# File 'lib/mofa/source_cookbook.rb', line 58

def autodetect_version
  say "Autodetecting Cookbook Version... "
  @version = open("#{source_dir}/metadata.rb").grep(/^version/)[0].gsub(/^version[^0-9\.]*/, '').gsub(/.$/, '').chomp
  say "#{version}"
end

#berks_install_packageObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mofa/source_cookbook.rb', line 102

def berks_install_package
  say "Running \"berks install\" and \"berks package\" on Cookbook in #{source_dir}...#{nl}"

  redirect_stdout = (Mofa::CLI::option_verbose) ? '' : '> /dev/null'
  Bundler.with_clean_env do
    inside source_dir do
      mkdir_p pkg_dir
      run "berks install #{redirect_stdout}"
      run "berks package #{pkg_dir}/#{pkg_name} #{redirect_stdout}"
    end
  end

  ok
end

#cleanupObject



32
33
34
35
36
# File 'lib/mofa/source_cookbook.rb', line 32

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

#cleanup!Object



69
70
71
72
73
74
75
76
# File 'lib/mofa/source_cookbook.rb', line 69

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

#cleanup_and_repackageObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mofa/source_cookbook.rb', line 117

def cleanup_and_repackage
  say "Shrinking Cookbook #{pkg_name}... "

  tar_verbose = (Mofa::CLI::option_debug) ? 'v' : ''

  inside source_dir do

    mkdir_p "#{pkg_dir}/tmp"
    run "tar x#{tar_verbose}fz #{pkg_dir}/#{pkg_name} -C #{pkg_dir}/tmp/"

    COOKBOOK_IGNORE.each do |remove_this|
      if File.exists?("#{pkg_dir}/tmp/cookbooks/#{name}/#{remove_this}")
        run "rm -rf #{pkg_dir}/tmp/cookbooks/#{name}/#{remove_this}"
      end
    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

  ok
end

#cookbook_folder?(source_dir) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mofa/source_cookbook.rb', line 89

def cookbook_folder?(source_dir)
  File.exist?(source_dir) && File.exists?("#{source_dir}/metadata.rb") && File.exists?("#{source_dir}/recipes")
end

#executeObject



28
29
30
# File 'lib/mofa/source_cookbook.rb', line 28

def execute
  package
end

#load_mofa_ymlObject



44
45
46
# File 'lib/mofa/source_cookbook.rb', line 44

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

#load_mofa_yml_localObject



48
49
50
# File 'lib/mofa/source_cookbook.rb', line 48

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

#mofahub_available?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mofa/source_cookbook.rb', line 93

def mofahub_available?
  false
end

#packageObject



97
98
99
100
# File 'lib/mofa/source_cookbook.rb', line 97

def package
  berks_install_package
  cleanup_and_repackage
end

#prepareObject

————- Interface Methods



19
20
21
22
23
24
25
26
# File 'lib/mofa/source_cookbook.rb', line 19

def prepare
  fail "Source URI is not a file:// URI!" unless source_uri =~ /^file:\/\/.*/
  fail "Folder #{source_dir} is not a Cookbook Folder!" unless cookbook_folder?(source_dir)

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

#recipesObject



64
65
66
67
# File 'lib/mofa/source_cookbook.rb', line 64

def recipes
  recipes = Dir.entries("#{source_dir}/recipes").select { |f| f.match(/.rb$/) }
  recipes.map! { |f| f.gsub(/\.rb/, '') }
end

#set_cookbooks_urlObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/mofa/source_cookbook.rb', line 78

def set_cookbooks_url
  if mofahub_available?
    say 'Staging (uploading to mofa-hub) Cookbook Snapshot: '
    @cookbooks_url = upload_to_mofahub
  else
    say 'Using local URI as cookbooks_url: '
    @cookbooks_url = "file://#{pkg_dir}/#{pkg_name}"
  end
  say "#{@cookbooks_url}"
end

#source_dirObject

————- /Interface Methods



40
41
42
# File 'lib/mofa/source_cookbook.rb', line 40

def source_dir
  source_uri.gsub(/^file:\/\//, '')
end