Module: ShellMethods
- Defined in:
- lib/chris_lib/shell_methods.rb
Overview
methods for bash ruby scripts
Instance Method Summary collapse
- #backup_database ⇒ Object
- #check_chris_lib_status ⇒ Object
- #check_git_clean ⇒ Object
- #file_size(file_path) ⇒ Object
- #migrate_if_necessary(remote: nil, skip_migration: false) ⇒ Object
- #notify_rollbar_of_deploy(access_token: nil) ⇒ Object
- #osx_hostname ⇒ Object
- #osx_imessage_admin(msg) ⇒ Object
- #osx_notification(msg, title) ⇒ Object
-
#osx_send_mail(subject, body = nil) ⇒ Object
mail to osx user stackoverflow.com/q/41602984/1299362.
- #parse_options ⇒ Object
-
#r_runner(script_path, arg1) ⇒ Object
runs an R script from ruby script_path is absolute path of R script arg1 is an argument passed to script, can access in R by arg1 <- commandArgs(trailingOnly=TRUE).
- #run_special_rake_task ⇒ Object
-
#same_db_version(remote: nil) ⇒ Object
change in response to [DEPRECATED] ‘Bundler.with_clean_env` has been deprecated in favor of `Bundler.with_unbundled_env`.
- #time_hash ⇒ Object
- #warn_users ⇒ Object
Instance Method Details
#backup_database ⇒ Object
59 60 61 62 |
# File 'lib/chris_lib/shell_methods.rb', line 59 def backup_database file_path = "../backups/prod#{time_hash}.dump" system('./script/getSnapShot.sh production ' + file_path) end |
#check_chris_lib_status ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/chris_lib/shell_methods.rb', line 113 def check_chris_lib_status gs=`cd ../chris_lib;git status`; lr=$?.successful? return unless gs['working tree clean'].nil? && gs['up-to-date'].nil? puts "Exiting, chris_lib is not up to date with master." exit 3 system('cd $OLDPWD') end |
#check_git_clean ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/chris_lib/shell_methods.rb', line 105 def check_git_clean puts "Checking git status" gs = `git status` return unless gs['working tree clean'].nil? puts "Exiting, you need to commit files" exit 1 end |
#file_size(file_path) ⇒ Object
16 17 18 |
# File 'lib/chris_lib/shell_methods.rb', line 16 def file_size(file_path) `stat -f%z #{file_path}`.to_i end |
#migrate_if_necessary(remote: nil, skip_migration: false) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/chris_lib/shell_methods.rb', line 121 def migrate_if_necessary(remote: nil, skip_migration: false) if skip_migration puts "No migration will be performed due to --fast or --skip_migration options" else destination=(remote.nil? ? nil : "--remote #{remote}") puts "Checking local and remote databases have same version and migrates if necessary" if same_db_version(remote: remote) puts "No migration necessary" else puts "Warning, different db versions" system('tput bel') puts "Press m<cr> to migrate or q<cr> to exit" ans=$stdin.gets() exit 2 if ans[0]!='m' system("heroku run rake db:migrate #{destination}") end end end |
#notify_rollbar_of_deploy(access_token: nil) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/chris_lib/shell_methods.rb', line 140 def (access_token: nil) system("ACCESS_TOKEN=#{access_token}") system("ENVIRONMENT=production") system("LOCAL_USERNAME=`whoami`") system("COMMENT=v#{TGM_VERSION}") sha = `git log -n 1 --pretty=format:'%H'` system("REVISION=sha") puts "Notifiying of revision #{sha}" cr = `curl https://api.rollbar.com/api/1/deploy/ \ -F access_token=$ACCESS_TOKEN \ -F environment=$ENVIRONMENT \ -F revision=$REVISION \ -F comment=$COMMENT \ -F local_username=$LOCAL_USERNAME` if cr.class == Hash && cr['data'].empty? puts "Rollbar was notified of deploy of v#{TGM_VERSION} with SHA #{sha[0..5]}" else system('tput bel;tput bel') puts "Failure to notify Rollbar of deploy of v#{TGM_VERSION} with SHA #{sha[0..5]}", cr end end |
#osx_hostname ⇒ Object
28 29 30 |
# File 'lib/chris_lib/shell_methods.rb', line 28 def osx_hostname `hostname` end |
#osx_imessage_admin(msg) ⇒ Object
20 21 22 |
# File 'lib/chris_lib/shell_methods.rb', line 20 def (msg) `osascript -e 'tell application "Messages" to send "#{msg}" to buddy "admin"'` end |
#osx_notification(msg, title) ⇒ Object
24 25 26 |
# File 'lib/chris_lib/shell_methods.rb', line 24 def osx_notification(msg, title) `osascript -e 'display notification "#{msg}" with title "#{title}"'` end |
#osx_send_mail(subject, body = nil) ⇒ Object
mail to osx user stackoverflow.com/q/41602984/1299362
34 35 36 |
# File 'lib/chris_lib/shell_methods.rb', line 34 def osx_send_mail(subject, body = nil) `echo "#{body}" | mail -s "#{subject}" 'Chris'` end |
#parse_options ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/chris_lib/shell_methods.rb', line 38 def @options = {} OptionParser.new do |opts| opts.on("-s", "--skip_migration", "Skip migrations") do @options[:skip_migration] = true end opts.on("-f", "--fast", "Deploy without warnings and skip migrations") do @options[:fast] = true end opts.on("-r", "--special_rake", "Run special rake task") do @options[:special_rake] = true end end.parse! @options[:skip_migration] = true if @options[:fast] end |
#r_runner(script_path, arg1) ⇒ Object
runs an R script from ruby script_path is absolute path of R script arg1 is an argument passed to script, can access in R by arg1 <- commandArgs(trailingOnly=TRUE)
12 13 14 |
# File 'lib/chris_lib/shell_methods.rb', line 12 def r_runner(script_path, arg1) `Rscript --vanilla #{script_path} #{arg1}` end |
#run_special_rake_task ⇒ Object
54 55 56 57 |
# File 'lib/chris_lib/shell_methods.rb', line 54 def run_special_rake_task fail 'Need to implement by asking for name of rake task and also requiring confirmation' end |
#same_db_version(remote: nil) ⇒ Object
change in response to
- DEPRECATED
-
‘Bundler.with_clean_env` has been deprecated in favor of `Bundler.with_unbundled_env`. If you instead want the environment before bundler was originally loaded, use `Bundler.with_original_env`
remove this comment when clear that this works.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/chris_lib/shell_methods.rb', line 87 def same_db_version(remote: nil) destination = (remote.nil? ? nil : "--remote #{remote}") lv = `rake db:version` puts 'Local version: ', lv hv = Bundler.with_unbundled_env{ `heroku run rake db:version #{destination}` } puts hv return nil if hv.nil? || hv.empty? return nil if lv.nil? || lv.empty? key = 'version: ' nl = lv.index(key) + 9 l_version = lv.slice(nl..-1) nh = hv.index(key) + 9 h_version = hv.slice(nh..-1) l_version == h_version end |
#time_hash ⇒ Object
79 80 81 82 |
# File 'lib/chris_lib/shell_methods.rb', line 79 def time_hash time = Time.now time.day.to_s + time.month.to_s + time.year.to_s + '-' + time.hour.to_s + time.min.to_s end |
#warn_users ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/chris_lib/shell_methods.rb', line 64 def warn_users system('heroku run rake util:three_min_warning --remote production') # spend one minute precompiling = ProgressBar.create # now 2 minutes waiting increment = 3 * 60 / 100 (1..100).each do |_i| sleep increment .increment .refresh end system('heroku run rake util:delete_newest_announcement --remote production') system('heroku run rake util:warn_under_maintenance --remote production') end |