4096
  "   ˆl–ßi~”Še¶¥8 Z¸® 
bÑ¿Äƒh¹ÇÆ      1
  "   ˆÁÄƒuÇ…a–äY>”ªe¶¥8 q·n	Õ1hßÇ     # Copyright (C) 2014 Rackspace, US Inc.
#
# Author: Nate House <nathan.house@rackspace.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import distros
from cloudinit import helpers
from cloudinit import log as logging
from cloudinit import util

from cloudinit.distros import net_util
from cloudinit.distros.parsers.hostname import HostnameConf

from cloudinit.net.renderers import RendererNotFoundError

from cloudinit.settings import PER_INSTANCE

import os

LOG = logging.getLogger(__name__)


class Distro(distros.Distro):
    locale_conf_fn = "/etc/locale.gen"
    network_conf_dir = "/etc/netctl"
    resolve_conf_fn = "/etc/resolv.conf"
    init_cmd = ['systemctl']  # init scripts
    renderer_configs = {
        "netplan": {"netplan_path": "/etc/netplan/50-cloud-init.yaml",
                    "netplan_header": "# generated by cloud-init\n",
                    "postcmds": True}
    }

    def __init__(self, name, cfg, paths):
        distros.Distro.__init__(self, name, cfg, paths)
        # This will be used to restrict certain
        # calls from repeatly happening (when they
        # should only happen say once per instance...)
        self._runner = helpers.Runners(paths)
        self.osfamily = 'arch'
        cfg['ssh_svcname'] = 'sshd'

    def apply_locale(self, locale, out_fn=None):
        if not out_fn:
            out_fn = self.locale_conf_fn
        util.subp(['locale-gen', '-G', locale], capture=False)
        # "" provides trailing newline during join
        lines = [
            util.make_header(),
            'LANG="%s"' % (locale),
            "",
        ]
        util.write_file(out_fn, "\n".join(lines))

    def install_packages(self, pkglist):
        self.update_package_sources()
        self.package_command('', pkgs=pkglist)

    def _write_network_config(self, netconfig):
        try:
            return self._supported_write_network_config(netconfig)
        except RendererNotFoundError:
            # Fall back to old _write_network
            raise NotImplementedError

    def _write_network(self, settings):
        entries = net_util.translate_network(settings)
        LOG.debug("Translated ubuntu style network settings %s into %s",
                  settings, entries)
        return _render_network(
            entries, resolv_conf=self.resolve_conf_fn,
            conf_dir=self.network_conf_dir,
            enable_func=self._enable_interface)

    def _enable_interface(self, device_name):
        cmd = ['netctl', 'reenable', device_name]
        try:
            (_out, err) = util.subp(cmd)
            if len(err):
                LOG.warning("Running %s resulted in stderr output: %s",
                            cmd, err)
        except util.ProcessExecutionError:
            util.logexc(LOG, "Running interface command %s failed", cmd)

    def _bring_up_interface(self, device_name):
        cmd = ['netctl', 'restart', device_name]
        LOG.debug("Attempting to run bring up interface %s using command %s",
                  device_name, cmd)
        try:
            (_out, err) = util.subp(cmd)
            if len(err):
                LOG.warning("Running %s resulted in stderr output: %s",
                            cmd, err)
            return True
        except util.ProcessExecutionError:
            util.logexc(LOG, "Running interface command %s failed", cmd)
            return False

    def _bring_up_interfaces(self, device_names):
        for d in device_names:
            if not self._bring_up_interface(d):
                return False
        return True

    def _write_hostname(self, your_hostname, out_fn):
        conf = None
        try:
            # Try to update the previous one
            # so lets see if we can read it first.
            conf = self._read_hostname_conf(out_fn)
        except IOError:
            pass
        if not conf:
            conf = HostnameConf('')
        conf.set_hostname(your_hostn