Skip to content

Commit

Permalink
a number of improvements and fixes to the new mms-agent recipe:
Browse files Browse the repository at this point in the history
- fix the cookbook name in runit
- adds 2 new attributes: mongodb.mms_agent.{install_dir, log_dir} to
  customize the agent
- forces the use of the standard community runit cookbook
- only restarts the agent service if a change has been detected
- sets two new 'dynamic' attributes for reference:
  mongodb.mms_agent.{version, checksum}

note that the last (bounce on change) required quite a bit of extra work
because it always downloads the zip; most of this can be removed when
the issue is resolved by the mms mongo team
  • Loading branch information
mattchukabam committed Nov 8, 2013
1 parent 29dcbaa commit 28aaaa6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 33 deletions.
14 changes: 11 additions & 3 deletions Berksfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
"path": "."
},
"apt": {
"locked_version": "1.8.4",
"constraint": ">= 1.8.2"
"locked_version": "2.1.1"
},
"python": {
"locked_version": "1.4.0"
},
"build-essential": {
"locked_version": "1.4.0"
},
"yum": {
"locked_version": "2.1.0"
"locked_version": "2.3.0"
},
"runit": {
"locked_version": "1.3.0"
}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ For examples see the USAGE section below.
* `mongodb[:replicaset_name]` - Define name of replicatset
* `mongodb[:mms_agent][:api_key]` - MMS Agent API Key
* `mongodb[:mms_agent][:secret_key]` - MMS Agent API Key
* `mongodb[:mms_agent][:install_dir]` - Location to install the agent
* `mongodb[:mms_agent][:log_dir]` - Location to write the agent logfile. If this is a relative path, it's relative to where the service is run (via runit), e.g. set to './main'

# USAGE:

Expand Down
7 changes: 5 additions & 2 deletions attributes/mms_agent.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
default['mongodb']['mms_agent']['api_key'] = ''
default['mongodb']['mms_agent']['secret_key'] = ''
default[:mongodb][:mms_agent][:api_key] = ""
default[:mongodb][:mms_agent][:secret_key] = ""

default[:mongodb][:mms_agent][:install_dir] = "/usr/local/share"
default[:mongodb][:mms_agent][:log_dir] = "#{node[:mongodb][:logpath]}/agent"
4 changes: 2 additions & 2 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
recipe "mongodb::configserver", "Installs and configures a configserver for mongodb sharding"
recipe "mongodb::shard", "Installs and configures a single shard"
recipe "mongodb::replicaset", "Installs and configures a mongodb replicaset"
recipe "mongodb::mms-agent", "Installs and configures a Mongo Management Service agent"

depends "apt", ">= 1.8.2"
depends "apt"
depends "python"
depends "runit"
depends "runit", ">= 1.1.6"
depends "yum"

%w{ ubuntu debian freebsd centos redhat fedora amazon scientific}.each do |os|
Expand Down
78 changes: 56 additions & 22 deletions recipes/mms-agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,86 @@
include_recipe 'python'

require 'fileutils'
chef_gem 'rubyzip'

# munin-node for hardware info
package 'munin-node'
# python dependencies
python_pip 'pymongo'

# download
# download, and unzip if it's changed
package 'unzip'
remote_file '/tmp/10gen-mms-agent.zip' do
source 'https://mms.10gen.com/settings/10gen-mms-agent.zip'
# irrelevant because of https://jira.mongodb.org/browse/MMSSUPPORT-2258
checksum node.mongodb.mms_agent.checksum if node.mongodb.mms_agent.key?(:checksum)
notifies :run, "bash[unzip 10gen-mms-agent]", :immediately
end

# unzip
bash 'unzip 10gen-mms-agent' do
cwd '/tmp/'
code <<-EOH
unzip -o -d /usr/local/share/ ./10gen-mms-agent.zip
EOH
not_if { File.exist?('/usr/local/share/mms-agent') }
code "rm -rf #{node.mongodb.mms_agent.install_dir} && unzip -o -d #{node.mongodb.mms_agent.install_dir} /tmp/10gen-mms-agent.zip"
action :nothing
only_if {
def checksum_zip_contents(zipfile)
require 'zip/filesystem'
require 'digest'

files = Zip::File.open(zipfile).collect.reject{|f| f.name_is_directory?}.sort
content = files.map{|f| f.get_input_stream.read}.join
Digest::SHA256.hexdigest content
end
new_checksum = checksum_zip_contents('/tmp/10gen-mms-agent.zip')
existing_checksum = node.mongodb.mms_agent.key?(:checksum) ? node.mongodb.mms_agent.checksum : 'NONE'
Chef::Log.debug "new checksum = #{new_checksum}, expected = #{existing_checksum}"

should_install = !File.exist?("#{node.mongodb.mms_agent.install_dir}/settings.py") || new_checksum != existing_checksum
# update the expected checksum in chef, for reference
node.default.mongodb.mms_agent.checksum = new_checksum
should_install
}
end

# install pymongo
python_pip 'pymongo' do
action :install
# runit and agent logging
directory node.mongodb.mms_agent.log_dir do
action :create
recursive true
end
include_recipe 'runit::default'
mms_agent_service = runit_service 'mms-agent' do
template_name 'mms-agent'
cookbook 'mongodb'
options({
:mms_agent_install_dir => node.mongodb.mms_agent.install_dir,
:mms_agent_log_dir => node.mongodb.mms_agent.log_dir
})
action :nothing
end

# modify settings.py
# update settings.py and restart the agent if there were any key changes
ruby_block 'modify settings.py' do
block do
Chef::Log.warn "Found empty mms_agent.api_key or mms_agent.secret_key attributes" if node.mongodb.mms_agent.api_key.empty? || node.mongodb.mms_agent.secret_key.empty?

orig_s = ''
open('/usr/local/share/mms-agent/settings.py') { |f|
open("#{node.mongodb.mms_agent.install_dir}/mms-agent/settings.py") { |f|
orig_s = f.read
}
s = orig_s
s = s.gsub(/mms\.10gen\.com/, 'mms.10gen.com')
s = s.gsub(/@API_KEY@/, node['mongodb']['mms_agent']['api_key'])
s = s.gsub(/@SECRET_KEY@/, node['mongodb']['mms_agent']['secret_key'])
s = s.gsub(/mms_key = ".*"/, "mms_key = \"#{node['mongodb']['mms_agent']['api_key']}\"")
s = s.gsub(/secret_key = ".*"/, "secret_key = \"#{node['mongodb']['mms_agent']['secret_key']}\"")
if s != orig_s
open('/usr/local/share/mms-agent/settings.py','w') { |f|
Chef::Log.debug "Settings changed, overwriting and restarting service"
open("#{node.mongodb.mms_agent.install_dir}/mms-agent/settings.py", 'w') { |f|
f.puts(s)
}

# update the agent version in chef, for reference
/settingsAgentVersion = "(?<mms_agent_version>.*)"/ =~ s
node.default.mongodb.mms_agent.version = mms_agent_version

notifies :enable, mms_agent_service, :delayed
notifies :restart, mms_agent_service, :delayed
end
end
end

# runit
runit_service 'mms-agent' do
template_name 'mms-agent'
cookbook 'mms-agent'
run_restart false
end
3 changes: 2 additions & 1 deletion templates/default/sv-mms-agent-log-run.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/sh
exec svlogd -tt ./main

exec svlogd -tt <%= @options[:mms_agent_log_dir] %>
6 changes: 3 additions & 3 deletions templates/default/sv-mms-agent-run.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#!/bin/sh

cd /usr/local/share/mms-agent/
cd <%= @options[:mms_agent_install_dir] %>

exec 2>&1
exec chpst -u root:root python /usr/local/share/mms-agent/agent.py 2>&1
exec chpst -u root:root python <%= @options[:mms_agent_install_dir] %>/mms-agent/agent.py 2>&1

0 comments on commit 28aaaa6

Please sign in to comment.