daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest May 31st, 2016 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. === modified file 'bin/cloud-init'
  2. --- bin/cloud-init  2016-05-26 19:51:09 +0000
  3. +++ bin/cloud-init  2016-05-31 20:58:35 +0000
  4. @@ -280,11 +280,7 @@
  5.      LOG.debug("[%s] %s will now be targeting instance id: %s. new=%s",
  6.                mode, name, iid, init.is_new_instance())
  7.  
  8. -    if init.is_new_instance():
  9. -        # on new instance, apply network config.
  10. -        # in network mode 'bring_up' must be passed in as the OS
  11. -        # has already brought up networking.
  12. -        init.apply_network_config(bring_up=bool(mode != sources.DSMODE_LOCAL))
  13. +    init.apply_network_config(bring_up=bool(mode != sources.DSMODE_LOCAL))
  14.  
  15.      if mode == sources.DSMODE_LOCAL:
  16.          if init.datasource.dsmode != mode:
  17.  
  18. === modified file 'cloudinit/distros/__init__.py'
  19. --- cloudinit/distros/__init__.py   2016-05-25 21:05:09 +0000
  20. +++ cloudinit/distros/__init__.py   2016-05-31 20:33:26 +0000
  21. @@ -31,6 +31,7 @@
  22.  
  23.  from cloudinit import importer
  24.  from cloudinit import log as logging
  25. +from cloudinit import net
  26.  from cloudinit import ssh_util
  27.  from cloudinit import type_utils
  28.  from cloudinit import util
  29. @@ -145,6 +146,9 @@
  30.              return self._bring_up_interfaces(dev_names)
  31.          return False
  32.  
  33. +    def apply_network_config_names(self, netconfig):
  34. +        net.apply_network_config_names(netconfig)
  35. +
  36.      @abc.abstractmethod
  37.      def apply_locale(self, locale, out_fn=None):
  38.          raise NotImplementedError()
  39.  
  40. === modified file 'cloudinit/net/__init__.py'
  41. --- cloudinit/net/__init__.py   2016-05-25 21:05:09 +0000
  42. +++ cloudinit/net/__init__.py   2016-05-31 20:38:50 +0000
  43. @@ -813,4 +813,90 @@
  44.              'config': [devs[d] for d in sorted(devs)]}
  45.  
  46.  
  47. +def apply_network_config_names(netcfg, strict_present=True, strict_busy=True):
  48. +    """read the network config and rename devices accordingly.
  49. +    if strict_present is false, then do not raise exception if no devices
  50. +    match.  if strict_busy is false, then do not raise exception if the
  51. +    device cannot be renamed because it is currently configured."""
  52. +    renames = []
  53. +    for ent in netcfg.get('config', {}):
  54. +        if ent.get('type') != 'physical':
  55. +            continue
  56. +        mac = ent.get('mac_address')
  57. +        name = ent.get('name')
  58. +        if not mac:
  59. +            continue
  60. +        renames.append([mac, name])
  61. +
  62. +    return rename_interfaces(renames)
  63. +
  64. +
  65. +def rename_interfaces(renames, strict_present=True, strict_busy=True):
  66. +    cur_bymac = {get_interface_mac(n): n for n in get_devicelist()}
  67. +    expected = {mac: name for mac, name in renames}
  68. +    cur_byname = {v: k for k, v in cur_bymac.items()}
  69. +
  70. +    tmpname_fmt = "cirename%d"
  71. +    tmpi = -1
  72. +
  73. +    moves = []
  74. +    changes = []
  75. +    errors = []
  76. +    for mac, new_name in expected.items():
  77. +        cur_name = cur_bymac.get(mac)
  78. +        if cur_name == new_name:
  79. +            # nothing to do
  80. +            continue
  81. +
  82. +        if not cur_name:
  83. +            if strict_present:
  84. +                errors.append(
  85. +                    "[nic not present] Cannot rename mac=%s to %s"
  86. +                    ", not available." % (mac, new_name))
  87. +        elif is_up(cur_name):
  88. +            if strict_busy:
  89. +                errors.append("[busy] Error renaming mac=%s from %s to %s." %
  90. +                              (mac, cur_name, new_name))
  91. +        elif new_name in cur_byname:
  92. +            if is_up(new_name):
  93. +                if strict_busy:
  94. +                    errors.append(
  95. +                        "[busy-target] Error renaming mac=%s from %s to %s." %
  96. +                        (mac, cur_name, new_name))
  97. +            else:
  98. +                tmp_name = None
  99. +                while tmp_name is None or tmp_name in cur_byname:
  100. +                    tmpi += 1
  101. +                    tmp_name = tmpname_fmt % tmpi
  102. +                moves.append((mac, cur_name, tmp_name))
  103. +                changes.append((mac, tmp_name, new_name))
  104. +        else:
  105. +            changes.append((mac, cur_name, new_name))
  106. +
  107. +    def rename_dev(cur, new):
  108. +        cmd = ["ip", "link", "set", cur, "name", new]
  109. +        util.subp(cmd)
  110. +
  111. +    for mac, cur, new in moves + changes:
  112. +        try:
  113. +            rename_dev(cur, new)
  114. +        except util.ProcessExecutionError as e:
  115. +            errors.append(
  116. +                "[unknown] Error renaming mac=%s from %s to %s. (%s)" %
  117. +                (mac, cur, new, e))
  118. +
  119. +    if len(errors):
  120. +        raise Exception('\n'.join(errors))
  121. +
  122. +
  123. +def get_interface_mac(ifname):
  124. +    """Returns the string value of an interface's MAC Address"""
  125. +    return read_sys_net(ifname, "address", enoent=False)
  126. +
  127. +
  128. +def get_ifname_mac_pairs():
  129. +    """Build a list of tuples (ifname, mac)"""
  130. +    return [(ifname, get_interface_mac(ifname)) for ifname in get_devicelist()]
  131. +
  132. +
  133.  # vi: ts=4 expandtab syntax=python
  134.  
  135. === modified file 'cloudinit/stages.py'
  136. --- cloudinit/stages.py 2016-05-27 20:57:18 +0000
  137. +++ cloudinit/stages.py 2016-05-31 21:01:31 +0000
  138. @@ -632,6 +632,16 @@
  139.              LOG.info("network config is disabled by %s", src)
  140.              return
  141.  
  142. +        try:
  143. +            LOG.debug("applying net config names for %s" % netcfg)
  144. +            self.distro.apply_network_config_names(netcfg)
  145. +        except Exception as e:
  146. +            LOG.warn("Failed to rename devices: %s", e)
  147. +
  148. +        if not self.is_new_instance():
  149. +            LOG.debug("not a new instance. network config is not applied.")
  150. +            return
  151. +
  152.          LOG.info("Applying network configuration from %s bringup=%s: %s",
  153.                   src, bring_up, netcfg)
  154.          try:
RAW Paste Data
Top