aboutsummaryrefslogtreecommitdiffstats
path: root/mikrotik-setup-script.rsc
blob: 058f609213286c890183827dbb10c8817f418a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{
  # name of bridge to add veth interface to
  :local BRIDGENAME "bridgeLocal"
  # name the veth interface will get
  :local VETHNAME "mDNSTrunk"
  # set hostname of container
  :local HOSTNAME "mDNS"
  # set vlan-ids to reflect traffic between
  :local VLANS {100;200}
  # define image filename
  :local IMAGENAME "mdns_x86.tar"
  # path to data root for the container
  :local CONTAINERROOT "docker/container/mdns_repeater"

  # -------------------------------
  # variable holds all vlans seperated by spaces
  :local ALLVLANS ""
  
  # check bridge existance
  :if ([:len [/interface bridge find name="$BRIDGENAME"]] = 0) do={
    :put "Could not find a bridge with name '$BRIDGENAME' !"
    :quit
  }
  # add veth interface
  :if ([:len [/interface veth find name="$VETHNAME"]] = 0) do={
    # add veth interface
    /interface veth add address=172.19.19.2/24 gateway=172.19.19.1 name=$VETHNAME
  }
  # add veth interface to bridge
  :if ([:len [/interface bridge port find interface=$VETHNAME]] = 0) do={
    /interface bridge port add interface=$VETHNAME bridge=$BRIDGENAME
  }
  
  # for each vlan id
  :foreach id in=$VLANS do={
    # find existing vlan entry
    :local vlanentry [/interface bridge vlan find vlan-ids ~ "(^|;)$id(;|\$)" and bridge=$BRIDGENAME]
    
    :if ([:len $vlanentry] = 0) do={
      # if entry does not exist add it to vlan list
      /interface bridge vlan add bridge=$BRIDGENAME tagged="$BRIDGENAME,$VETHNAME" vlan-ids=$id
    } else={
      # if entry exists and veth interface is not a tagged member append veth interface to vlan as tagged interface
      :if ([:len [/interface bridge vlan find vlan-ids=$id and current-tagged ~ "$VETHNAME"]] = 0) do={
        /interface bridge vlan set $vlanentry tagged=([get $vlanentry tagged],"$VETHNAME")
      }
    }
    # build ALLVLAN variable
    :if ($ALLVLANS != "") do={
      :set ALLVLANS ($ALLVLANS . " " . $id)
    } else={
      :set ALLVLANS $id
    }
  }
  # add container environment variable wich defines the vlans used
  :if ([:len [/container envs find name=mdns key=VLANS]] = 0) do={
    /container envs add name=mdns key=VLANS value=$ALLVLANS
  }
  # finally add container
  :if ([:len [/container find root-dir="$CONTAINERROOT"]] = 0) do={
    /container add file=$IMAGENAME envlist=mdns logging=yes start-on-boot=yes root-dir="$CONTAINERROOT" interface=$VETHNAME hostname=$HOSTNAME
  } else={
    :put "Container already exists, please delete it beforehand and run again!"
  }
}