From 47c2f2125ef06bf7d275018707a4567876c5e339 Mon Sep 17 00:00:00 2001 From: Paul Kelly Date: Sun, 3 Oct 2010 10:46:52 +0100 Subject: [PATCH] Switching over to using rails engines --- config/routes.rb | 3 ++ install.rb | 22 +++++++++- lib/app/controllers/subnets_controller.rb | 53 +++++++++++++++-------- lib/app/models/servertype.rb | 7 +++ lib/app/models/vendor.rb | 6 +++ lib/app/views/subnets/_form.html.erb | 37 ++++++++++++++++ lib/app/views/subnets/edit.html.erb | 6 +++ lib/app/views/subnets/index.html.erb | 32 ++++++++++++++ lib/app/views/subnets/new.html.erb | 5 +++ lib/netdb_manager.rb | 5 ++- lib/netdb_manager/host_ext.rb | 18 ++++---- lib/netdb_manager/routing.rb | 12 +++++ 12 files changed, 176 insertions(+), 30 deletions(-) create mode 100644 config/routes.rb mode change 100644 => 100755 install.rb create mode 100644 lib/app/models/servertype.rb create mode 100644 lib/app/models/vendor.rb create mode 100644 lib/app/views/subnets/_form.html.erb create mode 100644 lib/app/views/subnets/edit.html.erb create mode 100644 lib/app/views/subnets/index.html.erb create mode 100644 lib/app/views/subnets/new.html.erb create mode 100644 lib/netdb_manager/routing.rb diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..53491b7 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,3 @@ +ActionController::Routing::Routes.draw do |map| + map.resources :subnets +end diff --git a/install.rb b/install.rb old mode 100644 new mode 100755 index d1a3e94..17b1c2b --- a/install.rb +++ b/install.rb @@ -1,5 +1,23 @@ -Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file| - FileUtils.cp file, File.join(Rails.root, "db", "migrate"), :verbose => true +#!/usr/bin/ruby +require 'fileutils' +root = File.join(File.dirname(__FILE__),"..", "..", "..") +Dir.glob(File.join(File.dirname(__FILE__), "lib", "db", "migrate", "*")).each do |file| + FileUtils.cp file, File.join(root, "db", "migrate"), :verbose => true end +puts "********************************************************************" puts "Please run rake db:migrate from the root of your installation" puts "This will add support for vendors, servertypes and network databases" +puts "********************************************************************" + +routes = File.join(root, "config", "routes") +new_routes = File.join(root, "config", "routes.new") +unless `grep subnets #{routes}` + File.open(routes, "r") do |src| + File.open(new_routes, "w") do |dst| + line = src.readline + dst.write("map.NetdbManagers") if line.match(/Routes.draw/) + dst.write line + end + end + FileUtils.mv src, dst +end \ No newline at end of file diff --git a/lib/app/controllers/subnets_controller.rb b/lib/app/controllers/subnets_controller.rb index 7a213a4..1cefd1a 100644 --- a/lib/app/controllers/subnets_controller.rb +++ b/lib/app/controllers/subnets_controller.rb @@ -1,24 +1,41 @@ class SubnetsController < ApplicationController - layout 'standard' + def index + @search = Subnet.search params[:search] + @subnets = @search.paginate(:page => params[:page]) + end + + def new + @subnet = Subnet.new + end - active_scaffold :subnet do |config| - config.columns = [:domain, :name, :number, :mask, :ranges, :dhcp, :vlanid] - config.create.columns = [:domain, :name, :number, :mask, :ranges, :dhcp, :priority, :vlanid] - config.columns[:domain].label = "Site" - config.columns[:vlanid].label = "VLAN id" - columns[:dhcp].label = "DHCP Server" - columns[:ranges].label = "Address ranges" - config.columns[:ranges].description = "A list of comma separated single IPs or start-end couples." - columns[:mask].label = "Netmask" - config.columns[:domain].form_ui = :select - config.columns[:dhcp].form_ui = :select - list.columns.exclude :created_at, :updated_at - list.sorting = {:domain => 'DESC' } - columns['domain'].sort_by :sql + def create + @subnet = Subnet.new(params[:subnet]) + if @subnet.save + flash[:foreman_notice] = "Successfully created subnet." + redirect_to subnets_url + else + render :action => 'new' + end + end - # Deletes require a page update so as to show error messsages - config.delete.link.inline = false + def edit + @subnet = Subnet.find(params[:id]) + end + + def update + @subnet = Subnet.find(params[:id]) + if @subnet.update_attributes(params[:subnet]) + flash[:foreman_notice] = "Successfully updated subnet." + redirect_to subnets_url + else + render :action => 'edit' + end + end - config.nested.add_link "Hosts", [:hosts] + def destroy + @subnet = Subnet.find(params[:id]) + @subnet.destroy + flash[:foreman_notice] = "Successfully destroyed subnet." + redirect_to subnets_url end end diff --git a/lib/app/models/servertype.rb b/lib/app/models/servertype.rb new file mode 100644 index 0000000..c19bc72 --- /dev/null +++ b/lib/app/models/servertype.rb @@ -0,0 +1,7 @@ +class Servertype < ActiveRecord::Base + has_many :netdbs + + validates_uniqueness_of :name + validates_presence_of :name +end + diff --git a/lib/app/models/vendor.rb b/lib/app/models/vendor.rb new file mode 100644 index 0000000..63fab6f --- /dev/null +++ b/lib/app/models/vendor.rb @@ -0,0 +1,6 @@ +class Vendor < ActiveRecord::Base + has_many :netdbs + + validates_uniqueness_of :name + validates_presence_of :name +end diff --git a/lib/app/views/subnets/_form.html.erb b/lib/app/views/subnets/_form.html.erb new file mode 100644 index 0000000..f311fda --- /dev/null +++ b/lib/app/views/subnets/_form.html.erb @@ -0,0 +1,37 @@ +<% form_for @subnet do |f| %> + <%= f.error_messages %> +

+ <%= f.label :name %>
+ <%= f.text_field :name %> +

+

+ <%= f.label :domain %>
+ <%= f.collection_select :domain_id, Domain.all, :id, :name %> +

+

+ <%= f.label :number %>
+ <%= f.text_field :name %> +

+

+ <%= f.label :mask %>
+ <%= f.text_field :name %> +

+

+ <%= f.label :ranges %>
+ <%= f.text_field :name, :title => ""A list of comma separated single IPs or start-end couples."" %> +

+

+ <%= f.label :priority %>
+ <%= f.text_field :name %> +

+

+ <%= f.label :vlanid %>
+ <%= f.text_field :name %> +

+

+

<%= f.submit "Submit" %>

+ + <% unless @subnet.new_record? -%> + Used by <%= @subnet.hosts.count %> hosts + <% end -%> +<% end %> diff --git a/lib/app/views/subnets/edit.html.erb b/lib/app/views/subnets/edit.html.erb new file mode 100644 index 0000000..fe0afaa --- /dev/null +++ b/lib/app/views/subnets/edit.html.erb @@ -0,0 +1,6 @@ +<% title "Edit Subnet" %> + +<%= render :partial => 'form' %> + +

<%= link_to "View All Subnets", subnets_path %>

+ diff --git a/lib/app/views/subnets/index.html.erb b/lib/app/views/subnets/index.html.erb new file mode 100644 index 0000000..379c378 --- /dev/null +++ b/lib/app/views/subnets/index.html.erb @@ -0,0 +1,32 @@ +<% title "Subnets" %> + + + + + + + + + + + + + <% for subnet in @subnets %> + "> + + + + + + + + + + <% end %> +
NameDomainNumberMaskRangesPriorityVLAN id
<%=link_to h(subnet.name), edit_subnet_path(subnet)%><%=subnet.domain%><%=subnet.number%><%=subnet.mask%><%=subnet.ranges%><%=subnet.priority%><%=subnet.vlanid%> + <%= link_to "Destroy", subnet, :confirm => "Delete #{subnet.name}?", :method => :delete %> +
+<%= page_entries_info @subnets %> +<%= will_paginate @subnets %> + +

<%= link_to "New Subnet", new_subnet_path %>

diff --git a/lib/app/views/subnets/new.html.erb b/lib/app/views/subnets/new.html.erb new file mode 100644 index 0000000..fa1a003 --- /dev/null +++ b/lib/app/views/subnets/new.html.erb @@ -0,0 +1,5 @@ +<% title "New Subnet" %> + +<%= render :partial => 'form' %> + +

<%= link_to "Back to List", subnets_path %>

diff --git a/lib/netdb_manager.rb b/lib/netdb_manager.rb index d6e4df6..88583d5 100644 --- a/lib/netdb_manager.rb +++ b/lib/netdb_manager.rb @@ -1,8 +1,11 @@ require 'netdb_manager/host_ext' +#require "netdb_manager/routing" %w{ models controllers helpers}.each do |dir| path = File.join(File.dirname(__FILE__), 'app', dir) $LOAD_PATH << path ActiveSupport::Dependencies.load_paths << path ActiveSupport::Dependencies.load_once_paths.delete(path) -end \ No newline at end of file +end + +ActionController::Base.prepend_view_path File.join(File.dirname(__FILE__), 'app', 'views') \ No newline at end of file diff --git a/lib/netdb_manager/host_ext.rb b/lib/netdb_manager/host_ext.rb index 25db530..be53da0 100644 --- a/lib/netdb_manager/host_ext.rb +++ b/lib/netdb_manager/host_ext.rb @@ -7,15 +7,15 @@ def self.included(base) require 'ipaddr' include DHCP else - if SETTINGS[:unattended].nil? or SETTINGS[:unattended] - RAILS_DEFAULT_LOGGER.warn "*********************************************************************" - RAILS_DEFAULT_LOGGER.warn "DHCP and DNS management require that you install the memcache service" - RAILS_DEFAULT_LOGGER.warn "and that you add this line to environment.db" - RAILS_DEFAULT_LOGGER.warn "config.cache_store = :mem_cache_store" - RAILS_DEFAULT_LOGGER.warn "*********************************************************************" - end - @dhcp = nil - return + message = "*********************************************************************\n" + + "DHCP and DNS management require that you install the memcache service\n" + + "and that you add this line to environment.db \n" + + "config.cache_store = :mem_cache_store \n" + + "and edit config.initializers/session_store to set = :mem_cache_store \n" + + "*********************************************************************\n" + RAILS_DEFAULT_LOGGER.warn message + puts message + exit end base.extend ClassMethods diff --git a/lib/netdb_manager/routing.rb b/lib/netdb_manager/routing.rb new file mode 100644 index 0000000..b9ca1c3 --- /dev/null +++ b/lib/netdb_manager/routing.rb @@ -0,0 +1,12 @@ +module NetdbManager + #:nodoc: + module Routing + #:nodoc: + module MapperExtensions + def NetdbManagers + @set.add_route("/subnets", {:controller => "subnets", :action => "index"}) + end + end + end +end +ActionController::Routing::RouteSet::Mapper.send :include, NetdbManager::Routing::MapperExtensions