summaryrefslogtreecommitdiff
path: root/packages/python-google-compute-engine/google_compute_engine/instance_setup/instance_config.py
blob: 8f2fadf1fb40ff552522b74a4559784c5516a585 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python
# Copyright 2016 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.

"""A library used to set up the instance's default configurations file.

Note that the configurations in
/etc/default/instance_configs.cfg.template override the values set in
/etc/default/instance_configs.cfg. The system instance_configs.cfg may be
overridden during package upgrade.
"""

import logging
import os

from google_compute_engine import config_manager
from google_compute_engine import constants
from google_compute_engine.compat import parser
from google_compute_engine.compat import stringio


class InstanceConfig(config_manager.ConfigManager):
  """Creates a defaults config file for instance configuration."""

  instance_config = constants.SYSCONFDIR + '/instance_configs.cfg'
  instance_config_distro = '%s.distro' % instance_config
  instance_config_template = '%s.template' % instance_config
  instance_config_script = os.path.abspath(__file__)
  instance_config_header = (
      'This file is automatically created at boot time by the %s script. Do '
      'not edit this file directly. If you need to add items to this file, '
      'create or edit %s instead and then run '
      '/usr/bin/google_instance_setup.')
  instance_config_options = {
      'Accounts': {
          'deprovision_remove': 'false',
          'groups': 'adm,dip,docker,lxd,plugdev,video',

          # The encrypted password is set to '*' for SSH on Linux systems
          # without PAM.
          #
          # SSH uses '!' as its locked account token:
          # https://github.com/openssh/openssh-portable/blob/master/configure.ac
          #
          # When the token is specified, SSH denies login:
          # https://github.com/openssh/openssh-portable/blob/master/auth.c
          #
          # To solve the issue, make the password '*' which is also recognized
          # as locked but does not prevent SSH login.
          'gpasswd_add_cmd': 'gpasswd -a {user} {group}',
          'gpasswd_remove_cmd': 'gpasswd -d {user} {group}',
          'groupadd_cmd': 'groupadd {group}',
          'useradd_cmd': 'useradd -m -s /bin/bash -p * {user}',
          'userdel_cmd': 'userdel -r {user}',
          'usermod_cmd': 'usermod -G {groups} {user}',
      },
      'Daemons': {
          'accounts_daemon': 'true',
          'clock_skew_daemon': 'true',
          'ip_forwarding_daemon': 'true',  # Deprecated.
          'network_daemon': 'true',
      },
      'Instance': {
          'instance_id': '0',
      },
      'InstanceSetup': {
          'host_key_types': 'ecdsa,ed25519,rsa',
          'optimize_local_ssd': 'true',
          'network_enabled': 'true',
          # WARNING: Do not change the value of 'set_boto_config' without first
          # consulting the gsutil team (GoogleCloudPlatform/gsutil).
          'set_boto_config': 'true',
          'set_host_keys': 'true',
          'set_multiqueue': 'true',
      },
      'IpForwarding': {
          'ethernet_proto_id': '66',
          'ip_aliases': 'true',
          'target_instance_ips': 'true',
      },
      'MetadataScripts': {
          'run_dir': '',
          'startup': 'true',
          'shutdown': 'true',
          'default_shell': '/bin/bash',
      },
      'NetworkInterfaces': {
          'setup': 'true',
          'ip_forwarding': 'true',
          'dhcp_command': '',
          'dhclient_script': '/sbin/google-dhclient-script',
      },
  }

  def __init__(self, logger=logging, instance_config_metadata=None):
    """Constructor.

    Inherit from the ConfigManager class. Read the template for instance
    defaults and write new sections and options. This prevents package
    updates from overriding user set defaults.

    Args:
      logger: logger object, used to write to SysLog and serial port.
      instance_config_metadata: string, a config file specified in metadata.
    """
    self.logger = logger
    self.instance_config_metadata = instance_config_metadata
    self.instance_config_header %= (
        self.instance_config_script, self.instance_config_template)
    # User provided instance configs should always take precedence.
    super(InstanceConfig, self).__init__(
        config_file=self.instance_config_template,
        config_header=self.instance_config_header)

    # Use the instance config settings from metadata if specified. Then use
    # settings in an instance config file if one exists. If a config
    # file does not already exist, try to use the distro provided defaults. If
    # no file exists, use the default configuration settings.
    config_files = [self.instance_config, self.instance_config_distro]
    config_defaults = []
    if self.instance_config_metadata:
      config = parser.Parser()
      try:
        config.read_file(stringio.StringIO(self.instance_config_metadata))
      except parser.Error as e:
        self.logger.error('Error parsing metadata configs: %s', str(e))
      else:
        config_defaults.append(
            dict((s, dict(config.items(s))) for s in config.sections()))
    for config_file in config_files:
      if os.path.exists(config_file):
        config = parser.Parser()
        try:
          config.read(config_file)
        except parser.Error as e:
          self.logger.error('Error parsing config file: %s', str(e))
        else:
          config_defaults.append(
              dict((s, dict(config.items(s))) for s in config.sections()))
    config_defaults.append(self.instance_config_options)

    for defaults in config_defaults:
      for section, options in sorted(defaults.items()):
        for option, value in sorted(options.items()):
          super(InstanceConfig, self).SetOption(
              section, option, value, overwrite=False)

  def WriteConfig(self):
    """Write the config values to the instance defaults file."""
    super(InstanceConfig, self).WriteConfig(config_file=self.instance_config)