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



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

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



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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mofa/source_cookbook.rb', line 105

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



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

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



120
121
122
123
124
125
126
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 120

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
      if File.directory?("#{override_mofa_secrets}/#{name}/cookbooks")
        run "rsync -vr #{override_mofa_secrets}/#{name}/cookbooks/ cookbooks/"
        if File.file?("#{override_mofa_secrets}/#{name}/.mofa.local.yml")
          FileUtils.cp "#{override_mofa_secrets}/#{name}/.mofa.local.yml", "cookbooks/#{name}/"
        end
        if File.file?("#{override_mofa_secrets}/#{name}/.mofa.yml")
          FileUtils.cp "#{override_mofa_secrets}/#{name}/.mofa.yml", "cookbooks/#{name}/"
        end
      else
        run "rsync -vr #{override_mofa_secrets}/#{name}/ cookbooks/#{name}/"
      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)


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

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

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

#mofahub_available?Boolean

Returns:

  • (Boolean)


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

def mofahub_available?
  false
end

#packageObject



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

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



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

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

#set_cookbooks_urlObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/mofa/source_cookbook.rb', line 81

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