summaryrefslogtreecommitdiff
path: root/packages/python-google-compute-engine/google_compute_engine/distro_lib/sles_12/utils.py
blob: aacfc471a8905253e035ebd916ee697fd7bfc75c (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
66
67
68
69
70
71
72
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
123
124
125
126
127
#!/usr/bin/python
# Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities that are distro specific for use on SUSE 12."""

import os
import subprocess

from google_compute_engine import constants
from google_compute_engine.distro_lib import helpers
from google_compute_engine.distro_lib import ip_forwarding_utils
from google_compute_engine.distro_lib import utils


class Utils(utils.Utils):
  """Utilities used by Linux guest services on SUSE 12."""

  network_path = constants.LOCALBASE + '/etc/sysconfig/network'

  def EnableIpv6(self, interfaces, logger, dhclient_script=None):
    """Configure the network interfaces for IPv6 using dhclient.

    Args:
      interface: string, the output device names for enabling IPv6.
      logger: logger object, used to write to SysLog and serial port.
      dhclient_script: string, the path to a dhclient script used by dhclient.
    """
    pass

  def DisableIpv6(self, interfaces, logger):
    """Disable Ipv6.

    Args:
      interface: string, the output device names for enabling IPv6.
      logger: logger object, used to write to SysLog and serial port.
    """
    pass

  def EnableNetworkInterfaces(self, interfaces, logger, dhclient_script=None):
    """Enable the list of network interfaces.

    Args:
      interfaces: list of string, the output device names to enable.
      logger: logger object, used to write to SysLog and serial port.
      dhclient_script: string, the path to a dhclient script used by dhclient.
    """
    interfaces_to_up = [i for i in interfaces if i != 'eth0']
    if interfaces_to_up:
      logger.info('Enabling the Ethernet interfaces %s.', interfaces_to_up)
      self._WriteIfcfg(interfaces_to_up, logger)
      self._Ifup(interfaces_to_up, logger)

  def _WriteIfcfg(self, interfaces, logger):
    """Write ifcfg files for multi-NIC support.

    Overwrites the files. This allows us to update ifcfg-* in the future.
    Disable the network setup to override this behavior and customize the
    configurations.

    Args:
      interfaces: list of string, the output device names to enable.
      logger: logger object, used to write to SysLog and serial port.
    """
    for interface in interfaces:
      interface_config = os.path.join(
          self.network_path, 'ifcfg-%s' % interface)
      interface_content = [
          '# Added by Google.',
          'STARTMODE=hotplug',
          'BOOTPROTO=dhcp',
          'DHCLIENT_SET_DEFAULT_ROUTE=yes',
          'DHCLIENT_ROUTE_PRIORITY=10%s00' % interface,
          '',
      ]
      with open(interface_config, 'w') as interface_file:
        interface_file.write('\n'.join(interface_content))
      logger.info('Created ifcfg file for interface %s.', interface)

  def _Ifup(self, interfaces, logger):
    """Activate network interfaces.

    Args:
      interfaces: list of string, the output device names to enable.
      logger: logger object, used to write to SysLog and serial port.
    """
    ifup = ['/usr/sbin/wicked', 'ifup', '--timeout', '1']
    try:
      subprocess.check_call(ifup + interfaces)
    except subprocess.CalledProcessError:
      logger.warning('Could not activate interfaces %s.', interfaces)

  def HandleClockSync(self, logger):
    """Sync the software clock with the hypervisor clock.

    Args:
      logger: logger object, used to write to SysLog and serial port.
    """
    helpers.CallHwclock(logger)

  def IpForwardingUtils(self, logger, proto_id=None):
    """Get system IP address configuration utilities.

    Args:
      logger: logger object, used to write to SysLog and serial port.
      proto_id: string, the routing protocol identifier for Google IP changes.
    """
    return ip_forwarding_utils.IpForwardingUtilsIproute(logger, proto_id)

  def RestartNetworking(self, logger):
    """Restart the networking service to force a DHCP refresh.

    Args:
      logger: logger object, used to write to SysLog and serial port.
    """
    helpers.SystemctlRestart('wickedd-nanny', logger)