abtion-scripts 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ class AbtionScripts::Help < AbtionScripts::Base
2
+ def self.help
3
+ "Inception was a lame movie"
4
+ end
5
+
6
+ def self.help_subcommands
7
+ {}
8
+ end
9
+
10
+ def self.description
11
+ "Prints this message out"
12
+ end
13
+
14
+ def run
15
+ specific_script = argv[0]
16
+
17
+ if AbtionScripts::Base.script_names.include?(specific_script)
18
+ script = AbtionScripts::Base.scripts[specific_script]
19
+ puts full_help(script)
20
+ elsif specific_script
21
+ puts colorize(:red, "\"#{specific_script}\" does not exist, cannot display help")
22
+ else
23
+ basic_usage
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def full_help(script)
30
+ (script.help || "") + script.help_subcommands.map do |cmd, description|
31
+ " - #{colorize(:light_blue, cmd)} - #{description}"
32
+ end.join("\n")
33
+ end
34
+
35
+ def basic_usage
36
+ puts "Usage: abtion #{colorize(:light_blue, '[script name]')}"
37
+ puts
38
+ puts "Specify a specific script to run, options are: "
39
+ puts
40
+
41
+ names_and_descriptions = AbtionScripts::Base.scripts.map do |name, script|
42
+ [colorize(:light_green, name), colorize(:light_blue, script.description)]
43
+ end
44
+
45
+ padding = names_and_descriptions.map { |name, _| name.length }.max
46
+
47
+ names_and_descriptions.each do |name, description|
48
+ puts " %-#{padding}s %s" % [name, description]
49
+ end
50
+
51
+ puts
52
+ end
53
+ end
@@ -0,0 +1,71 @@
1
+ require_relative './doctor'
2
+
3
+ class HerokuDoctor < AbtionScripts::Doctor
4
+ def self.description
5
+ "Checks the health of your Heroku config"
6
+ end
7
+
8
+ def self.help
9
+ <<~TEXT
10
+ abtion heroku-doctor #{colorize(:light_blue, "environment")} = #{colorize(:yellow, "staging")}
11
+ TEXT
12
+ end
13
+
14
+ def run_checks
15
+ @env = argv[0] || "staging"
16
+ @heroku_app_name = app_names[@env.to_s]
17
+
18
+ puts "Environment: #{@env}"
19
+ puts "Heroku app: #{@heroku_app_name}"
20
+ puts
21
+
22
+ # this one should always be first - it will NEVER pass for the abtion-rails project which is OKAY!
23
+ check(
24
+ name: "The abtion.yml file has been configured properly",
25
+ command: "! grep 'PROJECT-NAME-staging' abtion.yml",
26
+ remedy: "configure your abtion.yml file to have the correct app names set for all your Heroku environments"
27
+ )
28
+
29
+ check(
30
+ name: "app #{@heroku_app_name} exists",
31
+ command: "cat .git/config | egrep -e git@heroku.com:#{@heroku_app_name}.git -e https://git.heroku.com/#{@heroku_app_name}.git",
32
+ remedy: [command("heroku apps:create #{@heroku_app_name}"), "and/or", command("git remote add staging git@heroku.com:#{@heroku_app_name}.git")]
33
+ )
34
+
35
+ check_remote("production")
36
+ check_remote("staging")
37
+
38
+ check_env("DEPLOY_TASKS", "db:migrate")
39
+ check_env("RAILS_ENV", "production")
40
+ check_env("DATABASE_URL", "postgres://", "go to https://dashboard.heroku.com/apps/#{@heroku_app_name}/resources and add the Heroku Postgress add-on")
41
+
42
+ check_buildpack("https://github.com/heroku/heroku-buildpack-ruby")
43
+ check_buildpack("https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks")
44
+ end
45
+
46
+ private
47
+
48
+ def check_remote(remote)
49
+ check(
50
+ name: %|Remote "#{remote}" exists|,
51
+ command: "git remote | grep #{remote}",
52
+ remedy: "heroku git:remote -a #{@heroku_app_name} -r #{remote}"
53
+ )
54
+ end
55
+
56
+ def check_env(env_var, value, remedy=nil)
57
+ check(
58
+ name: env_var,
59
+ command: "heroku config:get #{env_var} -a #{@heroku_app_name} | grep '#{value}'",
60
+ remedy: remedy || command("heroku config:set #{env_var}=#{value} -a #{@heroku_app_name}")
61
+ )
62
+ end
63
+
64
+ def check_buildpack(url)
65
+ check(
66
+ name: url.split("/").last,
67
+ command: "heroku buildpacks -a #{@heroku_app_name} | grep '#{url}'",
68
+ remedy: command("heroku buildpacks:add #{url} -a #{@heroku_app_name}")
69
+ )
70
+ end
71
+ end
@@ -0,0 +1,22 @@
1
+ class AbtionScripts::KillDbSessions < AbtionScripts::Base
2
+ def self.description
3
+ "Kills active Postgres sessions"
4
+ end
5
+
6
+ def run
7
+ print "Loading Rails... "
8
+ require app_root.join("./config/environment")
9
+
10
+ puts "done"
11
+
12
+ print "Killing DB sessions... "
13
+ ActiveRecord::Base.connection.execute(<<-SQL)
14
+ SELECT pg_terminate_backend(pg_stat_activity.pid)
15
+ FROM pg_stat_activity
16
+ WHERE datname = current_database()
17
+ AND pid <> pg_backend_pid()
18
+ SQL
19
+
20
+ puts "done"
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ class Levenstein
2
+ def self.edit_distance(s, t)
3
+ m = s.length
4
+ n = t.length
5
+ return m if n == 0
6
+ return n if m == 0
7
+ d = Array.new(m+1) { Array.new(n+1) }
8
+
9
+ (0..m).each { |i| d[i][0] = i }
10
+ (0..n).each { |j| d[0][j] = j }
11
+ (1..n).each do |j|
12
+ (1..m).each do |i|
13
+ d[i][j] = if s[i-1] == t[j-1] # adjust index into string
14
+ d[i-1][j-1] # no operation required
15
+ else
16
+ [d[i-1][j]+1, # deletion
17
+ d[i][j-1]+1, # insertion
18
+ d[i-1][j-1]+1, # substitution
19
+ ].min
20
+ end
21
+ end
22
+ end
23
+ d[m][n]
24
+ end
25
+
26
+ def self.closest_match(needle, haystack)
27
+ min_distance = haystack.map(&:size).max
28
+ results = nil
29
+ haystack.each do |value|
30
+ distance = edit_distance(needle, value)
31
+ if distance < min_distance
32
+ min_distance = distance
33
+ results = [value]
34
+ elsif distance == min_distance
35
+ results << value
36
+ end
37
+ end
38
+ results
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ class AbtionScripts::Promote < AbtionScripts::Base
2
+ def self.description
3
+ "Promote staging to production and run migrations on production"
4
+ end
5
+
6
+ def run
7
+ step "Promoting staging to production" do
8
+ heroku "pipelines:promote", remote: :staging
9
+ end
10
+
11
+ step "Running migrations on production" do
12
+ heroku "run rails db:migrate", remote: :production
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ class AbtionScripts::Pushit < AbtionScripts::Base
2
+ def self.description
3
+ "Pulls code, runs test, pushes your code"
4
+ end
5
+
6
+ def self.help
7
+ <<-EOF
8
+ abtion pushit
9
+
10
+ Pulls the latest code, restarts, runs the tests, and pushes
11
+ your new code up.
12
+ EOF
13
+ end
14
+
15
+ def run
16
+ run_script :update
17
+ run_script :test
18
+
19
+ step "Pushing" do
20
+ system('git push')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ class AbtionScripts::Rspec < AbtionScripts::Base
2
+ def self.description
3
+ "Runs your RSpec suite"
4
+ end
5
+
6
+ def run
7
+ begin
8
+ load(File.expand_path("./spring", __FILE__))
9
+ rescue LoadError
10
+ end
11
+
12
+ require 'bundler/setup'
13
+
14
+ step "Running RSpec" do
15
+ command = ['bundle', 'exec', 'rspec', *argv]
16
+ system!(command.join(" "))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ class AbtionScripts::Test < AbtionScripts::Base
2
+ def self.description
3
+ "Runs all test suites for CI/pushit"
4
+ end
5
+
6
+ def run
7
+ step "Running test suite" do
8
+ rspec
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def rspec
15
+ AbtionScripts::Rspec.run
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ class AbtionScripts::TodayI < AbtionScripts::Base
2
+ def self.help
3
+ <<-EOF
4
+ abtion today-i
5
+
6
+ Prints out a list of commit message names that you worked on today.
7
+ EOF
8
+ end
9
+
10
+ def self.description
11
+ "Prints a list of commit msgs from today"
12
+ end
13
+
14
+ def run
15
+ date_string = Time.now.to_s.split(' ')[0]
16
+
17
+ lines = %x{
18
+ git log \
19
+ --date=local \
20
+ --oneline \
21
+ --after="#{date_string} 00:00" \
22
+ --before="#{date_string} 23:59"
23
+ }
24
+
25
+ lines.each_line do |line|
26
+ puts line.split(" ", 2)[1]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,77 @@
1
+ class AbtionScripts::Update < AbtionScripts::Base
2
+ def self.help
3
+ <<-EOF
4
+ abtion update
5
+
6
+ This script is a way to update your development environment automatically.
7
+ EOF
8
+ end
9
+
10
+ def self.description
11
+ "Updates your dev environment automatically"
12
+ end
13
+
14
+ def run
15
+ pull_git
16
+ install_dependencies
17
+ update_db
18
+ remove_old_logs
19
+ restart_servers
20
+ end
21
+
22
+ private
23
+
24
+ def pull_git
25
+ step "Pulling from git" do
26
+ system! "git pull --rebase"
27
+ end
28
+ end
29
+
30
+ def install_dependencies
31
+ step "Installing dependencies" do
32
+ if bundler?
33
+ system! 'command -v bundler > /dev/null || gem install bundler --conservative'
34
+ system! 'bundle install'
35
+ end
36
+
37
+ if yarn?
38
+ system! "yarn install"
39
+ end
40
+ end
41
+ end
42
+
43
+ def update_db
44
+ if rails?
45
+ step "Updating database" do
46
+ system! 'rake db:migrate db:test:prepare'
47
+ end
48
+ end
49
+ end
50
+
51
+ def remove_old_logs
52
+ if rails?
53
+ step "Removing old logs and tempfiles" do
54
+ system! 'rake log:clear tmp:clear'
55
+ end
56
+ end
57
+ end
58
+
59
+ def restart_servers
60
+ restart_rails if rails?
61
+ end
62
+
63
+ def restart_rails
64
+ step "Attempting to restart Rails" do
65
+ output = `bin/rails restart`
66
+
67
+ if $?.exitstatus > 0
68
+ puts colorize(
69
+ :light_red,
70
+ "skipping restart, not supported in this version of Rails (needs >= 6)"
71
+ )
72
+ else
73
+ puts output
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,5 @@
1
+ describe "passing test" do
2
+ it "should not fail" do
3
+ expect(3).to eq(3)
4
+ end
5
+ end
@@ -0,0 +1,62 @@
1
+ RSpec.configure do |config|
2
+ # rspec-expectations config goes here. You can use an alternate
3
+ # assertion/expectation library such as wrong or the stdlib/minitest
4
+ # assertions if you prefer.
5
+ config.expect_with :rspec do |expectations|
6
+ # This option will default to `true` in RSpec 4. It makes the `description`
7
+ # and `failure_message` of custom matchers include text for helper methods
8
+ # defined using `chain`, e.g.:
9
+ # be_bigger_than(2).and_smaller_than(4).description
10
+ # # => "be bigger than 2 and smaller than 4"
11
+ # ...rather than:
12
+ # # => "be bigger than 2"
13
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
14
+ end
15
+
16
+ # rspec-mocks config goes here. You can use an alternate test double
17
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
18
+ config.mock_with :rspec do |mocks|
19
+ # Prevents you from mocking or stubbing a method that does not exist on
20
+ # a real object. This is generally recommended, and will default to
21
+ # `true` in RSpec 4.
22
+ mocks.verify_partial_doubles = true
23
+ end
24
+
25
+ # These two settings work together to allow you to limit a spec run
26
+ # to individual examples or groups you care about by tagging them with
27
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
28
+ # get run.
29
+ config.filter_run :focus
30
+ config.run_all_when_everything_filtered = true
31
+
32
+ # This setting enables warnings. It's recommended, but in some cases may
33
+ # be too noisy due to issues in dependencies.
34
+ config.warnings = true
35
+
36
+ # Many RSpec users commonly either run the entire suite or an individual
37
+ # file, and it's useful to allow more verbose output when running an
38
+ # individual spec file.
39
+ if config.files_to_run.one?
40
+ # Use the documentation formatter for detailed output,
41
+ # unless a formatter has already been configured
42
+ # (e.g. via a command-line flag).
43
+ config.default_formatter = 'doc'
44
+ end
45
+
46
+ # Print the 10 slowest examples and example groups at the
47
+ # end of the spec run, to help surface which specs are running
48
+ # particularly slow.
49
+ config.profile_examples = 10
50
+
51
+ # Run specs in random order to surface order dependencies. If you find an
52
+ # order dependency and want to debug it, you can fix the order by providing
53
+ # the seed, which is printed after each run.
54
+ # --seed 1234
55
+ config.order = :random
56
+
57
+ # Seed global randomization in this process using the `--seed` CLI option.
58
+ # Setting this allows you to use `--seed` to deterministically reproduce
59
+ # test failures related to randomization by passing the same `--seed` value
60
+ # as the one that triggered the failure.
61
+ Kernel.srand config.seed
62
+ end