#0

AWS postgres SSH TUNNEL

aws
Posted on: August 27th 2020, 2:17:30 pm

In Shell run

ssh -N -i ec2-bastion.pem -L localhost:5432:database-1.cwlfuyniopoi.us-west-1.rds.amazonaws.com:5432 ubuntu@ec2-34-217-99-999.us-west-1.compute.amazonaws.com

then in another shell

psql -U postgres -h localhost -p 5432 -d test-db
#1

Fixing missing action center in windows 10

windows
Posted on: June 21st 2020, 2:54:06 pm

Helpful link https://www.winhelponline.com/blog/start-and-taskbar-stuck-windows-10-fix-shellexperiencehost/#reinstall_start

  • Open powershell in admin mode
win + x
choose powershell admin
  • Open process manager via ctrl + shift esc

locate start and end task

  • Type in power shell
Get-AppxPackage Microsoft.Windows.StartMenuExperienceHost | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}


Get-AppxPackage Microsoft.Windows.ShellExperienceHost | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

Get-AppxPackage Microsoft.Windows.Cortana | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

#2

Mount Drive in WSL 2

windows
Posted on: June 9th 2020, 10:19:49 am

Mounting

$ sudo mkdir /mnt/z
$ sudo mount -t drvfs Z: /mnt/z -o metadata,uid=1000,gid=1000,umask=22,fmask=111

Unmounting

$ sudo umount /mnt/z
#3

AWS MFA to session token

aws
Posted on: May 12th 2020, 11:30:30 am

Add credentials to ~/.aws/credentials

vim ~/.aws/credentials


[org-dev]
aws_access_key_id=SAMPLEAWSACCESSKEYID
aws_secret_access_key=SAMPLEAWSSECRETACCESSKEY

[mfa]]
aws_access_key_id=
aws_secret_access_key=
aws_session_token=

Run the command

aws --profile org-dev sts get-session-token --serial-number arn:aws:iam::9310312321312321:mfa/User --token-code 372049

Response

{
    "Credentials": {
        "AccessKeyId": "AccessKeyId",
        "SecretAccessKey": "SecretAcccessKey",
        "SessionToken": "SESSTION-TOKEN-LONG_KEY",
        "Expiration": "2020-05-12T23:26:08Z"
    }
}

Copy response to ~/.aws/credentials in [mfa] section

Check if credentials works

aws --profile mfa s3 ls
#4

Fixing cortana search in windows 10

Posted on: April 2nd 2020, 6:33:05 am

This will disable bing integration thus fixing the blank search results. Use regedit to fix.

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Search

Add or update record

  1. Add or update record BingSearchEnabled
  2. Choose D-WORD (32-bit) Value and 0 as value
  3. Restart PC.

Source: https://www.windowscentral.com/windows-search-down-many-showing-blank-box-instead-search-results

#5

Ubuntu sync cron time

Posted on: January 6th 2020, 4:48:34 pm
# dpkg-reconfigure tzdata
# service cron restart
# timedatectl

Source

#6

Mailhog for dev setup

Posted on: December 23rd 2019, 4:02:05 pm
$ docker run -d -p 1025:1025 -p 8025:8025 --name mailhog mailhog/mailhog

Open browser in http://localhost:8025/#

Update settings of apps to use localhost smtp

# Django
EMAIL_HOST='127.0.0.1'
EMAIL_PORT=1025
EMAIL_HOST_USER=''
EMAIL_HOST_PASSWORD=''
EMAIL_USE_TLS=False

# nodemailer
const transporter = nodemailer.createTransporter({
host: '127.0.0.1,
port: '1025',
secure: false})
#7

Remove docker container logs

Posted on: December 4th 2019, 1:34:36 am

When a docker container is up for a long time its logs are sometimes not deletd. The size of the log file can be more than 100gb.

  • Check which container has the biggest size
$ sudo du -h --max-depth=2 /var/lib/docker/containers/

this will check which container has a large size

  • Grab container id
$ CONTAINER=<paste id here>
  • Grab log file path
$log=$(docker inspect -f '{{.LogPath}}' $CONTAINER)

  • Empty log file
$ truncate -s 0 $log
#8

Using generator for django queryset

Posted on: November 1st 2019, 3:20:42 pm
from book.models import Book

for book in Book.objects.all().iterator()
    book.delete()

Why iterator?

Python wont load all the results in memory. This is used for memory management.

#9

Remove Zombie Network

Posted on: October 18th 2019, 12:27:13 pm
$ export DOCKER_NETWORK=aloha_network
$ for i in ` docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' $DOCKER_NETWORK`; do docker network disconnect -f $DOCKER_NETWORK $i; done;
$ docker network prune
#10

Reverse proxy s3 bucket

Posted on: September 23rd 2019, 6:35:17 am
location /aws/ {
    rewrite /aws/(.*) /$1 break;
    proxy_pass https://{URL}.s3-ap-southeast-1.amazonaws.com;
    proxy_set_header Host '{URL}.s3-ap-southeast-1.amazonaws.com';
}
#11

Convert Markdown to HTML in DRF

Posted on: May 12th 2019, 1:11:22 pm

Save the markdown in text in django models then output it as HTML in api

# models.py
class Page(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True, max_length=255)
    short_description = models.CharField(max_length=255)
    # Input Markdown then render it as html
    content = models.TextField(blank=True)
    is_active = models.BooleanField(default=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

#serializers.py
class PageSerializer(serializers.ModelSerializer):
    categories = serializers.SlugRelatedField()
    banner = serializers.ImageField(max_length=None)
    content = serializers.SerializerMethodField()

    class Meta:
        model = Page
        fields = (
            'title',
            'slug',
            'short_description',
            'content',
            'date_created',
            'date_updated',
            'categories',
            'banner',
        )

    def get_content(self, obj):
        '''Return the markdown content of obj as html format'''
        import markdown2

        # Enable code blocks
        content = markdown2.markdown(
            obj.content,
            extras=['fenced-code-blocks',])
        return content