Skip to content

Instantly share code, notes, and snippets.

@ramlaxmanyadav
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramlaxmanyadav/8ad74896fe1172acc6c6 to your computer and use it in GitHub Desktop.
Save ramlaxmanyadav/8ad74896fe1172acc6c6 to your computer and use it in GitHub Desktop.
A capistrano recipe to keep deployment information such as environments,server_ip,deployed_by and time for deployment into a google spreadsheet.
#!/bin/bash
# Capistrano recipe to write deployment status into a google spreadsheet.
# Created by Ram Laxman yadav
# http://ramlaxmanyadav.github.io/
############################### How to use ###############################
# Do following changes in config/deploy.rb file
set :sheet_name, 'YOUR_SPREADSHEET_NAME'
set :work_sheet_name, 'YOUR_WORKBOOK_NAME'
# Do following changes inside deploy/environment.rb file
set :host_ip, 'YOUR_SERVER_IP'
#Create a file named product_deployment_status.rb inside config/recipes/ directory. Copy and paste following code inside this file.
# Dont forget to load "config/recipes/product_deployment_sheet" file inside deploy.rb
require 'google/api_client'
require 'google_drive'
namespace :product_deployment_sheet do
desc 'Test task for spreadsheet'
task :update do
status_row = [
host_ip,
rails_env,
Time.now,
`git config user.name`.chomp,
branch,
`git log --format="%H" -1 -b #{branch}`.chomp
]
raise 'Required data not set please check your deploy.rb and deploy/environment.rb file.' if (sheet_name.nil? or work_sheet_name.nil?)
puts '=============================================Updating Deployemnt Sheet started ==================================================='
puts 'Sr.no | Server ip | Rails Env | Date/Time | Deployed By | Git Branch | Commit ID'
DeploymentSheet.new.update_status(status_row, sheet_name, work_sheet_name,true) # true for write in descending order
puts '======================================Updating Deployemnt Sheet completed=========================================================='
end
#invoke sheet update after each final deployment
after 'deploy:cold', 'product_deployment_sheet:update'
end
class DeploymentSheet
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLENT_SECRET'
REFRESH_TOKEN = 'YOUR_REFRESH_TOKEN'
COLUMN_NAMES = ['Sr. No', 'Server ip address', 'Environment', 'Deployed on', 'Deployed by', 'Branch name', 'Commit id']
def update_status(row, sheet_title, work_sheet_title,is_descending = false)
_work_sheet = fetch_or_create_work_sheet(sheet_title, work_sheet_title)
begin
is_descending ? update_in_descending_format(_work_sheet, row) : update_ascending(_work_sheet, row)
rescue Exception => e
puts 'Exception raised while updating sheet unable to get the worksheet to write:'
puts e.backtrace
end
end
def fetch_access_token_from_refresh_token(client)
begin
client.authorization.grant_type = 'refresh_token'
client.authorization.refresh_token = DeploymentSheet::REFRESH_TOKEN
client.authorization.fetch_access_token!
client.authorization.access_token
rescue Exception => e
puts 'Exception raised while fetching access token from refresh token:'
puts e.backtrace
end
end
def get_google_session
begin
GoogleDrive.login_with_oauth(fetch_access_token_from_refresh_token(fetch_google_client))
rescue Exception => e
puts 'Exception raised while login with access_token:'
puts e.backtrace
end
end
def fetch_google_client
_client = Google::APIClient.new({application_name: 'GoogleSpreadSheet'})
auth = _client.authorization
auth.client_id = DeploymentSheet::CLIENT_ID
auth.client_secret = DeploymentSheet::CLIENT_SECRET
auth.scope = [
'https://www.googleapis.com/auth/drive',
'https://spreadsheets.google.com/feeds/'
]
auth.redirect_uri = 'https://www.example.com/oauth2callback'
_client
end
def fetch_or_create_work_sheet(sheet_title, work_sheet_title, session = get_google_session)
_sheet = session.spreadsheet_by_title(sheet_title).nil? ? session.create_spreadsheet(sheet_title) : session.spreadsheet_by_title(sheet_title)
_work_sheet = _sheet.worksheet_by_title(work_sheet_title).nil? ? create_work_sheet(_sheet, work_sheet_title) : _sheet.worksheet_by_title(work_sheet_title)
end
def create_work_sheet(sheet, title)
populate_headers((is_first_default_work_sheet?(sheet) ? sheet.worksheets.first : sheet.add_worksheet(title)), title)
end
def is_first_default_work_sheet?(sheet)
(sheet.worksheets.count == 1 and sheet.worksheets.first.title == 'Sheet1')
end
def populate_headers(work_sheet, title)
work_sheet.title = title
DeploymentSheet::COLUMN_NAMES.each_with_index do |value, index|
work_sheet[1, index+1] = value
end
work_sheet.save
work_sheet
end
def move_rows_forward(worksheet)
(1..worksheet.num_rows).to_a.reverse.each do |index|
(1..worksheet.num_cols).each do |col|
worksheet[index+1, col] = worksheet[index, col]
end
end
end
def update_ascending(worksheet, row)
row.insert(0, worksheet.num_rows+1)
row_index = worksheet.num_rows + 1
(1.. worksheet.num_cols).each do |i|
worksheet[row_index, i] = row[i-1]
end
puts "#{worksheet.rows.last.inspect }"
worksheet.save
end
def update_in_descending_format(worksheet, row)
row.insert(0, worksheet.num_rows+1)
move_rows_forward(worksheet)
(1.. worksheet.num_cols).each do |i|
worksheet[1, i] = row[i-1]
end
puts "#{worksheet.rows.last.inspect }"
worksheet.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment