Class: Mu::Netconfig
Constant Summary
Constants included from Helper
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#docroot ⇒ Object
Returns the value of attribute docroot.
-
#element ⇒ Object
Returns the value of attribute element.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#response ⇒ Object
Returns the value of attribute response.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#clear_hosts ⇒ Object
clears the network hosts.
-
#clear_interface(interface) ⇒ Object
clears a network interface * interface = the name of the interface to clear.
-
#clear_routes ⇒ Object
clears network routes.
-
#clear_vlans ⇒ Object
clears all network vlans.
-
#create(json, e) ⇒ Object
POST method to add a network element * json = the json string containing the element values * e = the element to create (interfaces|hosts|routes).
-
#delete(e) ⇒ Object
deletes the specified element * e = the element to delete.
-
#get(e) ⇒ Object
get - with no params, returns the entire netconfig json object * e = the element to retrieve (interfaces|hosts|routes, interfaces/a1 …).
-
#initialize(host = ENV['MU_IP'], username = ENV['MU_ADMIN_USER'], password = ENV['MU_ADMIN_PASS']) ⇒ Netconfig
constructor
A new instance of Netconfig.
-
#modify(json, e) ⇒ Object
PUT to modify netconfig element(s) to json values * json = the json string containing the modified values * e = the element to modify (interfaces|hosts|routes).
-
#resolve_hosts(name = nil) ⇒ Object
use Dns to update host ip addresses.
-
#restore(filepath = nil, clear_existing = false) ⇒ Object
updates a network configuration from file * filepath = the path to the json file * clear_existing - boolean determining whether or not existing elements should be cleared.
-
#restore_hosts(hosts) ⇒ Object
restores the network hosts to the initial system states.
-
#restore_interfaces(interfaces) ⇒ Object
restores network interfaces to system initial settings * interfaces - the names of the interfaces to restore.
-
#restore_routes(routes) ⇒ Object
restores network routes to system initial settings.
-
#save(e = "all", filepath = "config.json") ⇒ Object
writes the json config to filepath * e = the element to save, or ‘all’ * filepath - the fully qualified name of the file to save to.
Methods included from Helper
#ask, #bin2hex, #error, #escape, #format_float, #get_file_as_string_array, #make_xml, #msg, #shift, #to_boolean
Constructor Details
#initialize(host = ENV['MU_IP'], username = ENV['MU_ADMIN_USER'], password = ENV['MU_ADMIN_PASS']) ⇒ Netconfig
Returns a new instance of Netconfig.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/mu/api/netconfig.rb', line 7 def initialize(host=ENV['MU_IP'], username=ENV['MU_ADMIN_USER'], password=ENV['MU_ADMIN_PASS']) @host = host @username = username @password = password @docroot = "/restlet/netconfig/" @response = nil @http = HttpHelper.new(@host, @username, @password, @docroot) @element = "" # sticky variable will hold a default element, the last element specified msg "Created Netconfig API object to :#{@host}", Logger::DEBUG end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/mu/api/netconfig.rb', line 5 def config @config end |
#docroot ⇒ Object
Returns the value of attribute docroot.
5 6 7 |
# File 'lib/mu/api/netconfig.rb', line 5 def docroot @docroot end |
#element ⇒ Object
Returns the value of attribute element.
5 6 7 |
# File 'lib/mu/api/netconfig.rb', line 5 def element @element end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/mu/api/netconfig.rb', line 5 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
5 6 7 |
# File 'lib/mu/api/netconfig.rb', line 5 def password @password end |
#response ⇒ Object
Returns the value of attribute response.
5 6 7 |
# File 'lib/mu/api/netconfig.rb', line 5 def response @response end |
#username ⇒ Object
Returns the value of attribute username.
5 6 7 |
# File 'lib/mu/api/netconfig.rb', line 5 def username @username end |
Instance Method Details
#clear_hosts ⇒ Object
clears the network hosts
92 93 94 95 96 97 98 |
# File 'lib/mu/api/netconfig.rb', line 92 def clear_hosts h = get("hosts") h["hosts"].each do |h| msg "Clear host: #{h['name']}", Logger::DEBUG delete("hosts/#{h['name']}") end end |
#clear_interface(interface) ⇒ Object
clears a network interface
* interface = the name of the interface to clear
139 140 141 142 143 144 |
# File 'lib/mu/api/netconfig.rb', line 139 def clear_interface(interface) interface = interface json = { "v4_addrs"=>[], "v4_mask"=>"", "v4_dhcp"=>false, "v6_global_addrs"=>[], "v6_global_mask"=>""} modify json,"interfaces/#{interface}" end |
#clear_routes ⇒ Object
clears network routes
171 172 173 174 175 176 177 178 |
# File 'lib/mu/api/netconfig.rb', line 171 def clear_routes routes = get "routes" routes["routes"].each do |r| next if r['readonly'] == true msg "Clear route: #{r['dst']}-#{r['gateway']}-#{r['interface_display_name'].downcase}", Logger::DEBUG delete "routes/#{r['dst']}-#{r['gateway']}-#{r['interface_display_name'].downcase}" end end |
#clear_vlans ⇒ Object
clears all network vlans
147 148 149 150 151 152 153 154 |
# File 'lib/mu/api/netconfig.rb', line 147 def clear_vlans i = get "interfaces" i["interfaces"].each do |i| next if i['vlan'] == "" msg "Clear vlan: #{i['name']}", Logger::DEBUG delete "interfaces/#{i['name']}" end end |
#create(json, e) ⇒ Object
POST method to add a network element
* json = the json string containing the element values
* e = the element to create (interfaces|hosts|routes)
48 49 50 51 52 53 54 55 56 |
# File 'lib/mu/api/netconfig.rb', line 48 def create(json, e) jstring = json unless json.is_a? String jstring = JSON.generate json end response = @http.post_json(e, jstring) msg response, Logger::DEBUG return response end |
#delete(e) ⇒ Object
deletes the specified element
* e = the element to delete
60 61 62 63 64 |
# File 'lib/mu/api/netconfig.rb', line 60 def delete(e) response = @http.delete(e) msg response, Logger::DEBUG return response end |
#get(e) ⇒ Object
get - with no params, returns the entire netconfig json object
* e = the element to retrieve (interfaces|hosts|routes, interfaces/a1 ...)
20 21 22 23 24 |
# File 'lib/mu/api/netconfig.rb', line 20 def get(e) response = @http.get_json(e) msg response, Logger::DEBUG return response end |
#modify(json, e) ⇒ Object
PUT to modify netconfig element(s) to json values
* json = the json string containing the modified values
* e = the element to modify (interfaces|hosts|routes)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mu/api/netconfig.rb', line 29 def modify(json, e) response = do_modify(json, e) msg response, Logger::DEBUG return response =begin jstring = json @element = e unless json.is_a? String jstring = JSON.generate json end response = @http.put_json(e, jstring) msg response, Logger::DEBUG return response =end end |
#resolve_hosts(name = nil) ⇒ Object
use Dns to update host ip addresses. A new Host is added if not present when the name is provided as argument
* name = the name of the host to resolve
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/mu/api/netconfig.rb', line 112 def resolve_hosts(name=nil) response = "" hosts = Array.new if name.nil? hst = get "hosts" hst["hosts"].each {|h| hosts << h["name"]} else hosts << name end hosts.each do |h| msg "resolve host: #{h}", Logger::DEBUG v4_addr = Socket::gethostbyname(h)[3].unpack("CCCC").join(".") rescue nil next if v4_addr.nil? json = get "hosts/#{h}" if json["name"].nil? json = {"name" => h, "v4_addr" => v4_addr} response = create json, "hosts" else json["v4_addr"] = v4_addr response = modify json, "hosts/#{h}" end end return response end |
#restore(filepath = nil, clear_existing = false) ⇒ Object
updates a network configuration from file
* filepath = the path to the json file
* clear_existing - boolean determining whether or not existing elements should be cleared
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mu/api/netconfig.rb', line 69 def restore(filepath=nil,clear_existing=false) unless filepath.nil? @config = JSON.parse(File.read(filepath)) end @config.each do |c| case c.keys[0] when "hosts" msg "RESTORE HOSTS", Logger::DEBUG clear_existing and clear_hosts restore_hosts c["hosts"] when "routes" msg "RESTORE ROUTES", Logger::DEBUG clear_existing and clear_routes restore_routes c["routes"] when "interfaces" clear_existing and clear_vlans msg "RESTORE INTERFACES", Logger::DEBUG restore_interfaces c["interfaces"] end end end |
#restore_hosts(hosts) ⇒ Object
restores the network hosts to the initial system states
101 102 103 104 105 106 107 |
# File 'lib/mu/api/netconfig.rb', line 101 def restore_hosts(hosts) hosts.each do |h| msg "create host: #{h['name']}", Logger::DEBUG delete "hosts/#{h['name']}" # harmlessly fails when host does not exist create(h,"hosts") end end |
#restore_interfaces(interfaces) ⇒ Object
restores network interfaces to system initial settings
* interfaces - the names of the interfaces to restore
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/mu/api/netconfig.rb', line 158 def restore_interfaces(interfaces) interfaces.each do |i| next if i['name'].include? "eth" # don't do eth0 or eth1 msg "configure interface: #{i['name']}", Logger::DEBUG unless i['vlan'] == "" interface,vlan = i['name'].split(".") create [{"name"=>interface,"vlan"=>vlan},"interfaces"] end modify [i,"interfaces/#{i['name']}"] end end |
#restore_routes(routes) ⇒ Object
restores network routes to system initial settings
181 182 183 184 185 186 187 |
# File 'lib/mu/api/netconfig.rb', line 181 def restore_routes(routes) routes.each do |r| next if r['readonly'] == true msg "configure route: #{r['dst']}-#{r['gateway']}-#{r['interface_display_name'].downcase}", Logger::DEBUG create r,"routes" end end |
#save(e = "all", filepath = "config.json") ⇒ Object
writes the json config to filepath
* e = the element to save, or 'all'
* filepath - the fully qualified name of the file to save to
192 193 194 195 |
# File 'lib/mu/api/netconfig.rb', line 192 def save(e="all", filepath="config.json") json = get e File.open(filepath,'w'){|f| f.write(JSON.pretty_generate(json))} end |