Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
Added new actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Mar 10, 2014
1 parent 5302cac commit f5b8207
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
### 3.1.0 / 2014-03-09

* Added `restart`, `clean` and `status` commands.
* The `address` configuration option is now `bind_addresses` and it supports multiple values.

### 3.0.8 / 2014-03-08

* Installation fixes.
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -31,6 +31,15 @@ Of course, DevDNSd is inspired by [pow](https://github.com/37signals/pow), but i

**You're done!**

## Dual stack (IPv4/IPv6) usage

If you experience slowness in resolving host from clients (Chrome, curl etc.) when you don't specify the address type, make sure you have both a `A` and `AAAA` rules set for the same host, like this:

```ruby
config.add_rule(/\.dev$/, "127.0.0.1")
config.add_rule(/\.dev$/, "::1", :AAAA)
```

## Advanced usage

Just type `devdnsd help` and you'll see all available options.
Expand Down
23 changes: 23 additions & 0 deletions bin/devdnsd
Expand Up @@ -27,11 +27,24 @@ Bovem::Application.create do
action { |command| DevDNSd::Application.instance(command).action_start }
end

command :status do
description localizer.command_status
action { |command|
command.application.options[:log_file].set("STDOUT")
DevDNSd::Application.instance(command).action_status
}
end

command :stop do
description localizer.command_stop
action { |command| DevDNSd::Application.instance(command).action_stop }
end

command :restart do
description localizer.command_restart
action { |command| DevDNSd::Application.instance(command).action_restart }
end

command :install do
description localizer.command_install
action { |command|
Expand All @@ -48,6 +61,16 @@ Bovem::Application.create do
}
end

command :clean do
description localizer.command_clean
action { |command|
command.application.options[:log_file].set("STDOUT")
app = DevDNSd::Application.instance(command)
app.logger.warn(app.i18n.admin_privileges_warning)
app.dns_update
}
end

command :aliases do
description localizer.command_aliases
option :interface, [], {type: String, help: localizer.application_help_interface, default: "lo0", meta: localizer.application_meta_interface}
Expand Down
36 changes: 35 additions & 1 deletion lib/devdnsd/application.rb
Expand Up @@ -126,6 +126,31 @@ def action_stop
true
end

# Restarts the server in background.
#
# @return [Boolean] `true` if action succeeded, `false` otherwise.
def action_restart
action_stop
action_start
true
end

# Shows the status of the server
#
# @return [Boolean] `true` if action succeeded, `false` otherwise.
def action_status
daemon = self.class
status = RExec::Daemon::ProcessFile.status(daemon)
pid = RExec::Daemon::ProcessFile.recall(daemon).to_s
status = :crashed if status == :unknown && daemon.crashed?

if status == :running then
logger.info(replace_markers(i18n.status_running(pid)))
elsif
logger.info(replace_markers(i18n.send("status_#{status}")))
end
end

# Adds aliases to an interface.
#
# @param options [Hash] The options provided by the user.
Expand Down Expand Up @@ -512,7 +537,8 @@ module Server
# @return [Object] The result of stop callbacks.
def perform_server
application = self
RubyDNS::run_server(listen: [[:udp, @config.address, @config.port.to_integer]]) do

RubyDNS::run_server(listen: build_listen_interfaces) do
self.logger = application.logger

match(/.+/, DevDNSd::Application::ANY_CLASSES) do |transaction, match_data|
Expand Down Expand Up @@ -564,6 +590,14 @@ def process_rule_in_classes(rule, match_data, transaction)
end

private
# Builds the list of listening interfaces.
#
# @return [Array] Array of addresses.
def build_listen_interfaces
port = @config.port.to_integer
@config.bind_addresses.ensure_array {|address| [:udp, address, port] }
end

# Performs the processing of a rule.
#
# @param rule [Rule] The rule to process.
Expand Down
4 changes: 2 additions & 2 deletions lib/devdnsd/configuration.rb
Expand Up @@ -10,8 +10,8 @@ class Configuration < Bovem::Configuration
# If to run the server in foreground. Default: `false`.
property :foreground, default: false

# The address to listen to. Default: `0.0.0.0`.
property :address, default: "0.0.0.0"
# The addresses to listen to. Default: `["0.0.0.0"]`.
property :bind_addresses, default: ["0.0.0.0"]

# The port to listen to. Default: `7771`.
property :port, default: 7771
Expand Down
4 changes: 2 additions & 2 deletions lib/devdnsd/version.rb
Expand Up @@ -13,10 +13,10 @@ module Version
MAJOR = 3

# The minor version.
MINOR = 0
MINOR = 1

# The patch version.
PATCH = 8
PATCH = 0

# The current version number of DevDNSd.
STRING = [MAJOR, MINOR, PATCH].compact.join(".")
Expand Down
9 changes: 8 additions & 1 deletion locales/en.yml
Expand Up @@ -58,12 +58,19 @@
application_meta_command: "COMMAND"
application_create_config: "To execute devdnsd, please create the file {mark=bright}%1{/mark}. An empty file is sufficient."
command_start: "Starts the server."
command_stop: "Stops the server."
command_restart: "Restarts the server."
command_status: "Shows the status of the server."
command_clean: "Cleans the system's DNS."
command_install: "Installs the server."
command_uninstall: "Uninstalls the server."
command_stop: "Stops the server."
command_aliases: "Adds or removes aliases to network interfaces."
command_add: "Adds aliases."
command_remove: "Removes aliases."
status_running: "The server is running with process ID {mark=bright}%1{/mark}."
status_stopped: "The server is stopped."
status_crashed: "The server crashed. See the log for more information."
status_unknown: "The server status is unknown."
removing: "Removing"
adding: "Adding"
remove: "remove"
Expand Down
9 changes: 8 additions & 1 deletion locales/it.yml
Expand Up @@ -59,12 +59,19 @@
application_meta_command: "COMANDO"
application_create_config: "Per eseguire devdnsd, per favore crea il file {mark=bright}%1{/mark}. Un file vuoto è sufficiente."
command_start: "Avvia server."
command_stop: "Ferma il server."
command_restart: "Riavvia server."
command_status: "Mostra lo status del server."
command_clean: "Pulisce la cache DNS del sistema."
command_install: "Installa il server."
command_uninstall: "Disinstalla il server."
command_stop: "Ferma il server."
command_aliases: "Aggiunge or rimuove indirizzi dalle interfacce di rete."
command_add: "Aggiungi indirizzi"
command_remove: "Rimuovi indirizzi."
status_running: "Il server è in esecuzione con ID processo {mark=bright}%1{/mark}."
status_stopped: "Il server è stato fermato."
status_crashed: "Il server è terminato con errori. Guarda il log per maggiori informazioni."
status_unknown: "Lo status del server è sconosciuto."
removing: "Rimuovo"
adding: "Aggiungo"
remove: "rimuovere"
Expand Down
46 changes: 44 additions & 2 deletions spec/devdnsd/application_spec.rb
Expand Up @@ -192,9 +192,9 @@ def create_application(overrides = {})
end

describe "#perform_server" do
let(:application){ create_application({"log_file" => log_file, "configuration" => sample_config}) }
let(:application){ create_application({"log_file" => log_file, "configuration" => sample_config, "port" => 60771}) }

def test_resolve(host = "match_1.dev", type = "ANY", nameserver = "127.0.0.1", port = 7771, logger = nil)
def test_resolve(host = "match_1.dev", type = "ANY", nameserver = "127.0.0.1", port = 60771, logger = nil)
result = nil

EM.run do
Expand Down Expand Up @@ -567,6 +567,48 @@ def respond!(*_)
end
end

describe "#action_restart" do
it "should stop and restart the server" do
expect(application).to receive(:action_stop)
expect(application).to receive(:action_start)
application.action_restart
end
end

describe "#action_status" do
it "should get the status of the daemon when running" do
expect(RExec::Daemon::ProcessFile).to receive(:status).and_return(:running)
expect(RExec::Daemon::ProcessFile).to receive(:recall).and_return(123)

expect(application.logger).to receive(:info).with("The server is running with process ID 123.")
application.action_status
end

it "should get the status of the daemon when stopped" do
expect(RExec::Daemon::ProcessFile).to receive(:status).and_return(:stopped)
expect(RExec::Daemon::ProcessFile).to receive(:recall).and_return(123)

expect(application.logger).to receive(:info).with("The server is stopped.")
application.action_status
end

it "should get the status of the daemon when crashed" do
expect(RExec::Daemon::ProcessFile).to receive(:status).and_return(:unknown)
expect(application.class).to receive(:crashed?).and_return(true)

expect(application.logger).to receive(:info).with("The server crashed. See the log for more information.")
application.action_status
end

it "should get the status of the daemon when unknown" do
expect(RExec::Daemon::ProcessFile).to receive(:status).and_return(:unknown)
expect(application.class).to receive(:crashed?).and_return(false)

expect(application.logger).to receive(:info).with("The server status is unknown.")
application.action_status
end
end

describe "#action_install" do
before(:each) do
allow(application).to receive(:is_osx?).and_return(true)
Expand Down
2 changes: 1 addition & 1 deletion spec/devdnsd/configuration_spec.rb
Expand Up @@ -11,7 +11,7 @@
describe "#initialize" do
it "sets default arguments and rules" do
config = DevDNSd::Configuration.new
expect(config.address).to eq("0.0.0.0")
expect(config.bind_addresses).to eq(["0.0.0.0"])
expect(config.port).to eq(7771)
expect(config.tld).to eq("dev")
expect(config.log_file).to eq(File.absolute_path(File.expand_path("~/.devdnsd/daemon.log")))
Expand Down

0 comments on commit f5b8207

Please sign in to comment.