Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
generalized rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Dreessen committed Mar 28, 2015
1 parent 87567cf commit ca81a7e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 50 deletions.
10 changes: 5 additions & 5 deletions lib/cf_authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ def initialize

end

def authenticate_with(username,password)
authResult = `cf auth #{username} #{password}`
if authResult.include?("FAILED")
def authenticate_with(username,password,org,space)
auth_result = `cf auth #{username} #{password}`
if auth_result.include?("FAILED")
puts 'Authentication failed, exiting'
exit
end
authResult = `cf t -o pivotallabs -s project-monitor`
if authResult.include?("FAILED")
auth_result = `cf t -o #{org} -s #{space}`
if auth_result.include?("FAILED")
puts 'Authentication failed, exiting'
exit
end
Expand Down
23 changes: 8 additions & 15 deletions lib/cf_deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ class CF_deploy
require 'cf_authenticator'
require 'cf_git_tagger'

ENV_HASH = {:staging => 'project-monitor-staging', :production => 'project-monitor-production'}

def initialize(stdin, stdout, env, cf_authenticator = CF_authenticator.new(), cf_git_tagger = CF_git_tagger.new())
if env == nil
puts "Exit Code 1: Please supply a deployment environment name and try again. \nExample: 'rake cf_deploy[staging]'"
def initialize(stdin, stdout, org, space, env, cf_authenticator = CF_authenticator.new(), cf_git_tagger = CF_git_tagger.new())
if env == nil or env == ''
puts "Exit Code 1: Please supply a full deployment environment name and try again. \neg: 'rake cf_deploy[org-name,space-name,deployment-environment]'"
exit(1)
end
@stdin = stdin
@stdout = stdout
@actual_env = ENV_HASH[env.to_sym]
@org = org
@space = space
@actual_env = env
@authenticator = cf_authenticator
@git_tagger = cf_git_tagger
end
Expand All @@ -21,14 +21,7 @@ def deploy_to_env
puts "Exit Code 1: Invalid deployment environment. Possible environments include 'staging' and 'production'"
exit(1)
end

if @actual_env == "project-monitor-production"
confirm_deploy
end
if @actual_env == "project-monitor-staging"
execute_deploy
end

confirm_deploy
end

def confirm_deploy
Expand Down Expand Up @@ -56,7 +49,7 @@ def authenticate
username = wait_for_input_with('CF Email?: ')
puts 'CF Password?: '
password = @stdin.noecho(&:gets).chomp
@authenticator.authenticate_with(username,password)
@authenticator.authenticate_with(username,password,@org,@space)
end

def git_tag
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/cf_deploy.rake
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'cf_deploy'

desc "Securely deploy to Cloud Foundry: tag commits and require confirmation"
task :cf_deploy, :deploy_env do |t, args|
puts "starting deploy to #{args.deploy_env}"
task :cf_deploy, :deploy_org, :deploy_space, :deploy_env do |t, args|
puts "starting deploy to #{args.deploy_org} - #{args.deploy_space} : #{args.deploy_env}"
authenticator = CF_authenticator.new()
tagger = CF_git_tagger.new()
CF_deploy.new(STDIN, STDOUT, args.deploy_env.to_s, authenticator, tagger).deploy_to_env
CF_deploy.new(STDIN, STDOUT, args.deploy_org.to_s, args.deploy_space.to_s, args.deploy_env.to_s, authenticator, tagger).deploy_to_env

end
33 changes: 6 additions & 27 deletions spec/lib/cf_deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
exit_message = `rake cf_deploy`
expect(exit_message).to include('Exit Code 1:')
end

it 'should terminate if the user does not enter a valid deployment environment name' do
exit_message = `rake cf_deploy[THIS_SHOULD_REMAIN_AN_INVALID_DEPLOYMENT_ENVIRONMENT_AND_IN_ORDER_TO_DO_SO_I_AM_MAKING_IT_VERY_LONG]`
expect(exit_message).to include('Exit Code 1:')
end
end

describe 'confirming user input for production build' do
describe 'confirming user input for build' do

default_output_string = "Are you sure you want to deploy to project-monitor-production? (y/n)\n"
exit_output_string = default_output_string + "exiting\n"
Expand All @@ -23,7 +18,7 @@
stdin_double = double(STDIN)
allow(stdin_double).to receive(:gets) { 'y' }

cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'production')
cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'pivotallabs', 'project-monitor', 'project-monitor-production')
allow(cf_deploy_under_test).to receive(:execute_deploy) {}
expect { cf_deploy_under_test.confirm_deploy }.to output(default_output_string).to_stdout
end
Expand All @@ -32,35 +27,19 @@
stdin_double = double(STDIN)
allow(stdin_double).to receive(:gets) { 'n' }

cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'production')
cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'pivotallabs', 'project-monitor', 'project-monitor-production')
expect { cf_deploy_under_test.confirm_deploy }.to output(exit_output_string).to_stdout
end

it 'should repeat if the user types anything else' do
stdin_double = double(STDIN)
allow(stdin_double).to receive(:gets).and_return('o', 'hola', 'n')

cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'production')
cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'pivotallabs', 'project-monitor', 'project-monitor-production')
expect { cf_deploy_under_test.confirm_deploy }.to output(default_output_string+default_output_string+exit_output_string).to_stdout
end
end

describe 'not authenticating for a non-production build' do
it 'should just begin the push process for non-production build' do
authenticator_double = double(CF_authenticator)
git_tagger_double = double(CF_git_tagger)
stdin_double = double(STDIN)
allow(authenticator_double).to receive(:authenticate_with)
allow(git_tagger_double).to receive(:tag_commit_with_message)
allow(stdin_double).to receive(:gets) {'return value'}
allow(stdin_double).to receive(:noecho) {'return value'}

cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'staging', authenticator_double, git_tagger_double)
cf_deploy_under_test.deploy_to_env
expect(authenticator_double).to have_received(:authenticate_with).with(any_args)
expect(git_tagger_double).to have_received(:tag_commit_with_message).with(any_args)
end
end

describe 'authenticating with CloudFoundry' do
it 'should prompt the user for a username and password and authenticate with those inputs' do
Expand All @@ -73,7 +52,7 @@
authenticator_double = double(CF_authenticator)
allow(authenticator_double).to receive(:authenticate_with).and_return(0)

cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'staging', authenticator_double)
cf_deploy_under_test = CF_deploy.new(stdin_double, STDOUT, 'pivotallabs', 'project-monitor', 'staging', authenticator_double)
expect { cf_deploy_under_test.authenticate }.to output("CF Email?: \nCF Password?: \n").to_stdout

expect(authenticator_double).to have_received(:authenticate_with).with(randString1, randString2)
Expand All @@ -85,7 +64,7 @@
git_tagger_double = double(CF_git_tagger)
allow(git_tagger_double).to receive(:tag_commit_with_message).with(any_args)

cf_deploy_under_test = CF_deploy.new(STDIN, STDOUT, 'staging', CF_authenticator.new(), git_tagger_double)
cf_deploy_under_test = CF_deploy.new(STDIN, STDOUT, 'pivotallabs', 'poject-monitor', 'project-monitor-staging', CF_authenticator.new(), git_tagger_double)
cf_deploy_under_test.git_tag
expect(git_tagger_double).to have_received(:tag_commit_with_message).with(any_args)
end
Expand Down

0 comments on commit ca81a7e

Please sign in to comment.