setup_openshift 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env ruby
  2. require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'setup_tools'))
  3. include SetupTools
  4. if ARGV.length > 0
  5. mode = ARGV.shift
  6. else
  7. mode = ''
  8. end
  9. unless `which rhc` =~ /rhc/
  10. puts "It looks like the 'rhc' command line tool hasn't been installed yet. Please install"
  11. puts "it with 'gem install rhc', run 'rhc setup', and then run this script again."
  12. exit 1
  13. end
  14. def grab_openshift_config!
  15. grab_config_with_cmd!("rhc env list")
  16. end
  17. def set_env(key, value)
  18. capture("rhc env set #{key}=#{value}")
  19. end
  20. def check_login!
  21. token_file = Dir[File.join(File.expand_path('~/.openshift'), 'token_*')].first
  22. unless token_file
  23. puts "It looks like you need to log in to OpenShift. Please run 'rhc setup' before continuing a choose the option to 'Generate a token now', then run bin/setup_openshift again."
  24. exit 1
  25. end
  26. unless (Time.now - File.stat(token_file).mtime).to_i < 60 * 60 * 24 * 5
  27. puts "Please run 'rhc logout' and then 'rhc account' to refresh your session, then run bin/setup_openshift again."
  28. exit 1
  29. end
  30. puts "Welcome #{`rhc account`.scan(/Login (.*?) on /).first.first}! It looks like you're logged into OpenShift."
  31. puts
  32. end
  33. check_login!
  34. info = capture("rhc app show")
  35. just_made = false
  36. if info =~ /must specify an application/i
  37. foreman_cartridge = 'http://cartreflect-claytondev.rhcloud.com/reflect?github=cantino/huginn-openshift-foreman-cartridge'
  38. cmd = "rhc app create huginn ruby-2.0 mysql-5.5 #{foreman_cartridge} -s -r tmp-huginn"
  39. puts "It looks like you don't have an OpenShift app set up yet for this repo. I can make one for you."
  40. if yes?("Would you like me to create an OpenShift app for you now in this repo?")
  41. puts "Okay, this may take a moment..."
  42. puts `#{cmd}`
  43. git_config = capture("git config --list -f tmp-huginn/.git/config").split("\n")
  44. git_config.grep(/^rhc\./).each do |line|
  45. path, value = line.split('=')
  46. puts `git config #{path} #{value}`
  47. end
  48. url = git_config.grep(/^remote\.origin\.url/).first.split('=').last
  49. puts "Adding remote #{url}"
  50. puts `git remote add openshift #{url}`
  51. puts "Removing tmp OpenShift repo"
  52. puts `rm -rf ./tmp-huginn`
  53. puts "Updating git"
  54. puts `git fetch openshift`
  55. info = capture("rhc app show")
  56. just_made = true
  57. else
  58. puts "Okay, exiting so you can do it."
  59. exit 0
  60. end
  61. elsif info =~ /Application '.*?' not found/
  62. puts "It looks like you've deleted your OpenShift app. If that's the case, you should"
  63. puts "edit .git/config and remove the sections under [rhc] and under [remote \"openshift\"]."
  64. exit 1
  65. end
  66. app_name, app_url = info.scan(/^([\w\d]+) @ https?:\/\/([^\/ ]+)/i).flatten
  67. confirm_app_name app_name unless just_made
  68. grab_openshift_config!
  69. print_config
  70. set_defaults!
  71. first_time = mode =~ /^first/i
  72. unless $config['DOMAIN']
  73. set_value 'DOMAIN', app_url, force: false
  74. first_time = true
  75. end
  76. set_value 'BUNDLE_WITHOUT', 'development:test'
  77. puts `rhc ssh huginn 'gem install bundler'`
  78. puts
  79. puts "To setup outbound email, we suggest using Gmail. See the 'Outgoing email settings' section in .env.example."
  80. puts "You'll need to set those environment variables in OpenShift using 'rhc env set VARIABLE=VALUE'"
  81. puts
  82. branch = capture("git rev-parse --abbrev-ref HEAD")
  83. if first_time || yes?("Should I push your current branch (#{branch}) to OpenShift?", default: :yes)
  84. puts "This may take a moment..."
  85. puts capture("git push openshift #{branch}:master -f")
  86. end
  87. if first_time
  88. puts "Restarting..."
  89. puts capture("rhc app restart")
  90. puts capture("rhc cartridge restart foreman")
  91. puts "Done!"
  92. puts
  93. puts
  94. puts "I can make an admin user on your new Huginn instance and setup some example Agents."
  95. if yes?("Should I create a new admin user and some example Agents?", default: :yes)
  96. done = false
  97. while !done
  98. seed_email = nag "Okay, what is your email address?"
  99. seed_username = nag "And what username would you like to login as?"
  100. seed_password = nag "Finally, what password would you like to use?", noecho: true
  101. puts "\nJust a moment..."
  102. result = capture("rhc ssh huginn 'cd $OPENSHIFT_REPO_DIR && RAILS_ENV=production bundle exec rake db:seed SEED_EMAIL=#{seed_email} SEED_USERNAME=#{seed_username} SEED_PASSWORD=#{seed_password}'")
  103. puts result
  104. if result =~ /Validation failed/
  105. puts "ERROR:"
  106. puts
  107. puts result
  108. puts
  109. else
  110. done = true
  111. end
  112. end
  113. puts
  114. puts
  115. puts "Okay, you should be all set! Visit http://#{app_url} and login as '#{seed_username}' with your password."
  116. puts
  117. puts "If you'd like to make more users, you can visit http://#{app_url}/users/sign_up and use the invitation code:"
  118. else
  119. puts
  120. puts "Visit https://#{app_url}/users/sign_up and use the invitation code shown below:"
  121. end
  122. puts
  123. puts "\t#{$config['INVITATION_CODE']}"
  124. puts
  125. puts "We recommend that you read https://github.com/cantino/huginn/wiki/Run-Huginn-for-free-on-OpenShift and setup Pingdom to keep your app awake!"
  126. end
  127. puts
  128. puts "Done!"