class Napkin::CLI::Staging
Public Instance Methods
check_active(id)
click to toggle source
# File lib/napkin/cli/staging.rb, line 40 def check_active(id) commands = [ SSHKit::Command.new("[ `cat /proc/uptime | awk '{ printf \"%.0f\",($1/60/60/24) }'` -lt 3 ] && echo to_keep"), SSHKit::Command.new("cat /lift/logs/nginx/*.log | grep -v robots.txt | grep 'HTTP/1.1\" 200'"), SSHKit::Command.new("zcat /lift/logs/nginx/*.log.*.gz | grep -v robots.txt | grep 'HTTP/1.1\" 200' | egrep \"`date +%d/%b/%Y --date='1 day ago'`|`date +%d/%b/%Y --date='2 days ago'`\"") ] !check(id, *commands).empty? end
destroy(id)
click to toggle source
# File lib/napkin/cli/staging.rb, line 64 def destroy(id) if check_active(id) && !options[:force] say "#{id} is still active. Use -f to force.", :red return false end staging_name = name_for(instance(id)).rpartition('-').first if staging_name.empty? say "#{id} isn't a staging box.", :red return false end instance_ids = instances(true).collect { |instance| name_for(instance).start_with?(staging_name) ? instance.instance_id : nil }.compact instance_ids.each do |instance_id| show(instance_id) destroy_instance(instance_id) end notify(staging_name) end
destroy_inactive()
click to toggle source
# File lib/napkin/cli/staging.rb, line 51 def destroy_inactive instances.each do |i| destroy(i.instance_id) end end
list()
click to toggle source
# File lib/napkin/cli/staging.rb, line 26 def list instances.each do |instance| name = instance.tags.detect { |tag| tag.key == 'Name' && tag.value != '' }.value say "#{name.ljust(30)}\t#{instance.instance_id}\t#{instance.public_dns_name}" end end
show(id)
click to toggle source
# File lib/napkin/cli/staging.rb, line 34 def show(id) name = instance(id).tags.detect { |tag| tag.key == 'Name' && tag.value != '' }.value say "#{name.ljust(30)}\t#{instance(id).instance_id}\t#{instance(id).public_dns_name}" end
Private Instance Methods
check(id, *commands)
click to toggle source
# File lib/napkin/cli/staging.rb, line 114 def check(id, *commands) hostname = instance(id).public_dns_name name = instance(id).tags.detect { |tag| tag.key == 'Name' && tag.value != '' }.value say "checking #{hostname} (#{name})", :blue output = '' on hostname do commands.each do |command| begin output << capture(command) rescue SSHKit::Command::Failed # command failed, with a status code > 0, which means there were no logs found usually end end end output end
destroy_instance(id)
click to toggle source
# File lib/napkin/cli/staging.rb, line 103 def destroy_instance(id) say "destroying #{id}", :cyan instance(id).terminate end
ec2()
click to toggle source
# File lib/napkin/cli/staging.rb, line 150 def ec2 @ec2 ||= Aws::EC2::Client.new(:region => 'us-east-1') end
instance(id)
click to toggle source
# File lib/napkin/cli/staging.rb, line 133 def instance(id) unless @instance_id == id @instance = nil @instance_id = id end @instance ||= Aws::EC2::Resource.new.instance(id) end
instances(all = false)
click to toggle source
# File lib/napkin/cli/staging.rb, line 142 def instances(all = false) unless @all == all @instances = nil @all = all end @instances ||= ec2.describe_instances.reservations.map { |r| r.instances.map { |instance| instance if instance.tags.any? { |tag| tag.key == 'Name' && tag.value.end_with?('apps') || all } } }.flatten.compact.reject { |i| i.state.name != 'running' } end
name_for(instance)
click to toggle source
# File lib/napkin/cli/staging.rb, line 108 def name_for(instance) instance.tags.detect { |tag| tag.key == 'Name' && tag.value != '' }.value rescue NoMethodError '' end
notify(name)
click to toggle source
# File lib/napkin/cli/staging.rb, line 85 def notify(name) options = { :headers => { 'Content-Type' => 'application/json' }, :body => { :build => { :phase => 'FINISHED', :status => 'SUCCESS', :parameters => { :NODE_NAME => name } } }.to_json } HTTParty.post('http://lift-jarvis.herokuapp.com/jenkins/branch_destroy', options) end