Class: VagrantPlugins::CommandDns::Action::Host::Create
- Inherits:
-
Object
- Object
- VagrantPlugins::CommandDns::Action::Host::Create
- Defined in:
- lib/vagrant-command-dns/action/host/create.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ Create
constructor
A new instance of Create.
Constructor Details
#initialize(app, env) ⇒ Create
Returns a new instance of Create.
9 10 11 12 |
# File 'lib/vagrant-command-dns/action/host/create.rb', line 9 def initialize(app, env) @app = app @logger = Log4r::Logger.new('vagrant_command_dns::action::host::create') 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/create.rb', line 14 def call(env) if env[:machine].state.id != :running raise Errors::MachineStateError, state: 'running' end lines = [] env[:record_map].each do |hostname, ip| loose_pattern = Regexp.new('^\s*[0-9]{1,3}[0-9]{1,3}[0-9]{1,3}[0-9]{1,3}\s+' + hostname) precise_pattern = Regexp.new('^\s*' + ip + '\s+' + hostname) File.open('/etc/hosts', 'r') do |f| f.each_line do |line| if line.match(/#{precise_pattern}/) env[:ui].info(I18n.t('vagrant_command_dns.command.host.create_exists_match', ip: ip, hostname: hostname)) return elsif line.match(/#{loose_pattern}/) env[:ui].info(I18n.t('vagrant_command_dns.command.host.create_exists_conflict')) end end end lines.push("#{ip} #{hostname}") 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 |