Guest User

Untitled

a guest
Jan 14th, 2018
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. # Example rails app template script
  2. # I included some functions and other things in this file
  3. # that I would usually keep seperate and just require.
  4.  
  5. require 'yaml'
  6.  
  7. # I like to use the irb replacement pry to debug scripts like
  8. # this if you have the pry gem you can uncomment the following line
  9. #require 'pry'
  10. # Then place the next (uncommented) line near where you are
  11. # having issues and when the script is run it will break into
  12. # a pry session.
  13. #binding.pry
  14.  
  15. ######
  16. # SETUP
  17. ######
  18.  
  19. # A quick descriptive name for the template
  20. def template_name
  21. "Postgres ActiveAdmin setupper"
  22. end
  23. #Describe quickly what is going to happen when this template is run
  24. def desc
  25. <<-END_DESC
  26. Assumes that you are using postgres for development, test and production
  27. and that you have the psql command available.
  28.  
  29. Will setup config/database.yml, create postgres databases if necessary.
  30. Will install ActiveAdmin, and track the AdminUser as an ActiveAdmin resource.
  31.  
  32. NOTE: Due to the way templates work you may need to have the required gems installed
  33. on your system or current rvm gemset before you procede.
  34. (TODO: add a check to see if necessary gems are installed)
  35. END_DESC
  36. end
  37.  
  38. ######
  39. # Some Functions
  40. # Included here for example purposes(I ususally source from a seperate file)
  41. ######
  42.  
  43. #Ask a question if fallback is true otherwise just return the given default
  44. def ask_with_default(statement, default, fallback=false)
  45. if (fallback) then
  46. default
  47. else
  48. say("#{statement} [default: #{default} ]",:BLUE)
  49. input = $stdin.gets.strip
  50. input.empty? ? default : input
  51. end
  52. end
  53.  
  54. #use to report things that have been done or applied (just saves setting the color each time)
  55. def verbate(statement)
  56. say(" " + statement,:GREEN)
  57. end
  58.  
  59. #if called with true defaults will used and no questions will be asked.
  60. def config_database_yml(just_defaults = false)
  61. default_db_adapter = 'postgresql'
  62. default_db_username = ENV['USER'] # <= EDIT you may need to change the db username. default is to use shell login name.
  63. default_db_password = 'PASSWORD' # <= EDIT
  64. environment_names = %w(development test production)
  65. create_database_sql = '-------------'
  66. dbconfig = {}
  67. environment_names.each do |e|
  68. say("Configuring Database settings for #{e} environment.",:GREEN)
  69. say("Generated settings will be placed in db/database.yml")
  70. dbconfig[e] = {}
  71. dbconfig[e]['adapter'] = default_db_adapter
  72. dbconfig[e]['encoding'] = 'unicode'
  73. dbconfig[e]['pool'] = 5
  74. dbconfig[e]['database'] = ask_with_default("What is the name for the #{e} database","#{app_name}_#{e}",just_defaults)
  75. verbate("Using database #{dbconfig[e]['database']} for #{e} environment.")
  76. dbconfig[e]['username'] = ask_with_default("What is the username for the #{e} environment database (#{dbconfig[e]['database']})?", default_db_username,just_defaults)
  77. verbate("Using username '#{dbconfig[e]['username']}' for #{e} environment.")
  78. dbconfig[e]['password'] = ask_with_default("What is the password for user `#{dbconfig[e]['username']}` on database (#{dbconfig[e]['database']})?",default_db_password, just_defaults)
  79. verbate("Using password `#{dbconfig[e]['password']}` for '#{dbconfig[e]['username']}' for #{e} environment.")
  80. create_database_sql << <<-SQL_END
  81. --
  82. -- {e} Database
  83. --
  84. CREATE DATABASE #{dbconfig[e]['database']}
  85. WITH ENCODING='UTF8'
  86. OWNER=#{dbconfig[e]['username']}
  87. CONNECTION LIMIT=-1;
  88. --Uncomment next line if you want public connect access to DB then re-run this sql.
  89. --GRANT CONNECT ON DATABASE #{dbconfig[e]['database']} TO public;
  90. -------------
  91. SQL_END
  92. end
  93. file('db/create_dbs.sql',create_database_sql)
  94. file 'config/database.yml', dbconfig.to_yaml
  95. if yes?("Create the postgres databases?") then
  96. say("Provide your password for psql to create your databases",:magenta)
  97. inside('db') do
  98. #Due to the way psql works (on my system at least) an existing database is required to login.
  99. #The default is a database of the users login name, so I have a database that
  100. #has the same name as my shell login name, and my postgres username is also
  101. #my shell login name. You may need to change the -d flag to point to a db that
  102. #exists on your system, and perhaps add a -U flag followed by your postgresql username
  103. run "psql -d #{default_db_username} -f create_dbs.sql" # <= EDIT see note above
  104. end
  105. end
  106. end
  107.  
  108. #
  109. # Begin of actual template script
  110. #
  111. say("ABOUT TO RUN TEMPLATE: #{template_name} ", :MAGENTA)
  112. puts desc
  113. exit unless yes?("Shall I continue?")
  114.  
  115.  
  116. if yes?("Setup PostgresSql Database with defaults?") then
  117. config_database_yml(true)
  118. else
  119. config_database_yml(false)
  120. end
  121.  
  122. say("Adding Gems to Gemfile")
  123. gem 'pg'
  124. gem 'thin'
  125. #also write a simple procfile for thin
  126. file 'Procfile', <<-THIN_PROC_END
  127. web: bundle exec rails server thin -p $PORT
  128. THIN_PROC_END
  129. gem 'activeadmin'
  130. gem "meta_search", '>= 1.1.0.pre'
  131. gem 'sass-rails', " ~> 3.1.0"
  132.  
  133. # gem_group :development do
  134. # gem 'foreman'
  135. # end
  136. gem 'foreman'
  137.  
  138.  
  139. git :init
  140. git :add => "."
  141. git :commit => "-a -m 'Initial Commit'"
  142. generate 'active_admin:install'
  143. git :add => "."
  144. git :commit => "-a -m 'Install Active Admin'"
  145. rake 'db:migrate'
  146. generate 'active_admin:resource', "AdminUser"
  147. git :add => "."
  148. git :commit => "-a -m 'Track AdminUser as an ActiveAdmin resource'"
  149. say("You still need to set up mail delevery system for ActiveAdmin!
  150. Look at the source of this script
  151. (#{__FILE__})
  152. for a quick example provided in the comments", :RED)
  153. puts
  154. ask("Done running template. Just wanted you to see the note above ;-). About to let rails finish creating your project.[hit return]")
  155.  
  156. # For small production and pre-deploy on larger apps I use sendgrid via the heroku addon to send mail
  157. # if you have enabled sendgrid for your heroku app you can add the following to
  158.  
  159. # config/environments/production.rb
  160. ################################
  161. #
  162. # #This Address will be the host where the link in the email will point
  163. # # config.action_mailer.default_url_options = { :host => 'MYDOMAIN.EXT' } # <= EDIT
  164. #
  165. # #On heroku these ENV variables are setup for us (since we have enabled sendgrid)
  166. # ActionMailer::Base.smtp_settings = {
  167. # :user_name => ENV['SENDGRID_USERNAME'],
  168. # :password => ENV['SENDGRID_PASSWORD'],
  169. # :domain => ENV['SENDGRID_DOMAIN'],
  170. # :address => "smtp.sendgrid.net",
  171. # :port => 587,
  172. # :authentication => :plain,
  173. # :enable_starttls_auto => true
  174. # }
  175. ##############################
  176.  
  177. # To use sendgrid locally (once you have it setup on heroku). I use `heroku config`
  178. # to see my generated sendgrid variables then place them in a file called local_pass_store (export VAR=VALUE) at the root of my project
  179. # and then `source local_pass_store` before I start the server or console.
  180.  
  181. # config/environments/development.rb
  182. ################################
  183. #
  184. # #This Address will be the host where the link in the email will point
  185. # config.action_mailer.default_url_options = { :host => 'localhost:5000' }
  186. #
  187. # #Note that these variable need to be set in the shell before starting the
  188. # #local server or console (source local_pass_store)
  189. # ActionMailer::Base.smtp_settings = {
  190. # :user_name => ENV['SENDGRID_USERNAME'],
  191. # :password => ENV['SENDGRID_PASSWORD'],
  192. # :domain => ENV['SENDGRID_DOMAIN'],
  193. # :address => "smtp.sendgrid.net",
  194. # :port => 587,
  195. # :authentication => :plain,
  196. # :enable_starttls_auto => true
  197. # }
  198.  
  199. #You also need to edit the config/initializers/devise.rb file and change the line
  200. # config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"
  201. #to the account you want the mail to look like it was sent from
Add Comment
Please, Sign In to add comment