Rakefile 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. require 'yaml'
  2. require 'popen4'
  3. STDOUT.sync = true
  4. task :default => :build_full_ami
  5. ##########################################################################
  6. def run_command(cmd)
  7. cmdrun = IO.popen(cmd)
  8. output = cmdrun.read
  9. cmdrun.close
  10. if $?.to_i > 0
  11. puts "count not run #{cmd}, it returned an error #{output}"
  12. exit 2
  13. end
  14. puts "OK: ran command #{cmd}"
  15. end
  16. ##########################################################################
  17. hf = Dir.glob('ansible/playbook.yml')
  18. errors = []
  19. desc 'Check Playbook Syntax'
  20. task :lint_playbook do
  21. hf.each do |playbook_file|
  22. begin
  23. YAML.load_file(playbook_file)
  24. rescue Exception => e
  25. errors << e.message
  26. end
  27. end
  28. if errors.empty?
  29. puts "Rake: #{hf.length} playbook files all checkout!"
  30. else
  31. errors.each do |err|
  32. puts "ERROR: YAML parse errors"
  33. puts err
  34. end
  35. exit 1
  36. end
  37. end
  38. ##########################################################################
  39. desc 'Update the Galaxy Modules'
  40. task :update_galaxy do
  41. puts 'Rake: updating galaxy modules'
  42. run_command('cd ansible && rm -rf roles/* && ansible-galaxy install --roles-path roles -r requirements.yml')
  43. end
  44. task :validate_packer do
  45. puts 'Rake: validating packer.json'
  46. run_command('packer validate packer.json')
  47. end
  48. task :build_ami do
  49. puts 'Rake: Building ami'
  50. POpen4::popen4( "packer build packer.json" ) do |stdout, stderr, stdin|
  51. stdout.each do |line|
  52. puts line
  53. end
  54. end
  55. end
  56. desc 'Ensure environment is setup properly'
  57. task :check_env do
  58. ['SOURCE_AMI', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY'].each do |st|
  59. unless ENV.has_key? st
  60. puts "ENV var #{st} is not set - see the Readme"
  61. exit 1
  62. end
  63. end
  64. end
  65. desc 'Build the ami'
  66. task :build_full_ami => [:check_env, :validate_packer, :update_galaxy, :lint_playbook, :build_ami] do
  67. puts 'Rake: Building the ami '
  68. end