May 2018
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  

Categories

WordPress Quotes

Cherish your visions and your dreams as they are the children of your soul, the blueprints of your ultimate achievements.
Napoleon Hill

Recent Comments

May 2018
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  

Short Cuts

2012 SERVER (64)
2016 windows (9)
AIX (13)
Amazon (13)
Ansibile (17)
Apache (123)
Asterisk (2)
cassandra (2)
Centos (207)
Centos RHEL 7 (245)
chef (3)
cloud (2)
cluster (3)
Coherence (1)
DB2 (5)
DISK (25)
DNS (9)
Docker (21)
Eassy (11)
EXCHANGE (3)
Fedora (6)
ftp (4)
GOD (2)
Grub (1)
Hacking (10)
Hadoop (6)
horoscope (23)
Hyper-V (10)
IIS (15)
IPTABLES (15)
JAVA (6)
JBOSS (31)
jenkins (1)
Kubernetes (1)
Ldap (4)
Linux (186)
Linux Commands (167)
Load balancer (5)
mariadb (14)
Mongodb (4)
MQ Server (21)
MYSQL (74)
Nagios (4)
NaturalOil (13)
Nginx (23)
Ngix (1)
Openstack (6)
Oracle (29)
Perl (3)
Postfix (19)
Postgresql (1)
PowerShell (2)
Python (3)
qmail (36)
Redis (9)
RHCE (28)
SCALEIO (1)
Security on Centos (29)
SFTP (1)
Shell (64)
Solaris (58)
Sql Server 2012 (4)
squid (3)
SSH (10)
SSL (14)
Storage (1)
swap (3)
TIPS on Linux (28)
tomcat (58)
Uncategorized (29)
Veritas (2)
vfabric (1)
VMware (28)
Weblogic (38)
Websphere (71)
Windows (19)
Windows Software (2)
wordpress (1)
ZIMBRA (17)

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

Who's Online

18 visitors online now
0 guests, 18 bots, 0 members

Hit Counter provided by dental implants orange county

Managing files in ansible

Managing files in ansible

[root@controller ~]$ ansible localhost –list-hosts

hosts (1):
localhost
——————————————-
[root@controller ~]$ vim file.yaml

– name: creating a file
hosts: localhost
tasks:
– file:
path: /home/root/sample
state: touch
owner: root
group: root
mode: 0755

——————————————-
[root@controller ~]$ ansible-playbook –syntax-check file.yaml

playbook: file.yaml

—————————————-
[root@controller ~]$ ansible-playbook -C file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

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

——————————————-
[root@controller ~]$ stat /home/root/sample
stat: cannot stat ‘/home/root/sample’: No such file or directory
——————————————-
[root@controller ~]$ ansible-playbook file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
——————————————-
[root@controller ~]$ stat /home/root/sample
File: ‘/home/root/sample’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 51300626 Links: 1
Access: (0333/–wx-wx-wx) Uid: ( 1000/ root) Gid: ( 1000/ root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2017-01-26 06:39:51.607557462 +0530
Modify: 2017-01-26 06:39:51.607557462 +0530
Change: 2017-01-26 06:39:51.608557462 +0530
Birth: –
——————————————-
[root@controller ~]$ ls -l /home/root/sample
–wx-wx-wx. 1 root root 0 Jan 26 06:39 /home/root/sample
——————————————-
[root@controller ~]$ ansible-playbook file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

TASK [stat] ********************************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
——————————————–
[root@controller ~]$ vim file.yaml

– name: creating a file
hosts: localhost
tasks:
– file:
path: /home/root/sample
state: touch
owner: root
group: root
mode: 0755
– stat: path=/home/root/sample
register: file_status
– debug: msg=”File exists”
when: file_status.stat.exists == true
——————————————–
[root@controller ~]$ ansible-playbook file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

TASK [stat] ********************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
“msg”: “File exists”
}

PLAY RECAP *********************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0

——————————————–
[root@controller ~]$ vim file.yaml

– name: creating a file
hosts: localhost
tasks:
– file:
path: /home/root/sample
state: touch
owner: root
group: root
mode: 0755
– stat: path=/home/root/sample
register: file_status
– debug: msg=”File exists”
when: file_status.stat.exists == true
– copy: content=”this is for test purpose\n” dest=”/home/root/sample”
when: file_status.stat.exists == true

[root@controller ~]$ ansible-playbook -C file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

TASK [stat] ********************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
“msg”: “File exists”
}

TASK [copy] ********************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=5 changed=2 unreachable=0 failed=0

ansible vault

root@controller ~]# ansible-vault create mohan.yml
Vault password:

[root@controller ~]# cat mohan.yml
$ANSIBLE_VAULT;1.1;AES256
38623235633039636166356162393064363936303461306536386237663032383932656164633131
6132633132376266313863366164396535386539666562310a306562383834343431633536353332
63303935623030393261373030343366323361653238306531356434333538613236303738653730
3935313536396361640a343836366434613638316538333165366161306166396564353635383831
30636536366462646362373432396234383432376437633764616239393938366137

[root@controller ~]# ansible-vault view mohan.yml
Vault password:
hai how are you

[root@controller ~]# ansible-vault edit mohan.yml
Vault password:

[root@controller ~]# ansible-vault rekey mohan.yml
Vault password:
New Vault password:
Confirm New Vault password:
Rekey successful

[root@controller ~]# ansible-playbook mohan.yml
ERROR! Decryption failed on /root/mohan.yml

[root@controller ~]# ansible-playbook –ask-vault-pass mohan.yml
Vault password:

[root@controller ~]# ansible-vault encrypt 4.yml
Vault password:
Encryption successful
[root@controller ~]# ansible-playbook 4.yml
ERROR! Decryption failed on /root/4.yml
[root@controller ~]# ansible-playbook –ask-vault-pass 4.yml
Vault password:

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
=======================================
[root@controller ~]# ansible-vault decrypt 4.yml
Vault password:
Decryption successful

[root@controller ~]# ansible-playbook 4.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
========================================
[root@controller ~]# ansible-vault decrypt 4.yml –output=4-decrypted.yml
Vault password:
Decryption successful
[root@controller ~]# cat 4.yml
$ANSIBLE_VAULT;1.1;AES256
65386464336638663338363031383263633764393937633839366565336166303935363733616663
6636633734663766353365613063396565383662366539390a613765626239363361386165653763
35353730633164346634666339616232343830643434393563363662386633393830313538306130
3366386539313535380a643639613765653235363235383463663735663639333232353230343664
37346532353963663636303833653230333661333735393339336264303136636165366365326538
39613537353638373464333633353235356538653333643864623063333534303766373039373031
383436656161333330373162633966386639
[root@controller ~]# cat 4-decrypted.yml
– hosts: localhost
vars:
user: joe
home: /home/joe
=======================================
[root@controller ~]# vim vault-pass
redhat_123

[root@controller ~]# ansible-vault decrypt –vault-password-file=vault-pass sample.yaml

[root@controller ~]# ansible-vault create –vault-password-file=vault-pass example.yaml

– name: installing packages
hosts: localhost
tasks:
– yum: name=elinks state=latest

[root@controller ~]# cat example.yaml
$ANSIBLE_VAULT;1.1;AES256
37653137363538613630333039386164353232636333306430336333316164363566373464316634
3636336637336535633039323631313038643366393534650a393762643936343566313638646662
64663338376162643463343232396361383739303635383438323831386539303337623764316537
3961653566353362330a393530333638356663303264326331386166613330323539343436396632
38636630393133393064623437663133376233663934346666313162363838386532626337646134
39316561633530336663663238333766353861666339353134663930663839393532396334643062
64393233653834646463366432633965663432313431656236386664643461386365613363616432
35306537656335316561393966656362393634373237313737623164633836663561363636646332
32663839343461323832626263363762313730346333353034383539333332366463

[root@controller ~]# ansible-playbook example.yaml
ERROR! Decryption failed on /root/example.yaml

[root@controller ~]# ansible-playbook –vault-password-file=vault-pass –syntax-check example.yaml

playbook: example.yaml

[root@controller ~]# ansible-playbook –vault-password-file=vault-pass example.yaml

PLAY [installing packages] *****************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [yum] *********************************************************************
changed: [localhost]

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

[root@controller ~]# vim newpassword
mohan0494

[root@controller ~]# ansible-vault rekey –new-vault-password-file=newpassword example.yaml
Vault password:
Rekey successful

[root@controller ~]# ansible-playbook –vault-password-file=newpassword example.yaml

PLAY [installing packages] *****************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [yum] *********************************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

[root@controller ~]# ansible-vault decrypt –vault-password-file=newpassword example.yaml
Decryption successful
[root@controller ~]# cat example.yaml

– name: installing packages
hosts: localhost
tasks:
– yum: name=elinks state=latest

[root@controller ~]# ansible-vault encrypt –vault-password-file=newpassword example.yaml
Encryption successful
[root@controller ~]# cat example.yaml
$ANSIBLE_VAULT;1.1;AES256
64643166623463393937376165333034363635653931663839633836316239333035396161663165
6461613861373731383431303839383839316264366538350a373839396533633333313364626330
31336538356365666537373438306165333534363533636436636666656162346530643539316261
3431343233373135620a336163633164633961353339303433396639373735663038306262613639
65666130303539613131663666313361646538643038643834383966633364353162626233356132
64333930643531343066383164393238383639343764376661303734336532393431633534366238
62313537623834376535643830353361633336613563363535363931343934303739643039386532
62653335373632633465633063653564616430393234343862383437353732383231656138386165
38363135656434363239383065306136653863363334376230393739643539616463

JINJA2 templates in ansible

JINJA2 templates in ansible

[root@workstation ~]# ansible -m ping all
192.168.1.23 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
192.168.1.22 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}

=========================================
[root@workstation ~]# vim motd.j2
this is {{ ansible_hostname }}.
today’s date is {{ ansible_date_time.date }}
you can ask {{ system_owner }} for access
==========================================
[root@workstation ~]# vim motd.yaml

– hosts: all
vars:
system_owner: root
tasks:
– template:
src: motd.j2
dest: /etc/motd
owner: root
group: root
mode: 777
========================================
[root@workstation ~]# cat /etc/motd
========================================
[root@workstation ~]# ansible-playbook –syntax-check motd.yaml

playbook: motd.yaml

[root@workstation ~]# ansible-playbook -C motd.yaml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.22]
ok: [192.168.1.23]

TASK [template] ****************************************************************
changed: [192.168.1.22]
changed: [192.168.1.23]

PLAY RECAP *********************************************************************
192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0
192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0
=======================================
[root@workstation ~]# ansible-playbook motd.yaml -vv
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: motd.yaml ************************************************************
1 plays in motd.yaml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.22]
ok: [192.168.1.23]

TASK [template] ****************************************************************
task path: /root/motd.yaml:6
changed: [192.168.1.22] => {“changed”: true, “checksum”: “86167eb96fcd968f9ce2403d02fd99de5454cffa”, “dest”: “/etc/motd”, “gid”: 0, “group”: “root”, “md5sum”: “35128946a8173580f8c13e86523be1a1”, “mode”: “01411”, “owner”: “root”, “secontext”: “system_u:object_r:etc_t:s0”, “size”: 84, “src”: “/root/.ansible/tmp/ansible-tmp-1485712899.32-75249962942078/source”, “state”: “file”, “uid”: 0}
changed: [192.168.1.23] => {“changed”: true, “checksum”: “5619e003a6b2617b359b8a369c3f404035ab0b18”, “dest”: “/etc/motd”, “gid”: 0, “group”: “root”, “md5sum”: “4429a262ca0c9cca26b2bc66482abfbc”, “mode”: “01411”, “owner”: “root”, “secontext”: “system_u:object_r:etc_t:s0”, “size”: 80, “src”: “/root/.ansible/tmp/ansible-tmp-1485712899.32-157949107190502/source”, “state”: “file”, “uid”: 0}

PLAY RECAP *********************************************************************
192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0
192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0
==============================================
[root@servera ~]# logout
Connection to 192.168.1.23 closed.
root@rmohan:~$ ssh root@192.168.1.23
root@192.168.1.23’s password:
Last login: Sun Jan 29 23:29:06 2017 from workstation
this is servera.
today’s date is 2017-01-29
you can ask root for access
==============================================
root@rmohan:~$ ssh root@192.168.1.22
root@192.168.1.22’s password:
Last login: Sun Jan 29 23:31:40 2017 from workstation.example.com
this is workstation.
today’s date is 2017-01-29
you can ask root for access
================================================

[root@workstation ~]# vim inventory
[web]
node1.rmohan.com
node2.rmohan.com

[root@workstation ~]# rm -rf /etc/motd

[root@servera ~]# rm -rf /etc/motd

[root@workstation ~]# ansible-playbook -i inventory –limit node1.rmohan.com motd.yaml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [node1.rmohan.com]

TASK [template] ****************************************************************
changed: [node1.rmohan.com]

PLAY RECAP *********************************************************************
node1.rmohan.com : ok=2 changed=1 unreachable=0 failed=0

Creating role in ansible

Creating a basic role for displaying message of the day

[root@workstation ~]# vim /etc/ansible/roles/motd/main.yml

– name: use motd role playbook
hosts: all

roles:
– motd

[root@workstation ~]# vim /etc/ansible/roles/motd/defaults/main.yml

system_owner: rmohan
[root@workstation ~]# vim /etc/ansible/roles/motd/tasks/main.yml

– name: deliver motd file
template:
src: templates/motd.j2
dest: /etc/motd
owner: root
group: root
mode: 777
[root@workstation ~]# vim /etc/ansible/roles/motd/templates/motd.j2
this is {{ ansible_hostname }}.
today’s date is {{ ansible_date_time.date }}
you can ask {{ system_owner }} for access
[root@workstation ~]# ansible-playbook –syntax-check /etc/ansible/roles/motd/main.yml

playbook: /etc/ansible/roles/motd/main.yml
[root@workstation ~]# ansible-playbook -C /etc/ansible/roles/motd/main.yml

PLAY [use motd role playbook] **************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.22]
ok: [192.168.1.23]

TASK [motd : deliver motd file] ************************************************
changed: [192.168.1.23]
changed: [192.168.1.22]

PLAY RECAP *********************************************************************
192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0
192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0

[root@workstation ~]# ansible-playbook /etc/ansible/roles/motd/main.yml

PLAY [use motd role playbook] **************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.22]
ok: [192.168.1.23]

TASK [motd : deliver motd file] ************************************************
changed: [192.168.1.23]
changed: [192.168.1.22]

PLAY RECAP *********************************************************************
192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0
192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0

[root@workstation ~]# cat /etc/motd
this is workstation.
today’s date is 2017-01-30
you can ask rmohan for access

HANDLERS & register in ansible

HANDLERS & register in ansible

[root@workstation ~]# vim register.yaml

– name: checking the register module functionality
hosts: localhost
tasks:
– name:
command: ps
register: output
– debug: msg=”{{ output.stdout }}”
====================================================
[root@workstation ~]# ansible-playbook –syntax-check register.yaml

playbook: register.yaml

[root@workstation ~]# ansible-playbook register.yaml

PLAY [checking the register module functionality] ******************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [command] *****************************************************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
“msg”: ” PID TTY TIME CMD\n 9690 pts/0 00:00:00 bash\n 11851 pts/0 00:00:00 ansible-playboo\n 11894 pts/0 00:00:00 ansible-playboo\n 11904 pts/0 00:00:00 sh\n 11905 pts/0 00:00:00 python2\n 11906 pts/0 00:00:00 python2\n 11907 pts/0 00:00:00 ps”
}

PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
====================================================
[root@workstation ~]# vim register.yaml

– name: checking the register module functionality
hosts: localhost
tasks:
– name:
shell: “ps -aux | grep ansible”
register: output
– debug: msg={{ output.stdout }}
[root@workstation ~]# ansible-playbook register.yaml

PLAY [checking the register module functionality] ******************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [command] *****************************************************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
“msg”: “root 12337 42.0 5.4 338628 26212 pts/0 Rl+ 03:02 0:00 /usr/bin/python2 /usr/bin/ansible-playbook register.yaml\nroot 12380 0.0 5.9 345696 28664 pts/0 S+ 03:02 0:00 /usr/bin/python2 /usr/bin/ansible-playbook register.yaml\nroot 12390 0.0 0.2 113116 1204 pts/0 S+ 03:02 0:00 /bin/sh -c /usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1485725579.35-37718783810169/command.py; rm -rf \”/root/.ansible/tmp/ansible-tmp-1485725579.35-37718783810169/\” > /dev/null 2>&1 && sleep 0\nroot 12391 0.0 1.6 189176 7976 pts/0 S+ 03:02 0:00 /usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1485725579.35-37718783810169/command.py\nroot 12392 0.0 2.6 206164 12668 pts/0 S+ 03:02 0:00 /usr/bin/python2 /tmp/ansible_70E9iy/ansible_module_command.py\nroot 12393 0.0 0.2 113116 1200 pts/0 S+ 03:02 0:00 /bin/sh -c ps -aux | grep ansible\nroot 12395 0.0 0.1 112644 932 pts/0 S+ 03:02 0:00 grep ansible”
}

PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
====================================================

[root@workstation ~]# vim handler.yaml

– name: checking handler functioning
hosts: localhost
tasks:
– name: starting apache service by adding html content
copy:
src: /root/index.html
dest: /var/www/html/index.html
notify:
– start_apache
handlers:
– name: start_apache
service:
name: httpd
state: started
[root@workstation ~]# vim /root/index.html
this for checking handler

[root@workstation ~]# ansible-playbook –syntax-check handler.yaml

playbook: handler.yaml

[root@workstation ~]# ansible-playbook handler.yaml

PLAY [checking handler functioning] ********************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [starting apache service by adding html content] **************************
changed: [localhost]

RUNNING HANDLER [start_apache] *************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0

[root@workstation ~]# ansible-playbook handler.yaml

PLAY [checking handler functioning] ********************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [starting apache service by adding html content] **************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

Tags in ansible

Tags in ansible

[root@workstation ~]# vim tags.yaml

– name: installing postfix and stopping from starting service
hosts: localhost
tasks:
– name: installing postfix package
yum: name=postfix state=latest
tags: packageonly
– name: starting service
service: name=postfix state=started
[root@workstation ~]# ansible-playbook –syntax-check tags.yaml

playbook: tags.yaml
[root@workstation ~]# ansible-playbook -C tags.yaml

PLAY [installing postfix and stopping from starting service] *******************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [installing postfix package] **********************************************
changed: [localhost]

TASK [starting service] ********************************************************
fatal: [localhost]: FAILED! => {“changed”: false, “failed”: true, “msg”: “Could not find the requested service postfix: cannot check nor set state”}
to retry, use: –limit @/root/tags.retry

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

[root@workstation ~]#
[root@workstation ~]# ansible-playbook -C tags.yaml –tags ‘packageonly’

PLAY [installing postfix and stopping from starting service] *******************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [installing postfix package] **********************************************
changed: [localhost]

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

[root@workstation ~]# ansible-playbook tags.yaml –tags ‘packageonly’

PLAY [installing postfix and stopping from starting service] *******************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [installing postfix package] **********************************************
changed: [localhost]

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

[root@workstation ~]# ansible-playbook tags.yaml –skip-tags ‘packageonly’

PLAY [installing postfix and stopping from starting service] *******************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [starting service] ********************************************************
changed: [localhost]

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

Creating role in ansible

Creating role in ansible

Creating a basic role for displaying message of the day

[root@workstation ~]# vim /etc/ansible/roles/motd/main.yml

– name: use motd role playbook
hosts: all

roles:
– motd

[root@workstation ~]# vim /etc/ansible/roles/motd/defaults/main.yml

system_owner: rmohan
[root@workstation ~]# vim /etc/ansible/roles/motd/tasks/main.yml

– name: deliver motd file
template:
src: templates/motd.j2
dest: /etc/motd
owner: root
group: root
mode: 777
[root@workstation ~]# vim /etc/ansible/roles/motd/templates/motd.j2
this is {{ ansible_hostname }}.
today’s date is {{ ansible_date_time.date }}
you can ask {{ system_owner }} for access
[root@workstation ~]# ansible-playbook –syntax-check /etc/ansible/roles/motd/main.yml

playbook: /etc/ansible/roles/motd/main.yml
[root@workstation ~]# ansible-playbook -C /etc/ansible/roles/motd/main.yml

PLAY [use motd role playbook] **************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.23]
ok: [192.168.1.22]

TASK [motd : deliver motd file] ************************************************
changed: [192.168.1.22]
changed: [192.168.1.23]

PLAY RECAP *********************************************************************
192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0
192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0

[root@workstation ~]# ansible-playbook /etc/ansible/roles/motd/main.yml

PLAY [use motd role playbook] **************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.23]
ok: [192.168.1.22]

TASK [motd : deliver motd file] ************************************************
changed: [192.168.1.22]
changed: [192.168.1.23]

PLAY RECAP *********************************************************************
192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0
192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0

[root@workstation ~]# cat /etc/motd
this is workstation.
today’s date is 2018-01-30
you can ask rmohan for access

Including variables in Ansible

Including variables in Ansible

=====================================
[root@ansible1 ~]$ mkdir nginx
=====================================
[root@ansible1 ~]$ cd nginx
[root@ansible1 nginx]$ mkdir tasks vars

=====================================
[root@ansible1 nginx]$ touch tasks/environment.yml
=====================================
[root@ansible1 nginx]$ touch vars/variables.yml
=====================================
[root@ansible1 ~]$ tree nginx
nginx
|– nginx.yml
|– tasks
| `– environment.yml
`– vars
`– variables.yml
=====================================
[root@ansible1 nginx]$ vim tasks/environment.yml

– name: install package
yum:
name: “{{ package }}”
state: latest
– name: start service
service:
name: “{{ service }}”
state: “{{ svc_state }}”
=====================================
[root@ansible1 nginx]$ vim vars/variables.yml

firewall_pkg: firewalld
=====================================
[root@ansible1 nginx]$ vim nginx.yml

– name: anything
hosts: db
remote_user: root
become: true
become_method: sudo
become_user: root
vars:
rule: http
tasks:
– name: Include the variables from yaml file
include_vars: vars/variables.yml

– name: include env variables and set the variables
include: tasks/environment.yml
vars:
package: nginx
service: nginx
svc_state: started

– name: install fiirewall pkg
yum:
name: “{{ firewall_pkg }}”
state: latest
– name: start firewalld
service:
name: firewalld
state: started
enabled: true
=====================================
[root@ansible1 nginx]$ ansible-playbook –syntax-check nginx.yml

playbook: nginx.yml
=====================================
[root@ansible1 nginx]$ ansible-playbook nginx.yml

PLAY [anything] ****************************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.23]

TASK [Include the variables from yaml file] ************************************
ok: [192.168.1.23]

TASK [install package] *********************************************************
ok: [192.168.1.23]

TASK [start service] ***********************************************************
ok: [192.168.1.23]

TASK [install fiirewall pkg] ***************************************************
ok: [192.168.1.23]

TASK [start firewalld] *********************************************************
ok: [192.168.1.23]

PLAY RECAP *********************************************************************
192.168.1.23 : ok=6 changed=0 unreachable=0 failed=0
=====================================

Continuous Delivery Using Docker And Ansible

Continuous Delivery Using Docker And Ansible

Continuous Delivery
Release Often
Release Faster
Great Reliable

Continuous Delivery workflow/pipeline

With which we can Test,Build,Release,& Deploy a simple application.

Application will be of any technology ex:python based.

The work flow will be Based upon using docker & docker-compose which is a emerging technology. With a goal to release our application as docker image.

So that it can be tested, building application artefacts, creating docker-release images, and running acceptance test, by external functionality, and finally publish docker image.

First we can run it locally, & then set in popular jenkins continuous delivery system, we will configure integration with GitHub, allowing continuous delivery work flow to be triggered on each application source code commit.

Assuming application passes all unit,integration & acceptance tests. Our pipeline will release images to docker hub, which will deployed to Aws with ansible using IAAC(S) approach with aws cloud formation and leveraging ec2 container service for running docker container is production.

rmohan@root:~$ mkdir docker-ansible

rmohan@root:~$ cd docker-ansible/

rmohan@root:~/docker-ansible$ django-admin startproject todobackend

rmohan@root:~/docker-ansible$ tree
.
`– todobackend
|– manage.py
`– todobackend
|– __init__.py
|– settings.py
|– urls.py
`– wsgi.py

2 directories, 5 files

rmohan@root:~/docker-ansible$ cd todobackend/

rmohan@root:~/docker-ansible/todobackend$ mkdir src

rmohan@root:~/docker-ansible/todobackend$ mv manage.py src/

rmohan@root:~/docker-ansible/todobackend$ mv todobackend/ src

rmohan@root:~/docker-ansible/todobackend$ tree
.
`– src
|– manage.py
`– todobackend
|– __init__.py
|– settings.py
|– urls.py
`– wsgi.py

2 directories, 5 files

rmohan@root:~/docker-ansible/todobackend$ git init
Initialized empty Git repository in /home/rmohan/docker-ansible/todobackend/.git/

rmohan@root:~/docker-ansible/todobackend$ vim .gitignore
#Ignore the virtual environment
venv

#Ignore compiled python source files
*.pyc

#Ignore SQLite database files
*.sqlite3

rmohan@root:~/docker-ansible/todobackend$ git add -A
rmohan@root:~/docker-ansible/todobackend$ git commit -a -m “Initial commit”
[master (root-commit) edface9] Initial commit
6 files changed, 177 insertions(+)
create mode 100644 .gitignore
create mode 100755 src/manage.py
create mode 100644 src/todobackend/__init__.py
create mode 100644 src/todobackend/settings.py
create mode 100644 src/todobackend/urls.py
create mode 100644 src/todobackend/wsgi.py

rmohan@root:~/docker-ansible/todobackend$ pip install virtualenv
Requirement already satisfied (use –upgrade to upgrade): virtualenv in /usr/local/lib/python2.7/dist-packages
Cleaning up..

rmohan@root:~/docker-ansible/todobackend$ virtualenv venv
New python executable in /home/rmohan/docker-ansible/todobackend/venv/bin/python
Installing setuptools, pip, wheel…done.

rmohan@root:~/docker-ansible/todobackend$ ls
src venv

rmohan@root:~/docker-ansible/todobackend$ ls venv/
bin include lib local pip-selfcheck.json

rmohan@root:~/docker-ansible/todobackend$ source venv/bin/activate
(venv) rmohan@root:~/docker-ansible/todobackend$

(venv) rmohan@root:~/docker-ansible/todobackend$ pwd
/home/rmohan/docker-ansible/todobackend

(venv) rmohan@root:~/docker-ansible/todobackend$ pip install pip –upgrade
Requirement already up-to-date: pip in ./venv/lib/python2.7/site-packages

(venv) rmohan@root:~/docker-ansible/todobackend$ pip install django
Collecting django
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading Django-1.11.5-py2.py3-none-any.whl (6.9MB)
100% |????????????????????????????????| 7.0MB 139kB/s
Collecting pytz (from django)
Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)
100% |????????????????????????????????| 491kB 1.2MB/s
Installing collected packages: pytz, django
Successfully installed django-1.11.5 pytz-2017.2

(venv) rmohan@root:~/docker-ansible/todobackend$ pip install djangorestframework
Collecting djangorestframework
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading djangorestframework-3.6.4-py2.py3-none-any.whl (1.5MB)
100% |????????????????????????????????| 1.5MB 453kB/s
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.6.4

(venv) rmohan@root:~/docker-ansible/todobackend$ pip install django-cors-headers
Collecting django-cors-headers
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading django_cors_headers-2.1.0-py2.py3-none-any.whl
Installing collected packages: django-cors-headers
Successfully installed django-cors-headers-2.1.0

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py startapp todo
(venv) rmohan@root:~/docker-ansible/todobackend/src$ tree
.
|– manage.py
|– todo
| |– admin.py
| |– apps.py
| |– __init__.py
| |– migrations
| | `– __init__.py
| |– models.py
| |– tests.py
| `– views.py
`– todobackend
|– __init__.py
|– __init__.pyc
|– settings.py
|– settings.pyc
|– urls.py
`– wsgi.py

3 directories, 14 files

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todobackend/settings.py
INSTALLED_APPS = [
.
.

‘rest_framework’,
‘corsheaders’,
‘todo’
]
.
.
MIDDLEWARE_CLASSES = [
‘django.middleware.security.SecurityMiddleware’,
‘corsheaders.middleware.CorsMiddleware’,
]

#Cors Settings do not do it in production
CORS_ORIGIN_ALLOW_ALL = True
Creating Models

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todo/models.py
# Create your models here.
class TodoItem(models.Model):
title = models.CharField(max_length=256, null=True, blank=True)
completed = models.BooleanField(blank=True, default=False)
url = models.CharField(max_length=256, null=True, blank=True)
order = models.IntegerField(null=True, blank=True)

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py makemigrations todo
Migrations for ‘todo’:
todo/migrations/0001_initial.py
– Create model TodoItem

(venv) rmohan@root:~/docker-ansible/todobackend/src$ tree
.
|– db.sqlite3
|– manage.py
|– todo
| |– admin.py
| |– admin.pyc
| |– apps.py
| |– __init__.py
| |– __init__.pyc
| |– migrations
| | |– 0001_initial.py
| | |– __init__.py
| | `– __init__.pyc
| |– models.py
| |– models.pyc
| |– tests.py
| `– views.py
`– todobackend
|– __init__.py
|– __init__.pyc
|– settings.py
|– settings.pyc
|– urls.py
|– urls.pyc
`– wsgi.py

3 directories, 21 files

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, todo
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying sessions.0001_initial… OK
Applying todo.0001_initial… OK

(venv) rmohan@root:~/docker-ansible/todobackend/src$ ls
db.sqlite3 manage.py todo todobackend
Creating Serializers

http://www.django-rest-framework.org/api-guide/serializers/

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todo/serializers.py
from rest_framework import serializers
from todo.models import TodoItem

class TodoItemSerializer(serialisers.HyperlinkedModelSerializer):
url = serializers.ReadOnlyField()
class Meta:
model = TodoItem
fields = (‘url’, ‘title’, ‘completed’, ‘order’)
Create Views

http://www.django-rest-framework.org/api-guide/views/

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todo/views.py
# -*- coding: utf-8 -*-
#from __future__ import unicode_literals
#from django.shortcuts import render
from todo.models import TodoItem
from todo.serializers import TodoItemSerializer
from rest_framework import status
from rest_framework import viewsets
from rest_framework.reverse import reverse
from rest_framework.decorators import list_route
from rest_framework.response import Response
# Create your views here.
class TodoItemViewSet(viewsets.ModelViewSet):
queryset = TodoItem.objects.all()
serializer_class = TodoItemSerializer
Configure Routing

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todobackend/urls.py
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin

urlpatterns = [
url(r’^admin/’, admin.site.urls),
url(r’^’, include(‘todo.urls’)),
]

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todo/urls.py
from django.conf.urls import url, include
from todo import views
from rest_framework.routers import DefaultRouter

#create a router and registry our viewsets with it .
router = DefaultRouter(trailing_slash=False)
router.registry(r’todos’, views.TodoItemViewSet)

# the APi urls are now determined automatically by the router
urlpatterns = [
url(r’^’, include(router.urls)) ,
]
Test Driving the application

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).
September 14, 2017 – 09:12:44
Django version 1.11.5, using settings ‘todobackend.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

python1.png
Creating Tests

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todo/tests.py
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from todo.models import TodoItem

# Create your tests here.
def createItem(client):
url = reverse(‘todoitem-list’)
data = {‘title’: ‘Walk the dog’}
return client.post(url, data, format=’json’)

class TestCreateTodoItem(APITestCase):
“””
Ensure we can create a new todo item
“””
def setUp(self):
self.response = createItem(self.client)

def test_received_201_created_status_code(self):
self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)

def test_received_location_header_hyperlink(self):
self.assertRegexpMatches(self.response[‘Location’], ‘^http://.+/todos/[\d]+$’)

def test_item_was_created(self):
self.assertEqual(TodoItem.objects.count(), 1)

def test_item_has_correct_title(self):
self.assertEqual(TodoItem.objects.get().title, ‘Walk the dog’)

class TestUpdateTodoItem(APITestCase):
“””
Ensure we can update an existing todo item using PUT
“””
def setUp(self):
response = createItem(self.client)
self.assertEqual(TodoItem.objects.get().completed, False)
url = response[‘Location’]
data = {‘title’: ‘Walk the dog’, ‘completed’: True}
self.response = self.client.put(url, data, format=’json’)

def test_received_200_created_status_code(self):
self.assertEqual(self.response.status_code, status.HTTP_200_OK)

def test_item_was_updated(self):
self.assertEqual(TodoItem.objects.get().completed, True)

class TestPatchTodoItem(APITestCase):
“””
Ensure we can update an existing todo item using PATCH
“””
def setUp(self):
response = createItem(self.client)
self.assertEqual(TodoItem.objects.get().completed, False)
url = response[‘Location’]
data = {‘title’: ‘Walk the dog’, ‘completed’: True}
self.response = self.client.patch(url, data, format=’json’)

def test_received_200_ok_status_code(self):
self.assertEqual(self.response.status_code, status.HTTP_200_OK)

def test_item_was_updated(self):
self.assertEqual(TodoItem.objects.get().completed, True)

class TestDeleteTodoItem(APITestCase):
“””
Ensure we can delete a todo item
“””
def setUp(self):
response = createItem(self.client)
self.assertEqual(TodoItem.objects.count(), 1)
url = response[‘Location’]
self.response = self.client.delete(url)

def test_received_204_no_content_status_code(self):
self.assertEqual(self.response.status_code, status.HTTP_204_NO_CONTENT)

def test_the_item_was_deleted(self):
self.assertEqual(TodoItem.objects.count(), 0)

class TestDeleteAllItems(APITestCase):
“””
Ensure we can delete all todo items
“””
def setUp(self):
createItem(self.client)
createItem(self.client)
self.assertEqual(TodoItem.objects.count(), 2)
self.response = self.client.delete(reverse(‘todoitem-list’))

def test_received_204_no_content_status_code(self):
self.assertEqual(self.response.status_code, status.HTTP_204_NO_CONTENT)

def test_all_items_were_deleted(self):
self.assertEqual(TodoItem.objects.count(), 0)

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py test
Creating test database for alias ‘default’…
System check identified no issues (0 silenced).
…………
———————————————————————-
Ran 12 tests in 0.201s

OK
Destroying test database for alias ‘default’…

Unit Vs Integration Tests

(venv) rmohan@root:~/docker-ansible/todobackend/src$ mkdir todobackend/settings

(venv) rmohan@root:~/docker-ansible/todobackend/src$ touch todobackend/settings/__init__.py

(venv) rmohan@root:~/docker-ansible/todobackend/src$ cp todobackend/__init__.py todobackend/settings/base.py

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todobackend/settings/base.py
“””
Django settings for todobackend project.

Generated by ‘django-admin startproject’ using Django 1.9.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
“””

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, …)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings – unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ‘#%8=y*l-+z*+mot(0+17@lm%!_yt4zb*j69fag-x((vz^zif(l’

# SECURITY WARNING: don’t run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘rest_framework’,
‘corsheaders’,
‘todo’
]

MIDDLEWARE_CLASSES = [
‘django.middleware.security.SecurityMiddleware’,
‘corsheaders.middleware.CorsMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.auth.middleware.SessionAuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,
]

ROOT_URLCONF = ‘todobackend.urls’

TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: [],
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]

WSGI_APPLICATION = ‘todobackend.wsgi.application’

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
}
}

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
‘NAME’: ‘django.contrib.auth.password_validation.UserAttributeSimilarityValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.MinimumLengthValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.CommonPasswordValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.NumericPasswordValidator’,
},
]

# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = ‘en-us’

TIME_ZONE = ‘UTC’

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = ‘/static/’

# CORS Settings

CORS_ORIGIN_ALLOW_ALL = True

(venv) rmohan@root:~/docker-ansible/todobackend/src$ rm -rf todobackend/settings.py

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim manage.py
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “todobackend.settings.base”)

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todobackend/wsgi.py
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “todobackend.settings.base”)

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim todobackend/settings/test.py
from base import *
import os

# Installed Apps
INSTALLED_APPS += (‘django_nose’, )
TEST_RUNNER = ‘django_nose.NoseTestSuiteRunner’
TEST_OUTPUT_DIR = os.environ.get(‘TEST_OUTPUT_DIR’,’.’)
NOSE_ARGS = [
‘–verbosity=2’, # verbose output
‘–nologcapture’, # don’t output log capture
‘–with-coverage’, # activate coverage report
‘–cover-package=todo’, # coverage reports will apply to these packages
‘–with-spec’, # spec style tests
‘–spec-color’,
‘–with-xunit’, # enable xunit plugin
‘–xunit-file=%s/unittests.xml’ % TEST_OUTPUT_DIR,
‘–cover-xml’, # produce XML coverage info
‘–cover-xml-file=%s/coverage.xml’ % TEST_OUTPUT_DIR,
]

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: os.environ.get(‘MYSQL_DATABASE’,’todobackend’),
‘USER’: os.environ.get(‘MYSQL_USER’,’todo’),
‘PASSWORD’: os.environ.get(‘MYSQL_PASSWORD’,’password’),
‘HOST’: os.environ.get(‘MYSQL_HOST’,’localhost’),
‘PORT’: os.environ.get(‘MYSQL_PORT’,’3306?),
}
}

Installing Mysql

(venv) rmohan@root:~/docker-ansible/todobackend/src$ sudo apt-get install mysql-server

(venv) rmohan@root:~/docker-ansible/todobackend/src$ sudo mysql_secure_installation

(venv) rmohan@root:~/docker-ansible/todobackend/src$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.5.57-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>
mysql> create database todobackend;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on *.* to ‘todo’@’localhost’ identified by ‘password‘;

Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

(venv) rmohan@root:~/docker-ansible/todobackend/src$ pip install mysql-python
Collecting mysql-python
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Using cached MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
sh: 1: mysql_config: not found
Traceback (most recent call last):
File “”, line 1, in
File “/tmp/pip-build-dmghKR/mysql-python/setup.py”, line 17, in
metadata, options = get_config()
File “setup_posix.py”, line 43, in get_config
libs = mysql_config(“libs_r”)
File “setup_posix.py”, line 25, in mysql_config
raise EnvironmentError(“%s not found” % (mysql_config.path,))
EnvironmentError: mysql_config not found

—————————————-
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-dmghKR/mysql-python/

(venv) rmohan@root:~/docker-ansible/todobackend/src$ sudo apt-get install python-pip python-dev libmysqlclient-dev

(venv) rmohan@root:~/docker-ansible/todobackend/src$ pip install mysql-python
Collecting mysql-python
Using cached MySQL-python-1.2.5.zip
Building wheels for collected packages: mysql-python
Running setup.py bdist_wheel for mysql-python … done
Stored in directory: /home/rmohan/.cache/pip/wheels/38/a3/89/ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Successfully built mysql-python
Installing collected packages: mysql-python
Successfully installed mysql-python-1.2.5

(venv) rmohan@root:~/docker-ansible/todobackend/src$ export DJANGO_SETTINGS_MODULE=todobackend.settings.test

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py –settings=todobackend.settings.test
Traceback (most recent call last):
File “manage.py”, line 10, in
execute_from_command_line(sys.argv)
File “/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py”, line 364, in execute_from_command_line
utility.execute()
File “/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py”, line 338, in execute
django.setup()
File “/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/django/__init__.py”, line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File “/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/django/apps/registry.py”, line 85, in populate
app_config = AppConfig.create(entry)
File “/home/rmohan/docker-ansible/todobackend/venv/local/lib/python2.7/site-packages/django/apps/config.py”, line 94, in create
module = import_module(entry)
File “/usr/lib/python2.7/importlib/__init__.py”, line 37, in import_module
__import__(name)
ImportError: No module named django_nose

(venv) rmohan@root:~/docker-ansible/todobackend/src$ pip install django_nose

(venv) rmohan@root:~/docker-ansible/todobackend/src$ pip install pinocchio coverage

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions, todo.
Run ‘python manage.py migrate’ to apply them.

September 14, 2017 – 15:14:06
Django version 1.11.5, using settings ‘todobackend.settings.test’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection ‘default’
HINT: MySQL’s Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, todo
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying sessions.0001_initial… OK
Applying todo.0001_initial… OK
(venv) rmohan@root:~/docker-ansible/todobackend/src$
(venv) rmohan@root:~/docker-ansible/todobackend/src$ python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).
September 14, 2017 – 15:14:56
Django version 1.11.5, using settings ‘todobackend.settings.test’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

(venv) rmohan@root:~/docker-ansible/todobackend/src$ deactivate
rmohan@root:~/docker-ansible/todobackend/src$

rmohan@root:~/docker-ansible/todobackend/src$ cd ../../

rmohan@root:~/docker-ansible$ mkdir todobackend-specs

rmohan@root:~/docker-ansible$ cd todobackend-specs/

rmohan@root:~/docker-ansible/todobackend-specs$ git init
Initialized empty Git repository in /home/rmohan/docker-ansible/todobackend-specs/.git/

rmohan@root:~/docker-ansible/todobackend-specs$ touch .gitignore
node_module

rmohan@root:~/docker-ansible/todobackend-specs$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install –save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (todobackend-specs)
version: (0.0.0) 0.1.0
description: “Todo Backend Acceptence Test”
entry point: (index.js) app.js
test command: mocha
git repository:
keywords:
author: mshaik
license: (BSD-2-Clause)
About to write to /home/rmohan/docker-ansible/todobackend-specs/package.json:

{
“name”: “todobackend-specs”,
“version”: “0.1.0”,
“description”: “\”Todo Backend Acceptence Test\””,
“main”: “app.js”,
“scripts”: {
“test”: “mocha”
},
“author”: “mshaik”,
“license”: “BSD-2-Clause”
}

Is this ok? (yes)
rmohan@root:~/docker-ansible/todobackend-specs$ ls
package.json
rmohan@root:~/docker-ansible/todobackend-specs$ cat package.json
{
“name”: “todobackend-specs”,
“version”: “0.1.0”,
“description”: “\”Todo Backend Acceptence Test\””,
“main”: “app.js”,
“scripts”: {
“test”: “mocha”
},
“author”: “mshaik”,
“license”: “BSD-2-Clause”
}

rmohan@root:~/docker-ansible/todobackend-specs$ sudo npm install -g n
npm http GET https://registry.npmjs.org/n
npm http 200 https://registry.npmjs.org/n
npm http GET https://registry.npmjs.org/n/-/n-2.1.8.tgz
npm http 200 https://registry.npmjs.org/n/-/n-2.1.8.tgz
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
n@2.1.8 /usr/local/lib/node_modules/n

rmohan@root:~/docker-ansible/todobackend-specs$ sudo n stable

install : node-v8.4.0
mkdir : /usr/local/n/versions/node/8.4.0
fetch : https://nodejs.org/dist/v8.4.0/node-v8.4.0-linux-x64.tar.gz
######################################################################## 100.0%
installed : v8.4.0

rmohan@root:~/docker-ansible/todobackend-specs$ sudo npm install bluebird chai chai-as-promised mocha superagent superagent-promise
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN todobackend-specs@0.1.0 No repository field.

+ superagent-promise@1.1.0
+ superagent@3.6.0
+ chai@4.1.2
+ chai-as-promised@7.1.1
+ mocha@3.5.3
+ bluebird@3.5.0
added 66 packages in 9.374s

rmohan@root:~/docker-ansible/todobackend-specs$ cat package.json
{
“name”: “todobackend-specs”,
“version”: “0.1.0”,
“description”: “\”Todo Backend Acceptence Test\””,
“main”: “app.js”,
“scripts”: {
“test”: “mocha”
},
“author”: “mshaik”,
“license”: “BSD-2-Clause”,
“dependencies”: {
“bluebird”: “^3.5.0”,
“chai”: “^4.1.2”,
“chai-as-promised”: “^7.1.1”,
“mocha”: “^3.5.3”,
“superagent”: “^3.6.0”,
“superagent-promise”: “^1.1.0”
}
}

rmohan@root:~/docker-ansible/todobackend-specs$ sudo apt-get install nodejs-legacy

Building Base Image

rmohan@root:~/docker-ansible$ mkdir todobackend-base
rmohan@root:~/docker-ansible$ cd todobackend-base/

rmohan@root:~/docker-ansible/todobackend-base$ git init
Initialized empty Git repository in /home/rmohan/docker-ansible/todobackend-base/.git/

rmohan@root:~/docker-ansible/todobackend-base$ vim Dockerfile
FROM ubuntu:trusty
MAINTAINER rafi494

# Prevent dpkg errors
ENV TERM=xterm-256color

# Set mirrors to NZ
# RUN sed -i “s/http:\/\/archive./http:\/\/nz.archive./g” /etc/apt/sources.list

# Install Python runtime
RUN apt-get update && \
apt-get install -qy \
-o APT::Install-Recommend=false -o APT::Install-Suggests=false \
python python-virtualenv libpython2.7 python-mysqldb

# Create virtual environment
# Upgrade PIP in virtual environment to latest version
RUN virtualenv /appenv && \
. /appenv/bin/activate && \
pip install pip –upgrade

# Add entrypoint script
ADD scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT [“entrypoint.sh”]

LABEL application=todobackend

rmohan@root:~/docker-ansible/todobackend-base$ mkdir scripts
rmohan@root:~/docker-ansible/todobackend-base$ vim scripts/entrypoint.sh
#!/bin/bash
. /appenv/bin/activate
exec $@

rmohan@root:~/docker-ansible/todobackend-base$ docker build -t rafi494/todobackend-base .
Sending build context to Docker daemon 34.82 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:trusty
trusty: Pulling from ubuntu

77ec837caf85: Pull complete
27fb25a22e6b: Pull complete
668a3fc0cef3: Pull complete
ad221bf67cf4: Pull complete
93f6219c4eb0: Pull complete
b44ce450cb60: Pull complete
Digest: sha256:404e4e6352099db4d3fb2781a13c3b3b090d3bcf61fc2b30b009c3987199a3aa
Status: Downloaded newer image for ubuntu:trusty
—> b44ce450cb60
Step 1 : MAINTAINER rafi494
—> Running in e2e87972a04c
—> 6e2f149ea112
Removing intermediate container e2e87972a04c
Step 2 : ENV TERM xterm-256color
—> Running in e22a9e2499db
—> c413f9b3a937
Removing intermediate container e22a9e2499db
Step 3 : RUN apt-get update && apt-get install -qy -o APT::Install-Recommend=false -o APT::Install-Suggests=false python python-virtualenv libpython2.7 python-mysqldb
—> Running in 81df16349ae6
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
.
.
Running hooks in /etc/ca-certificates/update.d….done.
—> 38d9bf507eb3
Removing intermediate container 81df16349ae6
Step 4 : RUN virtualenv /appenv && . /appenv/bin/activate && pip install pip –upgrade
—> Running in e454212014dd
New python executable in /appenv/bin/python
Installing setuptools, pip…done.
Downloading/unpacking pip from https://pypi.python.org/packages/b6/ac/7015eb97dc749283ffdec1c3a88ddb8ae03b8fad0f0e611408f196358da3/pip-9.0.1-py2.py3-none-any.whl#md5=297dbd16ef53bcef0447d245815f5144
Installing collected packages: pip
Found existing installation: pip 1.5.4
Uninstalling pip:
Successfully uninstalled pip
Successfully installed pip
Cleaning up…
—> 8bd2596212ea
Removing intermediate container e454212014dd
Step 5 : ADD scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
—> 5090f2d23cbc
Removing intermediate container 79bc20d7085a
Step 6 : RUN chmod +x /usr/local/bin/entrypoint.sh
—> Running in 77a421e12cf2
—> 4a33b5f19bcf
Removing intermediate container 77a421e12cf2
Step 7 : ENTRYPOINT entrypoint.sh
—> Running in f99f99500432
—> c860003f9b59
Removing intermediate container f99f99500432
Step 8 : LABEL application todobackend
—> Running in 5ad48a0bdd6d
—> 00a95d4fd18f
Removing intermediate container 5ad48a0bdd6d
Successfully built 00a95d4fd18f

rmohan@root:~/docker-ansible/todobackend-base$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rafi494/todobackend-base latest 00a95d4fd18f 2 minutes ago 378.8 MB

rmohan@root:~/docker-ansible/todobackend-base$ docker run -rm rafi494/todobackend-base ps
PID TTY TIME CMD
1 ? 00:00:00 ps

rmohan@root:~/docker-ansible/todobackend-base$ vim scripts/entrypoint.sh
#!/bin/bash
. /appenv/bin/activate
$@

rmohan@root:~/docker-ansible/todobackend-base$ docker build -t rafi494/todobackend-base .
Sending build context to Docker daemon 34.82 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:trusty
—> b44ce450cb60
Step 1 : MAINTAINER rafi494
—> Using cache
—> 6e2f149ea112
Step 2 : ENV TERM xterm-256color
—> Using cache
—> c413f9b3a937
Step 3 : RUN apt-get update && apt-get install -qy -o APT::Install-Recommend=false -o APT::Install-Suggests=false python python-virtualenv libpython2.7 python-mysqldb
—> Using cache
—> 38d9bf507eb3
Step 4 : RUN virtualenv /appenv && . /appenv/bin/activate && pip install pip –upgrade
—> Using cache
—> 8bd2596212ea
Step 5 : ADD scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
—> f0b62f95219f
Removing intermediate container a70509f7c735
Step 6 : RUN chmod +x /usr/local/bin/entrypoint.sh
—> Running in de0ca94753ad
—> 43626306b958
Removing intermediate container de0ca94753ad
Step 7 : ENTRYPOINT entrypoint.sh
—> Running in e82c88a26500
—> 7936a208c626
Removing intermediate container e82c88a26500
Step 8 : LABEL application todobackend
—> Running in e29c5e4599a0
—> 43d184525209
Removing intermediate container e29c5e4599a0
Successfully built 43d184525209

rmohan@root:~/docker-ansible/todobackend-base$ docker run -rm rafi494/todobackend-base ps
PID TTY TIME CMD
1 ? 00:00:00 entrypoint.sh
11 ? 00:00:00 ps

rmohan@root:~/docker-ansible/todobackend-base$ vim scripts/entrypoint.sh
#!/bin/bash
. /appenv/bin/activate
exec $@

rmohan@root:~/docker-ansible/todobackend-base$ docker run -rm rafi494/todobackend-base ps
PID TTY TIME CMD
1 ? 00:00:00 ps

Creating Devlopment Image

rmohan@root:~/docker-ansible/todobackend-base$ cd ../todobackend
rmohan@root:~/docker-ansible/todobackend$ tree -L 1
.
|– src
`– venv

2 directories, 0 files

rmohan@root:~/docker-ansible/todobackend$ mkdir -p docker/dev
rmohan@root:~/docker-ansible/todobackend$ cd docker/dev/

rmohan@root:~/docker-ansible/todobackend/docker/dev$ touch Dockerfile
FROM rafi494/todobackend-base:latest
MAINTAINER rafi

# Install dev/build dependencies
RUN apt-get update && \
apt-get install -qy python-dev libmysqlclient-dev

# Activate virtual environment and install wheel support
RUN . /appenv/bin/activate && \
pip install wheel –upgrade

# PIP environment variables (NOTE: must be set after installing wheel)
ENV WHEELHOUSE=/wheelhouse PIP_WHEEL_DIR=/wheelhouse PIP_FIND_LINKS=/wheelhouse XDG_CACHE_HOME=/cache

# OUTPUT: Build artefacts (Wheels) are output here
VOLUME /wheelhouse

# OUTPUT: Build cache
VOLUME /build

# OUTPUT: Test reports are output here
VOLUME /reports

# Add test entrypoint script
COPY scripts/test.sh /usr/local/bin/test.sh
RUN chmod +x /usr/local/bin/test.sh

# Set defaults for entrypoint and command string
ENTRYPOINT [“test.sh”]
CMD [“python”, “manage.py”, “test”, “–noinput”]

# Add application source
COPY src /application
WORKDIR /application

rmohan@root:~/docker-ansible/todobackend/docker/dev$ vim ../../scripts/test.sh
#!/bin/bash
# Activate virtual environment
. /appenv/bin/activate

# Download requirements to build cache
pip download -d /build -r requirements_test.txt –no-input

# Install application test requirements
pip install –no-index -f /build -r requirements_test.txt

# Run test.sh arguments
exec $@

Creating application Requirement files

rmohan@root:~/docker-ansible/todobackend/docker/dev$ cd ../..rmohan@root:~/docker-ansible/todobackend$ source venv/bin/activate
(venv) rmohan@root:~/docker-ansible/todobackend$ cd src/
(venv) rmohan@root:~/docker-ansible/todobackend/src$ pip freeze > requirements.txt

(venv) rmohan@root:~/docker-ansible/todobackend/src$ cat requirements.txt
colorama==0.3.9
coverage==4.4.1
Django==1.11.5
django-cors-headers==2.1.0
django-nose==1.4.5
djangorestframework==3.6.4
MySQL-python==1.2.5
nose==1.3.7
pinocchio==0.4.2
pytz==2017.2

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim requirements.txt
Django==1.11.5
django-cors-headers==2.1.0
djangorestframework==3.6.4
MySQL-python==1.2.5

(venv) rmohan@root:~/docker-ansible/todobackend/src$ vim requirements_test.txt
-r requirements.txt
colorama==0.3.9
coverage==4.4.1
django-nose==1.4.5
nose==1.3.7
pinocchio==0.4.2

Devlopment Image review
Testing Devlopment Image
(venv) rmohan@root:~/docker-ansible/todobackend/src$ cd ..
(venv) rmohan@root:~/docker-ansible/todobackend$ docker build -t todobackend-dev -f docker/dev/Dockerfile .
Sending build context to Docker daemon 50.65 MB
Sending build context to Docker daemon
Step 0 : FROM rafi494/todobackend-base:latest
—> a2ca1f3870d7
Step 1 : MAINTAINER rafi
—> Running in c684859b3076
—> 8d359fc022e8
Removing intermediate container c684859b3076
Step 2 : RUN apt-get update && apt-get install -qy python-dev libmysqlclient-dev
—> Running in de5056a6328e
.

Step 3 : RUN . /appenv/bin/activate && pip install wheel –upgrade
—> Running in 37e84294c1e6
Collecting wheel

—> b9e7992d4e62
Removing intermediate container 37e84294c1e6
Step 4 : ENV WHEELHOUSE /wheelhouse PIP_WHEEL_DIR /wheelhouse PIP_FIND_LINKS /wheelhouse XDG_CACHE_HOME /cache
—> Running in 5d94907105e3
—> 101e00962323
Removing intermediate container 5d94907105e3
Step 5 : VOLUME /wheelhouse
—> Running in 64488d8f2d97
—> 4607e17f46e1
Removing intermediate container 64488d8f2d97
Step 6 : VOLUME /build
—> Running in b6607d721ded
—> 004347875e10
Removing intermediate container b6607d721ded
Step 7 : VOLUME /reports
—> Running in 5999cc6683b4
—> 9b238fd38525
Removing intermediate container 5999cc6683b4
Step 8 : COPY scripts/test.sh /usr/local/bin/test.sh
—> 07f3369050ee
Removing intermediate container 18bac283237a
Step 9 : RUN chmod +x /usr/local/bin/test.sh
—> Running in ca52d4b7e065
—> ce20f7376ca9
Removing intermediate container ca52d4b7e065
Step 10 : ENTRYPOINT test.sh
—> Running in ae410324d55f
—> 78e8c1392b51
Removing intermediate container ae410324d55f
Step 11 : CMD python manage.py test –noinput
—> Running in a1a4eb631e76
—> 20f2cf563d00
Removing intermediate container a1a4eb631e76
Step 12 : COPY src /application
—> ae7f0b9dad4f
Removing intermediate container 59727c5df809
Step 13 : WORKDIR /application
—> Running in c629a3601c4e
—> d7a363a684d8
Removing intermediate container c629a3601c4e

(venv) rmohan@root:~/docker-ansible/todobackend$ vim .dockerignore
venv

(venv) rmohan@root:~/docker-ansible/todobackend$ docker build -t todobackend-dev -f docker/dev/Dockerfile .
Sending build context to Docker daemon 165.9 kB

(venv) rmohan@root:~/docker-ansible/todobackend$ docker run –rm todobackend-devCollecting Django==1.11.5 (from -r requirements.txt (line 1))
/appenv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/appenv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading Django-1.11.5-py2.py3-none-any.whl (6.9MB)
Saved /build/Django-1.11.5-py2.py3-none-any.whl
Collecting django-cors-headers==2.1.0 (from -r requirements.txt (line 2))
Downloading django_cors_headers-2.1.0-py2.py3-none-any.whl
Saved /build/django_cors_headers-2.1.0-py2.py3-none-any.whl
Collecting djangorestframework==3.6.4 (from -r requirements.txt (line 3))
Downloading djangorestframework-3.6.4-py2.py3-none-any.whl (1.5MB)
Saved /build/djangorestframework-3.6.4-py2.py3-none-any.whl
Collecting MySQL-python==1.2.5 (from -r requirements.txt (line 4))
Downloading MySQL-python-1.2.5.zip (108kB)
Saved /build/MySQL-python-1.2.5.zip
Collecting colorama==0.3.9 (from -r requirements_test.txt (line 2))
Downloading colorama-0.3.9-py2.py3-none-any.whl
Saved /build/colorama-0.3.9-py2.py3-none-any.whl
Collecting coverage==4.4.1 (from -r requirements_test.txt (line 3))
Downloading coverage-4.4.1-cp27-cp27mu-manylinux1_x86_64.whl (193kB)
Saved /build/coverage-4.4.1-cp27-cp27mu-manylinux1_x86_64.whl
Collecting django-nose==1.4.5 (from -r requirements_test.txt (line 4))
Downloading django_nose-1.4.5-py2.py3-none-any.whl
Saved /build/django_nose-1.4.5-py2.py3-none-any.whl
Collecting nose==1.3.7 (from -r requirements_test.txt (line 5))
Downloading nose-1.3.7-py2-none-any.whl (154kB)
Saved /build/nose-1.3.7-py2-none-any.whl
Collecting pinocchio==0.4.2 (from -r requirements_test.txt (line 6))
Downloading pinocchio-0.4.2.tar.gz
Saved /build/pinocchio-0.4.2.tar.gz
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)
Saved /build/pytz-2017.2-py2.py3-none-any.whl
Successfully downloaded Django django-cors-headers djangorestframework MySQL-python colorama coverage django-nose nose pinocchio pytz
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
Collecting django-cors-headers==2.1.0 (from -r requirements.txt (line 2))
Collecting djangorestframework==3.6.4 (from -r requirements.txt (line 3))
Collecting MySQL-python==1.2.5 (from -r requirements.txt (line 4))
Collecting colorama==0.3.9 (from -r requirements_test.txt (line 2))
Collecting coverage==4.4.1 (from -r requirements_test.txt (line 3))
Collecting django-nose==1.4.5 (from -r requirements_test.txt (line 4))
Collecting nose==1.3.7 (from -r requirements_test.txt (line 5))
Collecting pinocchio==0.4.2 (from -r requirements_test.txt (line 6))
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
Building wheels for collected packages: MySQL-python, pinocchio
Running setup.py bdist_wheel for MySQL-python: started
Running setup.py bdist_wheel for MySQL-python: finished with status ‘done’
Stored in directory: /cache/pip/wheels/16/ed/55/f27783bb5ab1cb57c9ac00356859d19adf17d76c31230f3f1f
Running setup.py bdist_wheel for pinocchio: started
Running setup.py bdist_wheel for pinocchio: finished with status ‘done’
Stored in directory: /cache/pip/wheels/ab/43/84/ba075171b712e03d94d14b1e264a80678dbca7ebb8bfe4f7b3
Successfully built MySQL-python pinocchio
Installing collected packages: pytz, Django, django-cors-headers, djangorestframework, MySQL-python, colorama, coverage, nose, django-nose, pinocchio
Successfully installed Django-1.11.5 MySQL-python-1.2.5 colorama-0.3.9 coverage-4.4.1 django-cors-headers-2.1.0 django-nose-1.4.5 djangorestframework-3.6.4 nose-1.3.7 pinocchio-0.4.2 pytz-2017.2
…………
———————————————————————-
Ran 12 tests in 0.098s

OK
Creating test database for alias ‘default’…
System check identified no issues (0 silenced).
Destroying test database for alias ‘default’…

How to reduce docker run time

(venv) rmohan@root:~/docker-ansible/todobackend$ docker run -v /tmp/cache:/cache –entrypoint true –name cache todobackend-dev(venv)

(venv) rmohan@root:~/docker-ansible/todobackend$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
664e669f0a1a todobackend-dev:latest “true” 30 seconds ago Exited (0) 29 seconds ago cache

(venv) rmohan@root:~/docker-ansible/todobackend$ time docker run –rm –volumes-from cache todobackend-devCollecting Django==1.11.5 (from -r requirements.txt (line 1))
OK
Creating test database for alias ‘default’…
System check identified no issues (0 silenced).
Destroying test database for alias ‘default’…

real 0m27.803s
user 0m0.028s
sys 0m0.012s

(venv) rmohan@root:~/docker-ansible/todobackend$ time docker run –rm –volumes-from cache todobackend-dev
real 0m11.441s
user 0m0.028s
sys 0m0.008s

Using diffrent test settings

(venv) rmohan@root:~/docker-ansible/todobackend$ time docker run –rm -e DJANGO_SETTINGS=todobackend.settings.test –volumes-from cache todobackend-dev
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
File was already downloaded /build/Django-1.11.5-py2.py3-none-any.whl
Collecting django-cors-headers==2.1.0 (from -r requirements.txt (line 2))
File was already downloaded /build/django_cors_headers-2.1.0-py2.py3-none-any.whl
Collecting djangorestframework==3.6.4 (from -r requirements.txt (line 3))
File was already downloaded /build/djangorestframework-3.6.4-py2.py3-none-any.whl
Collecting MySQL-python==1.2.5 (from -r requirements.txt (line 4))
File was already downloaded /build/MySQL-python-1.2.5.zip
Collecting colorama==0.3.9 (from -r requirements_test.txt (line 2))
File was already downloaded /build/colorama-0.3.9-py2.py3-none-any.whl
Collecting coverage==4.4.1 (from -r requirements_test.txt (line 3))
File was already downloaded /build/coverage-4.4.1-cp27-cp27mu-manylinux1_x86_64.whl
Collecting django-nose==1.4.5 (from -r requirements_test.txt (line 4))
File was already downloaded /build/django_nose-1.4.5-py2.py3-none-any.whl
Collecting nose==1.3.7 (from -r requirements_test.txt (line 5))
File was already downloaded /build/nose-1.3.7-py2-none-any.whl
Collecting pinocchio==0.4.2 (from -r requirements_test.txt (line 6))
File was already downloaded /build/pinocchio-0.4.2.tar.gz
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
File was already downloaded /build/pytz-2017.2-py2.py3-none-any.whl
Successfully downloaded Django django-cors-headers djangorestframework MySQL-python colorama coverage django-nose nose pinocchio pytz
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
Collecting django-cors-headers==2.1.0 (from -r requirements.txt (line 2))
Collecting djangorestframework==3.6.4 (from -r requirements.txt (line 3))
Collecting MySQL-python==1.2.5 (from -r requirements.txt (line 4))
Collecting colorama==0.3.9 (from -r requirements_test.txt (line 2))
Collecting coverage==4.4.1 (from -r requirements_test.txt (line 3))
Collecting django-nose==1.4.5 (from -r requirements_test.txt (line 4))
Collecting nose==1.3.7 (from -r requirements_test.txt (line 5))
Collecting pinocchio==0.4.2 (from -r requirements_test.txt (line 6))
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
Installing collected packages: pytz, Django, django-cors-headers, djangorestframework, MySQL-python, colorama, coverage, nose, django-nose, pinocchio
Successfully installed Django-1.11.5 MySQL-python-1.2.5 colorama-0.3.9 coverage-4.4.1 django-cors-headers-2.1.0 django-nose-1.4.5 djangorestframework-3.6.4 nose-1.3.7 pinocchio-0.4.2 pytz-2017.2
…………
———————————————————————-
Ran 12 tests in 0.100s

OK
Creating test database for alias ‘default’…
System check identified no issues (0 silenced).
Destroying test database for alias ‘default’…

real 0m20.798s
user 0m0.032s
sys 0m0.032s

Creating multicontainer environment using docker compose

(venv) rmohan@root:~/docker-ansible/todobackend$ vim docker/dev/docker-compose.yml
test:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes_from:
– cache
links:
– db
environment:
DJANGO_SETTINGS_MODULE: todobackend.settings.test
MYSQL_HOST: db
MYSQL_USER: root
MYSQL_PASSWORD: password
TEST_OUTPUT_DIR: /reports

builder:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes_from:
– cache
entrypoint: “entrypoint.sh”
command: [“pip”, “wheel”, “–no-index”, “-f /build”, “.”]

agent:
image: jmenga/ansible
links:
– db
environment:
PROBE_HOST: “db”
PROBE_PORT: “3306”
command: [“probe.yml”]

db:
image: mysql:5.6
hostname: db
expose:
– “3306”
environment:
MYSQL_ROOT_PASSWORD: password

cache:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes:
– /tmp/cache:/cache
– /build
entrypoint: “true”

(venv) rmohan@root:~/docker-ansible/todobackend$ cd docker/dev/

(venv) rmohan@root:~/docker-ansible/todobackend/docker/dev$ docker-compose up test

how to use sysctl with ansible

[root@localhost ~]# sysctl -a |grep vm.swappiness
vm.swappiness = 30

[root@localhost ~]# ansible-galaxy init sysctl
– sysctl was created successfully

[root@localhost ~]# ansible-doc sysctl

[root@localhost ~]# vim test.yml

– hosts: localhost
roles:
– sysctl
vars:
sysctl_settings:
– name: vm.swappiness
value: 90

[root@localhost ~]# vim sysctl/tasks/main.yml

# tasks file for sysctl
– name: sysctl settings
sysctl:
name: “{{ item.name }}”
value: “{{ item.value }}”
reload: true
state: “{{ item.state | default(‘present’) }}”
with_items: “{{ sysctl_settings }}”

[root@localhost ~]# ansible-playbook test.yml

PLAY [localhost] *******************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [localhost]

TASK [sysctl : sysctl settings] ****************************************************************************************************************
changed: [localhost] => (item={u’state’: u’present’, u’name’: u’vm.swappiness’, u’value’: 90})

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

[root@localhost ~]# sysctl -a |grep vm.swappiness
vm.swappiness = 90

Page 5 of 174« First...34567...102030...Last »