Skip to content

Commit

Permalink
provider for rancid routers
Browse files Browse the repository at this point in the history
  • Loading branch information
Bao Nguyen committed Oct 8, 2014
1 parent f14c59e commit 5e326f2
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 84 deletions.
7 changes: 6 additions & 1 deletion .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ platforms:

suites:
- name: default
data_bags_path: 'test/integration/data_bags'
run_list:
- recipe[rancid-git]
attributes: { region: "sv2"}
attributes:
region: "sv2"
rancid:
configs:
groups: ["sjc1"]
16 changes: 13 additions & 3 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# basic attributes
default[:postfix][:enabled] = true
default[:rancid][:installed] = false
default[:rancid][:user] = 'rancid'
default[:rancid][:group] = 'rancid'
default[:rancid][:uid] = 2021
Expand All @@ -10,12 +11,21 @@
default[:rancid][:run_interval] = 1 # hourly
default[:rancid][:cleanup_interval] = 23 # daily at 23rd hour

# local setup path
default[:rancid][:prefix_dir] = '/home/rancid'
default[:rancid][:install_dir] = '/home/rancid'
default[:rancid][:prefix_dir] = "/home/rancid"
default[:rancid][:install_dir] = "#{node[:rancid][:prefix_dir]}"
default[:rancid][:key_dir] = "#{node[:rancid][:install_dir]}/keys"
default[:rancid][:local_state_dir] = "#{node[:rancid][:prefix_dir]}/var/rancid"

# fetch from remote git repo
default[:rancid][:url] = 'https://github.com/dotwaffle/rancid-git.git'
default[:rancid][:version] = 'af62ee744c0bb268fddb9715b57b6c60ec1463b0'

# configs
default[:rancid][:configs][:groups] = ["default"]
default[:rancid][:configs][:cloginrc] = {
:user => "default",
:method => "ssh",
:password => "default",
:identity => "#{node[:rancid][:prefix_dir]}/rancid",
:pattern => "*"
}
54 changes: 54 additions & 0 deletions providers/router.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Encoding: utf-8
# Cookbook Name:: rancid-git
# Provider:: router
# Author:: Bao Nguyen
# License:: Apache 2.0
#
# Copyright 2014, Bao Nguyen

use_inline_resources

action :remove do
db_file = "#{node[:rancid][:install_dir]}/var/#{@name}/router.db"
Chef::Log.info "Removing router db #{new_resource.name.to_s}: to #{db_file}"

if ::File.exists?(db_file)
Chef::Log.info "Removing router db #{new_resource.name.to_s}: to #{db_file}"
file db_file do
action :delete
end
new_resource.updated_by_last_action(true)
end
end

action :create do
var = "#{node[:rancid][:install_dir]}/var"
group = "#{var}/#{new_resource.name}"
db_file = "#{group}/router.db"

directory var do
owner node[:rancid][:user]
group node[:rancid][:group]
mode "0755"
action :create
end

directory group do
owner node[:rancid][:user]
group node[:rancid][:group]
mode "0755"
action :create
end

template db_file do
source "routers.db.erb"
owner node[:rancid][:user]
group node[:rancid][:group]
mode "0644"
variables(
devices: new_resource.devices
)
end
Chef::Log.info "Building router db #{new_resource.name.to_s}: to #{db_file}"
new_resource.updated_by_last_action(true)
end
14 changes: 14 additions & 0 deletions recipes/_cron.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include_recipe "cron"
# install the cron job to run diff but doesn't do anything until it's setup with
# the routers/switch to query and groups etc.
cron_d "hourly-rancid-diff" do
hour 1
command "#{node[:rancid][:install_dir]}/bin/rancid-run"
user node[:rancid][:user]
end

cron_d "daily-clean-up" do
hour 23
command "/usr/bin/find #{node[:rancid][:install_dir]}/var/rancid/logs -type f -mtime +2 -exec rm {} \;"
user node[:rancid][:user]
end
78 changes: 78 additions & 0 deletions recipes/_install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

# packages needed by rancid to build and run
packages = {}
packages.merge!({
'expect' => '',
'git' => '',
'build-essential' => '',
'autoconf' => '',
})

packages.each do |pkg_name, pkg_version|
package pkg_name do
action :install
version pkg_version unless pkg_version.empty?
end
end

# Rancid is going to run as a user, which query router configs
# we creating the user/group here

group node[:rancid][:group] do
gid node[:rancid][:gid]
end

user node[:rancid][:user] do
supports :manage_home => true
comment "RANCID User"
uid node[:rancid][:uid]
gid node[:rancid][:group]
home node[:rancid][:prefix_dir]
shell "/bin/bash"
end

# we sync with the upstream rancid-git revision 3.8.1
if node.chef_environment == "dev"
branch_name = "master"
else
branch_name = node[:rancid][:version]
end

unless node[:rancid][:installed]
git "#{node[:rancid][:install_dir]}/rancid-git" do
not_if do
File.exist?("#{node[:rancid][:install_dir]}/.cloginrc")
end
# we use http so we can delay dealing with ssh_known_host manangement
repository node[:rancid][:url]
revision branch_name
action :sync
user node[:rancid][:user]
group node[:rancid][:group]
end

# build and install rancid from source here
bash "build_install_rancid" do
not_if do
File.exist?("#{node[:rancid][:install_dir]}/etc/rancid.conf")
end
user node[:rancid][:user]
group node[:rancid][:group]
cwd "#{node[:rancid][:install_dir]}/rancid-git"
code <<-EOF
autoreconf
./configure --prefix=#{node[:rancid][:prefix_dir]} --localstatedir=#{node[:rancid][:install_dir]}/var/rancid && make install
EOF
end
node.set[:rancid][:installed] = true
end

# make the etc/rancid.conf file
template "#{node[:rancid][:install_dir]}/etc/rancid.conf" do
source "rancid.conf.erb"
mode "0644"
variables(
:admin_email => node[:rancid][:admin_email],
:install_dir => node[:rancid][:install_dir]
)
end
96 changes: 16 additions & 80 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,26 @@
#
# All rights reserved
#
#

## rancid version that modified to work with git

# packages to support rancid
include_recipe "cron"

# packages needed by rancid to build and run
packages = {}
packages.merge!({
'expect' => '',
'git' => '',
'build-essential' => '',
'autoconf' => '',
})

packages.each do |pkg_name, pkg_version|
package pkg_name do
action :install
version pkg_version unless pkg_version.empty?
end
end

# Rancid is going to run as a user, which query router configs
# we creating the user/group here

group node[:rancid][:group] do
gid node[:rancid][:gid]
end

user node[:rancid][:user] do
supports :manage_home => true
comment "RANCID User"
uid node[:rancid][:uid]
gid node[:rancid][:group]
home node[:rancid][:prefix_dir]
shell "/bin/bash"
end


# we sync with the upstream rancid-git revision 3.8.1
if node.chef_environment == "dev"
branch_name = "master"
else
branch_name = node[:rancid][:version]
end

git "#{node[:rancid][:install_dir]}/rancid-git" do
not_if do
File.exist?("#{node[:rancid][:install_dir]}/.cloginrc")
end
# we use http so we can delay dealing with ssh_known_host manangement
repository node[:rancid][:url]
revision branch_name
action :sync
user node[:rancid][:user]
group node[:rancid][:group]
end

# build and install rancid from source here
bash "build_install_rancid" do
not_if do
File.exist?("#{node[:rancid][:install_dir]}/etc/rancid.conf")
include_recipe "rancid-git::_install"

# install the routers
node['rancid']['configs']['groups'].each do |g|
puts "G: #{g}"
search(:rancid, "id:\"#{g}\"").each do |n|
puts "N: #{n}"
puts "N-id: #{n[:id]}"
puts "I: #{n[:routers][0][:name]}"
rancid_git_router n[:id] do
devices n[:routers]
end
end
user node[:rancid][:user]
group node[:rancid][:group]
cwd "#{node[:rancid][:install_dir]}/rancid-git"
code <<-EOF
autoreconf
./configure --prefix=#{node[:rancid][:prefix_dir]} --localstatedir=#{node[:rancid][:install_dir]}/var/rancid && make install
EOF
end

# install the cron job to run diff but doesn't do anything until it's setup with
# the routers/switch to query and groups etc.
cron_d "hourly-rancid-diff" do
hour 1
command "#{node[:rancid][:install_dir]}/bin/rancid-run"
user node[:rancid][:user]
end

cron_d "daily-clean-up" do
hour 23
command "/usr/bin/find #{node[:rancid][:install_dir]}/var/rancid/logs -type f -mtime +2 -exec rm {} \;"
user node[:rancid][:user]
end
# deploy chefvault
# ssh key
# cloginrc

include_recipe "rancid-git::_cron"
28 changes: 28 additions & 0 deletions resources/router.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Author:: Bao Nguyen <[email protected]>
# Cookbook:: rancid-git
# Resource:: router
#
# Copyright 2014, Ooyala
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# List of all actions supported by the provider
actions :create, :remove

# Make create the default action
default_action :create

# Require attributes
attribute :name, kind_of: String, name_attribute: true
attribute :devices, kind_of: Array
5 changes: 5 additions & 0 deletions templates/default/cloginrc.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# sjc1 cluster
add user *-sjc1* rancid
add password *-sjc1* {D3s3rtr1pRRR} {Kbf7Lsmqq9yv}
add identity *-sjc1* /home/rancid/.ssh/rancid
add method *-sjc1* ssh
40 changes: 40 additions & 0 deletions templates/default/rancid.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This file is generated by Chef
# rancid 2.3.1

TERM=network;export TERM
LC_COLLATE="POSIX"; export LC_COLLATE
umask 027
TMPDIR=/tmp; export TMPDIR
BASEDIR=<%= node['rancid']['install_dir']%>/var/rancid; export BASEDIR
PATH=<%= node['rancid']['install_dir']%>/bin:/usr/bin:/usr/sbin:/bin:.:/usr/local/bin:/usr/bin; export PATH
CVSROOT=$BASEDIR/CVS; export CVSROOT
LOGDIR=$BASEDIR/logs; export LOGDIR
RCSSYS=git; export RCSSYS
#ACLSORT=YES; export ACLSORT
#NOPIPE=YES; export NOPIPE
FILTER_PWDS=YES; export FILTER_PWDS
NOCOMMSTR=YES; export NOCOMMSTR
#MAX_ROUNDS=4; export MAX_ROUNDS
OLDTIME=4; export OLDTIME
#LOCKTIME=4; export LOCKTIME
PAR_COUNT=5; export PAR_COUNT


# list of rancid groups
#LIST_OF_GROUPS="sf sjc1 sj sv2 lon mtv nyc gdl syd"

#<% node['rancid']['groups'].each do |g| -%>
# <%= g %>
#<% end -%>

LIST_OF_GROUPS="<%= node['rancid']['configs']['groups'].reduce("") {|acc,v| acc.concat("#{v} ")} %>"

###########
## Ooyala specific to bypass the rancid-${group} mapping, due to no access to remote smtp-out2.sv2 server
## MAIL configurations
#[email protected]; export mailrcpt
#[email protected]; export adminmailrcpt
#MAILDOMAIN="@ooyala.com"; export MAILDOMAIN

HTMLMAILS=NO; export HTMLMAILS
MAILHEADERS="Precedence: bulk"; export MAILHEADERS
3 changes: 3 additions & 0 deletions templates/default/routers.db.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @devices.each do |i| -%>
<%= i[:name] %>:<%= i[:model] %>:<%= i[:status] %>
<% end -%>
Loading

0 comments on commit 5e326f2

Please sign in to comment.