środa, 29 czerwca 2016

forced remove docker images

┌─[ubuntu]─[~/docker/centos]
└──╼ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED                                                                                                                          SIZE
<none>              <none>              a9c0eefa1525        2 minutes ago                                                                                                                    400.9 MB
centos              7                   904d6c400333        3 weeks ago                                                                                                                      196.8 MB
 
┌─[✗]─[ubuntu]─[~/docker/centos]
└──╼ docker rmi a9c0eefa1525 -f
Failed to remove image (a9c0eefa1525): Error response from daemon: conflict: unable to delete a9c0eefa1525 (must be forced) - image is being used by stopped container 28fcdd89e896
Failed to remove image (-f): Error response from daemon: No such image: -f:latest

┌─[✗]─[ubuntu]─[~/docker/centos]
└──╼ docker rmi -f a9c0eefa1525
Deleted: sha256:a9c0eefa15257ca73c4df58f47934129238affcdf9f920b149847c299ed3cf17
Deleted: sha256:d1fc541edbb0104ec34ac12484495096c7153ec8cfe98a5fc0b7e4809bc1cd44
Deleted: sha256:dcae9a0cc9da5ae5ad705da038c2dcc65705944f97b9e21982994abd13676ec7


┌─[ubuntu]─[~/docker/centos]
└──╼ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   904d6c400333        3 weeks ago         196.8 MB
┌─[ubuntu]─[~/docker/centos]
└──╼ docker rmi -f 904d6c400333
Untagged: centos:7
Deleted: sha256:904d6c400333c03490f618654231344a9129cd4946049dc2878ce53558e7b816

czwartek, 23 czerwca 2016

Atom best editor -- how to run command in atom editor?

Normally atom is just a editor but if we want we can create IDE for our purpose.
about atom:


1. install

marek: ~/Documents/dev/ruby $ apm install atom-runner

Installing atom-runner to /Users/marek/.atom/packages ✓

apm is: atom package manager

2. each code you can execute by pressing ctrl + r(alt +r) in linux windows
to kill current running process - ctrl+shift+C

output will be showed on the right site of atom window

środa, 22 czerwca 2016

Run command threw Ansible

┌─[✗]─[marek@ubuntu]─[/etc/ansible]
└──╼ ansible all -a "dnf install docker -y" -u root

192.168.1.11 | SUCCESS | rc=0 >>
Last metadata expiration check: 0:12:33 ago on Wed Jun 22 23:31:30 2016.
Dependencies resolved.
================================================================================
 Package                      Arch   Version                      Repository
                                                                           Size
================================================================================
Installing:
 audit-libs-python3           x86_64 2.5.2-1.fc24                 fedora   95 k
 checkpolicy                  x86_64 2.5-2.fc24                   fedora  301 k
 docker                       x86_64 2:1.10.3-19.gitee81b72.fc24  updates 6.7 M
 docker-selinux               x86_64 2:1.10.3-19.gitee81b72.fc24  updates  72 k
 docker-v1.10-migrator        x86_64 2:1.10.3-19.gitee81b72.fc24  updates 1.9 M
 libcgroup                    x86_64 0.41-8.fc24                  fedora   67 k
 libsemanage-python3          x86_64 2.5-2.fc24                   fedora  112 k
 policycoreutils-python-utils x86_64 2.5-5.fc24                   fedora  215 k
 policycoreutils-python3      x86_64 2.5-5.fc24                   fedora  1.8 M
 setools-libs                 x86_64 3.3.8-10.fc24                fedora  561 k
 sqlite                       x86_64 3.11.0-3.fc24                fedora  486 k

Transaction Summary
================================================================================
Install  11 Packages

Total download size: 12 M
Installed size: 43 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           2.0 MB/s |  12 MB     00:06
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Installing  : docker-v1.10-migrator-2:1.10.3-19.gitee81b72.fc24.x86_6    1/11
  Installing  : libcgroup-0.41-8.fc24.x86_64                               2/11
  Installing  : sqlite-3.11.0-3.fc24.x86_64                                3/11
  Installing  : setools-libs-3.3.8-10.fc24.x86_64                          4/11
  Installing  : libsemanage-python3-2.5-2.fc24.x86_64                      5/11
  Installing  : checkpolicy-2.5-2.fc24.x86_64                              6/11
  Installing  : audit-libs-python3-2.5.2-1.fc24.x86_64                     7/11
  Installing  : policycoreutils-python3-2.5-5.fc24.x86_64                  8/11
  Installing  : policycoreutils-python-utils-2.5-5.fc24.x86_64             9/11
  Installing  : docker-selinux-2:1.10.3-19.gitee81b72.fc24.x86_64         10/11
  Installing  : docker-2:1.10.3-19.gitee81b72.fc24.x86_64                 11/11
  Verifying   : docker-2:1.10.3-19.gitee81b72.fc24.x86_64                  1/11
  Verifying   : docker-selinux-2:1.10.3-19.gitee81b72.fc24.x86_64          2/11
  Verifying   : policycoreutils-python-utils-2.5-5.fc24.x86_64             3/11
  Verifying   : policycoreutils-python3-2.5-5.fc24.x86_64                  4/11
  Verifying   : audit-libs-python3-2.5.2-1.fc24.x86_64                     5/11
  Verifying   : checkpolicy-2.5-2.fc24.x86_64                              6/11
  Verifying   : libsemanage-python3-2.5-2.fc24.x86_64                      7/11
  Verifying   : setools-libs-3.3.8-10.fc24.x86_64                          8/11
  Verifying   : sqlite-3.11.0-3.fc24.x86_64                                9/11
  Verifying   : libcgroup-0.41-8.fc24.x86_64                              10/11
  Verifying   : docker-v1.10-migrator-2:1.10.3-19.gitee81b72.fc24.x86_6   11/11

Installed:
  audit-libs-python3.x86_64 2.5.2-1.fc24
  checkpolicy.x86_64 2.5-2.fc24
  docker.x86_64 2:1.10.3-19.gitee81b72.fc24
  docker-selinux.x86_64 2:1.10.3-19.gitee81b72.fc24
  docker-v1.10-migrator.x86_64 2:1.10.3-19.gitee81b72.fc24
  libcgroup.x86_64 0.41-8.fc24
  libsemanage-python3.x86_64 2.5-2.fc24
  policycoreutils-python-utils.x86_64 2.5-5.fc24
  policycoreutils-python3.x86_64 2.5-5.fc24
  setools-libs.x86_64 3.3.8-10.fc24
  sqlite.x86_64 3.11.0-3.fc24

Complete!

wtorek, 21 czerwca 2016

Ansible playbook for ubuntu "clients" server

ansible-playbook ubuntu.yml -v --user=marek --extra-vars "ansible_sudo_pass=<your_password>"


[root@centos ansible]# cat ubuntu.yml
---
- name: Hello Ansible - quick start
  hosts: ubuntu
  user: marek
  sudo: yes

  tasks:
    - name: Hello server
      shell: date >> now.txt

Ansible hello world playbook

The more advanced option to use with ansible are playbook:

very simple example:
---
- name: Hello Ansible - quick start
  hosts: fedora
  user: root
  sudo: no

  tasks:
    - name: Hello server
      shell: date >> now.txt
~
~

[root@centos ansible]# ansible-playbook setup.yml -v
No config file found; using defaults
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default).
This feature will be removed in a future release. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY [Hello Ansible - quick start] *********************************************

TASK [setup] *******************************************************************
ok: [192.168.1.11]
ok: [192.168.1.14]

TASK [Hello server] ************************************************************
changed: [192.168.1.11] => {"changed": true, "cmd": "date >> now.txt", "delta": "0:00:00.002414", "end": "2016-06-21 23:03:38.561068", "rc": 0, "start": "2016-06-21 23:03:38.558654", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
changed: [192.168.1.14] => {"changed": true, "cmd": "date >> now.txt", "delta": "0:00:00.002988", "end": "2016-06-21 23:03:36.163002", "rc": 0, "start": "2016-06-21 23:03:36.160014", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}

PLAY RECAP *********************************************************************
192.168.1.11               : ok=2    changed=1    unreachable=0    failed=0
192.168.1.14 

The basics of Ansible

The Ansible is written in python tool to make the admin things more clear and comfortable.
We dont need any app server any client only python and ssh. Looks great - well .... we will see


[root@centos ansible]# ansible fedora -m shell -a "dnf install python-pip -y"
192.168.1.11 | SUCCESS | rc=0 >>
Ostatnio sprawdzono ważność metadanych: 1:25:40 temu w dniu Tue Jun 21 21:11:10 2016.
Rozwiązano zależności.
Nie ma niczego do zrobienia.
Ukończono.Pakiet python-pip-8.0.2-1.fc24.noarch jest już zainstalowany, pomijanie.

192.168.1.14 | SUCCESS | rc=0 >>
Ostatnio sprawdzono ważność metadanych: 0:34:44 temu w dniu Tue Jun 21 22:02:03 2016.
Rozwiązano zależności.
Nie ma niczego do zrobienia.
Ukończono.Pakiet python-pip-7.1.0-1.fc23.noarch jest już zainstalowany, pomijanie.


description:

Usage: ansible <host-pattern> [options]

fedora / [all] - group that we will check / all for all groups to change it or just check you need to find file:

hosts in ansible /etc/ dir

[root@centos ansible]# cat hosts
[fedora]
192.168.1.14
192.168.1.11
[ubuntu]
192.168.1.19


  -m MODULE_NAME, --module-name=MODULE_NAME
                        module name to execute (default=command)

  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments


login without password - ssh config

[root@centos ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
8f:8d:89:3b:3b:ed:80:48:a6:d5:e1:56:b2:ce:e0:0c root@centos
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|    o .          |
|   o =           |
|E = =   S        |
| O * . . *       |
|. + + o.+ o      |
|      oo.        |
|      o=.        |
+-----------------+
[root@centos ~]#
[root@centos ~]#
[root@centos ~]#
[root@centos ~]#
[root@centos ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.11
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter                                                                                                              out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompt                                                                                                             ed now it is to install the new keys
root@192.168.1.11's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.1.11'"
and check to make sure that only the key(s) you wanted were added.

[root@centos ~]# ssh 192.168.1.11
Last login: Tue Jun 21 22:04:45 2016 from 192.168.1.21
[root@fedora24 ~]#

sobota, 11 czerwca 2016

C Alphabet vs Java Alphabet

#include <stdio.h>
int main()
{
    int litera;
    for(litera = 'A'; litera <= 'Z'; litera++)
    {
        printf("%c\n",litera);
    }
    return 0;
}

the same case in java:
public class BattleShip {
    public static void main(String[] args){
        for(int i=0;i<26;i++){
            System.out.print((char)('A' + i));
        }
    }
}

how to get full string from user c++

void getMessage(string name)
{
    cout << "enter your name: ";
    getline(cin, name);
    cout << "Hello " << name << endl;
}

środa, 8 czerwca 2016

Java Dec to (bin,hex and oct) converter

public class IntToBin {
    public static void main(String[] args){
        java.util.Scanner scIn = new java.util.Scanner(System.in);
        System.out.print("Enter int: ");
        int IntDig = scIn.nextInt();

        System.out.println("bin: "+Integer.toBinaryString(IntDig));
        System.out.println("hex: "+Integer.toHexString(IntDig));
        System.out.println("octal: "+Integer.toOctalString(IntDig));


    }
}

wtorek, 7 czerwca 2016

check memory details via python script on mac os x

#!/usr/bin/python

import subprocess
import re

# Get process info
ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0]
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0]

# Iterate processes
processLines = ps.split('\n')
sep = re.compile('[\s]+')
rssTotal = 0 # kB
for row in range(1,len(processLines)):
    rowText = processLines[row].strip()
    rowElements = sep.split(rowText)
    try:
        rss = float(rowElements[0]) * 1024
    except:
        rss = 0 # ignore...
    rssTotal += rss

# Process vm_stat
vmLines = vm.split('\n')
sep = re.compile(':[\s]+')
vmStats = {}
for row in range(1,len(vmLines)-2):
    rowText = vmLines[row].strip()
    rowElements = sep.split(rowText)
    vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 4096

print 'Wired Memory:\t\t%d MB' % ( vmStats["Pages wired down"]/1024/1024 )
print 'Active Memory:\t\t%d MB' % ( vmStats["Pages active"]/1024/1024 )
print 'Inactive Memory:\t%d MB' % ( vmStats["Pages inactive"]/1024/1024 )
print 'Free Memory:\t\t%d MB' % ( vmStats["Pages free"]/1024/1024 )
print 'Real Mem Total (ps):\t%.3f MB' % ( rssTotal/1024/1024 )





MacBook-Pro-Marek:python marek$ python sys.py 
Wired Memory: 2239 MB
Active Memory: 6772 MB
Inactive Memory: 4624 MB
Free Memory: 1433 MB

Real Mem Total (ps): 8770.777 MB

how to read file via Go lang

package main

import (
       "fmt"       "os"       "bufio")

func main(){
       f, _ := os.Open("c:\\windows-version.txt")
       scanner := bufio.NewScanner(f)

       for scanner.Scan(){
              line := scanner.Text()
              fmt.Println(line)
       }
}

User Input String - go IO app

package main

import "fmt"
func main(){
       var name string
       fmt.Println("Enter name: ")
       fmt.Scanf("%v",&name )
       fmt.Println("Hello ",name)
}

What about go?

Hi All - what are you thinking about Go language?

syntax looks good, it could be compiled .... hmmm


package main

import "fmt"
func main(){
  var a string = "Marek"
  var b string = "Borkowski"

  var c string = a + " " +b

  s:= fmt.Sprintf("Hi my name is %s and my last name is %s",a,b)
  fmt.Println(s)

  fmt.Println(c)
}

piątek, 3 czerwca 2016

very simple calc function in C

#include <stdio.h>


int main(int argc, char *argv[]) {
int n1, n2, sum;

printf("enter a: ");
scanf("%d", &n1);

printf("enter b: ");
scanf("%d", &n2);

sum = addnum(n1, n2);
printf("sum = %d",sum);
return 0;
}

int addnum(int a,int b){
int res = a + b;
return res;
}

czwartek, 2 czerwca 2016

multiplication table C vs multiplication table java

#include <stdio.h>

int main(){
        int columns, row;
        for(row = 1;row <=9; row++){
                for(columns = 1; columns <=9; columns++){
                        printf("%i\t",columns*row);
                }
                printf("\n\n");
        }
}


public class MultiplyTable {
    public static void main(String[] args) {
        int columns, row;
        for (row = 1; row <= 9; row++) {
            for(columns = 1;columns <= 9; columns++){
                System.out.printf("%d \t", columns*row);
            }
            System.out.println();
        }
    }
}

środa, 1 czerwca 2016

InputMismatchException with Scanner double

import java.util.*;

public class Challenge_day1 {
    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);
        System.out.print("enter x: ");
        double x = scan.nextDouble();

        System.out.print("enter y: ");
        double y = scan.nextDouble();

        double suma = (x+y);
        System.out.print(suma);

   }
}


output error:

enter x: 2.2
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at Challenge_day1.main(Challenge_day1.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1

how to fix it?

change line Scanner to:
Scanner scan = new Scanner(System.in).useLocale(Locale.US);

it is related to jvm locale configuration -- in my example :
enter x: 2,1
enter y: 3,2

not 2.1 or 3.2