Sha256: 56d68266c0f603ba67f837947b77f328cc9f18186d362df569e6a3087f507bfe

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module Capstrap
  module Hostname

    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
  end
end

if Capistrano::Configuration.instance
  Capstrap::Hostname.load_into(Capistrano::Configuration.instance)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capstrap-0.4.1 lib/capistrano/ext/capstrap/hostname.rb