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
45
46
47
48
# 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(' ')
    for i in 0..line.length
      if line[i] == 'user'
        # add the equipment and user if not exist
        unless hash[line[i + 1]]
          hash[line[i + 1]] = { user: line[i + 2] }
        end
      # 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]
            if /\s*/.match(line[i + 3])
              h[:enable] = line[i + 3]
            end
            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



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

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
      unless /^\w/.match(line)
        next
      end

      source_reached = false
    end
    new_file.push(line)
    if /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
  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



143
144
145
146
147
# File 'lib/oxidized/web/mig.rb', line 143

def go_rancid_migration
  hash = rancid_group @hash_router_db
  write_router_db hash
  edit_conf_file "#{ENV['HOME']}/.config/oxidized/config", @path_new_router
end

#model_dico(model) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/oxidized/web/mig.rb', line 50

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

#rancid_group(router_db_list) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/oxidized/web/mig.rb', line 60

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]
      if hash[node]
        h = hash[node]
        model = model_dico line[1].to_s
        h[:model] = model
        h[:group] = group
      end
    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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/oxidized/web/mig.rb', line 86

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]}"
    if value[:enable]
      line += ":#{value[:enable]}"
    end
    router_db.puts(line)
  end
  router_db.close
end