Vagrant-Butcher 项目使用教程
1. 项目的目录结构及介绍
Vagrant-Butcher 项目的目录结构如下:
.
├── Gemfile
├── Gemfile.lock
├── LICENSE.txt
├── README.md
├── Rakefile
├── Vagrantfile
├── vagrant-butcher.gemspec
├── lib
│ └── vagrant-butcher
│ └── plugin.rb
└── spec
└── vagrant-butcher_spec.rb
目录介绍
Gemfile
和Gemfile.lock
:定义了项目的依赖关系。LICENSE.txt
:项目的许可证文件。README.md
:项目的基本介绍和使用说明。Rakefile
:用于定义项目的自动化任务。Vagrantfile
:Vagrant 的配置文件示例。vagrant-butcher.gemspec
:项目的 gem 规范文件。lib/vagrant-butcher
:包含项目的主要代码文件。spec
:包含项目的测试文件。
2. 项目的启动文件介绍
项目的启动文件位于 lib/vagrant-butcher/plugin.rb
。这个文件定义了 Vagrant-Butcher 插件的主要功能和行为。
module VagrantPlugins
module Butcher
class Plugin < Vagrant.plugin("2")
name "butcher"
description <<-DESC
This plugin will automatically delete the Chef client and node when you destroy the VM.
DESC
action_hook(:butcher, :machine_action_destroy) do |hook|
hook.after(Vagrant::Action::Builtin::DestroyConfirm, Action::DeleteChefNode)
hook.after(Vagrant::Action::Builtin::DestroyConfirm, Action::DeleteChefClient)
end
end
end
end
启动文件介绍
name "butcher"
:定义插件的名称为 "butcher"。description
:插件的描述信息。action_hook
:定义在销毁虚拟机时的动作钩子,用于删除 Chef 客户端和节点。
3. 项目的配置文件介绍
项目的配置文件主要是 Vagrantfile
和 vagrant-butcher.gemspec
。
Vagrantfile
Vagrantfile
是一个示例配置文件,展示了如何使用 Vagrant-Butcher 插件。
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.provision "chef_client" do |chef|
chef.chef_server_url = "https://api.chef.io/organizations/myorg"
chef.validation_key_path = "validation.pem"
chef.validation_client_name = "myorg-validator"
end
end
vagrant-butcher.gemspec
vagrant-butcher.gemspec
是项目的 gem 规范文件,定义了项目的元数据和依赖关系。
Gem::Specification.new do |spec|
spec.name = "vagrant-butcher"
spec.version = "2.3.1"
spec.authors = ["Cassiano Leal"]
spec.email = ["cassianoleal@gmail.com"]
spec.description = %q{Delete Chef client and node when destroying Vagrant VM}
spec.summary = spec.description
spec.homepage = "https://github.com/c10l/vagrant-butcher"
spec.license = "MIT"
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_runtime_dependency "ridley", ">= 4.0.0"
spec.add_development_dependency "bundler", ">= 1.3"
spec.add_development_dependency "date", ">= 0"
spec.add_development_dependency "did_you