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, override_mofa_secrets = nil) ⇒ SourceCookbook

Returns a new instance of SourceCookbook.



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

def initialize(cookbook_name_or_path, override_mofa_secrets = nil)
  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}"

  @override_mofa_secrets = override_mofa_secrets

  autodetect_name
  autodetect_version
end

Instance Method Details

#autodetect_nameObject



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

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



68
69
70
71
72
# File 'lib/mofa/source_cookbook.rb', line 68

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mofa/source_cookbook.rb', line 112

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



35
36
37
38
39
# File 'lib/mofa/source_cookbook.rb', line 35

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

#cleanup!Object



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

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

#cleanup_and_repackageObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mofa/source_cookbook.rb', line 127

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

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

  inside pkg_dir do
    empty_directory 'tmp'
    run "tar x#{tar_verbose}fz #{pkg_name} -C tmp/"

    COOKBOOK_IGNORE.each do |remove_this|
      if File.exists?("tmp/cookbooks/#{name}/#{remove_this}")
        run "rm -rf tmp/cookbooks/#{name}/#{remove_this}"
      end
    end
  end
  inside "#{pkg_dir}/tmp" do
    # Sync in mofa_secrets
    if override_mofa_secrets
      path_translated = @override_mofa_secrets.sub(/^([a-zA-Z]):/,"/\1/")
      if File.directory?("#{path_translated}/#{name}/cookbooks")
        run "rsync -vr #{path_translated}/#{name}/cookbooks/ cookbooks/"
      end
    end
  end
  inside "#{pkg_dir}/tmp" do
    run "tar c#{tar_verbose}fz ../#{pkg_name}.new ."
  end
  inside pkg_dir do
    run "rm #{pkg_name}"
    run "mv #{pkg_name}.new #{pkg_name}"
    run 'rm -rf tmp/'
  end

  ok
end

#cookbook_folder?(source_dir) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#executeObject



31
32
33
# File 'lib/mofa/source_cookbook.rb', line 31

def execute
  package
end

#load_mofa_ymlObject



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

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

#load_mofa_yml_localObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/mofa/source_cookbook.rb', line 51

def load_mofa_yml_local
  if override_mofa_secrets
    say "-S Switch found - checking for .mofa.local.yml..."
    if File.file?("#{override_mofa_secrets}/#{name}/.mofa.local.yml")
      say ".mofa.local.yml found at #{override_mofa_secrets}/#{name}/.mofa.local.yml - copying it into source dir..."
      FileUtils.cp "#{override_mofa_secrets}/#{name}/.mofa.local.yml", "#{source_dir}/.mofa.local.yml"
    end
  end
  @mofa_yml_local = MofaYml.load_from_file("#{source_dir}/.mofa.local.yml", self)
end

#mofahub_available?Boolean

Returns:

  • (Boolean)


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

def mofahub_available?
  false
end

#packageObject



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

def package
  berks_install_package
  cleanup_and_repackage
end

#prepareObject

————- Interface Methods



21
22
23
24
25
26
27
28
29
# 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}_#{version}-SNAPSHOT.tar.gz"
  @pkg_dir = "#{source_dir}/.mofa/#{token}"

  set_cookbooks_url
end

#recipesObject



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

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

#set_cookbooks_urlObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/mofa/source_cookbook.rb', line 88

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



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

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