~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to cloudinit/distros/gentoo.py

  • Committer: Scott Moser
  • Date: 2016-08-10 15:06:15 UTC
  • Revision ID: smoser@ubuntu.com-20160810150615-ma2fv107w3suy1ma
README: Mention move of revision control to git.

cloud-init development has moved its revision control to git.
It is available at 
  https://code.launchpad.net/cloud-init

Clone with 
  git clone https://git.launchpad.net/cloud-init
or
  git clone git+ssh://git.launchpad.net/cloud-init

For more information see
  https://git.launchpad.net/cloud-init/tree/HACKING.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vi: ts=4 expandtab
2
 
#
3
 
#    Copyright (C) 2014 Rackspace, US Inc.
4
 
#
5
 
#    Author: Nate House <nathan.house@rackspace.com>
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU General Public License version 3, as
9
 
#    published by the Free Software Foundation.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 
19
 
from cloudinit import distros
20
 
from cloudinit import helpers
21
 
from cloudinit import log as logging
22
 
from cloudinit import util
23
 
 
24
 
from cloudinit.distros.parsers.hostname import HostnameConf
25
 
 
26
 
from cloudinit.settings import PER_INSTANCE
27
 
 
28
 
LOG = logging.getLogger(__name__)
29
 
 
30
 
 
31
 
class Distro(distros.Distro):
32
 
    locale_conf_fn = "/etc/locale.gen"
33
 
    network_conf_fn = "/etc/conf.d/net"
34
 
    init_cmd = ['']  # init scripts
35
 
 
36
 
    def __init__(self, name, cfg, paths):
37
 
        distros.Distro.__init__(self, name, cfg, paths)
38
 
        # This will be used to restrict certain
39
 
        # calls from repeatly happening (when they
40
 
        # should only happen say once per instance...)
41
 
        self._runner = helpers.Runners(paths)
42
 
        self.osfamily = 'gentoo'
43
 
        # Fix sshd restarts
44
 
        cfg['ssh_svcname'] = '/etc/init.d/sshd'
45
 
 
46
 
    def apply_locale(self, locale, out_fn=None):
47
 
        if not out_fn:
48
 
            out_fn = self.locale_conf_fn
49
 
        util.subp(['locale-gen', '-G', locale], capture=False)
50
 
        # "" provides trailing newline during join
51
 
        lines = [
52
 
            util.make_header(),
53
 
            'LANG="%s"' % (locale),
54
 
            "",
55
 
        ]
56
 
        util.write_file(out_fn, "\n".join(lines))
57
 
 
58
 
    def install_packages(self, pkglist):
59
 
        self.update_package_sources()
60
 
        self.package_command('', pkgs=pkglist)
61
 
 
62
 
    def _write_network(self, settings):
63
 
        util.write_file(self.network_conf_fn, settings)
64
 
        return ['all']
65
 
 
66
 
    def _bring_up_interface(self, device_name):
67
 
        cmd = ['/etc/init.d/net.%s' % device_name, 'restart']
68
 
        LOG.debug("Attempting to run bring up interface %s using command %s",
69
 
                  device_name, cmd)
70
 
        try:
71
 
            (_out, err) = util.subp(cmd)
72
 
            if len(err):
73
 
                LOG.warn("Running %s resulted in stderr output: %s", cmd, err)
74
 
            return True
75
 
        except util.ProcessExecutionError:
76
 
            util.logexc(LOG, "Running interface command %s failed", cmd)
77
 
            return False
78
 
 
79
 
    def _bring_up_interfaces(self, device_names):
80
 
        use_all = False
81
 
        for d in device_names:
82
 
            if d == 'all':
83
 
                use_all = True
84
 
        if use_all:
85
 
            # Grab device names from init scripts
86
 
            cmd = ['ls', '/etc/init.d/net.*']
87
 
            try:
88
 
                (_out, err) = util.subp(cmd)
89
 
                if len(err):
90
 
                    LOG.warn("Running %s resulted in stderr output: %s", cmd,
91
 
                             err)
92
 
            except util.ProcessExecutionError:
93
 
                util.logexc(LOG, "Running interface command %s failed", cmd)
94
 
                return False
95
 
            devices = [x.split('.')[2] for x in _out.split('  ')]
96
 
            return distros.Distro._bring_up_interfaces(self, devices)
97
 
        else:
98
 
            return distros.Distro._bring_up_interfaces(self, device_names)
99
 
 
100
 
    def _write_hostname(self, your_hostname, out_fn):
101
 
        conf = None
102
 
        try:
103
 
            # Try to update the previous one
104
 
            # so lets see if we can read it first.
105
 
            conf = self._read_hostname_conf(out_fn)
106
 
        except IOError:
107
 
            pass
108
 
        if not conf:
109
 
            conf = HostnameConf('')
110
 
        conf.set_hostname(your_hostname)
111
 
        util.write_file(out_fn, conf, 0o644)
112
 
 
113
 
    def _read_system_hostname(self):
114
 
        sys_hostname = self._read_hostname(self.hostname_conf_fn)
115
 
        return (self.hostname_conf_fn, sys_hostname)
116
 
 
117
 
    def _read_hostname_conf(self, filename):
118
 
        conf = HostnameConf(util.load_file(filename))
119
 
        conf.parse()
120
 
        return conf
121
 
 
122
 
    def _read_hostname(self, filename, default=None):
123
 
        hostname = None
124
 
        try:
125
 
            conf = self._read_hostname_conf(filename)
126
 
            hostname = conf.hostname
127
 
        except IOError:
128
 
            pass
129
 
        if not hostname:
130
 
            return default
131
 
        return hostname
132
 
 
133
 
    def set_timezone(self, tz):
134
 
        distros.set_etc_timezone(tz=tz, tz_file=self._find_tz_file(tz))
135
 
 
136
 
    def package_command(self, command, args=None, pkgs=None):
137
 
        if pkgs is None:
138
 
            pkgs = []
139
 
 
140
 
        cmd = ['emerge']
141
 
        # Redirect output
142
 
        cmd.append("--quiet")
143
 
 
144
 
        if args and isinstance(args, str):
145
 
            cmd.append(args)
146
 
        elif args and isinstance(args, list):
147
 
            cmd.extend(args)
148
 
 
149
 
        if command:
150
 
            cmd.append(command)
151
 
 
152
 
        pkglist = util.expand_package_list('%s-%s', pkgs)
153
 
        cmd.extend(pkglist)
154
 
 
155
 
        # Allow the output of this to flow outwards (ie not be captured)
156
 
        util.subp(cmd, capture=False)
157
 
 
158
 
    def update_package_sources(self):
159
 
        self._runner.run("update-sources", self.package_command,
160
 
                         ["-u", "world"], freq=PER_INSTANCE)