Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created March 18, 2011 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/875642 to your computer and use it in GitHub Desktop.
Save havenwood/875642 to your computer and use it in GitHub Desktop.
require 'sinatra'
# routes
get '/' do
erb :body_vendor
end
post '/' do
@switch = params[:vendor]
case @switch
when "first"
@vendor = First.vendor
@model = First.model
@ports = First.ports
erb :body_ports
when "second"
@vendor = Second.vendor
@model = Second.model
@ports = Second.ports
erb :body_ports
else
erb :body_vendor
end
end
post '/results' do
@vendor = Second.vendor
@model = Second.model
@port = params[:port]
erb :body_result
end
# logs
class Switches
class << self
attr_accessor :vendor, :model, :ports
def log
File.open('tmp/swcfg-log.txt', 'a') { |log| log.puts "#{Time.now.ctime}: #{@vendor}, #{@model}: #{@ports}" }
end
end
end
# configurations
class First < Switches
@vendor = "Cisco"
@model = "3560G"
@ports = { "Port 0" => "GigabitEthernet 0/1",
"Port 1" => "GigabitEthernet 0/2" }
end
class Second < Switches
@vendor = "Stratix"
@model = "9000"
@ports = { "Port 0" => "TenGigabitEthernet 1/1",
"Port 1" => "tge0/1/1" }
end
__END__
@@ layout
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>swcfg.rb</title>
<style type="text/css">
html, body, form, fieldset, legend { margin: 5; padding: 5; }
body { font: 16px/18px helvetica; padding: 20px; color: #444444; }
</style>
</head>
<body>
<%= yield %>
</body>
</html>
@@ body_vendor
<form action="/" method="post">
<fieldset>
<legend>Select a Switch</legend>
<p><input type="radio" name="vendor" value="<%= "first" %>"><%= First.vendor %></p>
<p><input type="radio" name="vendor" value="<%= "second" %>"><%= Second.vendor %></p>
<p><input type="submit" value="Select"></p>
</fieldset>
</form>
@@ body_ports
<form action="/results" method="post">
<fieldset>
<legend>Select a Port</legend>
<p>Vendor: <%= @vendor %></p>
<p>Model: <%= @model %></p>
<% @ports.each do |port, name| %>
<% @port = "#{port}: #{name}" %>
<p><input type="radio" name="port" value="<%= @port %>"><%= @port %></p>
<% end %>
<p><a href="/"><button>Back</button></a><input type="submit" value="Generate Configuration"></p>
</fieldset>
</form>
@@ body_result
<fieldset>
<legend>Configuration</legend>
<p>Vendor: <%= @vendor %></p>
<p>Model: <%= @model %></p>
<p><%= @port %></p>
<p>However this is going to look...</p>
<p><a href="/"><button>Start Over</button></a></p>
</fieldset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment