1+ #
2+ # Rakefile for Chef Server Repository
3+ #
4+ # Author:: Adam Jacob (<adam@hjksolutions.com>)
5+ # Copyright:: Copyright (c) 2008 HJK Solutions, LLC
6+ # License:: Apache License, Version 2.0
7+ #
8+ # Licensed under the Apache License, Version 2.0 (the "License");
9+ # you may not use this file except in compliance with the License.
10+ # You may obtain a copy of the License at
11+ #
12+ # http://www.apache.org/licenses/LICENSE-2.0
13+ #
14+ # Unless required by applicable law or agreed to in writing, software
15+ # distributed under the License is distributed on an "AS IS" BASIS,
16+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+ # See the License for the specific language governing permissions and
18+ # limitations under the License.
19+ #
20+
21+ require File . join ( File . dirname ( __FILE__ ) , 'config' , 'rake' )
22+
23+ require 'tempfile'
24+
25+ $vcs = :svn
26+ if File . directory? ( File . join ( TOPDIR , ".svn" ) )
27+ $vcs = :svn
28+ elsif File . directory? ( File . join ( TOPDIR , ".git" ) )
29+ $vcs = :git
30+ end
31+
32+ desc "Update your repository from source control"
33+ task :update do
34+ puts "** Updating your repository"
35+
36+ case $vcs
37+ when :svn
38+ sh %{svn up}
39+ when :git
40+ pull = false
41+ pull = true if File . join ( TOPDIR , ".git" , "remotes" , "origin" )
42+ IO . foreach ( File . join ( TOPDIR , ".git" , "config" ) ) do |line |
43+ pull = true if line =~ /\[ remote "origin"\] /
44+ end
45+ if pull
46+ sh %{git pull}
47+ else
48+ puts "* Skipping git pull, no origin specified"
49+ end
50+ end
51+ end
52+
53+ desc "Test your cookbooks for syntax errors"
54+ task :test do
55+ puts "** Testing your cookbooks for syntax errors"
56+ Dir [ File . join ( TOPDIR , "cookbooks" , "**" , "*.rb" ) ] . each do |recipe |
57+ sh %{ruby -c #{ recipe } } do |ok , res |
58+ if ! ok
59+ raise "Syntax error in #{ recipe } "
60+ end
61+ end
62+ end
63+ end
64+
65+ desc "Install the latest copy of the repository on this Chef Server"
66+ task :install => [ :update , :test ] do
67+ puts "** Installing your cookbooks"
68+ directories = [
69+ COOKBOOK_PATH ,
70+ SITE_COOKBOOK_PATH ,
71+ CHEF_CONFIG_PATH
72+ ]
73+ puts "* Creating Directories"
74+ directories . each do |dir |
75+ sh "sudo mkdir -p #{ dir } "
76+ sh "sudo chown root #{ dir } "
77+ end
78+ puts "* Installing new Cookbooks"
79+ sh "sudo rsync -rlP --delete --exclude '.svn' cookbooks/ #{ COOKBOOK_PATH } "
80+ puts "* Installing new Site Cookbooks"
81+ sh "sudo rsync -rlP --delete --exclude '.svn' cookbooks/ #{ COOKBOOK_PATH } "
82+ puts "* Installing new Chef Server Config"
83+ sh "sudo cp config/server.rb #{ CHEF_SERVER_CONFIG } "
84+ puts "* Installing new Chef Client Config"
85+ sh "sudo cp config/client.rb #{ CHEF_CLIENT_CONFIG } "
86+ end
87+
88+ desc "By default, run rake test"
89+ task :default => [ :test ]
90+
91+ desc "Create a new cookbook (with COOKBOOK=name)"
92+ task :new_cookbook do
93+ create_cookbook ( File . join ( TOPDIR , "cookbooks" ) )
94+ end
95+
96+ def create_cookbook ( dir )
97+ raise "Must provide a COOKBOOK=" unless ENV [ "COOKBOOK" ]
98+ puts "** Creating cookbook #{ ENV [ "COOKBOOK" ] } "
99+ sh "mkdir -p #{ File . join ( dir , ENV [ "COOKBOOK" ] , "attributes" ) } "
100+ sh "mkdir -p #{ File . join ( dir , ENV [ "COOKBOOK" ] , "recipes" ) } "
101+ sh "mkdir -p #{ File . join ( dir , ENV [ "COOKBOOK" ] , "definitions" ) } "
102+ sh "mkdir -p #{ File . join ( dir , ENV [ "COOKBOOK" ] , "libraries" ) } "
103+ sh "mkdir -p #{ File . join ( dir , ENV [ "COOKBOOK" ] , "files" , "default" ) } "
104+ sh "mkdir -p #{ File . join ( dir , ENV [ "COOKBOOK" ] , "templates" , "default" ) } "
105+ unless File . exists? ( File . join ( dir , ENV [ "COOKBOOK" ] , "recipes" , "default.rb" ) )
106+ open ( File . join ( dir , ENV [ "COOKBOOK" ] , "recipes" , "default.rb" ) , "w" ) do |file |
107+ file . puts <<-EOH
108+ #
109+ # Cookbook Name:: #{ ENV [ "COOKBOOK" ] }
110+ # Recipe:: default
111+ #
112+ # Copyright #{ Time . now . year } , #{ COMPANY_NAME }
113+ #
114+ EOH
115+ case NEW_COOKBOOK_LICENSE
116+ when :apachev2
117+ file . puts <<-EOH
118+ # Licensed under the Apache License, Version 2.0 (the "License");
119+ # you may not use this file except in compliance with the License.
120+ # You may obtain a copy of the License at
121+ #
122+ # http://www.apache.org/licenses/LICENSE-2.0
123+ #
124+ # Unless required by applicable law or agreed to in writing, software
125+ # distributed under the License is distributed on an "AS IS" BASIS,
126+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127+ # See the License for the specific language governing permissions and
128+ # limitations under the License.
129+ #
130+ EOH
131+ when :none
132+ file . puts <<-EOH
133+ # All rights reserved - Do Not Redistribute
134+ #
135+ EOH
136+ end
137+ end
138+ end
139+ end
140+
141+ desc "Create a new self-signed SSL certificate for FQDN=foo.example.com"
142+ task :ssl_cert do
143+ $expect_verbose = true
144+ fqdn = ENV [ "FQDN" ]
145+ fqdn =~ /^(.+?)\. (.+)$/
146+ hostname = $1
147+ domain = $2
148+ raise "Must provide FQDN!" unless fqdn && hostname && domain
149+ puts "** Creating self signed SSL Certificate for #{ fqdn } "
150+ sh ( "(cd #{ CADIR } && openssl genrsa 2048 > #{ fqdn } .key)" )
151+ sh ( "(cd #{ CADIR } && chmod 644 #{ fqdn } .key)" )
152+ puts "* Generating Self Signed Certificate Request"
153+ tf = Tempfile . new ( "#{ fqdn } .ssl-conf" )
154+ ssl_config = <<EOH
155+ [ req ]
156+ distinguished_name = req_distinguished_name
157+ prompt = no
158+
159+ [ req_distinguished_name ]
160+ C = #{ SSL_COUNTRY_NAME }
161+ ST = #{ SSL_STATE_NAME }
162+ L = #{ SSL_LOCALITY_NAME }
163+ O = #{ COMPANY_NAME }
164+ OU = #{ SSL_ORGANIZATIONAL_UNIT_NAME }
165+ CN = #{ fqdn }
166+ emailAddress = #{ SSL_EMAIL_ADDRESS }
167+ EOH
168+ tf . puts ( ssl_config )
169+ tf . close
170+ sh ( "(cd #{ CADIR } && openssl req -config '#{ tf . path } ' -new -x509 -nodes -sha1 -days 3650 -key #{ fqdn } .key > #{ fqdn } .crt)" )
171+ sh ( "(cd #{ CADIR } && openssl x509 -noout -fingerprint -text < #{ fqdn } .crt > #{ fqdn } .info)" )
172+ sh ( "(cd #{ CADIR } && cat #{ fqdn } .crt #{ fqdn } .key > #{ fqdn } .pem)" )
173+ sh ( "(cd #{ CADIR } && chmod 644 #{ fqdn } .pem)" )
174+ end
0 commit comments