Category: Snippets

  • Django, psql & “permission denied” on migrate

    I got this error:

    psycopg2.ProgrammingError: permission denied for relation django_migrations
    

    … when I wanted to do a “python manage.py migrate”. This post had the solution. In short: You have to change the owner of the tables to the one specified in the Django configuration.

    This is how my script looks:

    #!/usr/bin/env bash
    echo "ALTER TABLE public.django_admin_log OWNER TO <new_owner>;" | psql -U <current_owner> <database>
    # ...
    
  • Firefox close tab buttons on mouse hover

    I used to have Firefox’s TabMix Plus addon installed. And the feature I missed most – surprisingly – is to have the “close tab” buttons appear on a tab when you hover the mouse over it.

    Googling a little bit told me how to bring it back:

    UPDATE 2024-08-24

    Forget everything you read here and go to this page: mattsch.com/2016/01/04/firefox-close-tab-button-on-hover

    It’s all you need 🙂 .

    For my personal reference (please still go to Matthias’ page, he’s the one who did the actual work):

    /* FF 113+ ONLY! */
    .tabbrowser-tab:not([selected]):not([pinned]):hover .tab-close-button {
        display: flex !important;
    }
    
  • Databases with dokku

    This is part 2 of a couple of blog posts about dokku, a amazing little Heroku clone.

    In the previous post I showed how to set up Dokku on a DigitalOcean droplet, and deployed a little hello-world container with a single git push. The reason why I wanted dokku thoug was the need of a database. As said – hosting comes cheap, databases usually come either expensive or with limited flexibility, or just too annoying configuration effort.

    Dokku is the perferct middle ground. Let’s see why.

    For me it was the existing postgres plugin which you can simply install and use. The whole process is incredibly easy, takes wbout two commands, and looks like this (let’s assume our “hello world” container uses a database):

    $ sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
    

    That’s it, again.

    $ dokku postgres:create hello-world
    
    dokku postgres:create hello-world
    Waiting for container to be ready
    Creating container database Securing connection to database
    =====> Postgres container created: hello-world
    =====> Container Information
           Config dir: /var/lib/dokku/services/postgres/hello-world/config
           Data dir: /var/lib/dokku/services/postgres/hello-world/data
           Dsn: postgres://postgres:bd6b0725d710bb5a662bb628eee787b1@dokku-postgres-hello-world:5432/hello_world
           Exposed ports: -
           Id: 785ef252c748ed85739d1d6ad375a1e1bd66e925ac79358e9ffaa30ab852d6c0
           Internal ip: 172.17.0.9
           Links: -
           Service root: /var/lib/dokku/services/postgres/hello-world
           Status: running
           Version: postgres:10.2
    
    $ docker ps
    
    CONTAINER ID   IMAGE                      COMMAND                  CREATED         STATUS         PORTS      NAMES
    cc99cccacf2c   dokku/hello-world:latest   "/bin/sh -c 'php-fpm…"   2 minutes ago   Up 2 minutes   80/tcp     hello-world.web.1
    785ef252c748   postgres:10.2              "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes   5432/tcp   dokku.postgres.hello-world
    [...]
    

    This creates a database container with postgres 10.2, as you can see. You can influence a lot of behavior by using environment variables, see the GitHub page for more info.

    Then you link the container to the running app:

    $ dokku postgres:link hello-world hello-world
    -----> Setting config vars
           DATABASE_URL: postgres://postgres:bd6b0725d710bb5a662bb628eee787b1@dokku-postgres-hello-world:5432/hello_world
    -----> Restarting app hello-world
    -----> Releasing hello-world (dokku/hello-world:latest)...
    -----> Deploying hello-world (dokku/hello-world:latest)...
    -----> Attempting to run scripts.dokku.predeploy from app.json (if defined)
    -----> No Procfile found in app image
    -----> DOKKU_SCALE file found (/home/dokku/hello-world/DOKKU_SCALE)
    =====> web=1
    -----> Attempting pre-flight checks
           For more efficient zero downtime deployments, create a file CHECKS.
           See http://dokku.viewdocs.io/dokku/deployment/zero-downtime-deploys/ for examples
           CHECKS file not found in container: Running simple container check...
    -----> Waiting for 10 seconds ...
    -----> Default container check successful!
    -----> Running post-deploy
    -----> Configuring hello-world.my-paas.for-myself.com...(using built-in template)
    -----> Creating http nginx.conf
    -----> Running nginx-pre-reload Reloading nginx
    -----> Setting config vars DOKKU_APP_RESTORE: 1
    -----> Found previous container(s) (14c349cb496d) named hello-world.web.1
    =====> Renaming container (14c349cb496d) hello-world.web.1 to hello-world.web.1.1535285386
    =====> Renaming container (cc99cccacf2c) serene_bassi to hello-world.web.1
    -----> Attempting to run scripts.dokku.postdeploy from app.json (if defined)
    -----> Shutting down old containers in 60 seconds
    =====> 14c349cb496d95cc4be1833f2e7f6ef2bef099a37c2a22cd4dcdb542f09bea0f
    =====> Application deployed:
           http://hello-world.my-paas.for-myself.com
    

    And done.

    What happened? You have now the environment variable $DATABASE_URL set in the hello-world app, that’s why the restart was necessary (which you can postpone, if you want, but you probably need it now, right?).

    Let’s check:

    $ docker exec -ti hello-world.web.1 /bin/sh
    
    [now in the container]
    
    # env | grep DATABASE
    DATABASE_URL=postgres://postgres:bd6b0725d710bb5a662bb628eee787b1@dokku-postgres-hello-world:5432/hello_world
    

    That’s it. Super easy. Now if you’re using Django, you could use kennethreitz/dj-database-url to automatically parse and use it, and you’re done. (Probably every framework has something similar, so just have a look).

  • Helm in a kops cluster with RBAC

    I created a K8S cluster on AWS with kops.

    I ran helm init to install tiller in the cluster.

    I ran helm list  to see if it worked.

    I got this:

    Error: configmaps is forbidden: User "system:serviceaccount:kube-system:default" \
        cannot list configmaps in the namespace "kube-system"
    

    That sucked. And google proved … reluctant. What I could figure out is:

    Causes

    • kops sets up the cluster with RBAC enabled (which is good)
    • helm (well, tiller) uses a standard role for doing things (which might be ok, at least it was with my stackpoint cluster), but in that case (for whatever reason) it did not have sufficient privileges
    • so we need to prepare some cluster admin roles for helm to use

    Fixes

    Just do exactly as it says in the helm docs 🙂 :

    • apply the RBAC yaml file which creates the kube-system/tiller service account, and binds this to the cluster-admin  role.
    • install helm with: helm init -service-account tiller

    Is that secure? Not so much. With helm you can still do anything to the cluster at all. I might get to this in a later post.

  • Mac three finger gestures in browsers

    Well, I love my three-finger-jump-to-top gesture in Firefox. It was gone.

    It took me about 2h to get it back by googling. And I blog so I don’t forget.

    Here’s where I should look next time straight away: System setting – Trackpad – rightmost tab – first setting.

    osx system settings for three finger swipes

  • Arch followup actions

    Updates:

    • 2022-06-23 | add networking, update packages, add GDM

    Overview

    Once you’ve installed Arch Linux, a couple of things are … nice. But very basic. Let’s get out of basic 😉 .

    Set up networking

    Just do the same as described in the installation post.

    Then, we set up IWD as backend for NetworkManger (NOTE: experimental!).

    ### FILE: /etc/NetworkManager/conf.d/wifi_backend.conf ###
    [device]
    wifi.backend=iwd
    

    Create user

    If you didn’t do it yet, create your user.

    useradd -m -G wheel USERNAME
    passwd USERNAME
    

    You should have sudo installed, so do this:

    # if you are really lazy and insecure
    echo "%wheel ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers.d/wheel
    
    # use this.
    echo "%wheel ALL=(ALL:ALL) ALL" >> /etc/sudoers.d/wheel
    

    Now log in as this user and check if you can become root by typing sudo -i.

    CLI packages

    Some CLI “apps”:

    pacman -S ack dnsmasq git ripgrep vim zsh git base-devel man-db man-pages
    

    Install yay (AUR helper)

    Remember: Do this as your “normal” user!

    mkdir /tmp/yay
    cd /tmp/yay
    
    # from here stolen from yay's github page:
    # https://github.com/Jguer/yay#source
    git clone https://aur.archlinux.org/yay-bin.git
    cd yay-bin
    makepkg -si
    

    Install UI

    And here it goes for UI and UI-related things:

    pacman -S gnome gnome-extra gnome-control-center \
              cups network-manager-applet \
              firefox
    

    Now some AUR packages:

    yay -S vscodium-bin
    

    Configurations

    For network manager, I prefer dnsmasq as the tool of choice, especially when using VPN connections:

    ### FILE: /etc/NetworkManager/conf.d/dns.conf ###
    [Main]
    dns=dnsmasq
    

    Enable services

    # enable
    systemctl enable iwd
    systemctl enable NetworkManager
    systemctl enable org.cups.cupsd
    
    # start
    systemctl start iwd
    systemctl start NetworkManager
    systemctl start org.cups.cupsd
    
    # to boot into GNOME
    systemctl enable gdm
    

    Some troubleshooting tricks

    Gnome-terminal does not start

    Set the locale, using “gnome control center”, and then re-login. (Source)

  • Python & Visual Studio code

    The official python plugin claims that the interpreters of Pipenv are automatically found.

    They are not.

    At least not on my machine.

    Here’s how you set them.

  • Arch linux + yubikeys

    To use “ykman” for arch linux, you do this:

    $ yaourt -S yubikey-manager pcsclite                 # THESE PACKAGES
    [...]
    $ systemctl start pcscd                              # START SERVICE
    $ ykman info                                         # TEST
    Device type: YubiKey NEO
    Serial number: 0123456
    Firmware version: 3.4.3
    Enabled connection(s): OTP+U2F+CCID
    
    Device capabilities:
        OTP:    Enabled
        U2F:    Enabled
        CCID:   Enabled
        OPGP:   Enabled
        PIV:    Enabled
        OATH:   Enabled
    $ _
    

    Sounds easy? Still had to google the things.

  • Ugly ligatures in Linux

    Unfortunately boohomil went off grid. I still haven’t replicated his config fully. And it still sucks.

    One more step was fixing those super-ugly ligatures in Linux. Works at least in LibreOffice (just restart the app to see changes).

  • crontab and nano

    Ever used update-alternatives to switch everything to vim and … crontab -e still used nano?

    Well, I had this. I found the answer:

    select-editor