summaryrefslogtreecommitdiffstats
path: root/genomed/lib/genomed.rb
blob: 3a1705e8e001503d2f1eae17a7a787c177a0f0a6 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# Copyright (C) 2008 Red Hat, Inc

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# a long with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

$: << File.dirname(File.expand_path(__FILE__))

gem 'reststop', '~> 0.2'

require 'yaml'
require 'fileutils'
require 'rubygems'
require 'picnic'
require 'reststop'
require 'genome-dsl'
require 'restr'
require 'cgi'
require 'xmlrpc/client'
require 'pp'

CONTEXT="/genome"

Camping.goes :Genomed
Genomed.picnic!

module Genomed::Controllers
  class Systems < REST 'systems'
    # OMG, this is ugly
    def _get_cobbler_installed_systems
      systems = {}
      `cat /var/log/cobbler/install.log | cut -f2-3`.each do |line| 
        name, ip = line.split("\t") 
        ip.chomp!
        name.chomp!
        hostname = _get_host_name(ip).chomp 
        puts "name: #{name}, ip: #{ip}, hostname: #{hostname}"

        systems[name] = {"ip" => ip, "hostname" => hostname}
      end
      return systems
    end

    def _get_host_name(ip)
      lookup = `nslookup #{ip} | grep name | cut -f2`
      unless lookup.empty?
        return lookup.split("=")[1].strip.split(".").join(".")
      end
    end

    def read(name)
      name = CGI::unescape(name)
      @systems = _get_cobbler_installed_systems
      @system = {}
      if @systems.has_key?(name)
        @system = {"name" => name, "ip" => @systems[name]["ip"], "hostname" => @systems[name]["hostname"]}
      end
      render :view_system
    end

    def list
      @systems = _get_cobbler_installed_systems
      pp @systems
      render :list_systems
    end
  end

  class MachineTypes < REST 'machine_types'
    include GenomeDsl

    # GET /types/foo
    # GET /types/foo.xml
    def read(type)
      @machine = machines.find {|m| m.name == type}
      render :view_type
    end

    # GET /types
    # GET /types.xml
    def list
      @machines = machines
      render :list_types
    end
  end

  class Nodes < REST 'nodes'
    # GET /nodes/foo
    # GET /nodes/foo.xml
    def read(name)
      @name = name
      begin
        @info = YAML.load(File.read("/etc/genome/nodes/#{@name}/node.yaml")) 
        render :view_node
      rescue
        # node doesn't exist yet
        render :add
      end
    end

    # GET /nodes
    # GET /nodes.xml
    def list
      @nodes = Dir["/etc/genome/nodes/*"].map { |m| File.basename(m) }
      render :list_nodes
    end

    def create
      if input.wizard
        puts "You're using the wizard!"   
        puts input.inspect
      elsif input.yaml
        node_dir = "/etc/genome/nodes/#{input.node_name}"
        persist_node(input.node_name, input.yaml)
      elsif input.xml
        xml = XmlSimple.xml_in(input.xml)

        params = Hash.new
        xml["parameters"].each do |p| 
          p.each_key do |k|
            params[k] = p[k].to_s #The value is an array. Yay.
          end
        end

        persist_node(xml["name"].to_s, YAML.dump({"classes" => xml["class"], "parameters" => params}))
      end

      redirect R(Nodes) if @format == :HTML 
    end

    def persist_node(node, yaml)
      node_dir = "/etc/genome/nodes/#{node}"
      FileUtils.mkdir_p(node_dir) unless File.exist?(node_dir)
      File.open(node_dir + "/node.yaml", "w") {|f| f.puts yaml}
    end
  end

  class Wizard < REST 'wizard'
    include GenomeDsl

    # GET /wizard/foo
    # GET /wizard/foo.xml
    def read(type)
      @machine = machines.find {|m| m.name == type}
      render :view_type
    end

    # GET /types
    # GET /types.xml
    def list
      @machines = machines
      render :list_types
    end

    def create
      @machine_type = input.machine_type

      redirect CONTEXT + R(Nodes)
    end
  end
end

module Genomed::Views
  def initialize(*args)
    super(*args)
    @x = Builder::XmlMarkup.new(:indent => 1)
  end

  module HTML
    def layout
      html do
        head do
          title 'Genome'
          link :rel => 'stylesheet', :type => 'text/css', :href => '/styles.css', :media => 'screen'
        end
        body do
          div.header! do
            div.headerLogo! {}
          end
          div.content! do
            h1 do
              [a("Machine Types", :href => CONTEXT + R(Genomed::Controllers::MachineTypes)), 
               a("Nodes", :href => CONTEXT + R(Genomed::Controllers::Nodes)),
               a("Systems", :href => CONTEXT + R(Genomed::Controllers::Systems))].join " | "
            end
            self << yield
          end
        end
      end
    end
 
    def add
      form(:method => 'post', :action => CONTEXT + R(Genomed::Controllers::Nodes)) do
        h2 "YAML"
 
        textarea :name => 'yaml', :rows => '25', :cols => '100'; br

        input :name => 'node_name', :value => @name
        input :type => 'submit', :value => "Submit"
      end
    end

    def view_type
      p do
        h1 @machine.name
        p @machine.desc

        form(:method => 'post', :action => CONTEXT + R(Genomed::Controllers::Nodes)) do
          h2 "Fully qualified domain name"
          input :name => "fqdn", :type => 'text', :size  => 45,
                :value => "[prefix]-#{@machine.name}.usersys.redhat.com"

          h2 "Classes"
          @machine.classes.each_with_index do |c, i|
            input :name => "class#{i}", :type => 'text', 
                  :value => c; br
          end
  
          h2 "Parameters"
          fieldset do
            @machine.facts.each do |f|
              a(f.name, :href => "fix me", :title => f.desc); br
              input :name => "param_" + f.name, :type => 'text', :size => 45, 
                    :value => f.default; br
            end
          end

          input :type => 'hidden', :name => 'node_name', :value => @name
          input :type => 'hidden', :name => 'wizard', :value => true
          input :type => 'submit', :value => "Submit"
        end
      end
    end 

    def view_node
      h1 @name
    
      p "This form allows you to submit the YAML to be used by the Puppetmaster to " +
        "determine the configuration to be compiled for a particular host."
      form(:method => 'post', :action => CONTEXT + R(Genomed::Controllers::Nodes)) do
 
        textarea ::YAML.dump(@info), :name => 'yaml', :rows => '25', :cols => '100'; br

        input :type => 'hidden', :name => 'node_name', :value => @name
        input :type => 'submit', :value => "Submit"
      end
    end

    def view_system
      h1 @system["name"]

      p do
        "IP Address: #{@system["ip"]} "
      end
      p do 
        "Hostname: #{@system["hostname"]}"
      end
    end

    def list_nodes
      h1 "Nodes:"
      p "To create a new node just browse to the page corresponding to the hostname.  " +
        "eg, #{CONTEXT + R(Genomed::Controllers::Nodes, "[hostname]")}."
      p do
        ol do
          @nodes.each do |n| 
            # BUG: default format is not getting preserved
            @format = :HTML
            li {a(n, :href => CONTEXT + R(Genomed::Controllers::Nodes, n))}
          end
        end
      end
    end

    def list_types
      p do
        p "Click the links to create a new instance of a particular machine type."
        ol do
          @machines.each do |m|
            li do 
              #h1 {a(m.name, :href => R(Genomed::Controllers::MachineTypes, m.name))}
              h1 m.name
              p(m.desc)
            end
          end
        end
      end
    end

    def list_systems
      host = ENV["HOSTNAME"]
      cobbler_web = "http://#{host}/cobbler/web"
      h1 "Systems:"
      h4 do
        text("This information is gathered from the /var/log/cobbler/install.log file.\n")
        text("If you don't see what you're looking for, try ")
        a("cobbler web", :href => "#{cobbler_web}/?mode=system_list")
      end
         
      p do
        ol do
          @systems.each_key do |name|
            li do 
              a(name, :href => URL("http://#{host}/#{CONTEXT}/systems/?id=#{name}"))
              text(" (")
              a("edit in cobbler", :href => "#{cobbler_web}/?mode=system_edit&name=#{name}")
              text(")")
              ul do
                li "ip: #{@systems[name]["ip"]}"
                li "hostname: #{@systems[name]["hostname"]}"
              end
            end
          end
        end
      end
    end
  end
  default_format :HTML

  module YAML
    CONTENT_TYPE = 'text/plain'
    
    def layout
      yield
    end

    # This should only be used for Puppet's external nodes
    def view_node
      ::YAML.dump(@info)
    end

    def view_system
      ::YAML.dump(@system)
    end

    def list_systems
      ::YAML.dump(@systems)
    end
  end

  module XML
    def layout
      yield
    end

    def list_types
      @x.instruct!
      @x.machine_types {
        @machines.each do |m|
          @x.machine_type {
            @x.name(m.name)
            @x.description(m.desc)
          }
        end
      }
    end

    def view_type
      @x.instruct!
      @x.machine {
        @x.name(@machine.name)
        @x.description(@machine.desc)
        @machine.classes.each {|c| @x.class(c)}
        @machine.facts.each {|f| 
          @x.fact {
            @x.name(f.name)
            @x.description(f.desc)
            @x.default(f.default)
          }
        }
      }
    end

    def list_nodes
      @x.instruct!
      @x.nodes {
        @nodes.each {|n| @x.node(n)}
      }
    end

    def view_node
      # There no reason we should only support params and classes.
      # It might be nice to simply iterate over all the keys and 
      # display their values.
      @x.instruct!
      @x.node {
        @x.name(@name)
        @info["parameters"].each_pair do |n,v| 
          @x.parameters {
            instance_eval("@x.#{n}(v)")
          }
        end
        @info["classes"].each {|c| @x.class(c)}
      } 
    end

    def list_systems
      @x.instruct!
      @x.systems {
        @systems.each_key {|system| 
          @x.system {
            @x.name(system)
            @x.ip(@systems[system]["ip"])
            @x.hostname(@systems[system]["hostname"])
          }
        }
      }
    end

    def view_system
      @x.instruct!
      @x.system {
        @x.name(@system["name"])
        @x.ip(@system["ip"])
        @x.hostname(@system["hostname"])
      }
    end
  end
end

# Required by picnic
def Genomed.create; end

Genomed.start_picnic