class DnsOne::Setup

Constants

SERVICE_NAME
SYSTEMD_SERVICES_DIR
SYSTEMD_SERVICE_FILE

Attributes

conf[RW]

Public Class Methods

new() click to toggle source
# File lib/dns_one/setup.rb, line 8
def initialize
    @thisdir = File.join File.dirname(__FILE__)
    DnsOne.new # just to load the configuration
               # TODO: move conf to own class
    @conf = Global.conf
end

Public Instance Methods

remove() click to toggle source
# File lib/dns_one/setup.rb, line 24
def remove
    unless is_setup?
        die "Nothing to remove. Exiting."
    end

    # SYSTEMD_SERVICE_FILE
    if Util.has_systemd? and File.exist? SYSTEMD_SERVICE_FILE 
        Util.run "systemctl stop #{SERVICE_NAME}" 
        Util.run "systemctl disable #{SERVICE_NAME}" 
        File.delete SYSTEMD_SERVICE_FILE
    end
    
    # conf_file
    if File.exists? conf.conf_file
        if confirm? "Delete #{conf.conf_file}?"
            FileUtils.rm conf.conf_file
        end
    end
    
    # work_dir
    if conf.work_dir == '/var/local/dnsone' # Only deletes if path matches the hardcoded one
        if Dir.exists? conf.work_dir
            if confirm? "Delete #{conf.work_dir}?"
                FileUtils.rm_rf conf.work_dir
            end
        end
    end

    puts "Removed."
end
setup() click to toggle source
# File lib/dns_one/setup.rb, line 15
def setup
    check_reqs
    add_user
    mkdirs
    copy_sample_conf
    install_systemd_service
    setup_finished_msg
end

Private Instance Methods

add_user() click to toggle source
# File lib/dns_one/setup.rb, line 66
def add_user
    if `cat /etc/passwd|grep ^#{conf.run_as}:`.strip.present?
        warn "User #{conf.run_as} exists, skipping creation."
        return
    end
    system "adduser --system --no-create-home '#{conf.run_as}'"
end
check_reqs() click to toggle source
# File lib/dns_one/setup.rb, line 57
def check_reqs
    unless Process.uid == 0
        die  "Install requires root privileges. Run with sudo or login as root. Aborting."
    end
    unless Util.has_systemd?
        warn "DnsOne will install without systemd."
    end
end
confirm?(msg) click to toggle source
# File lib/dns_one/setup.rb, line 141
def confirm? msg
    typed = nil
    loop do
        print "#{msg} (y/n): "
        typed = STDIN.gets.chomp
        break if %w(y n).include? typed
    end
    typed == 'y'
end
copy(from, to, mod = 0600) click to toggle source
# File lib/dns_one/setup.rb, line 120
def copy from, to, mod = 0600
    puts "Copying #{from} to #{to}..."
    FileUtils.cp from, to
    FileUtils.chmod mod, to

rescue => e
    warn "Error when copying #{from} to #{to}: #{e.message}"
    unless confirm? "Ignore error?"
        die "exiting"
    end
end
copy_sample_conf() click to toggle source
# File lib/dns_one/setup.rb, line 110
def copy_sample_conf
    if File.exist? conf.conf_file
        unless confirm? "File #{conf.conf_file} exists. Override it?"
            return
        end
    end
    copy "#{@thisdir}/../../util/sample_conf.yml", 
         conf.conf_file
end
die(msg) click to toggle source
# File lib/dns_one/setup.rb, line 132
def die msg
    STDERR.puts msg
    exit 1
end
install_systemd_service() click to toggle source
# File lib/dns_one/setup.rb, line 103
def install_systemd_service
    copy "#{@thisdir}/../../util/dnsone.service", 
         SYSTEMD_SERVICE_FILE

    Util.run "systemctl enable #{SERVICE_NAME}"
end
is_setup?() click to toggle source
# File lib/dns_one/setup.rb, line 99
def is_setup?
    File.exist? conf.conf_file
end
mkdirs() click to toggle source
# File lib/dns_one/setup.rb, line 74
def mkdirs
    if Dir.exists? conf.work_dir
        warn "Work dir #{conf.work_dir} exists, skipping creation."
        return
    end
    FileUtils.mkdir_p conf.work_dir
    File.chmod 0755, conf.work_dir
    File.chown `id -u #{conf.run_as}`.to_i, nil, conf.work_dir
end
setup_finished_msg() click to toggle source
# File lib/dns_one/setup.rb, line 84
    def setup_finished_msg
        print <<~HEREDOC

            Installed successfully

            Now:
            1) Edit #{conf.conf_file}
            2) After configuration run 'dnsone' (or 'systemctl start dnsone' if systemd is available)

            Logs are sent to /var/log/dnsone.log 
                             /var/log/dnsone_rubydns.log

        HEREDOC
    end
warn(msg) click to toggle source
# File lib/dns_one/setup.rb, line 137
def warn msg
    STDERR.puts msg
end