Example #1
0
def _write_config(server, state):
    """Write the configuration file."""
    dhcpd_config, interfaces_config = state.get_config(server)
    try:
        sudo_write_file(server.config_filename,
                        dhcpd_config.encode("utf-8"),
                        mode=0o640)
        sudo_write_file(
            server.interfaces_filename,
            interfaces_config.encode("utf-8"),
            mode=0o640,
        )
    except ExternalProcessError as e:
        # ExternalProcessError.__str__ contains a generic failure message
        # as well as the command and its error output. On the other hand,
        # ExternalProcessError.output_as_unicode contains just the error
        # output which is probably the best information on what went wrong.
        # Log the full error information, but keep the exception message
        # short and to the point.
        maaslog.error(
            "Could not rewrite %s server configuration (for network "
            "interfaces %s): %s",
            server.descriptive_name,
            interfaces_config,
            str(e),
        )
        raise CannotConfigureDHCP(
            "Could not rewrite %s server configuration: %s" %
            (server.descriptive_name, e.output_as_unicode))
Example #2
0
 def test_can_write_file_in_development(self):
     filename = get_maas_data_path("dhcpd.conf")
     contents = factory.make_bytes()  # Binary safe.
     mode = random.randint(0o000, 0o777) | 0o400  # Always u+r.
     sudo_write_file(filename, contents, mode)
     self.assertThat(filename, FileContains(contents))
     self.assertThat(os.stat(filename).st_mode & 0o777, Equals(mode))
Example #3
0
def configure(servers, peers, offset):
    """Configure the local NTP server with the given time references.

    This writes new ``chrony.chrony.conf`` and ``chrony.maas.conf`` files,
    using ``sudo`` in production.

    :param servers: An iterable of server addresses -- IPv4, IPv6, hostnames
        -- to use as time references.
    :param peers: An iterable of peer addresses -- IPv4, IPv6, hostnames -- to
        use as time references.
    :param offset: A relative stratum within MAAS's world. A region controller
        would be 0 and a rack controller would be 1.
    """
    ntp_maas_conf = _render_ntp_maas_conf(servers, peers, offset)
    ntp_maas_conf_path = get_tentative_data_path("etc", _ntp_maas_conf_name)
    sudo_write_file(
        ntp_maas_conf_path,
        ntp_maas_conf.encode("utf-8"),
        mode=0o644)
    ntp_conf = _render_ntp_conf(ntp_maas_conf_path)
    ntp_conf_path = get_tentative_data_path("etc", _ntp_conf_name)
    sudo_write_file(
        ntp_conf_path,
        ntp_conf.encode("utf-8"),
        mode=0o644)
Example #4
0
    def test_calls_atomic_write_dev_mode(self):
        patch_popen(self)
        patch_dev(self, True)

        path = os.path.join(self.make_dir(), factory.make_name("file"))
        contents = factory.make_bytes()
        sudo_write_file(path, contents)

        called_command = fs_module.Popen.call_args[0][0]
        self.assertNotIn("sudo", called_command)
Example #5
0
    def test_calls_atomic_write(self):
        patch_popen(self)
        patch_sudo(self)
        patch_dev(self, False)

        path = os.path.join(self.make_dir(), factory.make_name('file'))
        contents = factory.make_bytes()
        sudo_write_file(path, contents)

        self.assertThat(
            fs_module.Popen,
            MockCalledOnceWith([
                'sudo', '-n',
                get_library_script_path("maas-write-file"), path, "0644"
            ],
                               stdin=PIPE))