PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/repo_conf_generators/apt_conf_generators.rb

https://github.com/rightscale/right_link
Ruby | 107 lines | 65 code | 17 blank | 25 comment | 8 complexity | 311090a4d89b7a2a9764638bfb57e95c MD5 | raw file
  1. require 'right_agent/exceptions'
  2. #
  3. # Copyright (c) 2009-2011 RightScale Inc
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. module Apt
  24. module Ubuntu
  25. SUPPORTED_REPOS = ['hardy', 'intrepid', 'jaunty', 'karmic', 'lucid', 'maverick', 'precise', 'quantal', 'trusty']
  26. # The different generate classes will always generate an exception ("string") if there's anything that went wrong. If no exception, things went well.
  27. SUPPORTED_REPOS.each do |c|
  28. module_eval <<-EOS
  29. class #{c.capitalize}
  30. def self.generate(description, base_urls, frozen_date="latest")
  31. opts = { :repo_filename => "rightscale",
  32. :repo_name => "default",
  33. :description => description,
  34. :codename => '#{c}',
  35. :base_urls => base_urls,
  36. :frozen_date => frozen_date,
  37. :enabled => true }
  38. opts[:frozen_date] = frozen_date || "latest" # Optional frozen date
  39. Apt::Ubuntu::abstract_generate(opts)
  40. end
  41. end
  42. EOS
  43. end
  44. def self.path_to_sources_list
  45. "/etc/apt/sources.list.d"
  46. end
  47. ############## INTERNAL FUNCTIONS #######################################################
  48. def self.abstract_generate(params)
  49. return unless ::RightScale::Platform.linux? && ::RightScale::Platform.ubuntu?
  50. opts = { :enabled => true, :frozen_date => "latest"}
  51. opts.merge!(params)
  52. raise ArgumentError.new("missing parameters to generate file!") unless opts[:repo_filename] &&
  53. opts[:repo_name] &&
  54. opts[:base_urls] &&
  55. opts[:frozen_date] &&
  56. opts[:enabled]
  57. return unless opts[:enabled]
  58. target = opts[:codename].downcase
  59. codename = ::RightScale::Platform.codename.downcase
  60. raise ::RightScale::Exceptions::PlatformError, "Unsupported Ubuntu release #{codename}" unless SUPPORTED_REPOS.include?(codename)
  61. raise ::RightScale::Exceptions::PlatformError, "Wrong release; repo is for #{target}, we are #{codename}" unless target == codename
  62. FileUtils.mkdir_p(Apt::Ubuntu::path_to_sources_list)
  63. if opts[:frozen_date] != 'latest'
  64. x = Date.parse(opts[:frozen_date]).to_s
  65. x.gsub!(/-/,"/")
  66. opts[:frozen_date] = x
  67. end
  68. mirror_list = opts[:base_urls].map do |bu|
  69. bu +='/' unless bu[-1..-1] == '/' # ensure the base url is terminated with a '/'
  70. bu + opts[:frozen_date]
  71. end
  72. config_body = ""
  73. mirror_list.each do |mirror_url|
  74. config_body += <<END
  75. deb #{mirror_url} #{codename} main restricted multiverse universe
  76. deb #{mirror_url} #{codename}-updates main restricted multiverse universe
  77. deb #{mirror_url} #{codename}-security main restricted multiverse universe
  78. END
  79. end
  80. target_filename = "#{Apt::Ubuntu::path_to_sources_list}/#{opts[:repo_filename]}.sources.list"
  81. FileUtils.rm_f(target_filename) if File.exists?(target_filename)
  82. File.open(target_filename,'w') { |f| f.write(config_body) }
  83. FileUtils.mv("/etc/apt/sources.list", "/etc/apt/sources.list.ORIG") if File.size?("/etc/apt/sources.list")
  84. FileUtils.touch("/etc/apt/sources.list")
  85. mirror_list
  86. end
  87. end
  88. end
  89. # Examples of usage...
  90. #Apt::Ubuntu::Hardy.generate("Hardy", ["http://a.com/ubuntu_daily"], "20081010")