Class: VagrantPlugins::CommandDns::Action::Host::Destroy
- Inherits:
-
Object
- Object
- VagrantPlugins::CommandDns::Action::Host::Destroy
- Defined in:
- lib/vagrant-command-dns/action/host/destroy.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ Destroy
constructor
A new instance of Destroy.
Constructor Details
#initialize(app, env) ⇒ Destroy
Returns a new instance of Destroy.
9 10 11 12 |
# File 'lib/vagrant-command-dns/action/host/destroy.rb', line 9 def initialize(app, env) @app = app @logger = Log4r::Logger.new('vagrant_command_dns::action::host::destroy') end |
Instance Method Details
#call(env) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vagrant-command-dns/action/host/destroy.rb', line 14 def call(env) unless env[:machine].config.dns.__skip if env[:machine].state.id != :running raise Errors::MachineStateError, state: 'running' end end lines = [] env[:record_map].each do |hostname, ip| if env[:machine].config.dns.__skip record_pattern = Regexp.new('^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\s+' + hostname) else record_pattern = Regexp.new('^\s*' + ip + '\s+' + hostname) end File.open('/etc/hosts', 'r') do |f| f.each_line do |line| unless line.match(/#{record_pattern}/) lines.push(line.chomp) end end end end content = lines.join("\n").strip unless system("sudo sh -c 'echo \"#{content}\" > /etc/hosts'") env[:ui].error(I18n.t('vagrant_command_dns.command.host.edit_error')) end @app.call(env) end |