Module: DevDNSd::ApplicationMethods::System

Extended by:
ActiveSupport::Concern
Included in:
DevDNSd::Application
Defined in:
lib/devdnsd/application.rb

Overview

System management methods.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#action_add(options) ⇒ Boolean

Adds aliases to an interface.

Parameters:

  • options (Hash)

    The options provided by the user.

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



158
159
160
# File 'lib/devdnsd/application.rb', line 158

def action_add(options)
  manage_aliases(:add, i18n.add_empty, options)
end

#action_installBoolean

Installs the application into the autolaunch.

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



174
175
176
# File 'lib/devdnsd/application.rb', line 174

def action_install
  manage_installation(launch_agent_path, resolver_path, :create_resolver, :create_agent, :load_agent)
end

#action_remove(options) ⇒ Boolean

Removes aliases from an interface.

Parameters:

  • options (Hash)

    The options provided by the user.

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



166
167
168
# File 'lib/devdnsd/application.rb', line 166

def action_remove(options)
  manage_aliases(:remove, i18n.remove_empty, options)
end

#action_restartBoolean

Restarts the server in background.

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



132
133
134
135
136
# File 'lib/devdnsd/application.rb', line 132

def action_restart
  action_stop
  action_start
  true
end

#action_startBoolean

Starts the server in background.

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/devdnsd/application.rb', line 107

def action_start
  get_logger.info(i18n.starting)

  if !Process.respond_to?(:fork) then
    logger.warn(i18n.no_fork)
    @config.foreground = true
  elsif @command.options[:foreground].value then
    @config.foreground = true
  end

  @config.foreground ? perform_server : RExec::Daemon::Controller.start(self.class)
  true
end

#action_statusBoolean

Shows the status of the server

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/devdnsd/application.rb', line 141

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

#action_stopBoolean

Stops the server in background.

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



124
125
126
127
# File 'lib/devdnsd/application.rb', line 124

def action_stop
  RExec::Daemon::Controller.stop(self.class)
  true
end

#action_uninstallBoolean

Uninstalls the application from the autolaunch.

Returns:

  • (Boolean)

    true if action succeeded, false otherwise.



181
182
183
# File 'lib/devdnsd/application.rb', line 181

def action_uninstall
  manage_installation(launch_agent_path, resolver_path, :delete_resolver, :unload_agent, :delete_agent)
end

#dns_updateBoolean

Updates DNS cache.

Returns:

  • (Boolean)

    true if command succeeded, false otherwise.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/devdnsd/application.rb', line 82

def dns_update
  @logger.info(i18n.dns_update)

  script = Tempfile.new("devdnsd-dns-cache-script")
  script.write("dscacheutil -flushcache 2>&1 > /dev/null\n")
  script.write("killall -9 mDNSResponder 2>&1 > /dev/null\n")
  script.write("killall -9 mDNSResponderHelper 2>&1 > /dev/null\n")
  script.close

  Kernel.system("/usr/bin/osascript -e 'do shell script \"sh #{script.path}\" with administrator privileges' 2>&1 > /dev/null")
  script.unlink
end

#execute_command(command) ⇒ Boolean

Executes a shell command.

Parameters:

  • command (String)

    The command to execute.

Returns:

  • (Boolean)

    true if command succeeded, false otherwise.



75
76
77
# File 'lib/devdnsd/application.rb', line 75

def execute_command(command)
  system("#{command} 2>&1 > /dev/null")
end

#is_osx?Boolean

Checks if we are running on MacOS X.

System services are only available on that platform.

Returns:

  • (Boolean)

    true if the current platform is MacOS X, false otherwise.



100
101
102
# File 'lib/devdnsd/application.rb', line 100

def is_osx?
  ::RbConfig::CONFIG['host_os'] =~ /^darwin/
end

#launch_agent_path(name = "it.cowtech.devdnsd") ⇒ String

Gets the path for the launch agent file.

Parameters:

  • name (String) (defaults to: "it.cowtech.devdnsd")

    The base name for the agent.

Returns:

  • (String)

    The path for the launch agent file.



67
68
69
# File 'lib/devdnsd/application.rb', line 67

def launch_agent_path(name = "it.cowtech.devdnsd")
  ENV["HOME"] + "/Library/LaunchAgents/#{name}.plist"
end

#resolver_path(tld = nil) ⇒ String

Gets the path for the resolver file.

Parameters:

  • tld (String) (defaults to: nil)

    The TLD to manage.

Returns:

  • (String)

    The path for the resolver file.



58
59
60
61
# File 'lib/devdnsd/application.rb', line 58

def resolver_path(tld = nil)
  tld ||= @config.tld
  "/etc/resolver/#{tld}"
end