summaryrefslogtreecommitdiffstats
path: root/genome-bootstrap/lib/genome-bootstrap/core.rb
blob: f569562537001b670192f61ed46544f0a0e6aadf (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
# 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.

require 'rubygems'
require 'erb'
require 'yaml'
require 'restr'
require 'xmlrpc/client'

module GenomeBootstrap
  class GenomeRepo
    attr_reader :machines, :machine_types_url, :cobbler_profiles, :fqdn, :repo

    def initialize(repo) 
      @repo = repo 
      @fqdn = @repo
      @genomed = "http://#{@fqdn}/genome"
      @machine_types_url = @genomed + "/machine_types.html"

      @cobblerd = XMLRPC::Client.new2("http://#{@fqdn}/cobbler_api_rw")           

      # TODO: figure out a way to pass these values in
      @token = @cobblerd.call2("login", "cobbler", "password")[1]
      @cobbler_profiles = @cobblerd.call2("get_profiles", @token)[1].map {|p| p["name"]}
    end

    def machines
      @machines ||= fetch_data
    end

    def facts_for(type)
      # Don't want to pull in the genome lib.  See below fore more detail.
      facts = restr_get("#{@genomed}/machine_types/#{type}.xml", "fact")
      return (!facts or facts.empty?) ? {} : facts.map do |f|
        def f.name
          self["name"]
        end

        def f.desc
          self["description"]  
        end

        # Here's where we make use of the DSL's simple templating
        f.instance_variable_set(:@repo, @repo)
        def f.default
          # handle there case where there is no default
          self["default"].gsub("%repo%", @repo) rescue ""
        end
        
        f
      end
    end

    def bootstrapping_cobbler_profiles
      @cobbler_profiles.delete_if{|profile|
        profile.include?("__Appliance")
      }
    end

    def classes_for(type)
      restr_get("#{@genomed}/machine_types/#{type}.xml", "class")
    end

    def fetch_data
      # Build out some mock objects so we don't have to couple to the genome
      # lib.  That lib has lots of funky metaprogramming stuff in it and it
      # might need to be changed in the future.  The fewer tools relying on it
      # the better.
      machine_types = restr_get("#{@genomed}/machine_types.xml", "machine_type")

      return machine_types.map do |m|
        def m.name
          self["name"]
        end

        def m.desc
          self["description"]  
        end
         
        m
      end
    end
 
    def post_yaml(node_name, machine_type, yaml)
      Restr.post("#{@genomed}/nodes", :node_name => node_name, :yaml => yaml)
    end

    def add_system_to_cobbler(name, params, email)
      system_id = @cobblerd.call2("new_system", @token)[1]
      @cobblerd.call2('modify_system', system_id, 'name', name, @token)
      @cobblerd.call2('modify_system', system_id, 'profile', params["cobbler_profile"], @token)
   
      # These values ultimately get used by genome-firstboot
      ksmeta = "fqdn=#{name} " +
               "genome_repo=#{@fqdn}"

      ksmeta << " email=#{email}" if email

      @cobblerd.call2('modify_system', system_id, 'ksmeta', ksmeta, @token)
      @cobblerd.call2('save_system', system_id, @token)
    end

    def register_machine(machine_fqdn, config, email="")
      machine_type = config["parameters"]["genome_machine_type"]
      post_yaml(machine_fqdn, machine_type, YAML.dump(config))
      add_system_to_cobbler(machine_fqdn, config["parameters"], email)
    end

    def get_system_ip(system_name, max_tries=1)
      status = 1.upto(max_tries) do
        # Technically the yield should be after the remote call, but it would
        # trip up the 'find'
        yield if block_given?

        data = @cobblerd.call2("get_status")[1].find do |ip, s|
          s[2] == "system:%s" % system_name && s[5] =~ /^installing/
        end

        # This returns from the block
        break data unless data.nil?
      end

      return status.nil? ? nil : status[0]
    end

    # This is a workaround for a Restr "feature".  Restr does not set
    # 'forcearray => true' for it's XmlSimple instance.  This means exactly
    # what you would think.  We always want to work with arrays since puppet's
    # external nodes cares what data structures are used.
    def restr_get(url, xml_node=nil)
      data = xml_node ? Restr.get(url)[xml_node] : Restr.get(url)

      return case data
      when nil, Array
        data
      else
        Array.new << data
      end
    end
  end

  class CloudController
    attr_reader :repo, :cloud

    # Create a new CloudController with the given server name (prefix or fqdn)
    # and repo (full GenomeRepo object)
    def initialize(name, repo) 
      @repo = repo

      # If @repo is not an GenomeRepo object, assume it's the
      # fqdn of a repo
      if not @repo.is_a? GenomeRepo then
        @repo = GenomeRepo.new(@repo)
      end

      @cloud = "http://#{name}/cloud"
    end

    def create_machine(system_name, email)
      return Restr.post("#{@cloud}/koan", :system_name => system_name, :repo => @repo.fqdn, :email => email).split(":")
    end

    def update_hostname(id, hostname)
      Restr.post("#{@cloud}/koan", :machine_id => id, :hostname => hostname)
    end
  end
end