Monday, August 31, 2020

Pyenv Commands Quick Reference

 Recently, I've been spending more time learning Python as it's the core language used in the framework that we'll be building on. Similar to using docker to containerize apps or vm's to isolate the app env from messing up the local m/c, python provide's various options to isolate the development environment. One such tool is pyenv. It's such an effective tool that makes life much easier, especially when you're working with multiple python projects that may require different dependencies and different versions of one or more python modules. 

Here, I've listed out most of the pyenv commands I used to work around different projects in my development env: 


- To list versions that match a pattern

$ pyenv install --list | grep " 3\.[678]"


- To install a specific version

$ pyenv install 3.8.2


- To uninstall a specific version

$ pyenv uninstall 3.8.2


- Installation Location

$ ls ~/.pyenv/versions


- To list all available python versions

$ pyenv versions


- To list all pyenv commands

$ pyenv commands


- To set a python version globally

$ pyenv global 3.8.2


- To set a python version locally

$ pyenv local 3.8.2


- To set a python version current shell

$ pyenv shell 3.8.2


- Creating virtual environments

$ pyenv virtualenv 3.8.2 <env_name>


- Activating pyenv env (if necessary)

$ pyenv activate <env_name>

$ pyenv deactivate


Monday, March 9, 2020

Running Jenkins docker image with local file system mapped

Step 1: Pull down the Jenkins Docker Image from DockerHub: 


       docker pull jenkins/jenkins:latest

Note: You'll get the latest Jenkins image from jenkins/jenkins tag and not the official jenkins repo. Official one at the time when I downloaded was still referring to 2.60.3 version, whereas the jenkins/jenkins one was using 2.224. If you use the older version, certain plugins may not load properly.

Step 2: Create a local volume


       docker volume create jenkins-data

Step 3: Run Jenkins docker image with this local volume mapped to the jenkin container's path. 


      docker run --name jenkins \
           --d \
           -p 49001:8080 \
           -v jenkins-data:/var/jenkins_home \
           jenkins/jenkins:latest

Note: If you run into the following error:

/usr/local/bin/jenkins.sh: line 25: /var/jenkins_home/copy_reference_file.log: Permission denied

You will then need to give the correct permissions to the local directory which will be mapped to the containers path.

      sudo chown 1000 jenkins-data

Finally, when the container starts, you can access Jenkins Home by going to http://localhost:49001
When you access it for the first time, it'll ask for the admin password to be entered, which can be found from the logs or from $JENKINS_HOME/secrets/initialAdminPassword file.

      docker exec jenkins bash -c 'cat $JENKINS_HOME/secrets/initialAdminPassword'

Sunday, January 10, 2016

PostgreSQL 9.3 tidbits


Just came across an article that listed some of the cool new features of postgresql database. I've listed out some of those that I liked and thought might be useful...

  • pg_isready - New command-line utility to test whether a server is ready to accept connections.
$ pg_isready -p 5432 -h localhost
localhost:5432 - accepting connections

  • \watch - Allows convenient re-execution of queries. 
postgres=# SELECT datname, query, usename FROM pg_stat_activity ORDER BY query_start DESC LIMIT 1; \watch 60

  • \conninfo - Displays current connection information

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".

  • \l - List Databases
postgres=# \l
                                  List of databases
        Name         |  Owner   | Encoding | Collate | Ctype |   Access privileges
---------------------+----------+----------+---------+-------+-----------------------
 arts_v6             | postgres | UTF8     | C       | C     |
 enact_recreate_test | postgres | UTF8     | C       | C     |
 postgres            | postgres | UTF8     | C       | C     |
 quarrysb_13_0_0     | postgres | UTF8     | C       | C     |
 template0           | postgres | UTF8     | C       | C     | =c/postgres          +
                     |          |          |         |       | postgres=CTc/postgres
 template1           | postgres | UTF8     | C       | C     | =c/postgres          +
                     |          |          |         |       | postgres=CTc/postgres
(6 rows)




Installation and Setup of PostgreSQL in Ubuntu


First, install postgresql and commonly used add-on's. 


> sudo apt-get install postgresql postgresql-contrib
 
If you plan to use the pgAdmin tool as the GUI front-end for your database, then execute 
the following command: 

> sudo apt-get install pgadmin3
 
The basic installation creates a "postgres" user as the root user to access PostgreSQL. 
But it doesn't setup an password. So we'll have to setup a password for this user.
 
> sudo -u postgres psql postgres 
 
Set a password for the "postgres" database role using the command: 
 
# \password postgres 
 
You're all set up to play around and start using PostgreSQL in your linux environment now!
 

Create a database by running "createdb" command:

> sudo -u postgres createdb
 
If you have an import sql script, you can load that by running 
 
> sudo -u postgres psql <  

You can also execute sql commands by simply starting up the sql console via 

> psql

followed by 
# create database 
... 
 
 
To allow connections to your database server, you'd probably have to make a minor change to 
your pg_hba.conf file. 
 
Replace "peer" with "md5" in the following line in your 
/etc/postgresql/9.3/main/pg_hba.conf file: 
 
# Database administrative login by Unix domain socket
local    all       postgres                           peer

Reload the configuration changes for it to be effective:

> sudo /etc/init.d/postgresql reload

To enable postgresql to listen across networks other than local, modify the listen_address 
in the following file:

/etc/postgresql/current/main/postgresql.conf [listen_address = '*']
 

Sunday, February 3, 2013

iTerm2 - Where are my colors?



I was badly missing my colors from iTerm console and this is how doldrum they looked in black/white regardless of what color profile I picked...

                                 
The boring one...          


 After some googling, found these 3 nice little exports that did the trick for me.
Just copy the following exports to your .profile file and voila! Your console comes back to life with beaming colors.. just the way you'd want it to.

export TERM=xterm-color
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad


The 'not so boring anymore' one...















Saturday, September 29, 2012

Git Headache Solved: 'Modified Files'


Pretty much all the dev team work on a mac environment except one! No rewards for guessing that its mine which differed from everyone else's. I occasionally use my Windows 7 box to do some development and commit to repo.
We opted to use git for this project and I have to say that the first few days were quite challenging to use git in the way its meant to be used. But after few hiccups we got the hang of it. I initially had some trouble setting up git on my windows m/c to behave the same way as on my mac. When I did a clone for a git repository a whole bunch of 'Modified' files showed up when I ran 'git status'. After some googling and tweaking the core.autocrlf setting to 'true/false/input', I got it working right. All the 'Modified Files' list disappeared. But recently I decided to look around for git clients for windows and installed git extensions and smartgit. But something changed and when I did a clone, the dreaded 'Modified Files' reared its head again! I thought "Ah! Thats a cakewalk. I know what to do" and started changing my core.autocrlf setting.. but nothing changed. When you run 'git config -l', you might see a bunch of core.autocrlf entries.. I ran the following to make sure every core.autocrlf entries are set to false:

> git config -l
> git config --global core.autocrlf false
> git config core.autocrlf false
> git config --system core.autocrlf false

Followed by
> git reset --hard
> git status

I still kept getting the 'Modified Files'. Strangely, when I ran the 'git status' from cygwin, it was fine. It showed no modified files. Cygwin git was 1.6.x version, whereas the one run from MingW was 1.7.4
After an invisible hand slapped the back of my head, I realized that I could run diff to see what the changes were.
> git diff 

This showed that the file contents were fine.. it was the mode that was the problem.
All the files showed the following:

old mode 100755  (rwx r-x r-x)
new mode 100644 (rw- r-- r--)

Not sure why it decided to do that, but changing the core.filemode solved this problem.
> git config core.filemode false

So when you run into a 'Modified Files' issue, make sure you check the setting for

core.autocrlf
core.safecrlf
core.filemode

Good Luck!

Friday, September 28, 2012

Hibernate Criteria Snippets



The very basic
session.createCriteria(Car.class);

session.createCriteria(Car.class)
.add(Restrictions.eq(“engine.code”, code))
.uniqueResult();

Collections: isEmpty(), size()
session.createCriteria(Student.class)
         .add(Restrictions.isEmpty(“courses”, 2));

Ignore case:
session.createCriteria(Student.class)
         .add(Restrictions.eq(“name”, name).ignoreCase());

session.createCriteria(Student.class)
         .add(Restrictions.ilike(“name”, name));


Wildcard:
session.createCriteria(Student.class)
         .add(Restrictions.like(“name”, “test%”));

session.createCriteria(Student.class)
         .add(Restrictions.like(“name”, “test”, MatchMode.START));


Logical Operations:
session.createCriteria(Student.class)
         .add(Restrictions.or(
                  Restrictions.and(
                           Restrictions.eq(“firstname”, fn), Restrictions.eq(“lastname”, ln)
                  ),
                  Restrictions.in(“subject”, subjects)
         ));

Projections & Transformers:
session.createCriteria(Student.class)
         .setProjection(Projections.projectionList()
                  .add(Projections.property(“name”).as(“studentName”))
                  .add(Projections.property(“section”).as(“section”)))
         .setResultTransformer(Transformers.aliasToBean(StudentDTO.class));


Aggregation:
session.createCriteria(Order.class)
         .setProjection(Projections.projectionList()
                  .add(Property.forName(“id”).group())
                  .add(Property.forName(“style”).count())
                  .add(Property.forName(“amount”).avg()));

SqlProjections:
String sql = “(select avg(o.amount) from Orders o where o.id= order_id) as avgAmount”;
session.createCriteria(Receipt.class)
         .createAlias(“orders”, “o”)
         .setProjection(Projections.projectionList()
                  .add(Projections.groupProperty(“o.id”))
                  .add(Projections.sqlProjection(
                           sql,
new String[]{“avgAmount”},
new Type[]{new BigDecimalType()})));


Conjunction & Disjunction:
Criteria criteria = session.createCriteria(Order.class);
Conjunction contact1Filter = Restrictions.conjunction();
      contact1Filter.add(Restrictions.eq("c1_firstname", c1fn));
      contact1Filter.add(Restrictions.eq("c1_lastname", c1ln));


Conjunction contact2Filter = Restrictions.conjunction();
      contact2Filter.add(Restrictions.eq("c2_firstname", c2fn));
      contact2Filter.add(Restrictions.eq("c2_lastname", c2ln));

Disjunction contact1Or2Filter = Restrictions.disjunction();
      contact1Or2Filter.add(contact1Filter);
      contact1Or2Filter.add(contact2Filter);

criteria.add(contact1Or2Filter);

... results in >>
 FROM Order WHERE 
  (c1_firstname AND c1_lastname)  -- conjunction 
     OR                           -- disjunction
  (c2_firstname AND c2_lastname)  -- conjunction