Class: RailsPwnerer::Scaffolds::RubyGems

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rails_pwnerer/scaffolds/rubygems.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

_setup_unix, _setup_windows, all_packages, all_packages_without_caching, #atomic_erase, #atomic_read, #atomic_write, #best_package_matching, #check_rails_root, #control_boot_script, #cpu_cores, #current_user, #gem_exists?, #gid_for_username, #group_for_username, #hook_boot_script, #install_gem, #install_gems, #install_package, #install_package_impl, #install_package_matching, #install_packages, #kill_tree, #os_distro, package_info_hash, #path_to_boot_script, #path_to_boot_script_defaults, #path_to_gemdir, #process_info, #prompt_user_for_password, #remove_package, #remove_packages, #search_packages, #uid_for_username, #unroll_collection, #update_all_packages, #update_all_packages_impl, #update_gems, #update_package_metadata, #upgrade_gem, #upgrade_gems, #upgrade_package, #upgrade_package_impl, #upgrade_packages, #with_package_source, #with_temp_dir

Class Method Details

.goObject

standalone runner



84
85
86
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 84

def self.go
  self.new.run
end

.pre_goObject

called before packages get installed



79
80
81
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 79

def self.pre_go
  self.new.preflight
end

Instance Method Details

#configure_rubygemsObject

sets up good defaults for Rubygems



49
50
51
52
53
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 49

def configure_rubygems
  File.open('/etc/gemrc', 'w') do |f|
    f.write "gem: --no-force --no-rdoc --no-ri --no-user-install --wrappers\n"
  end
end

#google_lucky_uri(query) ⇒ Object

retrieves the URI for a Google “I’m Feeling Lucky” search



8
9
10
11
12
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 8

def google_lucky_uri(query)
  uri = URI.parse "http://www.google.com/search?q=#{URI.escape query}&btnI=Lucky"
  response = Net::HTTP.start(uri.host, uri.port) { |http| http.get "#{uri.path}?#{uri.query}" }
  response.header['location']
end

#install_rubygemsObject

installs Rubygems on the system



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 25

def install_rubygems
  with_temp_dir(:root => true) do
    tgz_uri = rubyforge_download_uri('rubygems', 'rubygems', 'tgz')
    file_name = File.basename tgz_uri.path
    loop do
      request_path = tgz_uri.query.to_s.empty? ? tgz_uri.path : "#{tgz_uri.path}?#{tgz_uri.query}"
      response = Net::HTTP.start(tgz_uri.host, tgz_uri.port) { |http| http.get request_path }
      if response.kind_of? Net::HTTPRedirection
        tgz_uri = URI.parse response.header['location']
        next
      end
      File.open(file_name, 'wb') { |f| f.write response.body }
      break
    end

    system "tar -xzf #{file_name}"
    File.unlink file_name
    Dir.chdir(Dir.glob('*').first) do
      system "ruby setup.rb"
    end
  end
end

#preflightObject



72
73
74
75
76
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 72

def preflight
  # save old lib path, in case we need to wipe it
  # we need to do this because setting up packages might wipe Ubuntu's gems
  @@old_gems = path_to_gemdir
end

#rubyforge_download_uri(project, gem_name = project, extension = ".gem") ⇒ Object

retrieves the download URI for a RubyForge project



15
16
17
18
19
20
21
22
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 15

def rubyforge_download_uri(project, gem_name = project, extension = ".gem")
  frs_uri = URI.parse google_lucky_uri("#{project} download page")
  frs_contents = Net::HTTP.get frs_uri
  frs_links = frs_contents.scan(/\<a.+href=\"(.+?)\"\>/).flatten.
                select { |link| link.index('.tgz') && link.index("#{gem_name}-") }
  latest_link = frs_links.sort_by { |n| n.match(/#{gem_name}-(.*)\.#{extension}/)[1] }.last
  return frs_uri.merge(latest_link)
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rails_pwnerer/scaffolds/rubygems.rb', line 55

def run
  # get the old path set by pre-go
  old_gems = @@old_gems

  # remove the Debian gems package and install from source
  remove_packages %w(rubygems)
  install_rubygems

  # configure rubygems so it doesn't waste time
  configure_rubygems

  # remove the gems that are trailing behind
  new_gems = path_to_gemdir
  return if new_gems == old_gems # don't wipe the new dir by mistake
  FileUtils.rm_r old_gems
end