Class: Oxidized::API::Mig

Inherits:
Object
  • Object
show all
Defined in:
lib/oxidized/web/mig.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash_router_db, cloginrc, path_new_router) ⇒ Mig

Returns a new instance of Mig.



4
5
6
7
8
# File 'lib/oxidized/web/mig.rb', line 4

def initialize(hash_router_db, cloginrc, path_new_router)
  @hash_router_db = hash_router_db
  @cloginrc = cloginrc
  @path_new_router = path_new_router
end

Instance Method Details

#cloginrc(clogin_file) ⇒ Object

read cloginrc and return a hash with node name, which a hash value which contains user, password, eventually enable



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
# File 'lib/oxidized/web/mig.rb', line 12

def cloginrc(clogin_file)
  close_file = clogin_file
  file = close_file.read
  file = file.gsub('add', '')

  hash = {}
  file.each_line do |line|
    # stock all device name, and password and enable if there is one
    line = line.split
    (0..line.length).each do |i|
      if line[i] == 'user'
        # add the equipment and user if not exist
        hash[line[i + 1]] = { user: line[i + 2] } unless hash[line[i + 1]]
      # if the equipment exist, add password and enable password
      elsif line[i] == 'password'
        if hash[line[i + 1]]
          if line.length > i + 2
            h = hash[line[i + 1]]
            h[:password] = line[i + 2]
            h[:enable] = line[i + 3] if /\s*/.match(line[i + 3])
            hash[line[i + 1]] = h
          elsif line.length == i + 2
            h = hash[line[i + 1]]
            h[:password] = line[i + 2]
            hash[line[i + 1]] = h
          end
        end
      end
    end
  end
  close_file.close
  hash
end

#edit_conf_file(path_conf, router_db_path) ⇒ Object



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
# File 'lib/oxidized/web/mig.rb', line 94

def edit_conf_file(path_conf, router_db_path)
  file_close = File.open(path_conf, 'r')
  file = file_close
  file = file.read
  source_reached = false
  new_file = []
  file.each_line do |line|
    if source_reached
      next unless /^\w/.match(line)

      source_reached = false
    end
    new_file.push(line)
    next unless /source:/.match(line)

    source_reached = true
    new_file.push("  default: csv\n")
    new_file.push("  csv:\n")
    new_file.push("    file: #{router_db_path}\n")
    new_file.push("    delimiter: !ruby/regexp /:/\n")
    new_file.push("    map:\n")
    new_file.push("      name: 0\n")
    new_file.push("      model: 1\n")
    new_file.push("      username: 2\n")
    new_file.push("      password: 3\n")
    new_file.push("      group: 4\n")
    new_file.push("    vars_map:\n")
    new_file.push("      enable: 5\n")
    next
  end
  file_close.close

  new_conf = File.new(path_conf, "w")
  new_file.each do |line|
    new_conf.puts(line)
  end
  new_conf.close
end

#go_rancid_migrationObject



133
134
135
136
137
# File 'lib/oxidized/web/mig.rb', line 133

def go_rancid_migration
  hash = rancid_group @hash_router_db
  write_router_db hash
  edit_conf_file "#{Dir.home}/.config/oxidized/config", @path_new_router
end

#model_dico(model) ⇒ Object



46
47
48
49
50
51
# File 'lib/oxidized/web/mig.rb', line 46

def model_dico(model)
  dico = { 'cisco' => 'ios', 'foundry' => 'ironware' }
  model = model.gsub("\n", '')
  model = dico[model] if dico[model]
  model
end

#rancid_group(router_db_list) ⇒ Object

add node and group for an equipment (take a list of router.db)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/oxidized/web/mig.rb', line 54

def rancid_group(router_db_list)
  model = {}
  hash = cloginrc @cloginrc
  router_db_list.each do |router_db|
    group = router_db[:group]
    file_close = router_db[:file]
    file = file_close.read
    file = file.gsub(':up', '')
    file.gsub(' ', '')

    file.each_line do |line|
      line = line.split(':')
      node = line[0]
      next unless hash[node]

      h = hash[node]
      model = model_dico line[1].to_s
      h[:model] = model
      h[:group] = group
    end
    file_close.close
  end
  hash
end

#write_router_db(hash) ⇒ Object

write a router.db conf, need the hash and the path of the file we whish create



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/oxidized/web/mig.rb', line 80

def write_router_db(hash)
  router_db = File.new(@path_new_router, 'w')
  hash.each do |key, value|
    line = key.to_s
    line += ":#{value[:model]}"
    line += ":#{value[:user]}"
    line += ":#{value[:password]}"
    line += ":#{value[:group]}"
    line += ":#{value[:enable]}" if value[:enable]
    router_db.puts(line)
  end
  router_db.close
end