Class: Construqt::Flavour::Mikrotik::Bond

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/construqt/flavour/mikrotik/flavour_mikrotik.rb

Instance Method Summary collapse

Constructor Details

#initialize(cfg) ⇒ Bond

Returns a new instance of Bond.



69
70
71
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik.rb', line 69

def initialize(cfg)
  super(cfg)
end

Instance Method Details

#build_config(host, iface) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik.rb', line 124

def build_config(host, iface)
  iface = iface.delegate
  default = {
    "mode" => Schema.string.default("active-backup"),
    "mtu" => Schema.int.required,
    "name" => Schema.identifier.required.key,
    "slaves" => Schema.identifiers.required,
  }
  host.result.render_mikrotik(default, {
    "mtu" => iface.mtu,
    "name" => iface.name,
    "mode" => iface.mode,
    "slaves" => iface.interfaces.map{|iface| iface.name}.join(',')
  }, "interface", "bonding")
  Interface.build_config(host, iface)
  scheduler_hack(host, iface)
end

#scheduler_hack(host, iface) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik.rb', line 73

def scheduler_hack(host, iface)
  #binding.pry if iface.name=="sw12"
  return [] unless iface.interfaces.find{|iface| iface.class.kind_of? self.class }

  system_script_schema = {
    "name" => Schema.identifier.key.required,
    "source" => Schema.source.required
  }
  host.result.render_mikrotik(system_script_schema, {
    "no_auto_disable" => true,
    "name" => "disable-#{iface.name}",
    "source" => <<SRC
/interface bonding disable [ find name=#{iface.name} ]
/system scheduler enable [ find name=enable-#{iface.name} ]
SRC
  }, "system", "script")

  or_condition = "(" + iface.interfaces.map{|iface| "name=#{iface.name}"}.join(" or ") + ")"
  host.result.render_mikrotik(system_script_schema, {
    "no_auto_disable" => true,
    "name" => "enable-#{iface.name}",
    "source" => <<SRC
:local run [ /interface bonding find running=yes and #{or_condition}]
:if ($run!="") do={
  /interface bonding enable [find name=sw12]
  /system schedule disable [ find name=enable-sw12 ]
}
SRC
  }, "system", "script")

  system_scheduler_script = {
    "name" => Schema.identifier.key.required,
    "on-event" => Schema.identifier.required,
    "start-time" => Schema.identifier.null,
    "interval" => Schema.interval.null,
    "disabled" => Schema.boolean.default(false)
  }
  host.result.render_mikrotik(system_scheduler_script, {
    "name" => "disable-#{iface.name}",
    "on-event" => "disable-#{iface.name}",
    "start-time" => "startup"
  }, "system", "scheduler")

  host.result.render_mikrotik(system_scheduler_script, {
    "name" => "enable-#{iface.name}",
    "on-event" => "enable-#{iface.name}",
    "interval" => "00:00:10",
    "disabled" => true
  }, "system", "scheduler")
end