necktie 0.3.3 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +72 -46
- data/lib/necktie/capistrano.rb +13 -0
- data/lib/necktie/gems.rb +1 -1
- data/lib/necktie/rake.rb +13 -2
- data/lib/necktie.rb +3 -0
- data/necktie.gemspec +2 -2
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -8,10 +8,9 @@ files, install and setup services, mount volumes, and basically anything you
|
|
8
8
|
can script with Ruby and a shell.
|
9
9
|
|
10
10
|
To use Necktie:
|
11
|
-
- Write your Necktie tasks
|
11
|
+
- Write your Necktie tasks
|
12
12
|
- Include support files (config, binaries, etc)
|
13
|
-
- git push
|
14
|
-
- cap necktie
|
13
|
+
- git push && cap necktie
|
15
14
|
|
16
15
|
I use it to:
|
17
16
|
|
@@ -25,8 +24,10 @@ I use it to:
|
|
25
24
|
== Tasking
|
26
25
|
|
27
26
|
Necktie is based on Rake, so if you know Rake you already know most of Necktie.
|
28
|
-
|
29
|
-
|
27
|
+
This document will teach you the rest.
|
28
|
+
|
29
|
+
At the base of your Necktie project there's a file called Necktie (or necktie,
|
30
|
+
or necktie.rb), with a list of tasks. For example:
|
30
31
|
|
31
32
|
file "/var/myapp" do
|
32
33
|
append "/etc/fstab", "\n/mnt/myapp /var/myapp none bind" unless read("/etc/fstab")["/mnt/myapp"]
|
@@ -34,12 +35,14 @@ or necktie.rb), with a list of task. For example:
|
|
34
35
|
sh "mount /var/myapp"
|
35
36
|
end
|
36
37
|
|
38
|
+
desc "Install/update ruby gems"
|
37
39
|
task :rubygems do
|
38
40
|
FileList["gems/*.gem"].each do |gem|
|
39
41
|
install_gem gem
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
45
|
+
desc "Setup and start Nginx"
|
43
46
|
task :nginx do
|
44
47
|
rm_rf "/etc/nginx/sites-enabled/*"
|
45
48
|
cp "etc/nginx/unicorn.conf", "/etc/nginx/sites-available/"
|
@@ -47,25 +50,27 @@ or necktie.rb), with a list of task. For example:
|
|
47
50
|
services.start "nginx"
|
48
51
|
end
|
49
52
|
|
53
|
+
desc "Setup and start Unicorn"
|
50
54
|
task :unicorn=>[:rubygems] do
|
51
55
|
cp "etc/init.d/unicorn", "/etc/init.d/"
|
52
56
|
chmod 0750, "/etc/init.d/unicorn"
|
53
57
|
end
|
54
58
|
|
59
|
+
desc "Perform all tasks for the role app"
|
55
60
|
task :app=>["/var/myapp", :nginx, :unicorn]
|
56
61
|
|
57
|
-
|
58
|
-
|
59
|
-
|
62
|
+
When you run the app task, it runs the tasks that setup the application
|
63
|
+
directory, install gems, configure Nginx and Unicorn. When it's done, you're
|
64
|
+
ready to deploy.
|
65
|
+
|
66
|
+
Necktie deals with initial configuration, and also ongoing maintenance. Say you
|
67
|
+
change a configuration file, or add another Gem to the gems directory. Push
|
68
|
+
these changes into your Git repository and run Necktime again. That simple.
|
60
69
|
|
61
|
-
If you changed one of the configuration files, you can run Necktie again: it
|
62
|
-
will pull updates from the Git repository and run these tasks with the new
|
63
|
-
configuration files.
|
64
70
|
|
65
|
-
Note:
|
66
|
-
|
67
|
-
|
68
|
-
doesn't exist).
|
71
|
+
Note: In the example above, the nginx and unicorn tasks run each time, you can
|
72
|
+
use them to push out new configurations. In contrast, the /var/myapp file task
|
73
|
+
only runs once, before that directory exists.
|
69
74
|
|
70
75
|
|
71
76
|
== cap necktie
|
@@ -73,23 +78,17 @@ doesn't exist).
|
|
73
78
|
I use Capistrano to setup new instances and upgrade existing ones. I then run
|
74
79
|
deploy:cold on the new instances, or deploy to upgrade running instances.
|
75
80
|
|
76
|
-
|
77
|
-
|
81
|
+
To use the necktie task set the necktie_url variable and require
|
82
|
+
necktie/capistrano:
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
gem_spec = Gem::SourceIndex.from_installed_gems.find_name("necktie").last
|
82
|
-
gem_file = File.join(Gem.dir, "cache", spec.file_name)
|
83
|
-
upload gem_file, File.basename(gem_file), :via=>:scp
|
84
|
-
sudo "gem install #{File.basename(gem_file)}"
|
85
|
-
# Run Necktie as sudo.
|
86
|
-
sudo "necktie #{necktie_url} #{ENV["ROLES"].to_s.gsub(',', ' ')}"
|
84
|
+
require "necktie/capistrano"
|
85
|
+
set :necktie_url, "git@example.com:mysetup"
|
87
86
|
|
88
87
|
To setup a new server:
|
89
88
|
|
90
|
-
cap necktie HOSTS=ec2-75-101-239-12.compute-1.amazonaws.com
|
89
|
+
cap necktie ROLES=app HOSTS=ec2-75-101-239-12.compute-1.amazonaws.com
|
91
90
|
|
92
|
-
To upgrade instances:
|
91
|
+
To upgrade running instances:
|
93
92
|
|
94
93
|
git push && cap necktie
|
95
94
|
|
@@ -100,46 +99,53 @@ Necktie includes Rush, so you can write tasks like this:
|
|
100
99
|
|
101
100
|
unless processes.find { |p| p.cmdline[/memcached\s.*-l\s0.0.0.0/] }
|
102
101
|
box["/etc/memcached.conf"].replace_contents! /^-l 127.0.0.1/, "-l 0.0.0.0"
|
103
|
-
|
102
|
+
services.start "memcached"
|
104
103
|
end
|
105
104
|
|
106
105
|
You can learn more about Rush here: http://rush.heroku.com
|
107
106
|
|
108
|
-
Of course, there's also FileUtils, system and sh, so you can
|
107
|
+
Of course, there's also FileUtils, system and sh (courtesy of Rake), so you can
|
108
|
+
also:
|
109
109
|
|
110
110
|
cp "etc/init.d/unicorn", "/etc/init.d"
|
111
111
|
chmod 0755, "/etc/init.d/unicorn"
|
112
112
|
sh "service start unicorn"
|
113
113
|
|
114
|
-
Note: The current directory (
|
115
|
-
|
116
|
-
Necktie repository.
|
114
|
+
Note: The current directory (Dir.pwd and launch_dir) is the root directory of
|
115
|
+
your Necktie repository. You can depend on relative paths when accessing files
|
116
|
+
in your Necktie repository.
|
117
117
|
|
118
118
|
|
119
119
|
== Role play
|
120
120
|
|
121
121
|
If you have different setups, split them into roles and give each role its own
|
122
|
-
|
123
|
-
|
122
|
+
main task. Have that main task depend on all the specific setup tasks for this
|
123
|
+
role. For example:
|
124
124
|
|
125
|
-
|
125
|
+
task :app=>[:rubygems, :nginx, :unicorn, "/var/myapp"]
|
126
|
+
task :db=>[:mount, :master, :backup]
|
126
127
|
|
127
|
-
|
128
|
-
|
128
|
+
You can then run Necktie with a list of roles. I recommend using the same roles
|
129
|
+
with Necktie and Capistrano, that way you can:
|
129
130
|
|
131
|
+
cap necktie ROLES=app,db
|
130
132
|
|
131
|
-
|
133
|
+
This usage will pass the role names as task names to the necktie command.
|
132
134
|
|
133
|
-
|
135
|
+
If you have only one role, or work frequently with a given role, you can define
|
136
|
+
the default task instead of setting ROLES each time. In your Necktie file add:
|
134
137
|
|
135
|
-
|
136
|
-
|
138
|
+
task :default=>:app
|
139
|
+
|
140
|
+
Note: As with Rake, command line arguments are either options (see necktie -H),
|
141
|
+
task names (executed in order), or name=value pairs that set environment
|
142
|
+
variables (e.g. RAILS_ENV=production).
|
137
143
|
|
138
144
|
|
139
145
|
== gem install
|
140
146
|
|
141
147
|
You can use install_gem in one of two ways. You can pass it a gem name and
|
142
|
-
|
148
|
+
version requirement. For example:
|
143
149
|
|
144
150
|
install_gem "unicorn", "~= 0.93"
|
145
151
|
|
@@ -147,17 +153,37 @@ You can store the gem file in your Necktie repository and install it from there:
|
|
147
153
|
|
148
154
|
install_gem "gems/unicorn-0.93.3.gem"
|
149
155
|
|
150
|
-
I prefer the later,
|
151
|
-
|
156
|
+
I prefer the later, not depending on the Gem servers being online when I decide
|
157
|
+
to upgrade.
|
152
158
|
|
153
159
|
Since install_gem will only install the same gem/version once, a run-always
|
154
160
|
rubygems.rb task is all you need:
|
155
161
|
|
156
|
-
|
157
|
-
install_gem gem
|
162
|
+
FileUtils["gems/*.gem"].each do |gem|
|
163
|
+
install_gem gem
|
158
164
|
end
|
159
165
|
|
160
166
|
|
167
|
+
== One on one
|
168
|
+
|
169
|
+
If you need to run Necktie interactively, use the necktie command. The first
|
170
|
+
time you run Necktie, it clones the Git repository into the .necktie directory
|
171
|
+
in your home directory. You'll need to give it the Git URL, for example:
|
172
|
+
|
173
|
+
necktie GIT_URL=git@example.com:mysetup app
|
174
|
+
|
175
|
+
Subsequent runs use that repository. You can launch the necktie command from
|
176
|
+
anywhere, it will always switch to the .necktie directory, run the tasks and
|
177
|
+
switch back to the previous working directory.
|
178
|
+
|
179
|
+
If you want to pull changes from the Git repository, use the -U option:
|
180
|
+
|
181
|
+
necktie -U app
|
182
|
+
|
183
|
+
If you have read/write access to the Git repository you use that to tweak and
|
184
|
+
add new tasks, and test them out before using your setup in production.
|
185
|
+
|
186
|
+
|
161
187
|
== License
|
162
188
|
|
163
189
|
Necktie, copyright (C) 2009 Assaf Arkin, released under the "Use for good, not
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Capistrano::Configuration.instance.load do
|
2
|
+
desc "Run necktie on all servers (you can use HOSTS or RAILS env vars)"
|
3
|
+
task :necktie do
|
4
|
+
fail "You need to set :necktie_url, <git_url>" unless necktie_url
|
5
|
+
spec = Gem::Specification.load(File.expand_path("../../necktie.gemspec", File.dirname(__FILE__)))
|
6
|
+
gem_spec = Gem::SourceIndex.from_installed_gems.find_name(spec.name, spec.version).last
|
7
|
+
gem_file = File.join(Gem.dir, "cache", gem_spec.file_name)
|
8
|
+
upload gem_file, File.basename(gem_file), :via=>:scp
|
9
|
+
sudo "gem install #{File.basename(gem_file)}"
|
10
|
+
roles = ENV["ROLES"].to_s.gsub(',', ' ') # roles => task names
|
11
|
+
sudo "necktie GIT_URL=#{necktie_url} -U #{roles} -e #{fetch(:rails_env, "production")}"
|
12
|
+
end
|
13
|
+
end
|
data/lib/necktie/gems.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "rubygems/dependency_installer"
|
2
2
|
|
3
|
-
def
|
3
|
+
def install_gem(name, version = nil)
|
4
4
|
installer = Gem::DependencyInstaller.new
|
5
5
|
spec = installer.find_spec_by_name_and_version(name, version).first.first
|
6
6
|
if Gem::SourceIndex.from_installed_gems.find_name(spec.name, spec.version).empty?
|
data/lib/necktie/rake.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
require "rake"
|
2
2
|
|
3
3
|
module Necktie
|
4
|
-
class Application < Rake::Application
|
4
|
+
class Application < Rake::Application #:nodoc:
|
5
5
|
|
6
6
|
def initialize
|
7
7
|
super
|
8
8
|
@name = "necktie"
|
9
9
|
@rakefiles = ["Necktie", "necktie", "Necktie.rb", "necktie.rb"]
|
10
10
|
options.nosearch = true
|
11
|
+
options.env = "production"
|
11
12
|
end
|
12
13
|
|
13
14
|
def run
|
@@ -33,6 +34,11 @@ module Necktie
|
|
33
34
|
|
34
35
|
def necktie_options
|
35
36
|
[
|
37
|
+
['--env', '-e [NAME]', "Sets the environment (defaults to 'production').",
|
38
|
+
lambda { |value|
|
39
|
+
options.env = value
|
40
|
+
}
|
41
|
+
],
|
36
42
|
['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
|
37
43
|
lambda { |value|
|
38
44
|
options.show_tasks = :describe
|
@@ -83,7 +89,7 @@ module Necktie
|
|
83
89
|
Rake::TaskManager.record_task_metadata = true
|
84
90
|
}
|
85
91
|
],
|
86
|
-
['--update', '-
|
92
|
+
['--update', '-U', "Update .necktie directory (git pull)",
|
87
93
|
lambda { |value| options.pull = true }
|
88
94
|
]
|
89
95
|
]
|
@@ -124,4 +130,9 @@ module Necktie
|
|
124
130
|
end
|
125
131
|
|
126
132
|
end
|
133
|
+
|
134
|
+
# Returns the environment name (set using -e command line option).
|
135
|
+
def self.env
|
136
|
+
Rake.application.options.env
|
137
|
+
end
|
127
138
|
end
|
data/lib/necktie.rb
CHANGED
data/necktie.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "necktie"
|
3
|
-
spec.version = "0.3.
|
3
|
+
spec.version = "0.3.5"
|
4
4
|
spec.author = "Assaf Arkin"
|
5
5
|
spec.email = "assaf@labnotes.org"
|
6
|
-
spec.homepage = ""
|
6
|
+
spec.homepage = "http://github.com/assaf/necktie"
|
7
7
|
spec.summary = "Dress to impress"
|
8
8
|
spec.description = "Configure your servers remotely using Ruby and Git"
|
9
9
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: necktie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Assaf Arkin
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-15 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -23,6 +23,7 @@ extra_rdoc_files: []
|
|
23
23
|
|
24
24
|
files:
|
25
25
|
- bin/necktie
|
26
|
+
- lib/necktie/capistrano.rb
|
26
27
|
- lib/necktie/files.rb
|
27
28
|
- lib/necktie/gems.rb
|
28
29
|
- lib/necktie/rake.rb
|
@@ -238,7 +239,7 @@ files:
|
|
238
239
|
- necktie.gemspec
|
239
240
|
- README.rdoc
|
240
241
|
has_rdoc: true
|
241
|
-
homepage:
|
242
|
+
homepage: http://github.com/assaf/necktie
|
242
243
|
licenses: []
|
243
244
|
|
244
245
|
post_install_message:
|