Class: Chef::Knife::CookbookSiteVendor

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/cookbook_site_vendor.rb

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#runObject



41
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/knife/cookbook_site_vendor.rb', line 41

def run
  if config[:cookbook_path]
    Chef::Config[:cookbook_path] = config[:cookbook_path]
  else
    config[:cookbook_path] = Chef::Config[:cookbook_path]
  end

  # Check to ensure we have a valid source of cookbooks before continuing
  unless File.directory?(config[:cookbook_path].first)
    Chef::Log.error( File.join(config[:cookbook_path].first, " doesn't exist!.  Make sure you have cookbook_path configured correctly"))
    exit 1
  end

  vendor_path = File.expand_path(File.join(config[:cookbook_path].first))
  cookbook_path = File.join(vendor_path, name_args[0])
  upstream_file = File.join(vendor_path, "#{name_args[0]}.tar.gz")
  branch_name = "chef-vendor-#{name_args[0]}"

  download = Chef::Knife::CookbookSiteDownload.new
  download.config[:file] = upstream_file 
  download.name_args = name_args
  download.run

  Chef::Log.info("Checking out the master branch.")
  Chef::Mixin::Command.run_command(:command => "git checkout master", :cwd => vendor_path) 
  Chef::Log.info("Checking the status of the vendor branch.")
  status, branch_output, branch_error = Chef::Mixin::Command.output_of_command("git branch --no-color | grep #{branch_name}", :cwd => vendor_path) 
  if branch_output =~ /#{Regexp.escape(branch_name)}$/m
    Chef::Log.info("Vendor branch found.")
    Chef::Mixin::Command.run_command(:command => "git checkout #{branch_name}", :cwd => vendor_path)
  else
    Chef::Log.info("Creating vendor branch.")
    Chef::Mixin::Command.run_command(:command => "git checkout -b #{branch_name}", :cwd => vendor_path)
  end
  Chef::Log.info("Removing pre-existing version.")
  Chef::Mixin::Command.run_command(:command => "rm -r #{cookbook_path}", :cwd => vendor_path) if File.directory?(cookbook_path)
  Chef::Log.info("Uncompressing #{name_args[0]} version #{download.version}.")
  Chef::Mixin::Command.run_command(:command => "tar zxvf #{upstream_file}", :cwd => vendor_path)
  Chef::Mixin::Command.run_command(:command => "rm #{upstream_file}", :cwd => vendor_path)
  Chef::Log.info("Adding changes.")
  Chef::Mixin::Command.run_command(:command => "git add #{name_args[0]}", :cwd => vendor_path)

  Chef::Log.info("Committing changes.")
  changes = true
  begin
    Chef::Mixin::Command.run_command(:command => "git commit -a -m 'Import #{name_args[0]} version #{download.version}'", :cwd => vendor_path)
  rescue Chef::Exceptions::Exec => e
    Chef::Log.warn("Checking out the master branch.")
    Chef::Log.warn("No changes from current vendor #{name_args[0]}")
    Chef::Mixin::Command.run_command(:command => "git checkout master", :cwd => vendor_path) 
    changes = false
  end

  if changes
    Chef::Log.info("Creating tag chef-vendor-#{name_args[0]}-#{download.version}.")
    Chef::Mixin::Command.run_command(:command => "git tag -f chef-vendor-#{name_args[0]}-#{download.version}", :cwd => vendor_path)
    Chef::Log.info("Checking out the master branch.")
    Chef::Mixin::Command.run_command(:command => "git checkout master", :cwd => vendor_path)
    Chef::Log.info("Merging changes from #{name_args[0]} version #{download.version}.")

    Dir.chdir(vendor_path) do
      if system("git merge #{branch_name}")
        Chef::Log.info("Cookbook #{name_args[0]} version #{download.version} successfully vendored!")
      else
        Chef::Log.error("You have merge conflicts - please resolve manually!")
        Chef::Log.error("(Hint: cd #{vendor_path}; git status)") 
        exit 1
      end
    end
  end

  if config[:deps]
    md = Chef::Cookbook::Metadata.new
    md.from_file(File.join(cookbook_path, "metadata.rb"))
    md.dependencies.each do |cookbook, version_list|
      # Doesn't do versions.. yet
      nv = Chef::Knife::CookbookSiteVendor.new
      nv.config = config
      nv.name_args = [ cookbook ]
      nv.run
    end
  end
end