Top Level Namespace

Defined Under Namespace

Modules: Capstrap

Instance Method Summary collapse

Instance Method Details

#apt_install(pkg, check = false) ⇒ Object

Installs a package via apt-get.

Parameters:

  • package (String)

    name to install



41
42
43
44
45
46
47
# File 'lib/capistrano/ext/capstrap/core.rb', line 41

def apt_install(pkg, check=false)
  if check && pkg_installed?(pkg)
    info %{Package "#{pkg}" is already installed, skipping.}
  else
    run %{apt-get install -y #{pkg}}
  end
end

#apt_updateObject

Updates package repository via apt-get.



51
52
53
# File 'lib/capistrano/ext/capstrap/core.rb', line 51

def apt_update
  run %{apt-get update -y}
end

Prints a message intended to catch attention.

Parameters:

  • message (String)

    to display



161
162
163
164
165
# File 'lib/capistrano/ext/capstrap/core.rb', line 161

def banner(msg)
  puts "\n  #{'*' * (msg.size + 6)}"
  puts "  *  #{msg}  *"
  puts "  #{'*' * (msg.size + 6)}\n"
end

#chef_installed?Boolean

Checks if the chef gem is installed on the remote host.

Returns:

  • (Boolean)


85
86
87
# File 'lib/capistrano/ext/capstrap/core.rb', line 85

def chef_installed?
  cmd_if %{rvm use #{ruby} >/dev/null && gem list --no-versions | grep -q "^chef$" >/dev/null}, true
end

#cmd_if(test, rvm = false) ⇒ true, false

Runs a remote if condition and returns true/false.

Parameters:

  • conditional (String)

    to be tested

Returns:

  • (true, false)

    whether or not the conditional passes



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/capistrano/ext/capstrap/core.rb', line 6

def cmd_if(test, rvm=false)
  load_rvm = ""
  load_rvm = "#{rvm_env} " if rvm
  r = capture %{#{load_rvm}if #{test} ; then echo true; else echo false; fi}, 
    :shell => "bash"
  puts "  * Result is: #{r.to_s}"
  if r.to_s =~ /true/
    true
  else
    false
  end
end

#cmd_test(test, rvm = false) ⇒ true, false

Runs a remote if test condition and returns true/false

Parameters:

  • test (String)

    to be tested

Returns:

  • (true, false)

    whether or not the test passes



24
25
26
# File 'lib/capistrano/ext/capstrap/core.rb', line 24

def cmd_test(test, rvm=false)
  cmd_if %{[ #{test} ]}, rvm
end

#config_repo_installed?Boolean

Checks if chef config repo is installed on the remote host.

Returns:

  • (Boolean)


100
101
102
# File 'lib/capistrano/ext/capstrap/core.rb', line 100

def config_repo_installed?
  cmd_test %{-d "#{config_path}"}
end

#cookbooks_repo_installed?Boolean

Checks if chef cookbook repo is installed on the remote host.

Returns:

  • (Boolean)


93
94
95
# File 'lib/capistrano/ext/capstrap/core.rb', line 93

def cookbooks_repo_installed?
  cmd_test %{-d "#{cookbooks_path}"}
end

#fetch_primary_ip_addressObject

Retrieve the primary IP address of the host.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/capistrano/ext/capstrap/core.rb', line 125

def fetch_primary_ip_address
  capture(<<-GETADDR, :shell => "bash").chomp
    _if="$(netstat -nr | grep ^0\.0\.0\.0 | awk '{print $8}')";
    _ip="$(/sbin/ifconfig $_if | \
      grep '^[[:space:]]*inet ' | awk '{print $2}' | \
      awk -F':' '{print $2}')";

    if [ -z "$_ip" -o "$_ip" == "" ] ; then
      echo "";
      return 10;
    else
      echo $_ip;
    fi
  GETADDR
end

#fqdn_correct?(host_name, domain_name, ip_addr) ⇒ Boolean

Checks if the full qualified domain name is current and correct.

Parameters:

  • ip (String)

    address of host

  • desired (String)

    host name

  • desired (String)

    domain name

Returns:

  • (Boolean)


118
119
120
# File 'lib/capistrano/ext/capstrap/core.rb', line 118

def fqdn_correct?(host_name, domain_name, ip_addr)
  cmd_if %{egrep -q '^#{ip_addr}[[:space:]]+#{host_name}.#{domain_name}' /etc/hosts >/dev/null}
end

#hostname_correct?(host_name) ⇒ Boolean

Checks if the hostname is current and correct.

Parameters:

  • desired (String)

    hostname

Returns:

  • (Boolean)


108
109
110
# File 'lib/capistrano/ext/capstrap/core.rb', line 108

def hostname_correct?(host_name)
  host_name == capture(%{hostname}).chomp
end

#info(msg) ⇒ Object

Prints an information message.

Parameters:

  • message (String)

    to display



153
154
155
# File 'lib/capistrano/ext/capstrap/core.rb', line 153

def info(msg)
  puts "\n ==> #{msg}"
end

#pkg_installed?(pkg) ⇒ true, false

Checks if a package is installed on the remote host.

Parameters:

  • package (String)

    name to check

Returns:

  • (true, false)

    whether or not the package is installed



33
34
35
# File 'lib/capistrano/ext/capstrap/core.rb', line 33

def pkg_installed?(pkg)
  cmd_if %{dpkg-query --showformat='${Essential}\n' --show '#{pkg}' > /dev/null 2>&1}
end

#ruby_installed?(ruby) ⇒ Boolean

Checks if an RVM ruby is installed on the remote host.

Parameters:

  • ruby (String)

    string

Returns:

  • (Boolean)


78
79
80
# File 'lib/capistrano/ext/capstrap/core.rb', line 78

def ruby_installed?(ruby)
  cmd_if %{rvm list strings | grep -q "#{ruby}" >/dev/null}, true
end

#rvm_envObject



55
56
57
# File 'lib/capistrano/ext/capstrap/core.rb', line 55

def rvm_env
  %{[[ -s "/usr/local/lib/rvm" ]] && source /usr/local/lib/rvm; }
end

#rvm_installed?Boolean

Checks if RVM is installed on the remote host.

Returns:

  • (Boolean)


70
71
72
# File 'lib/capistrano/ext/capstrap/core.rb', line 70

def rvm_installed?
  cmd_test %{-s "/usr/local/lib/rvm"}
end

#rvm_run(cmd) ⇒ Object

Runs a remote command in an RVM aware bubble.

Parameters:

  • command (String)

    to run



63
64
65
# File 'lib/capistrano/ext/capstrap/core.rb', line 63

def rvm_run(cmd)
  run %{#{rvm_env} rvm #{cmd}}, :shell => "bash"
end

#update_cmdObject



141
142
143
144
145
146
147
# File 'lib/capistrano/ext/capstrap/core.rb', line 141

def update_cmd
  if cookbooks_rake_update
    %{rake update}
  else
    %{git submodule init && git submodule update}
  end
end