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)

Instance Attribute Summary collapse

Attributes inherited from Cookbook

#cookbooks_url, #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.



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

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 Attribute Details

#pkg_dirObject

Returns the value of attribute pkg_dir.



2
3
4
# File 'lib/mofa/source_cookbook.rb', line 2

def pkg_dir
  @pkg_dir
end

#pkg_nameObject

Returns the value of attribute pkg_name.



3
4
5
# File 'lib/mofa/source_cookbook.rb', line 3

def pkg_name
  @pkg_name
end

Instance Method Details

#autodetect_nameObject



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

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



50
51
52
53
54
# File 'lib/mofa/source_cookbook.rb', line 50

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mofa/source_cookbook.rb', line 94

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



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

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

#cleanup!Object



61
62
63
64
65
66
67
68
# File 'lib/mofa/source_cookbook.rb', line 61

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

#cleanup_and_repackageObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mofa/source_cookbook.rb', line 109

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

  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 -r #{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 -r #{pkg_dir}/tmp/"

  end

  ok
end

#cookbook_folder?(source_dir) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/mofa/source_cookbook.rb', line 81

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
31
# File 'lib/mofa/source_cookbook.rb', line 28

def execute
  package
  stage
end

#mofahub_available?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/mofa/source_cookbook.rb', line 85

def mofahub_available?
  false
end

#packageObject



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

def package
  berks_install_package
  cleanup_and_repackage
end

#prepareObject

————- Interface Methods



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

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}-#{token}-SNAPSHOT.tar.gz"
  @pkg_dir = "#{source_dir}/.mofa/#{token}"
end

#recipesObject



56
57
58
59
# File 'lib/mofa/source_cookbook.rb', line 56

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

#source_dirObject

————- /Interface Methods



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

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

#stageObject



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

def stage
  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