public
Description: Airtruk Enterprise Configuration Manager
Homepage:
Clone URL: git://github.com/penguincoder/airtruk.git
airtruk / app.rb
100755 251 lines (217 sloc) 8.129 kb
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
#!/usr/bin/env ruby
#
# Airtruk -- It saved everybody after the ThunderDome
#
# Used to feed configuration to a set of hosts on a network.
#
 
%w{ rubygems sinatra sqlite3 active_record haml sass }.each do |lib|
  require lib
end
 
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
 
configure do
  %w{ active_record_connect machine shell_script }.each do |lib|
    require lib
  end
end
 
configure :development do
  set :logging, true
  ActiveRecord::Base.logger = Logger.new($stderr)
end
 
get '/' do
  @machines = Machine.find :all, :order => 'hostname ASC', :include => :shell_scripts
  @shell_scripts = ShellScript.alphabetical
  haml :index
end
 
get '/machines/new' do
  @machine = Machine.new
  haml :machine_edit
end
 
get '/machines/:id' do
  @machine = Machine.find(params[:id])
  haml :machine_edit
end
 
post '/machines' do
  @machine = Machine.new(params[:machine])
  if @machine.save
    redirect '/'
  else
    haml :machine_edit
  end
end
 
put '/machines/:id' do
  @machine = Machine.find(params[:id])
  if @machine.update_attributes params[:machine]
    redirect '/'
  else
    haml :machine_edit
  end
end
 
delete '/machines/:id' do
  @machine = Machine.find(params[:id])
  @machine.destroy
  redirect '/'
end
 
get '/shell_scripts/new' do
  @shell_script = ShellScript.new
  haml :shell_script_edit
end
 
get '/shell_scripts/:id' do
  @shell_script = ShellScript.find(params[:id])
  haml :shell_script_edit
end
 
post '/shell_scripts' do
  @shell_script = ShellScript.new(params[:shell_script])
  if @shell_script.save
    redirect '/'
  else
    haml :shell_script_edit
  end
end
 
put '/shell_scripts/:id' do
  @shell_script = ShellScript.find(params[:id])
  if @shell_script.update_attributes(params[:shell_script])
    redirect '/'
  else
    haml :shell_script_edit
  end
end
 
delete '/shell_scripts/:id' do
  @shell_script = ShellScript.find(params[:id])
  @shell_script.destroy
  redirect '/'
end
 
get '/autoscript/:hostname' do
  @machine = Machine.find_by_hostname(params[:hostname], :include => :shell_scripts)
  content_type 'text/plain'
  attachment 'autoscript.sh'
  body @machine.autoscript
end
 
get '/add_shell_script/:machine_id/:shell_script_id' do
  machine = Machine.find(params[:machine_id])
  shell_script = ShellScript.find(params[:shell_script_id])
  machine.shell_scripts << shell_script
  redirect '/'
end
 
get '/remove_shell_script/:machine_id/:shell_script_id' do
  machine = Machine.find(params[:machine_id])
  shell_script = ShellScript.find(params[:shell_script_id])
  machine.shell_scripts.delete(shell_script)
  redirect '/'
end
 
use_in_file_templates!
 
__END__
 
@@ layout
%html
%head
%title AirTruk Configuration
%style{ :type => 'text/css' }
:sass
a
img
border: none
td
text-align: center
%body
= yield
@@ index
 
%h1#title== Now serving #{Machine.count} machines with #{ShellScript.count} shell scripts!
 
%p#navigation
%a{ :href => '/machines/new', :title => 'New Machine' } New Machine
|
%a{ :href => '/shell_scripts/new', :title => 'New Shell Script' } New Shell Script
 
%table#configuration
%tr
%th &nbsp;
%th &nbsp;
- @shell_scripts.each do |shell_script|
%th
%a{ :href => "/shell_scripts/#{shell_script.id}", :title => "Edit shell script" }= shell_script.configuration_name
- @machines.each do |machine|
%tr
%td
%a{ :href => "/autoscript/#{machine.hostname}", :title => "Configuration for #{machine.hostname}" }= machine.hostname
%td
%a{ :href => "/machines/#{machine.id}", :title => "Update machine settings" } (edit)
- @shell_scripts.each do |shell_script|
%td
- if machine.shell_scripts.include?(shell_script)
%a{ :href => "/remove_shell_script/#{machine.id}/#{shell_script.id}", :title => "Remove #{shell_script.configuration_name} from #{machine.hostname}", :rel => "nofollow" }
%img{ :src => '/list-remove.png' }
- else
%a{ :href => "/add_shell_script/#{machine.id}/#{shell_script.id}", :title => "Add #{shell_script.configuration_name} to #{machine.hostname}", :rel => "nofollow" }
%img{ :src => '/list-add.png' }
 
%p#footer
Learn more about the
%a{ :href => 'http://en.wikipedia.org/wiki/Transavia_PL-12_Airtruk', :title => 'Transavia PL-12 Airtruk', :onclick => 'window.open(this.href);return false;' } Transavia PL-12 Airtruk
aircraft used in
%a{ :href => 'http://en.wikipedia.org/wiki/Mad_Max_Beyond_Thunderdome', :title => 'Mad Max Beyond Thunderdome', :onclick => 'window.open(this.href);return false;' } Mad Max Beyond Thunderdome.
%br
&copy; 2009
%a{ :href => 'http://github.com/penguincoder', :onclick => 'window.open(this.href);return false;' } Andrew Coleman
except the Airtruk.
 
@@ machine_edit
%p
%a{ :href => '/', :title => 'Configurator' } Configurator
- unless @machine.new_record?
|
%a{ :href => "/machines/#{@machine.id}", :title => "Destroy machine", :onclick => "if(confirm('Are you sure?')){var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;}" } Destroy Machine
 
 
- if @machine.new_record?
%form{ :action => '/machines', :method => 'post' }
= haml :machine_form
%input{ :type => 'submit', :value => 'Create' }
- else
%form{ :action => "/machines/#{@machine.id}", :method => 'post' }
%input{ :type => 'hidden', :name => '_method', :value => 'put' }
= haml :machine_form
%input{ :type => 'submit', :value => 'Update' }
 
@@ machine_form
%fieldset
%p
%label
Hostname
%input{ :type => 'text', :name => 'machine[hostname]', :size => 50, :maxsize => 255, :value => @machine.hostname }
 
@@ shell_script_edit
%p
%a{ :href => '/', :title => 'Configurator' } Configurator
- unless @shell_script.new_record?
|
%a{ :href => "/shell_scripts/#{@shell_script.id}", :title => "Destroy shell script", :onclick => "if(confirm('Are you sure?')){var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;}" } Destroy Shell Script
 
- if @shell_script.new_record?
%form{ :action => '/shell_scripts', :method => 'post' }
= haml :shell_script_form
%input{ :type => 'submit', :value => 'Create' }
- else
%form{ :action => "/shell_scripts/#{@shell_script.id}", :method => 'post' }
%input{ :type => 'hidden', :name => '_method', :value => 'put' }
= haml :shell_script_form
%input{ :type => 'submit', :value => 'Update' }
 
@@ shell_script_form
%fieldset
%p
%label
Configuration Name
%input{ :type => 'text', :name => 'shell_script[configuration_name]', :size => 50, :maxsize => 255, :value => @shell_script.configuration_name }
%br
%small Required. Must be unique.
%p
%label
Filename
%input{ :type => 'text', :name => 'shell_script[filename]', :size => 50, :maxsize => 255, :value => @shell_script.filename }
%br
%small Leave the filename blank to execute the contents of the script.
%p
%label
Owner/Group
%input{ :type => 'text', :name => 'shell_script[owner]', :size => 50, :maxsize => 255, :value => @shell_script.owner }
%br
%small Passed directly to <code>chown</code>, you can also specify a group.
%p
%label
Mode
%input{ :type => 'text', :name => 'shell_script[mode]', :size => 50, :maxsize => 255, :value => @shell_script.mode }
%br
%small Passed directly to <code>chmod</code>.
%p
%label
Contents
%textarea{ :name => 'shell_script[contents]', :rows => 10, :cols => 40, :style => "font-family: Lucida Console, Courier New, Courier, Monospace;" }= @shell_script.contents