Sunday, July 22, 2018

Django dyanimic url for employees or users

Let us say you are creating a site and you want your users to sign up and you want them to redirected once they log in. Below are some url formats for some well known sites.
linkedin
https://www.linkedin.com/in/<username>/
facebook
https://www.facebook.com/<username>/
How can we do that?
It is extremely inefficient to have an html page created for each user and it is a very heavy load on your site.
How to achieve this?
By default django and allauth redirect the logged in user to domain/profile so let us use that to do some magic. Here is my urls.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.contrib import admin
from django.urls import path, include
from . import views as core_views
from stars import views as star_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', core_views.welcome, name="welcome"),
    path('settings/', core_views.settings, name='settings'),

    # allauth
    path('accounts/', include('allauth.urls')),

    # stars
    path('profile/', star_views.profile, name="profile"),
    path('<slug:pid>/', star_views.rprofile, name='rprofile'),
]

Concentrate on the line 15,16. So I am letting the django/allauth redirect the url to domain/profile.html and then use the star_views.profile to redirect the request to star_views.rprofile and that is done like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.shortcuts import render
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect
##Create your views here.

def profile(request):
    site = 'http://127.0.0.1:8000'
    username = request.user.username
    url = f'{site}/{username}'
    return HttpResponseRedirect(url)

def rprofile(request, pid):
    u = User.objects.get(username=pid)
    if u:
        return render(request, 'profile.html')

Now every user has his own url which will be easy to share.

Django Allauth signal to trigger something

So I am still testing the waters of django and I am still like a baby swimmer with swim tires. I had some trouble in using django allauth signals to do something.
Django 2.0
Django allauth
All in a virtualenv.
So here is how you use signals trigger some action. Go to https://github.com/pennersr/django-allauth/blob/master/docs/signals.rst to checkout the signal allauth emits at various stages. I am interested in the signal which gets dispatched after the user signs up. I am using email+password to signup and signin.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.db import models
import time
from django.contrib.auth.models import User
from allauth.account.signals import user_signed_up, password_set
from django.dispatch import receiver, Signal

# Create your models here.
@receiver(user_signed_up)
def employeeID(sender=User, **kwargs):
    old_username = kwargs['user']
    user = User.objects.get(username = old_username)
    user.username = str(time.time()).split('.')[0]
    user.save()

1-5 import modules.
8. mention the signal that we want to use.
as per the allauth documents mentioned in the https://github.com/pennersr/django-allauth/blob/master/docs/signals.rst this signal contains the following.
user_signed_up = Signal(providing_args=["request", "user"])
So the entire info received from the signal is passed on to the funtion at line 9 as **kwargs.
10. extract the signedup user's username.
11. query the database for the data of that user.
12. change the username to some random number.
13. save the changes to the database.

So, when we are using email+password only to signup and signin, allauth still creates a username='email ID' of the user (without the @email.com, so if i signed up with mail@email.com then my username will be mail.) So I thought I will give my own username and it worked.

Thursday, June 28, 2018

Upgrading your vcenter server made easy with Hybrid Linked Mode

So whenever you upgrade an environment you must start with HCL [hardware compatibility list]. Let us pretend that you want to go from vsphere 6.5 to 6.7 without any downtime. hmm... yes you are asking for a pony and thanks to vmware you have it too.
I always say that the first point of contact should be upgrade first because it will be backward compatible of what lies beneath. Anyway assuming that you made sure that the rest of your stuff like version of your NSX, VRA is all taken care of. You have to then upgrade your
vcenter>esxi>vm tools>vm hardware.
Get a new vcenter 6.7 appliance ready.
Create HLM between the old and the new vcenter.
Decommission the old one.
Yes, that  is it. If you are not satisfied with this high level plan then there are so many bloggers wanting to be at the yearly top 100 virtualization blog list (and its awesome) by http://vsphere-land.com (http://vsphere-land.com/news/top-vblog-2017-full-results.html) and they have detailed posts on how to do hybrid linked mode. It is easy and you dont have to pull out any hair.
I still do recommend the old ways of having a plan B as backup. Yes take a backup of your old vcenter before you get on with this plan/task.
What if you have 2 vcsa already in linked mode and you want to retain the networking information of them?
Let us say that you have vcsa1 linked with vcsa2.

  1. Decommission the vcsa2 from linked mode with vcsa1.
  2. Shutdown the vcsa2, disable the network adapter
  3. get your new vcsa with your newer desired version and assign the vcsa2 networking details (hostname, ip..) and join the linked mode with vcsa1.
  4. Make sure all is well and they are in sync.
  5. Decommission the vcsa1
  6. deploy the newer versioned vcsa and assign vcsa1 networking details to it.
congratulate yourself.

Saturday, May 12, 2018

getting it on with docker

So I use centos for most of experiments, lab work at home. I just have this love hate relationship with it i simply cant explain or resist. Needed get the docker, docker swarm, docker compose to work on cent os.
Optionally please set up vmware tools on your centOS 7. I recommend it.
So here is how I set it up.


1
2
3
yum install epel-release # get the yum repository installed
yum install docker
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose # It installs the docker composer of a version which is needed for the docker to work with. 
lines
1. install the epel repository
2. install docker which will also get you the docker swarm
3. installs the docker compose of a version which is needed for the docker version that you installed at line 2.
Get the visual studio code if you do not already have it

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
yum check-update
sudo yum install code
you will notice that you cannot just run docker-compose because you will get permission denied error. So let us do this final bit.
1
2
chown root /usr/local/bin/docker-compose
chmod 777 /usr/local/bin/docker-compose
We are not done yet.

1
2
systemctl enable docker
service docker start
line 1. enable the docker service to start at boot up.
line 2. start the docker service manually for now.

Wednesday, May 2, 2018

Get that damn VMware tools working on centos 7

So I have realized that even though virtualbox is more suited for devops activities like vagrant, docker, container, kubernetes etc., I still somehow like vmware workstation; may be because I just like VMware since I have doing VMware stuff from a very long time. I just liked the grouping of VMs, folder, tabs and more.
I keep hitting a small hurdle and that is getting the VMware tools installed on it. I am currently using centos for jenkins, docker, kubernetes, vagrant, openstack and more. So here is a just a reminder for the future me to just throw these lines at the terminal (preferably as a root user).

UNAME=$(uname -r)
yum install g++ gcc make kernel-headers kernel-devel-${UNAME%.*} -y

  1. Then you can just open the mounted iso in a terminal.
  2. copy the archive to a different system folder.
  3. untar it.
  4. cd into the unarchived folder.
  5. run the perl installer of vmware tools.