Developer Journal: 02 May 2020

Posted on Sat 02 May 2020 in dev-journal

The goal for today is to clean up how site log files are stored

First, creating a subdirectory for each site in the /var/log/uwsgi/ folder will help keep things organized. This directory should be made when the site is created. The permissions will also need to be updated on the subdirectory so the conveyor user can write files to it.

First I'm going to create a couple of simple tests

The first will make sure that the create site calls the correct function to ensure the subdirectory is created.

def test_create_site_calls_create_log_subdirectory(self, 
        mock_create_log_subdirectory):
    mock_create_log_subdirectory.return_value = ""
    site = Site.query.get(1)
    site.site_type = SiteTypeEnum.static.value
    provisioner = Provisioner(
        '127.0.0.1',
        current_app.config['SSH_PRIV_KEY_FILE'])
    provisioner.create_site(site, "aoeu", "iso")
    mock_create_log_subdirectory.assert_called_once()
def test_create_log_subdirectory_commands(self):
    provisioner = Provisioner(
            '127.0.0.1',
            current_app.config['SSH_PRIV_KEY_FILE'])
    cmd = provisioner.get_log_subdirectory_command("test.test.com")
    expected = ["mkdir /var/log/uwsgi/test.test.com",
                "chown conveyor /var/log/uwsgi/test.test.com"]
    self.assertEqual(expected, cmd)

Then create the function that will actually do the work

def get_log_subdirectory_command(self, domain):
    path = '/var/log/uwsgi/{0}'.format(domain)
    commands = [
        'mkdir {0}'.format(path),
        'chown conveyor {0}'.format(path),
    ]
    return commands

Create the subdirectory on site deploys to keep backwards compatibility until a script is created to to this on all sites

if site.site_type is not SiteTypeEnum.static.value:
    # create log sub directory for site
    # TODO this is only needed until a script is created to
    # create this subdirectory for each site that been created on all
    # servers. Currently this will be done when a site is created,
    # but this allows for compatibility of the new site wsgi config
    # for logs.
    self.create_log_subdirectory(site.domain)
  • A further improvement will be to configure logs to consolidate after a period of time