3
# Copyright (C) 2014 Rackspace, US Inc.
5
# Author: Nate House <nathan.house@rackspace.com>
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.
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.
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/>.
19
from cloudinit import distros
20
from cloudinit import helpers
21
from cloudinit import log as logging
22
from cloudinit import util
24
from cloudinit.distros.parsers.hostname import HostnameConf
26
from cloudinit.settings import PER_INSTANCE
28
LOG = logging.getLogger(__name__)
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
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'
44
cfg['ssh_svcname'] = '/etc/init.d/sshd'
46
def apply_locale(self, locale, out_fn=None):
48
out_fn = self.locale_conf_fn
49
util.subp(['locale-gen', '-G', locale], capture=False)
50
# "" provides trailing newline during join
53
'LANG="%s"' % (locale),
56
util.write_file(out_fn, "\n".join(lines))
58
def install_packages(self, pkglist):
59
self.update_package_sources()
60
self.package_command('', pkgs=pkglist)
62
def _write_network(self, settings):
63
util.write_file(self.network_conf_fn, settings)
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",
71
(_out, err) = util.subp(cmd)
73
LOG.warn("Running %s resulted in stderr output: %s", cmd, err)
75
except util.ProcessExecutionError:
76
util.logexc(LOG, "Running interface command %s failed", cmd)
79
def _bring_up_interfaces(self, device_names):
81
for d in device_names:
85
# Grab device names from init scripts
86
cmd = ['ls', '/etc/init.d/net.*']
88
(_out, err) = util.subp(cmd)
90
LOG.warn("Running %s resulted in stderr output: %s", cmd,
92
except util.ProcessExecutionError:
93
util.logexc(LOG, "Running interface command %s failed", cmd)
95
devices = [x.split('.')[2] for x in _out.split(' ')]
96
return distros.Distro._bring_up_interfaces(self, devices)
98
return distros.Distro._bring_up_interfaces(self, device_names)
100
def _write_hostname(self, your_hostname, out_fn):
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)
109
conf = HostnameConf('')
110
conf.set_hostname(your_hostname)
111
util.write_file(out_fn, conf, 0o644)
113
def _read_system_hostname(self):
114
sys_hostname = self._read_hostname(self.hostname_conf_fn)
115
return (self.hostname_conf_fn, sys_hostname)
117
def _read_hostname_conf(self, filename):
118
conf = HostnameConf(util.load_file(filename))
122
def _read_hostname(self, filename, default=None):
125
conf = self._read_hostname_conf(filename)
126
hostname = conf.hostname
133
def set_timezone(self, tz):
134
distros.set_etc_timezone(tz=tz, tz_file=self._find_tz_file(tz))
136
def package_command(self, command, args=None, pkgs=None):
142
cmd.append("--quiet")
144
if args and isinstance(args, str):
146
elif args and isinstance(args, list):
152
pkglist = util.expand_package_list('%s-%s', pkgs)
155
# Allow the output of this to flow outwards (ie not be captured)
156
util.subp(cmd, capture=False)
158
def update_package_sources(self):
159
self._runner.run("update-sources", self.package_command,
160
["-u", "world"], freq=PER_INSTANCE)