Custom Search

Tuesday, May 28, 2019

How to list all databases in the mongo shell

mongo -u root

use admin

show dbs;
db.adminCommand('listDatabases')

db.system.users.find()

db.system.users.find().pretty()

How to show all users of all databases in mongodb


mongo -u root

use admin

show users;
db.getUsers()

db.system.users.find()

db.system.users.find().pretty()

bitnami docker mongodb login username password - kubernetes

#https://github.com/bitnami/bitnami-docker-mongodb
#mongodbUsername: username
#mongodbPassword: password
#mongodbDatabase: mydb1
#mongodbRootPassword: password

mongo mydb1 -u username

mongo -u admin

db.auth('root', 'password')

db.auth('username', 'password')

Monday, May 20, 2019

How to Create Dockerfile from an Existing Image

1)
$ sudo docker history [image_id_or_name]

$ sudo docker history [image_id_or_name] --no-trunc $argv  | tac | tr -s ' ' | cut -d " " -f 5- | sed 's,^/bin/sh -c #(nop) ,,g' | sed 's,^/bin/sh -c,RUN,g' | sed 's, && ,\n  & ,g' | sed 's,\s*[0-9]*[\.]*[0-9]*[kMG]*B\s*$,,g' | head -n -1


2)
sudo docker run -v /var/run/docker.sock:/var/run/docker.sock --rm chenzj/dfimage [image_id_or_name]

How to see Docker Image Contents

1)
$ sudo docker history [image_id_or_name]

$ sudo docker history [image_id_or_name] --no-trunc $argv  | tac | tr -s ' ' | cut -d " " -f 5- | sed 's,^/bin/sh -c #(nop) ,,g' | sed 's,^/bin/sh -c,RUN,g' | sed 's, && ,\n  & ,g' | sed 's,\s*[0-9]*[\.]*[0-9]*[kMG]*B\s*$,,g' | head -n -1


2)
sudo docker run -v /var/run/docker.sock:/var/run/docker.sock --rm chenzj/dfimage [image_id_or_name]

How to Generate a Dockerfile from an Image

1)
$ sudo docker history [image_id_or_name]

$ sudo docker history [image_id_or_name] --no-trunc $argv  | tac | tr -s ' ' | cut -d " " -f 5- | sed 's,^/bin/sh -c #(nop) ,,g' | sed 's,^/bin/sh -c,RUN,g' | sed 's, && ,\n  & ,g' | sed 's,\s*[0-9]*[\.]*[0-9]*[kMG]*B\s*$,,g' | head -n -1


2)
sudo docker run -v /var/run/docker.sock:/var/run/docker.sock --rm chenzj/dfimage [image_id_or_name]

 

Sunday, May 19, 2019

How to Install Helm and Create Helm Chart on Ubuntu

0)
Verify the status of kubernetes cluster
kubectl get nodes
#
kubectl get pods --all-namespaces
#
kubectl cluster-info
#
kubectl config get-contexts

1)
download the script from Helm's GitHub repository:

$ cd /tmp
$ curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > install-helm.sh

2)
Make the script executable
$ chmod u+x install-helm.sh

3)
#Install helm
$ ./install-helm.sh

4)
Installing Tiller
Tiller is a companion to the helm command that runs on your cluster, receiving commands from helm and communicating directly with the Kubernetes API to do the actual work of creating and deleting resources. To give Tiller the permissions it needs to run on the cluster, we are going to make a Kubernetes serviceaccount resource.

#Create the tiller serviceaccount:
$ kubectl -n kube-system create serviceaccount tiller

#bind the tiller serviceaccount to the cluster-admin role:
$ kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller

# Now we can run helm init, which installs Tiller on our cluster, along with some local housekeeping tasks such as downloading the stable repo details:
$ helm init --service-account tiller

#To verify that Tiller is running, list the pods in thekube-system namespace:
$ kubectl get pods --namespace kube-system

#Now that we've installed both Helm components, we're ready to use helm to install our first application.

5)
Installing a Helm Chart

#Use helm to install the kubernetes-dashboard package from the stable repo:
$ helm install stable/kubernetes-dashboard --name dashboard-demo

#We can ask Helm for a list of releases on this cluster:
$ helm list

# We can now use kubectl to verify that a new service has been deployed on the cluster:
$ kubectl get services

How to Install and Configure Kubernetes (k8s) on Ubuntu 18.04 LTS 19.04

1)
$ sudo apt update

#install docker
$ sudo apt-get install docker.io -y

$ sudo systemctl start docker
$ sudo systemctl enable docker

$ docker --version

2)

$ sudo apt-get install apt-transport-https curl -y

# add Kubernetes package repository key
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add

# configure Kubernetes repository
$ sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

$ sudo apt update

3)
#disable swap temporary,
$ sudo swapoff -a

4)
$ Kubeadm is one of the most common method used to deploy kubernetes cluster

#Install Kubeadm package
$ sudo apt-get install kubeadm -y

$ kubeadm version

#install the parts we need for Kubernetes
$ sudo apt-get install -y kubelet kubectl kubernetes-cni

5)
Kubernetes requires a Pod Network for the pods to communicate. For this guide we will use Flannel although there are several other Pod Networks availabl

#We can now initialize Kubernetes by running the initialization command and passing --pod-network-cidr which is required for Flannel to work correctly
$ sudo kubeadm init --pod-network-cidr=172.168.10.0/24


$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

#check status of node
$ kubectl get nodes

6)
Once Kubernetes has been initialized we then install the Flannel Pod Network by running.
Let’s deploy the pod network, Pod network is the network through which our cluster nodes will communicate with each other. We will deploy Flannel as our pod network, Flannel will provide the overlay network between cluster nodes.

First we need to set /proc/sys/net/bridge/bridge-nf-call-iptables to 1 to pass bridged IPv4 traffic to iptables` chains which is required by certain CNI networks (in this case Flannel). Do this by issueing

$ sudo sysctl net.bridge.bridge-nf-call-iptables=1

$ sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

#We can check that the pod is up by running
$ kubectl get pods --all-namespaces

7)
$ sudo  kubectl get nodes

8)
Because we are running only a single Kubernetes node we want to be able to run Pods on the master node. To do this we need to untaint the master node so it can run regular pods. To do so run
$ kubectl taint nodes --all node-role.kubernetes.io/master-













Saturday, May 18, 2019

How to Import an existing git project into GitLab

git clone --bare https://github.com/openstack/python-keystoneclient
cd python-keystoneclient.git/
git push --mirror https://gitlab.com/golangtutorials/python-keystoneclient.git

Monday, May 13, 2019

Git How to modify existing unpushed commit message

git pull
git commit --amend
git pull
git push -f

Sunday, May 12, 2019

Install a Python package into a different directory using pip

mkdir /home/puser/pypack_test1
mkdir /home/puser/pypack_test2

a)
pip install requests -t /home/puser/pypack_test1
pip install requests --target /home/puser/pypack_test1

b)
export PYTHONUSERBASE=/home/puser/pypack_test1
pip install --user requests

Saturday, May 11, 2019

Python Find which version of package is installed with pip

1)
pip show requests

2)
pip list | grep requests

3)
python
import requests
requests.__version__


Python difference between dist-packages and site-packages

#apt-get install request
#/usr/lib/python2.7/dist-packages

#easy_install and pip
#/usr/local/lib/python2.7/dist-packages

#form source code, setup.py install
#/usr/local/lib/python2.7/site-packages/

#pip install --user
#/home/puser/.local/lib/python2.7/site-packages/

How to find the location of Python site-packages directory

There are two types of site-packages directories, global and per user.

#Global site-packages:
import site
site.getsitepackages()

#Global site-packages:
python -m site

#Global site-packages:
import sys
sys.path

#User site-packages:
python -m site --user-site

#you can find the user base binary directory by running
python -m site --user-base


* Running #pip list or #pip freeze gives you a list of all installed global site-packages.
* Running #pip list --user or #pip freeze --user gives you a list of all installed per user site-packages.

How to find the location of Python module sources

1)
pip show request

2)
python
import request
request.__file__

Python What is the purpose “pip install --user"

There are two types of site-packages directories, global and per user.

#Global site-packages:
import site
site.getsitepackages()

#Global site-packages:
python -m site

#Global site-packages:
import sys
sys.path

#User site-packages:
python -m site --user-site

#you can find the user base binary directory by running
python -m site --user-base


* Running #pip list or #pip freeze gives you a list of all installed global site-packages.
* Running #pip list --user or #pip freeze --user gives you a list of all installed per user site-packages.

Wednesday, May 8, 2019

How to Redirect output of mongoDB query to a JSON file

a)
$ vim myquery.js
printjson(db.media.find().toArray())

b)
mongo mediadb myquery.js
mongo mediadb myquery.js > output.json

mongo [host]/[dbname] -u [username] -p [password] myquery.js > output.json

How to Redirect output of mongoDB query to a csv file

a)
$ vim export_to_cv.js

print("id,city")
cursor = db.media.find();
while (cursor.hasNext()) {
    jsonObject = cursor.next();
    print(jsonObject._id.valueOf() + "," + jsonObject.city)

}

b)
mongo mediadb export_to_cv.js

mongo [host]/[dbname] -u [username] -p [password] export_to_cv.js > output.csv

How to remove a field completely from a MongoDB document

db.media.find()
db.media.update({}, {$unset: {"new_field3": 1}},  {multi: true})
db.media.find()

db.media.find({'city':'BARRE'})
db.media.update({'city':'BARRE'}, {$unset: {"new_field2": 10}},  {multi: true})


//db.media.update({}, {$unset: {"new_field3": 1}},  {multi: true})
//db.media.update({'city':'BARRE'}, {$unset: {"new_field2": 10}},  {multi: true})

How to Rename a field for all Documents in a MongoDB Collection

db.media.find()
db.media.updateMany( {}, { $rename: { "new_field1": "new_field3" } } )
db.media.find()
db.media.updateMany( {'city':'BARRE'}, { $rename: { "new_field3": "new_field4" } } )
db.media.find({'city':'BARRE'})

//db.media.updateMany( {}, { $rename: { "new_field1": "new_field3" } } )
//db.media.updateMany( {'city':'BARRE'}, { $rename: { "new_field3": "new_field4" } } )

How to add new field to every document in a MongoDB collection


db.media.find()
db.media.update({}, {$set : {"new_field1":1}}, {upsert:false, multi:true})

db.media.update({'city':'BARRE'}, {$set : {"new_field2":2}}, {upsert:false, multi:true})

db.media.find()
db.media.find({'city':'BARRE'})


//db.media.update({}, {$set : {"new_field1":1}}, {upsert:false, multi:true})
//db.media.update({'city':'BARRE'}, {$set : {"new_field2":2}}, {upsert:false, multi:true})

Tuesday, May 7, 2019

How to MongoDB make a case-insensitive query?

db.media.find({city:/HAD/i}) // Note the 'i' flag for case-insensitivity
db.media.find({'city' : {'$regex' : '.*HAD.*', "$options" : "i"}}); // Note the 'i' flag for case-insensitivity

db.company_profile.find({ "companyName" : { "$regex" : "Nilesh" , "$options" : "i"}});

db.zipcodes.find({city : "NEW YORK"}); // Case-sensitive
db.zipcodes.find({city : /NEW york/i}); // Note the 'i' flag for case-insensitivity

How to drop a MongoDb database from Command Line

use

db.getUsers()
db.getRoles()
db.getCollectionNames()

db.dropAllUsers()
db.dropAllRoles()
db.dropDatabase()

Monday, May 6, 2019

mongodb Checking if a field contains a string

db.media.find({city:/HAD/})
db.media.find({'city' : {'$regex' : '.*HAD.*'}});

How to Pretty print in MongoDB shell

db.media.find().pretty();

db.media.find().toArray()

db.media.find().forEach(printjson);

How to list all collections in the mongodb shell ?

show collections
db.getCollectionNames()

How to Delete everything in a MongoDB database

use
db.dropAllUsers()
db.dropAllRoles()
db.dropDatabase()

How to query MongoDB with "like" ?

db.media.find({'city' : /HAD/}); //like '%HAD%'
db.media.find({'city' : {'$regex' : '.*HAD.*'}}); //like '%HAD%'

db.media.find({'city' : /^HAD/}); //like 'HAD%'
db.media.find({'city' : {'$regex' : '^HAD.*'}}); //like 'HAD%'

db.media.find({'city' : /HAD$/}); //like '%HAD'
db.media.find({'city' : {'$regex' : '.*HAD$'}}); //like '%HAD'

db.media.find({'city' : {'$regex' : '.*LEY$'}}).count(); //like '%LEY'
db.media.find({'city' : /LEY$/}).count(); //like '%LEY'
 

How to run mongoimport of json file

wget http://media.mongodb.org/zips.json
mongoimport --db mediadb --collection media --file zips.json
mongo
use mediadb
show collections
db.media.find().limit(2);

Wednesday, May 1, 2019

Install Atom Text Editor on Ubuntu 19.04 18.04

1)
sudo apt-get install snapd
sudo snap install atom --classic

OR

2)
sudo apt-get -y install wget

wget -qO - https://packagecloud.io/AtomEditor/atom/gpgkey | sudo apt-key add -

sudo sh -c 'echo "deb [arch=amd64] https://packagecloud.io/AtomEditor/atom/any/ any main" > /etc/apt/sources.list.d/atom.list'

sudo apt-get update
sudo apt-get -y install atom



Install Sublime Text 3 in Ubuntu 19.04 18.04

1)
run command to add the keyring:
wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -

2)
Then run command to add the apt repository:
echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list

3)
Install sublime text 3
sudo apt update && sudo apt install sublime-text