Search in this blog

Wednesday, July 5, 2017

if...elif...else in Python


That’s the difference in Python between an if...elif...else and a series of ifs:
if a:
    do this
elif b:
    do that
elif c:
    do something else
else:
    print "none of the above"
is equivalent to (exactly the same as) this:
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
As you can see, the elif...else notation allows you to write the same logic (the same control flow) without repeating the a condition over and over again.

Copied from here: https://www.codecademy.com/en/forum_questions/51684a3d4ce76309b4001b9c

Thursday, June 22, 2017

Tag and release last updates on repository to pypi

Tag and release last updates on repository to pypi.

At first GPG key pair should be created. Look for previous post about it.

Checkout to required branch:
$ git checkout master
Create signed version tag:
$ git tag -s 0.1.13
Verify tag that was created:
$ git tag -v 0.1.13
Check list of git remotes:
$ git remote
gerrit
origin
Push tag to gerrit remote:
$ git push gerrit 0.1.13

Generate GPG key pair for git commits

Use command:
$ gpg --gen-key
Select default options (or specify other).

  • Select what kind of key you want: (1) RSA and RSA (default).
  • RSA keys may be between 1024 and 4096 bits long.
  • Please specify how long the key should be valid: 0 = key does not expire.

Then enter your real name, email address and comment.
Then we need to generate a lot of random bytes for entropy. Load computer with disk operations to provide random number generator a better chance to gain enough entropy.
Wait while public and secret keys will be created.

List keys:
$ gpg --list-keys
/home/username/.gnupg/pubring.gpg
-----------------------------------
pub   2048R/9DA4A3D1 
...

Configure the key before using it for single repository, or globally if you are going to use the same key for every repository:
$ git config user.signingkey 9DA4A3D1 
$ git config --global user.signingkey 9DA4A3D1

Done.

Wednesday, March 22, 2017

How to get IP address of specified interface in bash scripting?

There is a one-line pipeline of commands to get IP address assigned to a specified interface. For example to eth0:
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

Tuesday, February 7, 2017

Create a dummy file of specified size

If you need to create a big file of specified size for testing purposes (for testing Glance or Cinder for example), you can follow next few steps:

  • Install fallocate tool from util-linux
    $ sudo apt-get install util-linux
  • Create your dummy file of specified size. Let's do a file of 1Gb size
    $ fallocate -l 1G dummy.iso
Then you can use it, for example upload to Glance:
$ glance image-create --name dummy.iso --disk-format iso --container-format bare --file dummy.iso

Friday, January 20, 2017

Create SSH tunnel to the virtual server running on hypervisor

To create SSH tunnel to the virtual server running on hypervisor:
ssh -f -N -L 5001:10.100.15.5:5000 us1234.hypervisor.lab.net
Where:
5001 - source port number (port on local machine)
5000 - destination port (port on virtual server)
us1234.hypervisor.lab.net - hypervisor domain address

Opening http://localhost:5001/ in browser actually will open 10.100.15.5:5000 via ssh tunnel for you.

Friday, January 6, 2017

How to create and manage python virtualenv?

Create, activate and deactivate virtualenv
cd my_project_folder

virtualenv .venv
. .venv/bin/activate

deactivate