sobota, 29 kwietnia 2017

Run Docker Commands without Sudo

1. curl -sSL https://get.docker.io/ubuntu/ | sudo sh
or apt-get install docker docker.io

2. sudo usermod -a -G docker $USER

3. sudo service docker restart

4. logout

output:

marek@ubuntu:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

how to list and delete users in windows cmd terminal

C:\WINDOWS\system32>net user

Konta użytkowników dla \\LAPTOP-UCG24LRB

-------------------------------------------------------------------------------
Administrator            defaultuser0             Gość
Konto domyślne           Marek                    SvcCOPSSH
SvcCWRSYNC



C:\WINDOWS\system32>net user SvcCWRSYNC /delete
Polecenie zostało wykonane pomyślnie.

vagrant problem - kernel_require.rb:54:in `require': cannot load such file

issue:
C:\Users\Marek
λ vagrant
C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- vagrant-share/helper/api (LoadError)
        from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-share-1.1.7/lib/vagrant-share/activate.rb:244:in `<encoded>'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-share-1.1.7/lib/vagrant-share/activate.rb:16:in `RGLoader_load'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-share-1.1.7/lib/vagrant-share/activate.rb:16:in `<top (required)>'
        from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-share-1.1.7/lib/vagrant-share.rb:23:in `block in <class:Plugin>'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/cli.rb:75:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/cli.rb:75:in `block (2 levels) in help'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/registry.rb:49:in `block in each'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/registry.rb:48:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/registry.rb:48:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/cli.rb:69:in `block in help'
        from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/optparse.rb:917:in `initialize'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/cli.rb:57:in `new'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/cli.rb:57:in `help'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/cli.rb:32:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/environment.rb:308:in `cli'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/bin/vagrant:127:in `<main>'


Solution:

C:\Users\Marek
λ vagrant plugin install vagrant-share --plugin-version 1.1.8
Installing the 'vagrant-share --version '1.1.8'' plugin. This can take a few minutes...
Fetching: vagrant-share-1.1.8.gem (100%)

piątek, 28 kwietnia 2017

docker laboratory

http://labs.play-with-docker.com/

if you want to test your app / deployment you can use it - very handy and use full tool.

czwartek, 27 kwietnia 2017

sed handy examples

example file:

-> cat test.file
1 file
2 line
3 lin
4 l
5
#6 comment

1. delete first line from file

sed -e '1d' test.file
bunny-> sed -e '1d' test.file
2 line
3 lin
4 l
5
#6 comment


2. delete lines 1,2,3 from file
sed -e '1,3d' test.file
bunny-> sed -e '1,3d' test.file
4 l
5
#6 comment

3. delete lines started with '#'
sed -e '/^#/d' test.file
bunny-> sed -e '/^#/d' test.file
1 file
2 line
3 lin
4 l
5

4. change char to string
bunny-> sed -e 's/l$/another line/' test.file
1 file
2 line
3 lin
4 another line
5
#6 comment

5. insert double/single spaces between lines
ansible-> sed 'G;G' test.file - for double
ansible-> sed G test.file - for single
1 file


2 line


3 lin


4 l


5


#6 comment


7 l


8 l


6. 

środa, 26 kwietnia 2017

check if docker was killed by OOM error

docker inspect -f '{{.Config.Hostname}} {{.State.ExitCode}} {{.State.OOMKilled}}' $(docker ps -q)

output:
crm-prd-n1-3 0 false
crm-prd-n1-2 0 false
crm-prd-n1-1 0 false

Menu build in python with KeyboardInterrupt exception

import sys
import os
import requests

def print_menu():

    print 30 * "-", "MENU", 30 * "-"
    print "1. Call Date"
    print "2. Check logged users"
    print "3. GET Request"
    print "4. Exit"
    print 67 * "-"

loop = True
try:
      while loop:
            print_menu()
            choice = input("enter your choice [1-4]: ")
            if choice == 1:
                  x = os.system('date')
                  print x
            elif choice == 2:
                  print "logged uers"
                  os.system('who')
            elif choice == 3:
                  address = 'https://google.com'
                  print "GET Request of {}".format(address)
                  req = requests.get(address)
                  print(req.encoding)
                  print(req.status_code)
            else:
                  print "exit"
                  sys.exit()
except KeyboardInterrupt:

wtorek, 25 kwietnia 2017

how to run nginx on docker with index page on local resource

docker run -d -v /var/www/html:/usr/share/nginx/html/:ro -p 80:80 nginx

root@bunny:~# docker inspect 40c5179cabef | grep -i nginx
"Path": "nginx",
"/var/www/html:/usr/share/nginx/html/:ro"
"Destination": "/usr/share/nginx/html",

analyze short strings – python

zd = "There are 123 apples"
alphas = 0
digits = 0
spaces = 0
for i in zd:
if i.isalpha():
alphas += 1
if i.isdigit():
digits += 1
if i.isspace():
spaces += 1
print 30*"-="
#marked string after we change it by separate string in quote
print("original string is: \"{}\"".format(zd))
print("there are {} characters".format(len(zd)))
print("there are {} alphas characters".format(alphas))
print("there are {} digits ".format(digits))
print("there are {} spaces.".format(spaces))
output:

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
original string is: "There are 123 apples"
there are 20 characters
there are 14 alphas characters
there are 3 digits
there are 3 dpaces.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
string formatting
height: 189.50 cm
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
hex: 12c
0x12c
100101100
454
3.000000e+05
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Python regexp example

import re
exampleString = '''My Name is Marek Borkowski and i have 34 years old'''
ages = re.findall(r'\d{1,3}',exampleString)
print(ages)
names = re.findall(r'[A-Z][a-z]*',exampleString)
print(names)

ansible-> python regexp.py
['34']
['My', 'Name', 'Marek', 'Borkowski']

Platform-independent python script

import os
if (os.name == “posix”):
    print(os.system(‘ls -ltr’))
elif(os.name == “nt”):
    print(os.system(‘dir’))
else:
    print(“unknown os”)

poniedziałek, 10 kwietnia 2017

awk handy examples

Testing input:
Marek
Testing
Hello
TesT
123

1. All lines include string 
marek-> awk '/Test/ {print}' test.txt
Testing 1
123 Testing

2. All lines include numbers
marek-> awk '/[0-9]/ {print}' test.txt
Testing 1
123 Testing

3. All lines started with numbers

marek-> awk '/^[0-9]/ {print}' test.txt
123 Testing

4. All lines ended with numbers

marek-> awk '/[0-9]$/ {print}' test.txt
Testing 1


5. Print if in first column 123 will be found

marek-> awk '{ if($1 ~ /123/) print }' test.txt
123 Testing
123 Marek

6. Print if in second column awk found numbers

marek-> awk '{ if($2 ~/[0-9]/) print }' test.txt
Testing 1
Marek 1983


7. Grep Lines in file consist string

marek-> grep -i test test.txt
Testing 1
TesT
123 Testing

[~/dev/bash/awk]
marek-> grep -i test test.txt | awk '/[0-9]/{ print }'
Testing 1
123 Testing


8. Delete duplicate lines from file

[~/dev/bash/awk]
ansible-> cat file.txt
1
1
1
1
1
2
2
2
2
2
13
[~/dev/bash/awk]
ansible-> awk '!x[$0]++' file.txt
1
2
13
[~/dev/bash/awk]


9. print lines number - like wc

marek-> awk '{print NR-1 " " $0}' /etc/passwd
0 root:x:0:0:root:/root:/bin/bash
1 daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
2 bin:x:2:2:bin:/bin:/usr/sbin/nologin


10. sed vs. awk string replacement

marek-> sed 's/Marek/Wiktor/g' test.txt
Wiktor
Testing 1
Hello:World
TesT
123 Testing
123 Wiktor
Wiktor:1983
OMG:Test

marek-> cat test.txt | awk '{gsub("Wiktor","Marek"); system("echo " $0) }'
Marek
Testing 1
Hello:World
TesT
123 Testing
123 Marek
Marek:1983
OMG:Test



niedziela, 9 kwietnia 2017

python format vs python classic way output

def f(x,y):
       print("you called f({},{})".format(x,y))
       print("you called f(x,y), with a value x = "+str(x) + "and y = " + str(y))
       print("x * y = " + str(x*y))

f(4,2)

środa, 5 kwietnia 2017

check docker OOM state via python + paramiko

import sys
import time
import select
import paramiko
import time

hosty = ['app01.prod.crm.polsatc','app02.prod.crm.polsatc','app03.prod.crm.polsatc','app04.prod.crm.polsatc','app05.prod.crm.polsatc','app06.prod.crm.polsatc','app07.prod.crm.polsatc']
for h in hosty:

        i = 0
        imax = 5
        while True:
                print "Trying to connect to %s (%i/%i)" % (h, i, imax)

                try:
                        ssh = paramiko.SSHClient()
                        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                        ssh.connect(h, port=3300)
                        print "Connected to %s" % h
                        break
                except paramiko.AuthenticationException:
                        print "Authentication failed when connecting to %s" % h
                        sys.exit(1)
                except:
                        print "Could not SSH to %s, waiting for it to start" % h
                        i += 1
                        time.sleep(2)

                        # If we could not connect within time limit
                if i == 5:
                        print "Could not connect to %s. Giving up" % h
                        sys.exit(1)


        stdin, stdout, stderr = ssh.exec_command("docker inspect -f '{{.Config.Hostname}} {{.State.ExitCode}} {{.State.OOMKilled}}' $(docker ps -q)")
        while not stdout.channel.exit_status_ready():
                if stdout.channel.recv_ready():
                        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
                        if len(rl) > 0:
                                print stdout.channel.recv(1024),
        print "Command done, closing SSH connection"
        ssh.close()



ansible-> python testParamiko.py
Trying to connect to app01.prod.crm.polsatc (0/5)
Connected to app01.prod.crm.polsatc
crm-prd-n1-3 0 false
crm-prd-n1-2 0 false
crm-prd-n1-1 0 false
Command done, closing SSH connection
Trying to connect to app02.prod.crm.polsatc (0/5)
Connected to app02.prod.crm.polsatc
crm-prd-n2-3 0 false
crm-prd-n2-2 0 false
crm-prd-n2-1 0 false
Command done, closing SSH connection
Trying to connect to app03.prod.crm.polsatc (0/5)
Connected to app03.prod.crm.polsatc
crm-prd-n3-3 0 false
crm-prd-n3-2 0 false
crm-prd-n3-1 0 false
Command done, closing SSH connection
Trying to connect to app04.prod.crm.polsatc (0/5)
Connected to app04.prod.crm.polsatc
crm-prd-n4-3 0 false
crm-prd-n4-2 0 false
crm-prd-n4-1 0 false
Command done, closing SSH connection
Trying to connect to app05.prod.crm.polsatc (0/5)
Connected to app05.prod.crm.polsatc
crm-prd-n5-3 0 false
crm-prd-n5-2 0 false
crm-prd-n5-1 0 false
Command done, closing SSH connection
Trying to connect to app06.prod.crm.polsatc (0/5)
Connected to app06.prod.crm.polsatc
crm-prd-n6-3 0 false
crm-prd-n6-2 0 false
crm-prd-n6-1 0 false
konfigurator-prd-n1 0 false
Command done, closing SSH connection
Trying to connect to app07.prod.crm.polsatc (0/5)
Connected to app07.prod.crm.polsatc
crm-prd-n7-3 0 false
crm-prd-n7-2 0 false
crm-prd-n7-1 0 false
Command done, closing SSH connection

poniedziałek, 3 kwietnia 2017

how to print function name in python

def say_my_name(func):
    def wrapped(*args, **kwargs):
        print(func.__name__)
        return func(*args, **kwargs)
    return wrapped

@say_my_name

def heisenberg():
    print("you are right")


heisenberg()
def Wiktor():
    print("Wiktor Borkowski")


Wiktor()

python timer with keyboard interruption

import time

timer = time.time()
try:
    while True:
        if time.time()-timer > 2:
            print("2 sec")
            timer = time.time()
except KeyboardInterrupt:
    print "you hit ctr+c"
except:

    print "error"