Class: Spatula::Prepare

Inherits:
SshCommand show all
Defined in:
lib/spatula/prepare.rb

Constant Summary collapse

RUBYGEMS_VERSION =
"1.6.2"
DEFAULT_RUBY_VERSION =
"1.9.2-p180"

Instance Method Summary collapse

Methods inherited from SshCommand

#initialize, run, #ssh, #ssh_command, #ssh_opts

Constructor Details

This class inherits a constructor from Spatula::SshCommand

Instance Method Details

#install_chefObject



81
82
83
# File 'lib/spatula/prepare.rb', line 81

def install_chef
  ssh "#{sudo} gem install rdoc chef ohai --no-ri --no-rdoc --source http://gems.opscode.com --source http://gems.rubyforge.org"
end

#install_rubyObject



70
71
72
73
# File 'lib/spatula/prepare.rb', line 70

def install_ruby
  ssh "curl -L 'ftp://ftp.ruby-lang.org/pub/ruby/#{ruby_path}' | tar xvzf -"
  ssh "cd ruby-#{ruby_version} && ./configure && make && #{sudo} make install"
end

#install_rubygemsObject



75
76
77
78
79
# File 'lib/spatula/prepare.rb', line 75

def install_rubygems
  ssh "curl -L 'http://production.cf.rubygems.org/rubygems/rubygems-#{RUBYGEMS_VERSION}.tgz' | tar xvzf -"
  ssh "cd rubygems* && #{sudo} ruby setup.rb --no-ri --no-rdoc"
  ssh "#{sudo} ln -sfv /usr/bin/gem1.8 /usr/bin/gem"
end

#osObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spatula/prepare.rb', line 18

def os
  etc_issue = `#{ssh_command("cat /etc/issue")}`
  case etc_issue
  when /ubuntu/i
    "ubuntu"
  when /debian/i
    "debian"
  when /fedora/i
    "fedora"
  when /CentOS/i
    "centos"
  when ""
    raise "Couldn't get system info from /etc/issue. Please check your SSH credentials."
  else
    raise "Sorry, we currently only support prepare on ubuntu, debian & fedora. Please fork http://github.com/trotter/spatula and add support for your OS. I'm happy to incorporate pull requests."
  end
end

#ruby_pathObject



65
66
67
68
# File 'lib/spatula/prepare.rb', line 65

def ruby_path
  rev = ruby_version.match(/^(\d+\.\d+)/)[1]
  "#{rev}/ruby-#{ruby_version}.tar.gz"
end

#ruby_versionObject



61
62
63
# File 'lib/spatula/prepare.rb', line 61

def ruby_version
  @ruby_version || DEFAULT_RUBY_VERSION
end

#runObject



8
9
10
11
12
13
14
15
16
# File 'lib/spatula/prepare.rb', line 8

def run

  if @key_file and !@upload_key
    @upload_key = true
  end

  upload_ssh_key if @upload_key
  send "run_for_#{os}"
end

#run_for_centosObject



55
56
57
58
59
# File 'lib/spatula/prepare.rb', line 55

def run_for_centos
  ssh "#{sudo} yum install -y make gcc gcc-c++ rsync sudo openssl-devel curl"
  install_ruby
  install_chef
end

#run_for_debianObject



43
44
45
46
47
48
# File 'lib/spatula/prepare.rb', line 43

def run_for_debian
  ssh "#{sudo} apt-get update"
  ssh "#{sudo} apt-get install -y build-essential zlib1g-dev libssl-dev libreadline5-dev curl rsync"
  install_rubygems
  install_chef
end

#run_for_fedoraObject



50
51
52
53
# File 'lib/spatula/prepare.rb', line 50

def run_for_fedora
  sudo = ssh('which sudo > /dev/null 2>&1') ? 'sudo' : ''
  ssh "#{sudo} yum install -y make gcc gcc-c++ rsync sudo openssl-devel rubygems ruby-devel ruby-shadow curl"
end

#run_for_ubuntuObject



36
37
38
39
40
41
# File 'lib/spatula/prepare.rb', line 36

def run_for_ubuntu
  ssh "#{sudo} apt-get update"
  ssh "#{sudo} apt-get install -y ruby irb ri libopenssl-ruby1.8 libshadow-ruby1.8 ruby1.8-dev build-essential rsync curl"
  install_rubygems
  install_chef
end

#sudoObject



85
86
87
# File 'lib/spatula/prepare.rb', line 85

def sudo
  ssh('which sudo > /dev/null 2>&1') ? 'sudo' : ''
end

#upload_ssh_keyObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/spatula/prepare.rb', line 89

def upload_ssh_key
  authorized_file = "~/.ssh/authorized_keys"

  unless @key_file
    %w{rsa dsa}.each do |key_type|
      filename = "#{ENV['HOME']}/.ssh/id_#{key_type}.pub"
      if File.exists?(filename)
        @key_file = filename
        break
      end
    end
  end

  raise "Key file '#{@key_file}' not found: aborting." unless File.exists?(@key_file)

  key = File.open(@key_file).read.split(' ')[0..1].join(' ')

  ssh "mkdir -p .ssh && echo #{key} >> #{authorized_file}"
  ssh "cat #{authorized_file} | sort | uniq > #{authorized_file}.tmp && mv #{authorized_file}.tmp #{authorized_file}"
  ssh "chmod 0700 .ssh && chmod 0600 #{authorized_file}"
end