Module: Capstrap::Hostname

Defined in:
lib/capistrano/ext/capstrap/hostname.rb

Class Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object



4
5
6
7
8
9
10
11
12
13
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
58
59
60
# File 'lib/capistrano/ext/capstrap/hostname.rb', line 4

def self.load_into(configuration)
  configuration.load do

    namespace :hostname do

      desc "Sets the hostname."
      task :set_hostname do
        if exists?(:host_name)
          hname = fetch(:host_name)
        else
          hname = capture(%{hostname}).chomp
        end

        unless hostname_correct?(hname)
          cmd = [
            %{echo "#{hname}" > /etc/hostname},
            %{chown root:root /etc/hostname},
            %{chmod 0644 /etc/hostname},
            %{start hostname}
          ]
          run cmd.join(" && ")
        end
      end

      desc "Sets the full qualified domain name."
      task :set_fqdn do
        begin
          current_fqdn = capture(%{hostname -f}).chomp
        rescue
          current_fqdn = "fubarname.fubardomain"
        end

        if exists?(:host_name)
          hname = fetch(:host_name)
        else
          hname = current_fqdn.split('.').shift
        end

        if exists?(:domain_name)
          dname = fetch(:domain_name)
        else
          dname = current_fqdn.split('.').drop(1).join('.')
        end

        unless fqdn_correct?(hname, dname, "127.0.1.1")
          run <<-UPDATE_HOSTS
            if egrep -q '^127.0.1.1[[:space:]]' /etc/hosts >/dev/null ; then
              perl -pi -e 's|^(127.0.1.1[[:space:]]+).*$|\$1#{hname}.#{dname} #{hname}|' /etc/hosts;
            else
              perl -pi -e 's|^(127\.0\.0\.1[[:space:]]+.*)$|\$1\n127.0.1.1 #{hname}.#{dname} #{hname}|' /etc/hosts;
            fi;
          UPDATE_HOSTS
        end
      end
    end
  end
end