Skip to content

Instantly share code, notes, and snippets.

@trentonstrong
Created November 23, 2011 23:14
Show Gist options
  • Save trentonstrong/1390212 to your computer and use it in GitHub Desktop.
Save trentonstrong/1390212 to your computer and use it in GitHub Desktop.
My First Ruby Class
require 'chef/shef/ext'
require 'net/ssh'
require 'net/ssh/multi'
require 'readline'
module IchangeKnifePlugins
PROJECT_HOME = "/home/ubuntu/ichange.com"
SYMFONY_BIN = "#{PROJECT_HOME}/skylight/symfony"
DEFAULT_ROLES_LIST = [ "loadbalancer", "webserver", "taskserver" ]
class Deploy < Chef::Knife
banner "knife deploy ENVIRONMENT"
option :identity,
:short => '-i IDENTITY_FILE',
:long => '--identity IDENTITY_FILE',
:description => 'Path to SSH identity file'
option :hotfix,
:short => '-H CHANGE_REF',
:long => '--hotfix CHANGE_REF',
:description => 'Push an individual changeset as a hotfix, rather than a full update'
def run
unless name_args.size == 1
ui.fatal "You must specify an environment to deploy"
show_usage
exit 1
end
Shef::Extensions.extend_context_object(self)
environment = name_args.first
if config[:identity]
@identity = config[:identity]
end
if config[:hotfix]
@hotfix = config[:hotfix]
end
if environment == "production"
@version = increment_version
end
DEFAULT_ROLES_LIST.each do |role|
update(environment, role)
end
end
def send_ssh(node, command)
knife_ssh = Chef::Knife::Ssh.new
if @identity
knife_ssh.config[:identity_file] = @identity # e.g. "~/ichangekey.pem"
end
knife_ssh.config[:attribute] = "ec2.public_hostname"
knife_ssh.config[:ssh_user] = "ubuntu"
query = "name:#{node.name}"
knife_ssh.name_args = [query, command]
knife_ssh.run
end
def increment_version
version_db_item = data_bag_item('configuration', 'version')
version = version_db_item['version'].to_i
version = version += 1
version_db_item['version'] = version.to_s
version_db_item.save
return version
end
def update(environment, role)
nodes.find("roles:#{role} AND chef_environment:#{environment}") do |ws|
puts "Updating #{ws.name}"
send_ssh(ws, "#{SYMFONY_BIN} project:disable frontend prod")
send_ssh(ws, "sudo chef-client")
if environment == "production"
send_ssh(ws, "git --git-dir=#{PROJECT_HOME} tag #{@version}")
end
send_ssh(ws, "#{SYMFONY_BIN} project:enable frontend prod")
end
end
def hotfix(environment, role)
end
end
end
@durbin
Copy link

durbin commented Mar 28, 2013

Haha, I just clicked on this from a google search I did. Looked up and saw it was yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment