Skip to content
This repository has been archived by the owner on Jul 11, 2020. It is now read-only.

Commit

Permalink
hubot works!
Browse files Browse the repository at this point in the history
dwradcliffe committed Dec 24, 2014

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 72ca278 commit dcee8bd
Showing 14 changed files with 218 additions and 63 deletions.
1 change: 1 addition & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ cookbook 'rubygems-fail2ban', path: 'cookbooks/rubygems-fail2ban'
cookbook 'rubygems-ganglia', path: 'cookbooks/rubygems-ganglia'
cookbook 'rubygems-hostname', path: 'cookbooks/rubygems-hostname'
cookbook 'rubygems-hosts', path: 'cookbooks/rubygems-hosts'
cookbook 'rubygems-hubot', path: 'cookbooks/rubygems-hubot'
cookbook 'rubygems-logging', path: 'cookbooks/rubygems-logging'
cookbook 'rubygems-metrics', path: 'cookbooks/rubygems-metrics'
cookbook 'rubygems-monitoring', path: 'cookbooks/rubygems-monitoring'
15 changes: 13 additions & 2 deletions Berksfile.lock
Original file line number Diff line number Diff line change
@@ -53,6 +53,8 @@ DEPENDENCIES
path: cookbooks/rubygems-hostname
rubygems-hosts
path: cookbooks/rubygems-hosts
rubygems-hubot
path: cookbooks/rubygems-hubot
rubygems-logging
path: cookbooks/rubygems-logging
rubygems-metrics
@@ -143,6 +145,10 @@ GRAPH
hostname (0.3.0)
hostsfile (>= 0.0.0)
hostsfile (2.4.5)
hubot (1.0.2)
git (>= 0.0.0)
nodejs (>= 0.0.0)
runit (>= 0.0.0)
iptables (0.13.2)
java (1.26.0)
jenkins (1.2.2)
@@ -209,7 +215,7 @@ GRAPH
rubygems-sensu (>= 0.0.0)
rubygems-ssh (>= 0.0.0)
rubygems-utility (>= 0.0.0)
rubygems-app (0.0.34)
rubygems-app (0.0.36)
apt (>= 0.0.0)
chef-vault (>= 0.0.0)
git (>= 0.0.0)
@@ -269,6 +275,11 @@ GRAPH
dnsimple (>= 0.0.0)
hostname (>= 0.0.0)
rubygems-hosts (0.0.10)
rubygems-hubot (0.0.23)
chef-vault (>= 0.0.0)
hubot (~> 1.0.2)
rsyslog (>= 0.0.0)
rubygems-ruby (>= 0.0.0)
rubygems-logging (0.0.6)
chef-vault (>= 0.0.0)
rsyslog (>= 0.0.0)
@@ -288,7 +299,7 @@ GRAPH
motd (>= 0.0.0)
rubygems-ntp (0.0.3)
ntp (>= 0.0.0)
rubygems-people (0.0.32)
rubygems-people (0.0.34)
sudo (>= 0.0.0)
user (>= 0.0.0)
rubygems-redis (0.1.6)
2 changes: 1 addition & 1 deletion cookbooks/rubygems-app/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name 'rubygems-app'

version '0.0.34'
version '0.0.36'

depends 'apt'
depends 'chef-vault'
1 change: 1 addition & 0 deletions cookbooks/rubygems-hubot/attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default['rubygems-hubot']['deploy_dir'] = '/var/lib/hubot-capistrano'
46 changes: 46 additions & 0 deletions cookbooks/rubygems-hubot/files/default/scripts/deploy.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Description:
# Allows hubot to interact with capistrano.
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot deploy <env>
#
# Author:
# Shopify, David Radcliffe

spawn = require("child_process").spawn

exec = (command, args, callback) ->
data = ""

options = { env: { HOME: process.env["HOME"], PATH: process.env["PATH"], PWD: '/var/lib/hubot-capistrano/staging' } }
proc = spawn(command, args, options)

proc.stdout.on "data", (chunk) ->
data += chunk

proc.stderr.on "data", (chunk) ->
data += chunk

proc.on "error", (err) ->
callback "Error: #{err}"

proc.on "close", (exit_code)->
if exit_code == 0
callback data
else
callback "Exited with #{exit_code}\n#{data}"

module.exports = (robot) ->
robot.respond /deploy\s+(production|staging)/i, (msg) ->
env = msg.match[1]
msg.send "Deploying to #{env}..."
cap_command = "cap #{env} deploy"
server = if env == 'production' then 'bastion02.production.rubygems.org' else 'bastion01.staging.rubygems.org'
exec "bundle", ["exec", cap_command], (result) ->
msg.send "```\n#{result}\n```\n"
41 changes: 28 additions & 13 deletions cookbooks/rubygems-hubot/files/default/scripts/maintenance.coffee
Original file line number Diff line number Diff line change
@@ -13,12 +13,28 @@
# Author:
# David Radcliffe

exec = require('child_process').exec
spawn = require("child_process").spawn

runCommand = (cmd, server, callback) ->
cmd = "ssh #{server} \"#{cmd}\""
exec cmd, (error, stdout, stderr) ->
callback(error, stdout, stderr)
exec = (command, args, callback) ->
data = ""

options = { env: { HOME: process.env["HOME"], PATH: process.env["PATH"], PWD: process.env["PWD"] } }
proc = spawn(command, args, options)

proc.stdout.on "data", (chunk) ->
data += chunk

proc.stderr.on "data", (chunk) ->
data += chunk

proc.on "error", (err) ->
callback "Error: #{err}"

proc.on "close", (exit_code)->
if exit_code == 0
callback null, data
else
callback "Exited with #{exit_code}:\n#{data}"

getServer = (environment) ->
return if environment == 'production' then 'lb02.production.rubygems.org' else 'lb01.staging.rubygems.org'
@@ -27,13 +43,12 @@ module.exports = (robot) ->

robot.hear /turn (on|off) maint(enance)?( mode)? for (staging|production)/i, (msg) ->
if msg.match[1] == 'on'
command = 'ln -s /etc/nginx/maintenance.html /var/www/rubygems/maintenance.html'
command = 'sudo ln -s /etc/nginx/maintenance.html /var/www/rubygems/maintenance.html'
else if msg.match[1] == 'off'
command = 'rm /var/www/rubygems/maintenance.html'
runCommand command, getServer(msg.match[4]), (error, stdout, stderr) ->
if error
msg.send error
else if stderr
msg.send "Error: #{stderr}"
command = 'sudo rm /var/www/rubygems/maintenance.html'
server = getServer(msg.match[4])
exec "ssh", ["hubot@#{server}", command], (err, result) ->
if err
msg.send "```\n#{err}\n```\n"
else
msg.send "I have turned #{msg.match[1]} maintenance mode for the #{msg.match[4]} environment!"
msg.send ":+1: I have turned #{msg.match[1]} maintenance mode for the #{msg.match[4]} environment!"
3 changes: 2 additions & 1 deletion cookbooks/rubygems-hubot/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name 'rubygems-hubot'

version '0.0.2'
version '0.0.23'

depends 'chef-vault'
depends 'hubot', '~> 1.0.2'
depends 'rsyslog'
depends 'rubygems-ruby'
91 changes: 47 additions & 44 deletions cookbooks/rubygems-hubot/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -3,55 +3,55 @@
# Recipe:: default
#

include_recipe 'rubygems-ruby'

include_recipe 'chef-vault'
slack_secrets = chef_vault_item('slack', 'hubot')

node.set['hubot']['version'] = '2.4.6'
node.set['hubot']['scripts_version'] = '2.4.1'
node.set['hubot']['version'] = '2.11.0'
node.set['hubot']['scripts_version'] = '2.5.16'
node.set['hubot']['install_dir'] = '/srv/hubot'
node.set['hubot']['adapter'] = 'slack'

node.set['hubot']['dependencies'] = {
'hubot-slack' => '2.1.0',
'hubot-slack' => '~3.1.0',
'hubot-chef' => '>= 0.0.0',
'hubot-capistrano' => '1.0.2'
}

node.set['hubot']['hubot_scripts'] = %w(
ascii.coffee
base64.coffee
beerme.coffee
gemwhois.coffee
github-status.coffee
goooood.coffee
haters.coffee
kittens.coffee
likeaboss.coffee
look-of-disapproval.coffee
nice.coffee
rubygems.coffee
shipit.coffee
sudo.coffee
wunderground.coffee
xkcd.coffee
)

node.set['hubot']['external_scripts'] = %w(
hubot-chef
hubot-capistrano
)

directory '/var/lib/hubot-capistrano'
node.set['hubot']['hubot_scripts'] = []

node.set['hubot']['external_scripts'] = []

# Disabled external scripts:
# hubot-chef
# hubot-capistrano

node.set['hubot']['config'] = {
'HUBOT_SLACK_TOKEN' => slack_secrets['token'],
'HUBOT_SLACK_TEAM' => 'rubygems',
'HUBOT_SLACK_TEAM' => 'bundler',
'HUBOT_SLACK_BOTNAME' => 'hubot',
'HUBOT_CAP_DIR' => '/var/lib/hubot-capistrano'
# 'HUBOT_CAP_DIR' => '/var/lib/hubot-capistrano/'
}

template "#{node['hubot']['install_dir']}/external-scripts.json" do
source 'hubot-scripts.json.erb'
cookbook 'hubot'
owner node['hubot']['user']
group node['hubot']['group']
mode 0644
variables(hubot_scripts: node['hubot']['external_scripts'])
notifies :restart, "service[hubot]", :delayed
end

include_recipe 'hubot'

%w(math.coffee pugme.coffee rules.coffee translate.coffee google-images.coffee maps.coffee roles.coffee youtube.coffee).each do |filename|
file "#{node['hubot']['install_dir']}/scripts/#{filename}" do
action :delete
end
end

remote_directory "#{node['hubot']['install_dir']}/scripts" do
source 'scripts'
files_backup 0
@@ -65,19 +65,22 @@
notifies :restart, 'service[hubot]'
end

include_recipe 'rsyslog'
# include_recipe 'rsyslog'

template '/etc/rsyslog.d/30-hubot.conf' do
source 'rsyslog.conf.erb'
owner 'root'
group 'root'
mode '644'
notifies :restart, 'service[rsyslog]'
end
# template '/etc/rsyslog.d/30-hubot.conf' do
# source 'rsyslog.conf.erb'
# owner 'root'
# group 'root'
# mode '644'
# notifies :restart, 'service[rsyslog]'
# end

template '/etc/logrotate.d/hubot' do
source 'logrotate.erb'
owner 'root'
group 'root'
mode '644'
end
# template '/etc/logrotate.d/hubot' do
# source 'logrotate.erb'
# owner 'root'
# group 'root'
# mode '644'
# end

include_recipe 'rubygems-hubot::ssh'
include_recipe 'rubygems-hubot::deploy'
28 changes: 28 additions & 0 deletions cookbooks/rubygems-hubot/recipes/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Cookbook Name:: rubygems-hubot
# Recipe:: deploy
#

package 'libpq-dev'

directory node['rubygems-hubot']['deploy_dir'] do
user 'hubot'
group 'hubot'
end

git "#{node['rubygems-hubot']['deploy_dir']}/staging" do
repository 'https://github.com/rubygems/rubygems.org.git'
reference 'master'
user node['hubot']['user']
group node['hubot']['group']
action :sync
notifies :run, "execute[bundle_install_deploy]", :immediately
end

execute "bundle_install_deploy" do
command "bundle install --local --deployment"
cwd "#{node['rubygems-hubot']['deploy_dir']}/staging"
user node['hubot']['user']
group node['hubot']['group']
action :nothing
end
23 changes: 23 additions & 0 deletions cookbooks/rubygems-hubot/recipes/ssh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Cookbook Name:: rubygems-hubot
# Recipe:: ssh
#

include_recipe 'chef-vault'
hubot_secrets = chef_vault_item('hubot', 'keys')

directory "#{node['hubot']['install_dir']}/.ssh" do
owner node['hubot']['user']
mode 0700
end

file "#{node['hubot']['install_dir']}/.ssh/id_rsa" do
owner node['hubot']['user']
mode 0600
content hubot_secrets['private']
end

file "#{node['hubot']['install_dir']}/.ssh/id_rsa.pub" do
owner node['hubot']['user']
content hubot_secrets['public']
end
2 changes: 1 addition & 1 deletion cookbooks/rubygems-people/metadata.rb
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
maintainer 'RubyGems.org Ops Team'
license 'MIT'
description 'RubyGems user recipes'
version '0.0.32'
version '0.0.34'

depends 'user'
depends 'sudo'
8 changes: 7 additions & 1 deletion cookbooks/rubygems-people/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -59,7 +59,13 @@
sudo 'deploy' do
user '%sysadmin'
nopasswd true
commands ['/usr/sbin/service unicorn restart', '/usr/sbin/service delayed_job restart']
commands [
'/usr/sbin/service unicorn restart',
'/usr/sbin/service delayed_job restart',
'/usr/bin/chef-client',
'/bin/ln -s /etc/nginx/maintenance.html /var/www/rubygems/maintenance.html',
'/bin/rm /var/www/rubygems/maintenance.html'
]
end

include_recipe 'rubygems-people::meg'
14 changes: 14 additions & 0 deletions data_bags/users/hubot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id":"hubot",
"username": "hubot",
"comment": "Hubot",
"admin" : true,
"deployer": true,
"environments": [
"staging",
"production"
],
"ssh_keys": [
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCru1+Cxtmeyv4MQ2n+j+67JhIV2C/azQmF/gqogh4degA73NwUs++aCPcrXQ3nN3YKJ6Bs/1s7n8kDK7Sl+bwaefTwBxsP+5rlOvnJ0TvGW2YgzneJybEcd83CBUg9wrgaSslMvAcOPQqcQnrlnjrh6m/Tn0lWMH3bKBAFGq/0iQRDFDXJu3xLaW6mQER51MyFPUbtBQYF5XTaI5rny+Z3DxY/k0EPN/Ojkte64I+KGgUkWAiweT0ZA8LJfM6Y84MtRbXcDliCLiadlwWsbJ6TQplRy8a2jUSWcLdOLZv5CsI0qhvjRTPxetpHaaQBxDmR2rbdXCL4G4MCTk96p+Oj hubot"
]
}
6 changes: 6 additions & 0 deletions roles/bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name 'bot'
description 'Hubot'
run_list(
'recipe[rubygems]',
'recipe[rubygems-hubot]'
)

0 comments on commit dcee8bd

Please sign in to comment.