search

Friday, October 28, 2016

How to set custom time and date on virtual machine

If your virtual machine is managed by VirtualBox, there are 2 services which synchronize date and time. The first step is to stop them from restoring the time:
sudo service systemd-timesyncd stop
sudo service vboxadd-service stop
After that you can set time with date command, for example:
sudo date +%T -s "00:12:00"
or
date -s "27 OCT 2016 00:12:00"
or
date +%Y%m%d -s "20161027"

systemd: Create a service

Example of the service configuration file `selenium_hub.service`:
[Unit]
Description=Selenium hub

[Service]
Environment=DISPLAY=:99
ExecStart=/bin/bash -c 'java -jar /opt/selenium/selenium-server-standalone.jar -role hub'
User=root

[Install]
WantedBy=multi-user.target
Place this file to /etc/systemd/system/ (symlinking won't work) and refresh available services:
sudo systemctl daemon-reload
Now it is possible to start the service with command:
sudo service selenium_hub status
You can also make it start when a system starts by executing one of the following commands:
sudo chkconfig selenium_hub on
sudo systemctl enable selenium_hub.service

Wednesday, October 19, 2016

Overwrite response body with mitmproxy

It is possible to run python script with mitmproxy. For example:
mitmproxy -s "modify_response_body.py"
The modification process is very simple. Take a look at the modify_response_body.py code:
from mitmproxy.models import decoded


def response(context, flow):
    if "site.custom.min.js" in flow.request.path:
        with decoded(flow.response):  # automatically decode gzipped responses.
            flow.response.content = flow.response.content.replace(
                "surfly.com",
                "sitesupport.net")
The script will look for a request with path that contains site.custom.min.js and replace all occurences of surfly.com with sitesupport.net

mitmproxy and websocket connections

mitmproxy does not really supports websocket connections. However, there is a workaround: it is possible to just ignore them. For example all your websocket conections are encrypted and go to the special subdomain:
mitmproxy --ignore ws.sitesupport.net:443

Thursday, October 13, 2016

Slow performance and no sound with pulseaudio-equalizer

I installed pulseaudio-equalizer on my laptop, but after 3 days my laptop became slow and there wes no sound at all. It happened because of the pulseaudio-equalizer package. Even if you remove it, the problem won't go because it adds some configuration to the pulseaudio itself and doesn't remove it.
Open pulseaudio configuration file, find pulseaudio-equalizer section and remove it completely:
sudo nano ~/.pulse/default.pa
The section should look like this:
### Generated from: pulseaudio-equalizer
load-module module-ladspa-sink sink_name=ladspa_output.mbeq_1197.mbeq master=alsa_output.pci-0000_05_00.0.analog-stereo plugin=mbeq_1197 label=mbeq #control=-0.2,-0.2,-0.2,-0.2,3.5,3.5,3.5,3.5,3.5,3.5,3.5,2.5,2.5,0.0,0.0
set-default-sink ladspa_output.mbeq_1197.mbeq
set-sink-volume alsa_output.pci-0000_05_00.0.analog-stereo 65536
set-sink-mute alsa_output.pci-0000_05_00.0.analog-stereo 0
### END: Equalized audio configuration
Reboot your computer and enjoy restored performance and sound!

Monday, October 10, 2016

fedora: Install latest VirtualBox

The latest virtualbox can be installed from the Oracle repository. First of all we need to install oracle repository:
cd /etc/yum.repos.d/
sudo wget http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo
Install dependencies:
sudo dnf install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-PAE-devel dkms
Install virtualbox. Note: pointing 5.1 is important as at the moment that is how it is called in the oracle's repository:
sudo dnf install VirtualBox-5.1
You might need to rebuild kernel modules with command:
sudo /usr/lib/virtualbox/vboxdrv.sh setup
And add current user to the xboxusers group:
sudo usermod -a -G vboxusers user_name
I had a problem reconfiguring kernel modules on my laptop. The error wasn't indicating anything specific. The problem was in the BIOS settings. Secure Boot should be disabled to allow module reconfiguration.

Monday, October 3, 2016

Speeding up Django tests: Disable migrations during tests

Running all migrations before the testrun can take a lot of time. For some reason, they are very slow. Running 50 migrations could easily take more than 1 minute! Fourtunately, disabling migrations is very easy. Just add the following code to the test's settings file:
# Disable migrations when running tests
MIGRATION_MODULES = {
    app[app.rfind('.') + 1:]: 'your_app.migrations_not_used_in_tests'
    for app in INSTALLED_APPS
}
Some of our migrations add data to the database and tests expect this data to be there. To fix that problem we will need to overwrite test runner. Add to the settings file:
TEST_RUNNER = 'your_app.test_runner.CustomDiscoverRunner'
Then create test_runner.py in the project root with the following content:
from django.test.runner import DiscoverRunner

from myapp.management.commands._setup_functions import create_groups


class CustomDiscoverRunner(DiscoverRunner):
    def setup_databases(self, **kwargs):
        result = super(CustomDiscoverRunner, self).setup_databases(**kwargs)
        create_groups()
        return result
After recreating test database directly from the models (what is really fast), test runner will execute create_groups() function which populates database with all necessary data.