-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
264 lines (233 loc) · 25.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>her</title>
<link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
</head>
<body>
<div id='container'>
<div id="background"></div>
<table cellspacing=0 cellpadding=0>
<thead>
<tr>
<th class=docs><h1>her</h1></th>
<th class=code></th>
</tr>
</thead>
<tbody>
<tr id='section-1'>
<td class=docs>
<div class="octowrap">
<a class="octothorpe" href="#section-1">#</a>
</div>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-2'>
<td class=docs>
<div class="octowrap">
<a class="octothorpe" href="#section-2">#</a>
</div>
<p> <strong>her</strong> is an easy to use deployment tool. It assumes that you are using nginx
together with <a href='http://google.com?q=unicorn'>unicorn</a>.</p>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-3'>
<td class=docs>
<div class="octowrap">
<a class="octothorpe" href="#section-3">#</a>
</div>
<h3>Workflow</h3>
<pre><code>her init
</code></pre>
<p> (this generates all configuration files in config folder of your project)</p>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-4'>
<td class=docs>
<div class="octowrap">
<a class="octothorpe" href="#section-4">#</a>
</div>
<p> FileUtils are used to initialize <em>her</em> in the project directory</p>
</td>
<td class=code>
<div class='highlight'><pre><span class="sx">%w{fileutils}</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">lib</span><span class="o">|</span> <span class="nb">require</span> <span class="n">lib</span> <span class="p">}</span>
<span class="n">lib</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">expand_path</span><span class="p">(</span><span class="s1">'../lib'</span><span class="p">,</span> <span class="no">File</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">))</span>
<span class="vg">$:</span><span class="o">.</span><span class="n">unshift</span> <span class="n">lib</span> <span class="k">unless</span> <span class="vg">$:</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">lib</span><span class="p">)</span>
<span class="nb">require</span> <span class="s1">'her'</span></pre></div>
</td>
</tr>
<tr id='section-5'>
<td class=docs>
<div class="octowrap">
<a class="octothorpe" href="#section-5">#</a>
</div>
<p> Tty class is inspired by Homebrew Tty class. It is used for almost all output.</p>
<p> <code>onoe</code> prints an error if one arose.
<code>okay</code> tries to execute a block</p>
</td>
<td class=code>
<div class='highlight'><pre><span class="k">class</span> <span class="nc">Tty</span>
<span class="k">class</span> <span class="o"><<</span> <span class="nb">self</span>
<span class="k">def</span> <span class="nf">onoe</span> <span class="n">error</span>
<span class="n">lines</span> <span class="o">=</span> <span class="n">error</span><span class="o">.</span><span class="n">to_s</span><span class="o">.</span><span class="n">split</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span>
<span class="nb">puts</span> <span class="s2">"</span><span class="si">#{</span><span class="n">red</span><span class="si">}</span><span class="s2">Error</span><span class="si">#{</span><span class="n">reset</span><span class="si">}</span><span class="s2">: </span><span class="si">#{</span><span class="n">lines</span><span class="o">.</span><span class="n">shift</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">puts</span> <span class="o">*</span><span class="n">lines</span> <span class="k">unless</span> <span class="n">lines</span><span class="o">.</span><span class="n">empty?</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">okay</span> <span class="n">title</span><span class="p">,</span> <span class="o">&</span><span class="nb">proc</span>
<span class="nb">puts</span> <span class="s2">"</span><span class="si">#{</span><span class="n">blue</span><span class="si">}</span><span class="s2">==></span><span class="si">#{</span><span class="n">white</span><span class="si">}</span><span class="s2"> </span><span class="si">#{</span><span class="n">title</span><span class="si">}#{</span><span class="n">reset</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">proc</span><span class="o">.</span><span class="n">call</span> <span class="k">if</span> <span class="nb">proc</span>
<span class="k">rescue</span> <span class="no">Exception</span> <span class="o">=></span> <span class="n">error</span>
<span class="nb">print</span> <span class="s2">"</span><span class="si">#{</span><span class="n">red</span><span class="si">}</span><span class="s2">Error</span><span class="si">#{</span><span class="n">reset</span><span class="si">}</span><span class="s2">: "</span>
<span class="k">raise</span> <span class="n">error</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">blue</span><span class="p">;</span> <span class="n">bold</span> <span class="mi">34</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">white</span><span class="p">;</span> <span class="n">bold</span> <span class="mi">39</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">red</span><span class="p">;</span> <span class="n">underline</span> <span class="mi">31</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">yellow</span><span class="p">;</span> <span class="n">escape</span> <span class="mi">33</span> <span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">green</span><span class="p">;</span> <span class="n">escape</span> <span class="mi">32</span> <span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">reset</span><span class="p">;</span> <span class="n">escape</span> <span class="mi">0</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">em</span><span class="p">;</span> <span class="n">underline</span> <span class="mi">39</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">color</span> <span class="n">n</span><span class="p">;</span> <span class="n">escape</span> <span class="s2">"0;</span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">"</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">bold</span> <span class="n">n</span><span class="p">;</span> <span class="n">escape</span> <span class="s2">"1;</span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">"</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">underline</span> <span class="n">n</span><span class="p">;</span> <span class="n">escape</span> <span class="s2">"4;</span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">"</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">escape</span> <span class="n">n</span><span class="p">;</span> <span class="s2">"</span><span class="se">\033</span><span class="s2">[</span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">m"</span> <span class="k">if</span> <span class="vg">$stdout</span><span class="o">.</span><span class="n">tty?</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">Her</span>
<span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{})</span>
<span class="nb">puts</span> <span class="s2">"$ </span><span class="si">#{</span><span class="n">q</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">system</span> <span class="n">q</span> <span class="ow">or</span> <span class="k">raise</span> <span class="s2">"Error executing: </span><span class="si">#{</span><span class="n">q</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">run?</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{});</span> <span class="n">run</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span> <span class="k">rescue</span> <span class="kp">false</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">load_configurations!</span></pre></div>
</td>
</tr>
<tr id='section-6'>
<td class=docs>
<div class="octowrap">
<a class="octothorpe" href="#section-6">#</a>
</div>
<p> Defaults</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">deploy_base</span> <span class="s2">"/var/www"</span>
<span class="n">rails</span> <span class="kp">true</span>
<span class="n">is_first_deploy</span> <span class="kp">false</span>
<span class="n">env</span> <span class="s2">"production"</span>
<span class="nb">load</span> <span class="s2">"./config/her.rb"</span>
<span class="k">raise</span> <span class="s2">"Must include 'app_name'"</span> <span class="k">unless</span> <span class="n">app_name</span>
<span class="k">raise</span> <span class="s2">"Must include 'rev'"</span> <span class="k">unless</span> <span class="n">rev</span>
<span class="n">app_root</span> <span class="s2">"</span><span class="si">#{</span><span class="n">deploy_base</span><span class="si">}</span><span class="s2">/</span><span class="si">#{</span><span class="n">app_name</span><span class="si">}</span><span class="s2">"</span>
<span class="n">rev_root</span> <span class="s2">"</span><span class="si">#{</span><span class="n">app_root</span><span class="si">}</span><span class="s2">/release/</span><span class="si">#{</span><span class="n">rev</span><span class="si">}</span><span class="s2">"</span>
<span class="n">shared_root</span> <span class="s2">"</span><span class="si">#{</span><span class="n">app_root</span><span class="si">}</span><span class="s2">/shared"</span>
<span class="n">unicorn</span><span class="p">(</span><span class="n">unicorn</span> <span class="p">?</span> <span class="n">unicorn</span> <span class="p">:</span> <span class="n">rails</span> <span class="p">?</span> <span class="s2">"unicorn_rails"</span> <span class="p">:</span> <span class="s2">"unicorn"</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">exec!</span> <span class="n">args</span>
<span class="k">case</span> <span class="n">args</span><span class="o">.</span><span class="n">shift</span>
<span class="k">when</span> <span class="s2">"help"</span><span class="p">,</span><span class="s2">"h"</span><span class="p">,</span><span class="kp">nil</span><span class="p">;</span> <span class="nb">puts</span> <span class="n">docs</span><span class="o">[</span><span class="ss">:help</span><span class="o">].</span><span class="n">chomp</span>
<span class="k">when</span> <span class="s2">"version"</span><span class="p">,</span> <span class="s2">"v"</span><span class="p">;</span> <span class="nb">puts</span> <span class="no">Her</span><span class="o">::</span><span class="no">VERSION</span>
<span class="k">when</span> <span class="s2">"init"</span><span class="p">,</span><span class="s2">"i"</span>
<span class="no">FileUtils</span><span class="o">.</span><span class="n">mkdir_p</span><span class="p">(</span><span class="s2">"config"</span><span class="p">)</span>
<span class="o">[</span><span class="s2">"unicorn.rb"</span><span class="p">,</span> <span class="s2">"her.rb"</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="o">|</span>
<span class="k">if</span> <span class="no">File</span><span class="o">.</span><span class="n">exists?</span><span class="p">(</span><span class="s2">"config/</span><span class="si">#{</span><span class="n">file</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"</span><span class="se">\t</span><span class="si">#{</span><span class="no">Tty</span><span class="o">.</span><span class="n">yellow</span><span class="si">}</span><span class="s2">exists</span><span class="si">#{</span><span class="no">Tty</span><span class="o">.</span><span class="n">reset</span><span class="si">}</span><span class="s2"> config/</span><span class="si">#{</span><span class="n">file</span><span class="si">}</span><span class="s2">"</span>
<span class="k">else</span>
<span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"config/</span><span class="si">#{</span><span class="n">file</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span><span class="s2">"w"</span><span class="p">){</span><span class="o">|</span><span class="n">f</span><span class="o">|</span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">docs</span><span class="o">[</span><span class="n">file</span><span class="o">.</span><span class="n">to_sym</span><span class="o">].</span><span class="n">chomp</span><span class="p">)}</span>
<span class="nb">puts</span> <span class="s2">"</span><span class="se">\t</span><span class="si">#{</span><span class="no">Tty</span><span class="o">.</span><span class="n">green</span><span class="si">}</span><span class="s2">create</span><span class="si">#{</span><span class="no">Tty</span><span class="o">.</span><span class="n">reset</span><span class="si">}</span><span class="s2"> config/</span><span class="si">#{</span><span class="n">file</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="k">end</span></pre></div>
</td>
</tr>
<tr id='section-7'>
<td class=docs>
<div class="octowrap">
<a class="octothorpe" href="#section-7">#</a>
</div>
<p> This command is run on server</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">when</span> <span class="s2">"deploy"</span><span class="p">,</span><span class="s2">"d"</span>
<span class="no">Tty</span><span class="o">.</span><span class="n">okay</span> <span class="s2">"Reading configuration file"</span> <span class="k">do</span>
<span class="n">rev</span> <span class="n">args</span><span class="o">.</span><span class="n">shift</span>
<span class="n">load_configurations!</span>
<span class="k">end</span>
<span class="no">Tty</span><span class="o">.</span><span class="n">okay</span> <span class="s2">"Symlinking shared files"</span> <span class="k">do</span>
<span class="n">run</span> <span class="s2">"ln -s </span><span class="si">#{</span><span class="n">shared_root</span><span class="si">}</span><span class="s2">/tmp </span><span class="si">#{</span><span class="n">rev_root</span><span class="si">}</span><span class="s2">/tmp"</span>
<span class="n">run</span> <span class="s2">"ln -s </span><span class="si">#{</span><span class="n">shared_root</span><span class="si">}</span><span class="s2">/config/database.yml </span><span class="si">#{</span><span class="n">rev_root</span><span class="si">}</span><span class="s2">/config/database.yml"</span>
<span class="n">run</span> <span class="s2">"ln -s </span><span class="si">#{</span><span class="n">shared_root</span><span class="si">}</span><span class="s2">/vendor/bundle </span><span class="si">#{</span><span class="n">rev_root</span><span class="si">}</span><span class="s2">/vendor/bundle"</span>
<span class="n">run</span> <span class="s2">"ln -s </span><span class="si">#{</span><span class="n">shared_root</span><span class="si">}</span><span class="s2">/log </span><span class="si">#{</span><span class="n">rev_root</span><span class="si">}</span><span class="s2">/log"</span>
<span class="k">end</span>
<span class="no">Tty</span><span class="o">.</span><span class="n">okay</span> <span class="s2">"Running on_first_deploy callback"</span><span class="p">,</span> <span class="o">&</span><span class="n">on_first_deploy</span> <span class="k">if</span> <span class="n">is_first_deploy</span>
<span class="no">Tty</span><span class="o">.</span><span class="n">okay</span> <span class="s2">"Running on_deploy callback"</span><span class="p">,</span> <span class="o">&</span><span class="n">on_deploy</span>
<span class="no">Tty</span><span class="o">.</span><span class="n">okay</span> <span class="s2">"Remapping current"</span> <span class="k">do</span>
<span class="n">run</span> <span class="s2">"ln -nsf </span><span class="si">#{</span><span class="n">rev_root</span><span class="si">}</span><span class="s2"> </span><span class="si">#{</span><span class="n">app_root</span><span class="si">}</span><span class="s2">/current"</span>
<span class="k">end</span>
<span class="no">Tty</span><span class="o">.</span><span class="n">okay</span> <span class="s2">"Reloading unicorn"</span> <span class="k">do</span>
<span class="n">run</span> <span class="s2">"cat </span><span class="si">#{</span><span class="n">shared_root</span><span class="si">}</span><span class="s2">/tmp/pids/unicorn.pid | xargs kill -SIGUSR2"</span>
<span class="k">end</span> <span class="k">unless</span> <span class="n">is_first_deploy</span>
<span class="k">else</span> <span class="no">Tty</span><span class="o">.</span><span class="n">onoe</span><span class="p">(</span><span class="s2">"Unknown command. Run 'her help' for usage."</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="k">def</span> <span class="nf">load_inline_files!</span>
<span class="n">file</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span> <span class="n">docs</span><span class="p">(</span><span class="no">Hash</span><span class="o">.</span><span class="n">new</span><span class="p">)</span>
<span class="n">io</span> <span class="o">=</span> <span class="o">::</span><span class="no">IO</span><span class="o">.</span><span class="n">respond_to?</span><span class="p">(</span><span class="ss">:binread</span><span class="p">)</span> <span class="p">?</span> <span class="o">::</span><span class="no">IO</span><span class="o">.</span><span class="n">binread</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">)</span> <span class="p">:</span> <span class="o">::</span><span class="no">IO</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">)</span>
<span class="n">io</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="s2">"</span><span class="se">\r\n</span><span class="s2">"</span><span class="p">,</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sr">/^__END__$/</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="o">[</span><span class="mi">1</span><span class="o">].</span><span class="n">each_line</span> <span class="k">do</span> <span class="o">|</span><span class="n">ln</span><span class="o">|</span>
<span class="k">if</span> <span class="n">ln</span> <span class="o">=~</span> <span class="sr">/^@@\s*(.*\S)\s*$/</span>
<span class="n">docs</span><span class="o">[</span><span class="vg">$1</span><span class="o">.</span><span class="n">to_sym</span><span class="o">]</span> <span class="o">=</span> <span class="n">file</span> <span class="o">=</span> <span class="s2">""</span>
<span class="k">else</span>
<span class="n">file</span> <span class="o"><<</span> <span class="n">ln</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">enable_options</span><span class="p">(</span><span class="o">*</span><span class="n">opts</span><span class="p">)</span>
<span class="n">opts</span><span class="o">.</span><span class="n">each</span><span class="p">{</span><span class="o">|</span><span class="n">op</span><span class="o">|</span> <span class="nb">eval</span><span class="s2">"def </span><span class="si">#{</span><span class="n">op</span><span class="si">}</span><span class="s2">(v = @</span><span class="si">#{</span><span class="n">op</span><span class="si">}</span><span class="s2">, &b); @</span><span class="si">#{</span><span class="n">op</span><span class="si">}</span><span class="s2"> = b || v; end"</span><span class="p">}</span>
<span class="k">end</span>
<span class="n">enable_options</span> <span class="ss">:app_url</span><span class="p">,</span> <span class="ss">:app_name</span><span class="p">,</span>
<span class="ss">:deploy_base</span><span class="p">,</span>
<span class="ss">:on_deploy</span><span class="p">,</span> <span class="ss">:on_first_deploy</span><span class="p">,</span> <span class="ss">:is_first_deploy</span><span class="p">,</span>
<span class="ss">:rails</span><span class="p">,</span> <span class="ss">:env</span><span class="p">,</span>
<span class="ss">:docs</span><span class="p">,</span>
<span class="ss">:unicorn</span><span class="p">,</span>
<span class="ss">:app_root</span><span class="p">,</span> <span class="ss">:shared_root</span><span class="p">,</span> <span class="ss">:rev_root</span><span class="p">,</span> <span class="ss">:rev</span>
<span class="k">end</span>
<span class="k">if</span> <span class="no">File</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">)</span> <span class="o">==</span> <span class="no">File</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="vg">$0</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="no">ARGV</span><span class="o">.</span><span class="n">dup</span>
<span class="no">ARGV</span><span class="o">.</span><span class="n">clear</span>
<span class="kp">include</span> <span class="no">Her</span>
<span class="n">load_inline_files!</span>
<span class="nb">exec</span><span class="o">!</span> <span class="n">args</span>
<span class="k">end</span>
<span class="cp">__END__</span>
<span class="cp">@@ help</span>
<span class="cp">Usage: her <command> [-v] ...</span>
<span class="cp">init # Generate configuration files (run once)</span>
<span class="cp">deploy rev_id # Script run on server (DO NOT run yourself)</span>
<span class="cp">== Others ==</span>
<span class="cp">help # Show this screen</span>
<span class="cp">version # Show version number</span>
<span class="cp">@@ her.rb</span>
<span class="cp">app_url "myapp.company.com"</span>
<span class="cp">app_name "myapp"</span>
<span class="cp">on_deploy do</span>
<span class="cp"> run "bundle install --deployment --without development test"</span>
<span class="cp"> run "RAILS_ENV=#{env} rake db:migrate"</span>
<span class="cp">end</span>
<span class="cp">on_first_deploy do</span>
<span class="cp"> run "RAILS_ENV=#{env} rake db:seed"</span>
<span class="cp">end</span></pre></div>
</td>
</tr>
</table>
</div>
</body>