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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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::MachineState, state: 'running' end end # read the file into memory to save reading it for each alias lines = [] File.open('/etc/hosts', 'r') do |file| file.each_line do |line| lines.push(line.chomp) end end destroyed = {} 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 lines.each do |line| if line.match(/#{record_pattern}/) lines.delete(line) destroyed[hostname] = ip end end end if destroyed.length > 0 content = lines.join("\n").strip if system("sudo sh -c 'echo \"#{content}\" > /etc/hosts'") destroyed.each do |hostname, ip| env[:ui].info(I18n.t('vagrant_command_dns.command.host.destroy_success', hostname: hostname)) end else env[:ui].error(I18n.t('vagrant_command_dns.command.host.edit_error')) end end @app.call(env) end |