Class: Rtprov::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/rtprov/cli.rb

Instance Method Summary collapse

Instance Method Details

#diff(router_name, template_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rtprov/cli.rb', line 31

def diff(router_name, template_name)
  current_file = options[:filename]
  router = Router.load(router_name)

  template = Template.find(router_name, template_name)
  new_config = template.render(router.variables)

  sftp = Sftp.new(router.host, router.user, router.administrator_password)
  current_config = sftp.get(current_file)
  diff = ENV["RTPROV_DIFF"] || %w(colordiff diff).find {|cmd| system("which", cmd, out: "/dev/null", err: "/dev/null") }

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      File.write("new.conf", new_config.gsub(/^\s*#.*$/, "").gsub(/(\r\n|\r|\n)+/, "\r\n").chomp)
      File.write("current.conf", current_config.gsub(/^\s*#.*$/, "").gsub(/\s+$/, "").gsub(/(\r\n|\r|\n)+/, "\r\n").chomp)
      system("#{diff} -u current.conf new.conf", out: $stdout, err: $stderr)
    end
  end
end

#edit(router_name) ⇒ Object



12
13
14
# File 'lib/rtprov/cli.rb', line 12

def edit(router_name)
  Router.edit(router_name)
end

#get(router_name) ⇒ Object



23
24
25
26
27
# File 'lib/rtprov/cli.rb', line 23

def get(router_name)
  router = Router.load(router_name)
  sftp = Sftp.new(router.host, router.user, router.administrator_password)
  puts sftp.get(options[:filename])
end

#lsObject



101
102
103
104
105
# File 'lib/rtprov/cli.rb', line 101

def ls
  Router.names.each do |name|
    puts name
  end
end

#new(name) ⇒ Object



7
8
9
# File 'lib/rtprov/cli.rb', line 7

def new(name)
  Initializer.run(name)
end


92
93
94
95
96
97
98
# File 'lib/rtprov/cli.rb', line 92

def print(router_name, template_name)
  router = Router.load(router_name)

  template = Template.find(router_name, template_name)
  new_config = template.render(router.variables)
  puts new_config
end

#put(router_name, template_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rtprov/cli.rb', line 54

def put(router_name, template_name)
  current_file = "/system/config#{options[:number]}"
  router = Router.load(router_name)

  template = Template.find(router_name, template_name)
  new_config = template.render(router.variables)

  sftp = Sftp.new(router.host, router.user, router.administrator_password)
  current_config = sftp.get(current_file)
  diff = ENV["RTPROV_DIFF"] || %w(colordiff diff).find {|cmd| system("which", cmd, out: "/dev/null", err: "/dev/null") }

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      File.write("new.conf", new_config.gsub(/^#.*$/, "").gsub(/(\r\n|\r|\n)+/, "\r\n"))
      File.write("current.conf", current_config.gsub(/^#.*$/, "").gsub(/(\r\n|\r|\n)+/, "\r\n"))
      system("#{diff} -u current.conf new.conf", out: $stdout, err: $stderr)

      unless options[:force]
        loop do
          print "apply? (y/n): "
          case $stdin.gets.strip
          when "y"
            break
          when "n"
            return nil
          end
        end
      end

      sftp.put("new.conf", current_file)
      Session.start(router) do |s|
        s.exec_with_passwords "load config #{options[:number]} silent no-key-generate"
      end
    end
  end
end

#show(router_name) ⇒ Object



17
18
19
# File 'lib/rtprov/cli.rb', line 17

def show(router_name)
  puts Router.decrypt(router_name)
end

#ssh(router_name) ⇒ Object



108
109
110
111
112
# File 'lib/rtprov/cli.rb', line 108

def ssh(router_name)
  router = Router.load(router_name)
  warn "Password: #{router.password}"
  exec "ssh", "#{router.user}@#{router.host}"
end