Skip to content

Commit

Permalink
Square cube recipe WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
peregrinator committed Feb 12, 2013
1 parent dbabbbe commit 2346f3f
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 0 deletions.
39 changes: 39 additions & 0 deletions square_cube/attributes/square_cube.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# Cookbook Name:: square_cube
# Recipe:: default
#
# Copyright 2012, Critical Juncture, LLC
#
# 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.

default[:square_cube][:repo_url] = "git://github.com/square/cube.git"
default[:square_cube][:app_path] = "/var/www/apps/cube"

default[:square_cube][:collector][:path] = "#{node[:square_cube][:app_path]}/bin/collector.js"
default[:square_cube][:evaluator][:path] = "#{node[:square_cube][:app_path]}/bin/evaluator.js"

default[:square_cube][:collector][:mongo_host] = "127.0.0.1"
default[:square_cube][:collector][:mongo_port] = 27017
default[:square_cube][:collector][:mongo_database] = "cube_development"
default[:square_cube][:collector][:mongo_username] = ""
default[:square_cube][:collector][:mongo_password] = ""
default[:square_cube][:collector][:http_port] = 1080
default[:square_cube][:collector][:udp_port] = 1180

default[:square_cube][:evaluator][:mongo_host] = "127.0.0.1"
default[:square_cube][:evaluator][:mongo_port] = 27017
default[:square_cube][:evaluator][:mongo_database] = "cube_development"
default[:square_cube][:evaluator][:mongo_username] = ""
default[:square_cube][:evaluator][:mongo_password] = ""
default[:square_cube][:evaluator][:http_port] = 1081

138 changes: 138 additions & 0 deletions square_cube/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#
# Cookbook Name:: square_cube
# Recipe:: default
#
# Copyright 2012, Critical Juncture, LLC
#
# 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.

if node[:chef][:roles].include?('square_cube')
git node[:square_cube][:app_path] do
user "deploy"
group "deploy"
repository node[:square_cube][:repo_url]
reference "master"
action :sync
end

bash "NPM install Square's cube" do
user "root"
cwd node[:square_cube][:app_path]
code <<-EOH
npm install
EOH

not_if do
system("npm list | grep -q cube")
end
end


#set up variables for collector service / upstart script
service_name = "cube_collector"
service_user = "deploy"
service_user_home = (%x[cat /etc/passwd | grep #{service_user} | cut -d":" -f6]).chomp

# Create a node service for this program with upstart
template "/etc/init/#{service_name}.conf" do
source "upstart.erb"
variables :name => service_name,
:script => node[:square_cube][:collector][:path],
:user => service_user,
:user_home => service_user_home,
:args => ""
end

file "/var/log/#{service_name}.log" do
owner service_user
group "root"
mode 0644
action :create
end

file node[:square_cube][:collector][:path] do
mode 0775
end

service service_name do
provider Chef::Provider::Service::Upstart
action [:enable, :start]
end

# generate collector config file
template "#{node[:square_cube][:app_path]}/bin/collector-config.js" do
source "collector-config.js.erb"
owner service_user
group service_user
variables :mongo_host => node[:square_cube][:collector][:mongo_host],
:mongo_port => node[:square_cube][:collector][:mongo_port],
:mongo_database => node[:square_cube][:collector][:mongo_database],
:mongo_username => node[:square_cube][:collector][:mongo_username],
:mongo_password => node[:square_cube][:collector][:mongo_password],
:http_port => node[:square_cube][:collector][:http_port],
:udp_port => node[:square_cube][:collector][:udp_port]
mode 0755

notifies :restart, resources(:service => service_name)
end


#set up variables for evaluator service / upstart script
service_name = "cube_evaluator"
service_user = "deploy"
service_user_home = (%x[cat /etc/passwd | grep #{service_user} | cut -d":" -f6]).chomp

# Create a node service for this program with upstart
template "/etc/init/#{service_name}.conf" do
source "upstart.erb"
variables :name => service_name,
:script => node[:square_cube][:evaluator][:path],
:user => service_user,
:user_home => service_user_home,
:args => ""
end

file "/var/log/#{service_name}.log" do
owner service_user
group "root"
mode 0644
action :create
end

file node[:square_cube][:evaluator][:path] do
mode 0775
end


service service_name do
provider Chef::Provider::Service::Upstart
action [:enable, :start]
end

# generate collector config file
template "#{node[:square_cube][:app_path]}/bin/evaluator-config.js" do
source "evaluator-config.js.erb"
owner service_user
group service_user
variables :mongo_host => node[:square_cube][:evaluator][:mongo_host],
:mongo_port => node[:square_cube][:evaluator][:mongo_port],
:mongo_database => node[:square_cube][:evaluator][:mongo_database],
:mongo_username => node[:square_cube][:evaluator][:mongo_username],
:mongo_password => node[:square_cube][:evaluator][:mongo_password],
:http_port => node[:square_cube][:evaluator][:http_port]
mode 0755

notifies :restart, resources(:service => service_name)
end

end
11 changes: 11 additions & 0 deletions square_cube/templates/default/collector-config.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* automatically generated by chef - do not edit, changes will not persist. */

module.exports = {
"mongo-host": "<%= @mongo_host %>",
"mongo-port": <%= @mongo_port %>,
"mongo-database": "<%= @mongo_database %>",
"mongo-username": <%= @mongo_username.empty? ? 'null' : '"#{@mongo_username}"' %>,
"mongo-password": <%= @mongo_password.empty? ? 'null' : '"#{@mongo_password}"' %>,
"http-port": <%= @http_port %>,
"udp-port": <%= @udp_port %>
};
10 changes: 10 additions & 0 deletions square_cube/templates/default/evaluator-config.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* automatically generated by chef - do not edit, changes will not persist. */

module.exports = {
"mongo-host": "<%= @mongo_host %>",
"mongo-port": <%= @mongo_port %>,
"mongo-database": "<%= @mongo_database %>",
"mongo-username": <%= @mongo_username.empty? ? 'null' : '"#{@mongo_username}"' %>,
"mongo-password": <%= @mongo_password.empty? ? 'null' : '"#{@mongo_password}"' %>,
"http-port": <%= @http_port %>
};
20 changes: 20 additions & 0 deletions square_cube/templates/default/upstart.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
description "node.js <%= @name %> server"

start on started mountall
stop on shutdown

expect daemon

script
export HOME="<%= @user_home %>"
exec sudo -u <%= @user %> forever start <%= @script %> <%= @args %> >> /var/log/<%= @name %>.log 2>&1
end script

post-start script
echo "`date`: <%= @name %> (re)started" >> /var/log/<%= @name %>.log 2>&1
end script

pre-stop script
export HOME="<%= @user_home %>"
exec sudo -u <%= @user %> forever stop <%= @script %> >> /var/log/<%= @name %>.log 2>&1
end script

0 comments on commit 2346f3f

Please sign in to comment.