├── README.rdoc ├── attributes └── default.rb ├── metadata.rb ├── providers ├── nodejs.rb ├── npm.rb └── server.rb ├── recipes ├── default.rb └── packages.rb ├── resources ├── nodejs.rb ├── npm.rb └── server.rb └── templates ├── debian └── init.sh.erb └── default ├── sv-nodejs-log-run.erb ├── sv-nodejs-run.erb └── upstart.erb /README.rdoc: -------------------------------------------------------------------------------- 1 | = DEPRECATED: 2 | 3 | This repo is no longer the recommended cookbook to use for installing and maintaining node and npm. The 'nodejs' cookbook here would be the one to use: https://github.com/redguide/nodejs/ 4 | 5 | = DESCRIPTION: 6 | 7 | This cookbook will deploy node and npm (node package manager) to your system. It also defines providers to install/uninstall node module and start/stop/restart node servers. 8 | 9 | * homepage : https://github.com/digitalbutter/cookbook-node 10 | 11 | = REQUIREMENTS: 12 | 13 | * a working git framework (for example provided by Opscode git cookbook) 14 | 15 | Tested on ubuntu. Should work on other system with upstart. 16 | 17 | = ATTRIBUTES: 18 | 19 | * node/revision : indicates which revision to install. Default is the latest ('HEAD'). Since node evolves very quickly and is not always that stable, you might want to set it to a stable version (for example 'v0.4.3') 20 | 21 | * `node[:node][:packages]` - An array of npm packages to be installed globally 22 | 23 | = USAGE: 24 | 25 | Include the node recipe to download, compile and install node along with npm. 26 | 27 | Optionally, you can add NPM packages by creating a packages array: 28 | 29 | ``` 30 | default_attributes({ 31 | node: { 32 | packages: ['coffee-script', 'lineman'] 33 | } 34 | }) 35 | ``` 36 | 37 | == defines: 38 | 39 | +node_server+ provider to start/stop server. The following example will start a provided script with node. Logs can be found in /var/log/node-my_node_server.log. 40 | 41 | node_server "my_node_server" do 42 | script "/path/to/your/node/script.js" # mandatory 43 | user "my_user" # default : nodejs 44 | dependency ["required","node","modules"] # default []. Will be installed with npm before starting the server 45 | args "arguments for your script" # default "" 46 | init_style :runit # default - by platform. Spported values: :upstart, :init, :runit 47 | action :start # Will start a node server. In [stop,start,restart], default :start 48 | end 49 | 50 | +node_npm+ provider to manage nodejs modules. The following example will install the connect module for node. 51 | 52 | node_npm "connect" do 53 | action :install 54 | end 55 | 56 | = LICENSE & AUTHOR: 57 | 58 | Author:: 59 | * Emmanuel Prochasson () 60 | * Ed Bosher () 61 | 62 | Contributor: 63 | * Jon Wood () 64 | 65 | Copyright:: 2011, Tikibooth Limited 66 | 67 | Licensed under the Apache License, Version 2.0 (the "License"); 68 | you may not use this file except in compliance with the License. 69 | You may obtain a copy of the License at 70 | 71 | http://www.apache.org/licenses/LICENSE-2.0 72 | 73 | Unless required by applicable law or agreed to in writing, software 74 | distributed under the License is distributed on an "AS IS" BASIS, 75 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 76 | See the License for the specific language governing permissions and 77 | limitations under the License. 78 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | default[:node][:revision] = "HEAD" 2 | default[:node][:repo_url] = "https://github.com/joyent/node.git" 3 | default[:node][:user] = "nodejs" 4 | default[:node][:packages] = [] 5 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | name "node" 2 | maintainer "Tikibooth Limited" 3 | maintainer_email "devops@butter.com.hk" 4 | license "Apache 2.0" 5 | description "Installs/Configures node, npm and node server providers" 6 | long_description IO.read(File.join(File.dirname(__FILE__), 'README.rdoc')) 7 | version "1.1.3" 8 | depends "git" 9 | depends "runit" 10 | depends "build-essential" 11 | supports "ubuntu" 12 | recipe "node-js", "Install node and npm" 13 | 14 | attribute "node-js/version", 15 | :display_name => "The version of node to install", 16 | :default => "HEAD" 17 | -------------------------------------------------------------------------------- /providers/nodejs.rb: -------------------------------------------------------------------------------- 1 | action :start do 2 | 3 | # Install dependency with npm 4 | if (not new_resource.dependency.empty?) 5 | new_resource.dependency.each do |dep| 6 | execute "install dependency module #{dep}" do 7 | command "sudo npm -g install #{dep}" 8 | end 9 | end 10 | end 11 | 12 | # Get the user's home 13 | user_home = (%x[cat /etc/passwd | grep #{new_resource.user} | cut -d":" -f6]).chomp 14 | 15 | # Create a node service for this program with upstart 16 | template "/etc/init/node-#{new_resource.name}.conf" do 17 | cookbook "node-js" 18 | source "upstart.erb" 19 | variables( 20 | :name => new_resource.name, 21 | :script => new_resource.script, 22 | :user => new_resource.user, 23 | :user_home => user_home 24 | ) 25 | end 26 | 27 | # Start the server 28 | execute "start node server" do 29 | command "start node-#{new_resource.name}" 30 | end 31 | end 32 | 33 | action :stop do 34 | execute "stop node server" do 35 | command "stop node-#{new_resource.name}" 36 | end 37 | end 38 | 39 | action :restart do 40 | execute "restart node server" do 41 | command "restart node-#{new_resource.name}" 42 | end 43 | end -------------------------------------------------------------------------------- /providers/npm.rb: -------------------------------------------------------------------------------- 1 | require 'chef/mixin/shell_out' 2 | require 'chef/mixin/language' 3 | include Chef::Mixin::ShellOut 4 | 5 | action :install do 6 | # If we specified a version, and it's not the current version, move to the specified version 7 | if @new_resource.version != nil && @new_resource.version != @current_resource.version 8 | install_version = @new_resource.version 9 | # If it's not installed at all, install it 10 | elsif @current_resource.version == nil 11 | install_version = candidate_version 12 | end 13 | 14 | if install_version 15 | Chef::Log.info("Installing #{@new_resource} version #{install_version}") 16 | status = install_package(@new_resource.name, install_version) 17 | if status 18 | @new_resource.updated_by_last_action(true) 19 | end 20 | end 21 | end 22 | 23 | action :uninstall do 24 | if removing_package? 25 | Chef::Log.info("Removing #{@new_resource}") 26 | remove_package(@current_resource.name, @new_resource.version) 27 | @new_resource.updated_by_last_action(true) 28 | else 29 | end 30 | end 31 | 32 | 33 | # OBSOLETE 34 | action :linstall do 35 | execute "install npm module locally" do 36 | command "npm install #{new_resource.name}" 37 | end 38 | end 39 | 40 | 41 | # OBSOLETE 42 | action :luninstall do 43 | execute "uninstall npm module locally" do 44 | command "npm uninstall #{new_resource.name}" 45 | end 46 | end 47 | 48 | def load_current_resource 49 | @current_resource = Chef::Resource::NodeNpm.new(@new_resource.name) 50 | @current_resource.name(@new_resource.name) 51 | @current_resource.version(nil) 52 | 53 | unless current_installed_version.nil? 54 | @current_resource.version(current_installed_version) 55 | end 56 | 57 | @current_resource 58 | end 59 | 60 | def current_installed_version 61 | @current_installed_version ||= begin 62 | delimeter = /@/ 63 | version_check_cmd = "npm -g list --parseable --long | grep -i -o -P -e #{@new_resource.name}@\[0-9]*.\[0-9]*.\[0-9]*" 64 | p = shell_out!(version_check_cmd) 65 | p.stdout.split(delimeter)[1].strip 66 | rescue Chef::Exceptions::ShellCommandFailed 67 | end 68 | end 69 | 70 | def candidate_version 71 | @candidate_version ||= begin 72 | # `npm search` does not return versions yet 73 | @new_resource.version||'latest' 74 | end 75 | end 76 | 77 | def install_package(name, version) 78 | v = "@#{version}" unless version == 'latest' 79 | shell_out!("npm -g install #{name}#{v}") 80 | end 81 | 82 | def removing_package? 83 | if @current_resource.version.nil? 84 | false # nothing to remove 85 | elsif @new_resource.version.nil? 86 | true # remove any version of a package 87 | elsif @new_resource.version == @current_resource.version 88 | true # remove the version we have 89 | else 90 | false # we don't have the version we want to remove 91 | end 92 | end 93 | 94 | def remove_package(name, version) 95 | shell_out!("npm -g uninstall #{@new_resource.name}") 96 | end 97 | -------------------------------------------------------------------------------- /providers/server.rb: -------------------------------------------------------------------------------- 1 | provider_for_init_style = { 2 | :upstart => Chef::Provider::Service::Upstart, 3 | :runit => Chef::Provider::Service::Init, 4 | :service => Chef::Provider::Service::Init 5 | } 6 | 7 | action :start do 8 | 9 | # Install dependency with npm 10 | if (not new_resource.dependency.empty?) 11 | new_resource.dependency.each do |dep| 12 | execute "install dependency module #{dep}" do 13 | command "sudo npm -g install #{dep}" 14 | end 15 | end 16 | end 17 | 18 | # Get the user's home 19 | user_home = (%x[cat /etc/passwd | grep #{new_resource.user} | cut -d":" -f6]).chomp 20 | user = new_resource.user || node[:node][:user] 21 | 22 | case new_resource.init_style 23 | when :upstart 24 | # Create a node service for this program with upstart 25 | template "/etc/init/node-#{new_resource.name}.conf" do 26 | cookbook "node" 27 | source "upstart.erb" 28 | variables( 29 | :name => new_resource.name, 30 | :script => new_resource.script, 31 | :user => user, 32 | :user_home => user_home, 33 | :args => new_resource.args, 34 | :path => new_resource.path 35 | ) 36 | end 37 | 38 | # Start the server 39 | service "node-#{new_resource.name}" do 40 | provider provider_for_init_style[:upstart] 41 | action :start 42 | end 43 | 44 | when :runit 45 | # if we passed this inside the runit_server block new_resource would have been evaluated in the context of the definition and thus fail 46 | template_options = { 47 | :name => new_resource.name, 48 | :script => new_resource.script, 49 | :user => user, 50 | :user_home => user_home, 51 | :args => new_resource.args 52 | } 53 | runit_service "node-#{new_resource.name}" do 54 | template_name "nodejs" 55 | log_template_name "nodejs" 56 | cookbook "node" 57 | options template_options 58 | end 59 | when :init 60 | template "/etc/init.d/node-#{new_resource.name}" do 61 | mode "0755" 62 | source "init.sh.erb" 63 | cookbok "node" 64 | variables( 65 | :name => new_resource.name, 66 | :script => new_resource.script, 67 | :user => user, 68 | :user_home => user_home, 69 | :args => new_resource.args 70 | ) 71 | end 72 | 73 | # Start the server 74 | service "node-#{new_resource.name}" do 75 | action :start 76 | end 77 | 78 | end 79 | end 80 | 81 | action :stop do 82 | service "node-#{new_resource.name}" do 83 | provider provider_for_init_style[new_resource.init_style] 84 | action :stop 85 | end 86 | end 87 | 88 | action :restart do 89 | service "node-#{new_resource.name}" do 90 | provider provider_for_init_style[new_resource.init_style] 91 | action :start 92 | end 93 | end 94 | 95 | action :enable do 96 | service "node-#{new_resource.name}" do 97 | provider provider_for_init_style[new_resource.init_style] 98 | action :enable 99 | end 100 | end 101 | 102 | action :disable do 103 | service "node-#{new_resource.name}" do 104 | provider provider_for_init_style[new_resource.init_style] 105 | action :disable 106 | end 107 | end 108 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: node 3 | # Recipe:: default 4 | # 5 | # Copyright 2011, Tikibooth Limited 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | include_recipe "git" 21 | 22 | %w.each do |pkg| 23 | package pkg 24 | end 25 | 26 | case node[:platform] 27 | when "centos","redhat","fedora" 28 | package "openssl-devel" 29 | when "debian","ubuntu" 30 | package "libssl-dev" 31 | end 32 | 33 | git "/opt/node-src" do 34 | repo node[:node][:repo_url] 35 | revision node[:node][:revision] 36 | notifies :run, "bash[compile_nodejs_source]", :immediately 37 | end 38 | 39 | bash "compile_nodejs_source" do 40 | cwd "/opt/node-src/" 41 | code <<-EOH 42 | ./configure && make -j#{node[:cpu][:total]} && make install && git rev-parse HEAD > /usr/local/share/node-version 43 | EOH 44 | not_if '[ -f /usr/local/share/node-version ] && [ "$(git rev-parse HEAD)" = "$(cat /usr/local/share/node-version)" ]', :cwd => "/opt/node-src" 45 | end 46 | 47 | 48 | bash "install_npm" do 49 | user "root" 50 | cwd "/tmp/" 51 | code <<-EOH 52 | curl http://npmjs.org/install.sh | clean=no sh 53 | EOH 54 | creates "/usr/local/bin/npm" 55 | end 56 | 57 | user node[:node][:user] do 58 | system true 59 | end 60 | 61 | include_recipe 'node::packages' 62 | -------------------------------------------------------------------------------- /recipes/packages.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: node 3 | # Recipe:: packages 4 | # 5 | # Copyright 2011, Tikibooth Limited 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | node[:node][:packages].each do |package| 21 | node_npm package do 22 | action :install 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /resources/nodejs.rb: -------------------------------------------------------------------------------- 1 | actions :start 2 | 3 | attribute :name, :kind_of => String, :name_attribute => true 4 | attribute :dependency, :kind_of => Array, :default => [] 5 | attribute :script, :kind_of => String 6 | attribute :user, :kind_of => String, :default => "root" 7 | attribute :args, :kind_of => String, :default => "" -------------------------------------------------------------------------------- /resources/npm.rb: -------------------------------------------------------------------------------- 1 | actions :install,:uninstall,:linstall,:luninstall 2 | default_action :install 3 | 4 | attribute :name, :kind_of => String, :name_attribute => true 5 | attribute :version, :default => nil 6 | -------------------------------------------------------------------------------- /resources/server.rb: -------------------------------------------------------------------------------- 1 | actions :start,:stop,:restart,:enable,:disable 2 | 3 | attribute :name, :kind_of => String, :name_attribute => true 4 | attribute :dependency, :kind_of => Array, :default => [] 5 | attribute :script, :kind_of => String 6 | attribute :path, :kind_of => String 7 | attribute :user, :kind_of => String 8 | attribute :args, :kind_of => String, :default => "" 9 | attribute :init_style, :kind_of => Symbol, :equal_to => [:upstart, :init, :runit] 10 | 11 | # some default values need to be set late because they are dynamic 12 | def initialize(*args) 13 | super 14 | @init_style ||= value_for_platform(:ubuntu => { :default => :upstart }, :default => :init) 15 | @user ||= node[:node][:user] 16 | @action = :start 17 | end 18 | 19 | -------------------------------------------------------------------------------- /templates/debian/init.sh.erb: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: node-<%= @name %> 4 | # Required-Start: $remote_fs $syslog 5 | # Required-Stop: $remote_fs $syslog 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Short-Description: node-<%= @name %> initscript 9 | # Description: NodeJS init script for <%= @name %> 10 | ### END INIT INFO 11 | 12 | # PATH should only include /usr/* if it runs after the mountnfs.sh script 13 | PATH=/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin 14 | DESC="NodeJS <%= @name %>" 15 | NAME=node-<%= @name %> 16 | DAEMON=/usr/local/bin/node 17 | DAEMON_ARGS="<%= @args %>" 18 | PIDFILE=/var/run/$NAME.pid 19 | SCRIPTNAME=/etc/init.d/$NAME 20 | USER=<%= @user %> 21 | 22 | # Exit if the package is not installed 23 | [ -x "$DAEMON" ] || exit 0 24 | 25 | # Read configuration variable file if it is present 26 | [ -r /etc/default/$NAME ] && . /etc/default/$NAME 27 | 28 | # Load the VERBOSE setting and other rcS variables 29 | . /lib/init/vars.sh 30 | 31 | # Define LSB log_* functions. 32 | # Depend on lsb-base (>= 3.2-14) to ensure that this file is present 33 | # and status_of_proc is working. 34 | . /lib/lsb/init-functions 35 | 36 | # 37 | # Function that starts the daemon/service 38 | # 39 | do_start() 40 | { 41 | # Return 42 | # 0 if daemon has been started 43 | # 1 if daemon was already running 44 | # 2 if daemon could not be started 45 | start-stop-daemon --start --quiet --chuid $USER --pidfile $PIDFILE -b -m --exec $DAEMON --test > /dev/null \ 46 | || return 1 47 | start-stop-daemon --start --quiet --chuid $USER --pidfile $PIDFILE -b -m --exec $DAEMON -- \ 48 | $DAEMON_ARGS \ 49 | || return 2 50 | # Add code here, if necessary, that waits for the process to be ready 51 | # to handle requests from services started subsequently which depend 52 | # on this one. As a last resort, sleep for some time. 53 | } 54 | 55 | # 56 | # Function that stops the daemon/service 57 | # 58 | do_stop() 59 | { 60 | # Return 61 | # 0 if daemon has been stopped 62 | # 1 if daemon was already stopped 63 | # 2 if daemon could not be stopped 64 | # other if a failure occurred 65 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME 66 | RETVAL="$?" 67 | [ "$RETVAL" = 2 ] && return 2 68 | # Wait for children to finish too if this is a daemon that forks 69 | # and if the daemon is only ever run from this initscript. 70 | # If the above conditions are not satisfied then add some other code 71 | # that waits for the process to drop all resources that could be 72 | # needed by services started subsequently. A last resort is to 73 | # sleep for some time. 74 | start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON 75 | [ "$?" = 2 ] && return 2 76 | # Many daemons don't delete their pidfiles when they exit. 77 | rm -f $PIDFILE 78 | return "$RETVAL" 79 | } 80 | 81 | # 82 | # Function that sends a SIGHUP to the daemon/service 83 | # 84 | do_reload() { 85 | # 86 | # If the daemon can reload its configuration without 87 | # restarting (for example, when it is sent a SIGHUP), 88 | # then implement that here. 89 | # 90 | start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME 91 | return 0 92 | } 93 | 94 | case "$1" in 95 | start) 96 | [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" 97 | do_start 98 | case "$?" in 99 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 100 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; 101 | esac 102 | ;; 103 | stop) 104 | [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" 105 | do_stop 106 | case "$?" in 107 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 108 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; 109 | esac 110 | ;; 111 | status) 112 | status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? 113 | ;; 114 | #reload|force-reload) 115 | # 116 | # If do_reload() is not implemented then leave this commented out 117 | # and leave 'force-reload' as an alias for 'restart'. 118 | # 119 | #log_daemon_msg "Reloading $DESC" "$NAME" 120 | #do_reload 121 | #log_end_msg $? 122 | #;; 123 | restart|force-reload) 124 | # 125 | # If the "reload" option is implemented then remove the 126 | # 'force-reload' alias 127 | # 128 | log_daemon_msg "Restarting $DESC" "$NAME" 129 | do_stop 130 | case "$?" in 131 | 0|1) 132 | do_start 133 | case "$?" in 134 | 0) log_end_msg 0 ;; 135 | 1) log_end_msg 1 ;; # Old process is still running 136 | *) log_end_msg 1 ;; # Failed to start 137 | esac 138 | ;; 139 | *) 140 | # Failed to stop 141 | log_end_msg 1 142 | ;; 143 | esac 144 | ;; 145 | *) 146 | #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 147 | echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 148 | exit 3 149 | ;; 150 | esac 151 | 152 | : 153 | -------------------------------------------------------------------------------- /templates/default/sv-nodejs-log-run.erb: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | exec 2>&1 3 | exec svlogd -tt ./main 4 | -------------------------------------------------------------------------------- /templates/default/sv-nodejs-run.erb: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | exec 2>&1 3 | <% if @options.has_key? :dir %> 4 | cd <%= @options[:dir] %> 5 | <% end %> 6 | exec chpst -u <%= @options[:user] %> -U <%= @options[:user] %> /usr/local/bin/node <%= @options[:script] %> <%= @options[:args] %> 7 | -------------------------------------------------------------------------------- /templates/default/upstart.erb: -------------------------------------------------------------------------------- 1 | description "node.js <%= @name %> server" 2 | author "Tikibooth, super inspired by kvz - http://kevin.vanzonneveld.net" 3 | 4 | 5 | start on started mountall 6 | stop on shutdown 7 | 8 | script 9 | export HOME=<%= @user_home %> 10 | <%= @path.nil? ? '' : "cd #{@path}" %> 11 | exec sudo -u <%= @user %> /usr/local/bin/node <%= @script %> <%= @args %> >> /var/log/node-<%= @name %>.log 2>&1 12 | end script 13 | 14 | post-start script 15 | echo "`date`: node-<%= @name %> (re)started" >> /var/log/node-<%= @name %>.log 2>&1 16 | end script --------------------------------------------------------------------------------