sous_chef 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,72 @@
1
+ set -e
2
+
3
+ exec 1>/root/stdout.log 2>/root/stderr.log
4
+
5
+ if test -e /root/report.log; then
6
+ rm /root/report.log
7
+ fi
8
+
9
+ if ! test -e /etc/config.yml; then
10
+ echo '--- {}
11
+
12
+ ' > /etc/config.yml
13
+ fi
14
+ chmod 0600 /etc/config.yml
15
+
16
+ if ! test -e /usr/local/rvm/scripts/rvm; then
17
+ echo 'Installing ruby version manager' >> /root/report.log
18
+ gem install rvm && rvm-install
19
+ fi
20
+
21
+ RUBYOPT=""
22
+ source /usr/local/rvm/scripts/rvm
23
+
24
+ if ! test -e /etc/profile.d/rvm.sh; then
25
+ echo 'Setting up rvm source'
26
+ echo '# rvm configuration
27
+ RUBYOPT=""
28
+ if [[ -s /usr/local/rvm/scripts/rvm ]] ; then source /usr/local/rvm/scripts/rvm ; fi
29
+ ' > /etc/profile.d/rvm.sh
30
+ fi
31
+
32
+ echo 'installing ruby'
33
+
34
+ if ! rvm list | grep ruby-1.8.6; then
35
+ rvm install ruby-1.8.6
36
+ fi
37
+
38
+ rvm use ruby-1.8.6
39
+
40
+ if ! gem -v | grep 1.3.5; then
41
+ echo 'upgrading rubygems' >> /root/report.log
42
+ gem uninstall rubygems-update
43
+ gem install rubygems-update -v 1.3.5 --no-ri --no-rdoc
44
+ update_rubygems
45
+ gem uninstall rubygems-update
46
+ fi
47
+
48
+ mkdir -p /home/sous_chef
49
+ chmod 0755 /home/sous_chef
50
+
51
+ if ! test -e /home/sous_chef/Gemfile; then
52
+ echo 'source "http://gemcutter.org/"
53
+
54
+ gem "chef"
55
+ gem "dbd-mysql", "0.4.3"
56
+ gem "dbi", "0.4.3"
57
+ gem "open4", "0.9.6"' > /home/sous_chef/Gemfile
58
+ fi
59
+
60
+ gem install bundler --no-ri --no-rdoc
61
+
62
+ cd /home/sous_chef
63
+ gem bundle
64
+
65
+ if ! test -e /etc/profile.d/bin-path.sh; then
66
+ echo 'Setting up bin-path'
67
+ echo '# bundled gem bin path configuration
68
+ export PATH=/home/sous_chef/bin:$PATH
69
+ ' > /etc/profile.d/bin-path.sh
70
+ fi
71
+
72
+ nohup /home/sous_chef/bin/chef --main &
@@ -0,0 +1,62 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SousChef::Recipe do
4
+ context "flags" do
5
+ describe "verbose" do
6
+ before do
7
+ @recipe = SousChef::Recipe.new(:verbose) do
8
+ execute "run ls" do
9
+ command "ls"
10
+ end
11
+ end
12
+ end
13
+
14
+ it "is verbose" do
15
+ @recipe.should be_verbose
16
+ end
17
+
18
+ it "includes comments" do
19
+ @recipe.to_script.should == "# run ls\nls"
20
+ end
21
+ end
22
+
23
+ describe "shebang" do
24
+ before do
25
+ @recipe = SousChef::Recipe.new(:shebang) do
26
+ execute "run ls" do
27
+ command "ls"
28
+ end
29
+ end
30
+ end
31
+
32
+ it "has shebang set" do
33
+ @recipe.should be_shebang
34
+ end
35
+
36
+ it "includes a shebang line" do
37
+ @recipe.to_script.should == "#!/bin/bash\n\nls"
38
+ end
39
+ end
40
+ end
41
+
42
+ it "doesn't change the script when run twice" do
43
+ recipe = SousChef::Recipe.new do
44
+ execute "change directory before commands" do
45
+ command "cp foo bar"
46
+ cwd "/home/user"
47
+ end
48
+ end
49
+ recipe.to_script.should == recipe.to_script
50
+ end
51
+
52
+ describe ".load" do
53
+ before do
54
+ @recipe = SousChef::Recipe.load(File.dirname(__FILE__) + '/fixtures/deploy_command.rb')
55
+ @recipe.node = { :config => {}, :chef_args => "--main" }
56
+ end
57
+
58
+ it "loads a recipe from a file" do
59
+ @recipe.to_script.should == File.read(File.dirname(__FILE__) + '/fixtures/deploy_command_expected.sh').strip
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SousChef::Resource::Base do
4
+ before do
5
+ @execute = SousChef::Resource::Base.new(nil, "install bundler")
6
+ end
7
+
8
+ it "has a name" do
9
+ @execute.name.should == "install bundler"
10
+ end
11
+
12
+ it "responds to a method its context responds to" do
13
+ @context = stub(:foobar => 5)
14
+ @execute = SousChef::Resource::Base.new(@context, "install bundler")
15
+ @execute.should respond_to(:foobar)
16
+ end
17
+
18
+ it "delegates missing methods to its context when it responds" do
19
+ @context = stub(:foobar => 5)
20
+ @execute = SousChef::Resource::Base.new(@context, "install bundler")
21
+ @execute.foobar.should == 5
22
+ end
23
+
24
+ it "does not resource_respond_to? a method its context responds to" do
25
+ @context = stub(:foobar => 5)
26
+ @execute = SousChef::Resource::Base.new(@context, "install bundler")
27
+ @execute.should_not be_resource_respond_to(:foobar)
28
+ end
29
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SousChef::Resource::Directory do
4
+ before do
5
+ @directory = resource("bin") do
6
+ end
7
+ @directory.to_script # evaluate the block
8
+ end
9
+
10
+ it "has a name" do
11
+ @directory.name.should == "bin"
12
+ end
13
+
14
+ it "has a path equal to the name when no explicit path is given" do
15
+ @directory.path.should == "bin"
16
+ end
17
+
18
+ it "has a path as set when an explicit path is given" do
19
+ @directory = resource("bin") do
20
+ path "/home/user/bin"
21
+ end
22
+ @directory.to_script # evaluate the block
23
+ @directory.path.should == "/home/user/bin"
24
+ end
25
+
26
+ it "creates the directory" do
27
+ @directory.to_script.should == %{mkdir -p bin}
28
+ end
29
+
30
+ it "allows deleting the directory" do
31
+ directory = resource("bin") do
32
+ action :delete
33
+ end
34
+ directory.to_script.should == %{rmdir bin}
35
+ end
36
+
37
+ it "raises an argument error on bad action" do
38
+ lambda {
39
+ resource("bin") { action :email }.setup
40
+ }.should raise_error(ArgumentError)
41
+ end
42
+
43
+ it "sets the mode of the file" do
44
+ @directory = resource("bin") do
45
+ mode 0600
46
+ end
47
+
48
+ @directory.to_script.should == %q{
49
+ mkdir -p bin
50
+ chmod 0600 bin
51
+ }.strip
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SousChef::Resource::Execute do
4
+ before do
5
+ @execute = SousChef::Resource::Execute.new(nil, "install bundler") do
6
+ command "gem install bundler"
7
+ end
8
+ end
9
+
10
+ it "has a name" do
11
+ @execute.name.should == "install bundler"
12
+ end
13
+
14
+ it "responds to a method its context responds to" do
15
+ @context = stub(:foobar => 5)
16
+ @execute = SousChef::Resource::Execute.new(@context, "install bundler") do
17
+ command "gem install bundler"
18
+ end
19
+ @execute.should respond_to(:foobar)
20
+ end
21
+
22
+ it "delegates missing methods to its context when it responds" do
23
+ @context = stub(:foobar => 5)
24
+ @execute = SousChef::Resource::Execute.new(@context, "install bundler") do
25
+ command "gem install bundler"
26
+ end
27
+ @execute.foobar.should == 5
28
+ end
29
+
30
+ it "does not resource_respond_to? a method its context responds to" do
31
+ @context = stub(:foobar => 5)
32
+ @execute = SousChef::Resource::Execute.new(@context, "install bundler") do
33
+ command "gem install bundler"
34
+ end
35
+ @execute.should_not be_resource_respond_to(:foobar)
36
+ end
37
+ end
@@ -0,0 +1,135 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SousChef::Resource::File do
4
+ before do
5
+ @file = resource("note.txt") do
6
+ content "this is a note"
7
+ end
8
+ @file.setup # evaluate the block
9
+ end
10
+
11
+ it "has a name" do
12
+ @file.name.should == "note.txt"
13
+ end
14
+
15
+ it "has a path equal to the name when no explicit path is given" do
16
+ @file.path.should == "note.txt"
17
+ end
18
+
19
+ it "has a path as set when an explicit path is given" do
20
+ @file = resource("note.txt") do
21
+ path "/home/user/note.txt"
22
+ content "this is a note"
23
+ end
24
+ @file.setup # evaluate the block
25
+ @file.path.should == "/home/user/note.txt"
26
+ end
27
+
28
+ it "has content as set when explicit content is given" do
29
+ @file.content.should == "this is a note"
30
+ end
31
+
32
+ it "has nil content when no content is given" do
33
+ @file = resource("note.txt")
34
+ @file.setup # evaluate the block
35
+ @file.content.should == nil
36
+ end
37
+
38
+ it "echos content to file" do
39
+ @file.to_script.should == %{
40
+ if ! test -e note.txt; then
41
+ echo 'this is a note' > note.txt
42
+ fi
43
+ }.strip
44
+ end
45
+
46
+ it "handles single-quotes in the content" do
47
+ @file = resource("note.txt") do
48
+ content %{this is a 'note'}
49
+ end
50
+ @file.to_script.should == %q{
51
+ if ! test -e note.txt; then
52
+ echo 'this is a \'note\'' > note.txt
53
+ fi
54
+ }.strip
55
+ end
56
+
57
+ it "handles dollar signs in the content without allowing them to expand" do
58
+ @file = resource("note.txt") do
59
+ content %{export PATH=/my/bin:$PATH}
60
+ end
61
+ @file.to_script.should == %q{
62
+ if ! test -e note.txt; then
63
+ echo 'export PATH=/my/bin:$PATH' > note.txt
64
+ fi
65
+ }.strip
66
+ end
67
+
68
+ it "handles newlines in the content" do
69
+ @file = resource("note.txt") do
70
+ content <<-EOS
71
+ This
72
+ is
73
+
74
+ a
75
+ NOTE
76
+ EOS
77
+ end
78
+ @file.to_script.should == %q{
79
+ if ! test -e note.txt; then
80
+ echo 'This
81
+ is
82
+
83
+ a
84
+ NOTE
85
+ ' > note.txt
86
+ fi
87
+ }.strip
88
+ end
89
+
90
+ it "outputs the path instead of the name if it exists" do
91
+ @file = resource("note.txt") do
92
+ path "/home/user/note.txt"
93
+ content %{this is a note}
94
+ end
95
+ @file.to_script.should == %q{
96
+ if ! test -e /home/user/note.txt; then
97
+ echo 'this is a note' > /home/user/note.txt
98
+ fi
99
+ }.strip
100
+ end
101
+
102
+ it "touches the file if the content is nil" do
103
+ @file = resource("note.txt")
104
+ @file.to_script.should == %q{
105
+ if ! test -e note.txt; then
106
+ touch note.txt
107
+ fi
108
+ }.strip
109
+ end
110
+
111
+ it "sets the mode of the file" do
112
+ @file = resource("note.txt") do
113
+ mode 0600
114
+ end
115
+
116
+ @file.to_script.should == %q{
117
+ if ! test -e note.txt; then
118
+ touch note.txt
119
+ fi
120
+ chmod 0600 note.txt
121
+ }.strip
122
+ end
123
+
124
+ it "deletes a file" do
125
+ @file = resource("note.txt") do
126
+ action :delete
127
+ end
128
+
129
+ @file.to_script.should == %q{
130
+ if test -e note.txt; then
131
+ rm note.txt
132
+ fi
133
+ }.strip
134
+ end
135
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SousChef::Resource::Gemfile do
4
+ it "uses the name as the path without an explicit path" do
5
+ gemfile = resource('/path/to/project/Gemfile')
6
+ gemfile.path.should == '/path/to/project/Gemfile'
7
+ end
8
+
9
+ it "uses an explicit path if one is given" do
10
+ gemfile = resource('My Project dependencies') do
11
+ path '/path/to/project/Gemfile'
12
+ end
13
+ gemfile.to_script # evaluate the block
14
+ gemfile.path.should == '/path/to/project/Gemfile'
15
+ end
16
+
17
+ it "appends Gemfile to the path if it isn't there" do
18
+ gemfile = resource('/path/to/project')
19
+ gemfile.path.should == '/path/to/project/Gemfile'
20
+ end
21
+
22
+ it "makes content a protected method" do
23
+ gemfile = resource('/path/to/project')
24
+ gemfile.protected_methods.should include('content')
25
+ end
26
+ end
data/spec/sous_spec.rb ADDED
@@ -0,0 +1,245 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SousChef do
4
+ describe "execute" do
5
+ it "returns a simple command" do
6
+ script = SousChef.prep do
7
+ execute "run a command" do
8
+ command "ls"
9
+ end
10
+ end
11
+ script.should == "ls"
12
+ end
13
+
14
+ it "concatenates multiple commands" do
15
+ script = SousChef.prep do
16
+ execute "run multiple commands" do
17
+ command "curl -O http://www.example.com/file.tgz"
18
+ command "tar xvfz file.tgz"
19
+ end
20
+ end
21
+ script.should == "curl -O http://www.example.com/file.tgz\ntar xvfz file.tgz"
22
+ end
23
+
24
+ it "respects multiline commands" do
25
+ script = SousChef.prep do
26
+ execute "run multiline commands" do
27
+ command <<-CODE
28
+ curl -O http://www.example.com/file.tgz
29
+ tar xvfz file.tgz
30
+ CODE
31
+ end
32
+ end
33
+ script.should == "curl -O http://www.example.com/file.tgz\ntar xvfz file.tgz"
34
+ end
35
+
36
+ it "changes current working directory on cwd" do
37
+ script = SousChef.prep do
38
+ execute "change directory" do
39
+ cwd "/home/user"
40
+ end
41
+ end
42
+ script.should == %{cd /home/user}
43
+ end
44
+
45
+ it "changes the current working directory before any commands" do
46
+ script = SousChef.prep do
47
+ execute "change directory before commands" do
48
+ command "cp foo bar"
49
+ cwd "/home/user"
50
+ end
51
+ end
52
+ script.should == %{cd /home/user\ncp foo bar}
53
+ end
54
+
55
+ it "can use a method defined within the prep block" do
56
+ script = SousChef.prep do
57
+ def report(message)
58
+ command "echo #{message}"
59
+ end
60
+
61
+ execute "say the date" do
62
+ report "`date`"
63
+ end
64
+ end
65
+ script.should == %{echo `date`}
66
+ end
67
+
68
+ it "scopes cwd to the correct execute block" do
69
+ script = SousChef.prep do
70
+ execute "install bundler" do
71
+ command "gem install bundler"
72
+ end
73
+
74
+ execute "bundle some project" do
75
+ cwd "/data/projects/foo"
76
+ command "gem bundle"
77
+ end
78
+ end
79
+ script.should == %{gem install bundler\n\ncd /data/projects/foo\ngem bundle}
80
+ end
81
+
82
+ it "wraps commands in an if block on not_if" do
83
+ script = SousChef.prep do
84
+ execute "bundle some project" do
85
+ cwd "/data/projects/foo"
86
+ command "gem bundle"
87
+ not_if "test -d /data/projects/foo/vendor"
88
+ end
89
+ end
90
+ script.should == %{
91
+ if ! test -d /data/projects/foo/vendor; then
92
+ cd /data/projects/foo
93
+ gem bundle
94
+ fi
95
+ }.strip
96
+ end
97
+
98
+ it "wraps commands in an if block with test -e on creates" do
99
+ script = SousChef.prep do
100
+ execute "bundle some project" do
101
+ creates "/data/projects/foo/vendor"
102
+ cwd "/data/projects/foo"
103
+ command "gem bundle"
104
+ end
105
+ end
106
+ script.should == %{
107
+ if ! test -e /data/projects/foo/vendor; then
108
+ cd /data/projects/foo
109
+ gem bundle
110
+ fi
111
+ }.strip
112
+ end
113
+ end
114
+
115
+ describe "file" do
116
+ it "creates a file" do
117
+ script = SousChef.prep do
118
+ file "bash config" do
119
+ path "~/.bash_profile"
120
+ content "export PATH=~/bin:$PATH"
121
+ end
122
+ end
123
+ script.should == %{
124
+ if ! test -e ~/.bash_profile; then
125
+ echo 'export PATH=~/bin:$PATH' > ~/.bash_profile
126
+ fi
127
+ }.strip
128
+ end
129
+ end
130
+
131
+ describe "directory" do
132
+ it "creates a directory" do
133
+ script = SousChef.prep do
134
+ directory "/usr/local/bin"
135
+ end
136
+ script.should == %{mkdir -p /usr/local/bin}
137
+ end
138
+ end
139
+
140
+ describe "log" do
141
+ it "sets logging to a file" do
142
+ script = SousChef.prep do
143
+ log "~/script.log"
144
+ end
145
+ script.should == %{exec 1>~/script.log 2>&1}
146
+ end
147
+
148
+ it "allows logging stdout only" do
149
+ script = SousChef.prep do
150
+ log do
151
+ stdout "stdout.log"
152
+ end
153
+ end
154
+ script.should == %{exec 1>stdout.log}
155
+ end
156
+
157
+ it "allows logging stdout and stderr" do
158
+ script = SousChef.prep do
159
+ log do
160
+ stdout "stdout.log"
161
+ stderr "stderr.log"
162
+ end
163
+ end
164
+ script.should == %{exec 1>stdout.log 2>stderr.log}
165
+ end
166
+ end
167
+
168
+ describe "halt_on_failed_command" do
169
+ it "outputs set -e" do
170
+ script = SousChef.prep do
171
+ halt_on_failed_command
172
+ end
173
+ script.should == %{set -e}
174
+ end
175
+ end
176
+
177
+ describe "echo" do
178
+ it "echoes the given string" do
179
+ script = SousChef.prep do
180
+ echo "I'm in bash!"
181
+ end
182
+ script.should == %q{echo 'I\'m in bash!'}
183
+ end
184
+
185
+ it "echoes from anywhere" do
186
+ script = SousChef.prep do
187
+ file "newfile.txt" do
188
+ echo "I'm in bash!"
189
+ content "something"
190
+ end
191
+ end
192
+ script.should == %q{
193
+ if ! test -e newfile.txt; then
194
+ echo 'I\'m in bash!'
195
+ echo 'something' > newfile.txt
196
+ fi
197
+ }.strip
198
+ end
199
+ end
200
+
201
+ describe "gemfile" do
202
+ it "creates a gemfile with the given path" do
203
+ script = SousChef.prep do
204
+ gemfile "/data/projects/foo" do
205
+ gem "rails", "2.3.2"
206
+ gem "rack", "1.0.0"
207
+ end
208
+ end
209
+ script.should == %q{
210
+ if ! test -e /data/projects/foo/Gemfile; then
211
+ echo 'gem "rack", "1.0.0"
212
+ gem "rails", "2.3.2"' > /data/projects/foo/Gemfile
213
+ fi
214
+ }.strip
215
+ end
216
+
217
+ it "can have sources" do
218
+ script = SousChef.prep do
219
+ gemfile "/data/projects/foo" do
220
+ gem "rails"
221
+ source 'http://gems.engineyard.com/'
222
+ end
223
+ end
224
+ script.should == %q{
225
+ if ! test -e /data/projects/foo/Gemfile; then
226
+ echo 'source "http://gems.engineyard.com/"
227
+
228
+ gem "rails"' > /data/projects/foo/Gemfile
229
+ fi
230
+ }.strip
231
+ end
232
+ end
233
+
234
+ describe "node" do
235
+ it "has a node accessible as set within the recipe" do
236
+ recipe = SousChef::Recipe.new do
237
+ execute "run a command" do
238
+ command "ls #{node[:dir]}"
239
+ end
240
+ end
241
+ recipe.node = { :dir => '/home' }
242
+ recipe.to_script.should == "ls /home"
243
+ end
244
+ end
245
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=specdoc
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'sous_chef'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ def resource(*args, &block)
9
+ described_class.new(SousChef::Recipe.new, *args, &block)
10
+ end
11
+ end