Riak: Enabling Solr Search 
General notes about Riak Search -ac

# install java JRE (so solr can run)
[entadmin@dev3 equeuelib]$ sudo yum install java-1.8.0-openjdk

# enable it in riak.conf: search=on
# reload/restart riak as needed

#create index (can also be done via PHP)
[aesteban@localhost ansible]$ curl -X PUT riak.example.com:8098/search/index/story_index

# associate index to bucket (can also be done via PHP)
[aesteban@localhost ansible]$ curl -X PUT riak.example.com:8098/buckets/story/props -H'content-type:application/json' -d'{"props":{"search_index":"story_index"}}'

# verify it by viewing bucket props, search for 'search_index'
riak.example.com:8098/buckets/story/props

# add indexable fields to a Story object per se
eg: authorId_i and publicationDate_dt

# query it !
riak.example.com:8098/search/query/story_index?wt=json&q=authorId_i:8144

# it returns all story keys associated with that author.

# looks like there's sort capabilities available too:
riak.example.com:8098/search/query/story_index?wt=json&q=authorId_i:8144&sort=publicationDate_dt%20desc

# by default it seems to put a limit of 10 rows, we can change that...
riak.example.com:8098/search/query/story_index?wt=json&q=authorId_i:8144&sort=publicationDate_dt%20desc&rows=50

# Solr dashboard riak.example.com:8093/internal_solr/#/story_index


PHP

$indexes = [
[
"name" => "story_index",
"bucket" => "story"
]
];

$node = (new \Basho\Riak\Node\Builder)
->atHost('riak host')
->onPort('riak port'))
->build();

$riak = new \Basho\Riak ([$node]);

foreach ($indexes as $index)
{
// creates index
$response = (new \Basho\Riak\Command\Builder\Search\StoreIndex($riak))
->withName( $index['name'] )
->usingSchema('_yz_default')
->build()
->execute();
// associates index with bucket
(new \Basho\Riak\Command\Builder\Search\AssociateIndex($riak))
->withName( $index['name'] )
->buildBucket($index['bucket'])
->build()
->execute();
}



Docs:
http://docs.basho.com/riak/kv/2.2.3/dev ... ge/search/
https://docs.riak.com/riak/kv/latest/developing/usage/search/index.html

Default schema:
https://raw.githubusercontent.com/basho/yokozuna/develop/priv/default_schema.xml


12/7/2020

Sample query
$query = [
// "defType" => "edismax", // query parser
"sort" => "score DESC", // override default score sorting
"q" => "{!edismax}{$parameters['phrase']}", // query
"qf" => "authorName^6 objectId^4 headline^2 deck", // query fields
"fq" => "{!lucene}
edition:{$parameters['edition']}
AND statusId:4
AND objectTypeId:(1 2 4 12 15)
AND publicationDateISO8601:[NOW-10YEAR TO NOW]", // filter query
"qs" => "5", // query phrase slop
"bq" => "publicationDateISO8601:[NOW-2YEAR TO NOW]", // boost query
"fl" => "*,score", // field list
"hl" => "true", // highlight
"mm" => "3<80%", // minimum match
"wt" => "json", // response writer
"rows" => $rows, // rows
"start" => $start, // offset

// spell check parameters
"df" => "entspellcheck",
"spellcheck" => "true" ,
"spellcheck.q" => "\"{$parameters['phrase']}\"~10" , // proximity added
// "spellcheck.build" => "true" ,
"spellcheck.collate" => "true" ,
"spellcheck.maxCollations" => "30" ,
"spellcheck.maxCollationTries" => "30" ,
"spellcheck.maxCollationEvaluations" => "30" ,
"spellcheck.collateExtendedResults" => "true" ,
"spellcheck.collateMaxCollectDocs" => "30" ,
"spellcheck.count" => "10" ,
// "spellcheck.dictionary" => 'wordbreak' ,
"spellcheck.extendedResults" => "true" ,
// "spellcheck.onlyMorePopular" => "true" ,
"spellcheck.maxResultsForSuggest" => "5" ,
"spellcheck.alternativeTermCount" => "10" ,
// "spellcheck.reload" => "true",
"spellcheck.accuracy" => "0.5" ,
];


Which will be something like this:
http://app01.example.com:8098/search/query/article_index?sort=score DESC
&q={!edismax}how to become a millionaire
&qf=authorName^6 objectId^4 headline^2 deck
&fq={!lucene}
edition:us
AND statusId:4
AND objectTypeId:(1 2 4 12 15)
AND publicationDateISO8601:[NOW-10YEAR TO NOW]
&qs=5
&bq=publicationDateISO8601:[NOW-2YEAR TO NOW]
&fl=*,score
&hl=true
&mm=3<80%
&wt=json
&rows=20
&start=0
&df=entspellcheck
&spellcheck=true
&spellcheck.q="how to become a millionaire"~10
&spellcheck.collate=true
&spellcheck.maxCollations=30
&spellcheck.maxCollationTries=30
&spellcheck.maxCollationEvaluations=30
&spellcheck.collateExtendedResults=true
&spellcheck.collateMaxCollectDocs=30
&spellcheck.count=10
&spellcheck.extendedResults=true
&spellcheck.maxResultsForSuggest=5
&spellcheck.alternativeTermCount=10
&spellcheck.accuracy=0.5


[ view entry ] ( 688 views )   |  print article
BASH Fun: Running a command at a specific time using 'at'. 
[acool@acool2 ~]$ sudo yum install at
...
[acool@acool2 ~]$ sudo systemctl start atd
[acool@acool2 ~]$ sudo systemctl enable atd


[acool@acool2 ~]$ date
Mon Apr 15 16:09:15 PDT 2019
[acool@acool2 ~]$
[acool@acool2 ~]$ echo 'date >> at-fun.txt' | at now + 1 minute
job 79 at Mon Apr 15 16:10:00 2019
[acool@acool2 ~]$
[acool@acool2 ~]$ atq
79 Mon Apr 15 16:10:00 2019 a acool
[acool@acool2 ~]$
[acool@acool2 ~]$ cat at-fun.txt
Mon Apr 15 16:10:00 PDT 2019


[acool@acool2 ~]$
[acool@acool2 ~]$ cat at-fun.txt
[acool@acool2 ~]$ echo 'php -v >> at-fun.txt' | at 4:16 PM April 15 2019
job 84 at Mon Apr 15 16:16:00 2019
[acool@acool2 ~]$
[acool@acool2 ~]$ date
Mon Apr 15 16:15:21 PDT 2019
[acool@acool2 ~]$ atq
84 Mon Apr 15 16:16:00 2019 a acool
[acool@acool2 ~]$
[acool@acool2 ~]$ cat at-fun.txt
PHP 7.0.27 (cli) (built: Apr 4 2018 13:48:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
[acool@acool2 ~]$

https://stackoverflow.com/questions/55524769

[ view entry ] ( 790 views )   |  print article
Memcached: Looping through a set of servers to delete a key. 
Cool way to make sure a key does not exist in a group of memcached servers. Handy when troubleshooting or when cached data needs a purge. Tested:
[aesteban@localhost ~]$ 
[aesteban@localhost ~]$ for i in {1..6};do ( echo 'delete events.slick-range') | nc server-${i}.example.com 11211; done;
NOT_FOUND
NOT_FOUND
NOT_FOUND
DELETED
NOT_FOUND
NOT_FOUND
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$

Try adding a delay if the above doesn't seem to work:
[aesteban@localhost ~]$ 
[aesteban@localhost ~]$ for i in {1..6};do ( sleep 2; echo 'delete events.slick-range') | nc -d 2 server-${i}.example.com 11211; done;
NOT_FOUND
NOT_FOUND
NOT_FOUND
DELETED
NOT_FOUND
NOT_FOUND
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$




BONUS (6/12/2019) - Find out the expiration date for a key:
(sleep 1; echo "stats cachedump 7 0"; sleep 1; echo "quit";) | nc www-server01.example.com  11211 | grep 'memc.sess.tpnm0jd8aottlturb0ml510sd0'


Increase/decrease the slab (seven) number as need until the key is found.

[ view entry ] ( 754 views )   |  print article
MySQL Fun: Master-Slave Replication Example With Docker 
# for historical purposes:
[acool@localhost ~]$ date
Thu Jan 24 18:44:23 PST 2019
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ cat /etc/redhat-release
Fedora release 28 (Twenty Eight)
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker --version
Docker version 1.13.1, build 1556cce-unsupported
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker network create mysql-cluster
72af69d434e8cefa031555d2ba6039c2260aa73fe27df380b9454e3790c89337
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
5dfcb11eac94 bridge bridge local
c146b3afd86e host host local
72af69d434e8 mysql-cluster bridge local
1299a7349094 none null local
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ tree mysql-fun
mysql-fun
├── master
│   ├── config
│   │   └── mysqld.cnf
│   └── data
└── slave
├── config
│   └── mysqld.cnf
└── data

6 directories, 2 files
[acool@localhost ~]$
[acool@localhost ~]$ cat mysql-fun/master/config/mysqld.cnf
[mysqld]
server_id = 1
log_bin = /var/log/mysql/mysql-bin-master
[acool@localhost ~]$
[acool@localhost ~]$ cat mysql-fun/slave/config/mysqld.cnf
[mysqld]
server_id = 2
relay_log = /var/log/mysql/mysql-relay-slave-bin
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ getenforce
Permissive
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.7 5cd9bd4c8a5e 3 days ago 372 MB
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker run -v /home/acool/mysql-fun/slave/config:/etc/mysql/conf.d -v /home/acool/mysql-fun/slave/data/:/var/lib/mysql/:rw --detach --network mysql-cluster --name=slave-mysql --env="MYSQL_ROOT_PASSWORD=temp123" mysql:5.7
599f302193cda964ada60d42e90324705c8d5b578c805c57f395958b97ed6702
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker run -v /home/acool/mysql-fun/master/config:/etc/mysql/conf.d -v /home/acool/mysql-fun/master/data/:/var/lib/mysql/:rw --detach --network mysql-cluster --name=master-mysql --env="MYSQL_ROOT_PASSWORD=temp123" mysql:5.7
32d0c2ecc7e6d17ae99ebabd6c75f0566846d60451bfb953b747b767dda92129
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32d0c2ecc7e6 mysql:5.7 "docker-entrypoint..." 15 seconds ago Up 14 seconds 3306/tcp, 33060/tcp master-mysql
599f302193cd mysql:5.7 "docker-entrypoint..." 46 seconds ago Up 44 seconds 3306/tcp, 33060/tcp slave-mysql
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker inspect master-mysql |grep \"IPAddress\"
"IPAddress": "",
"IPAddress": "172.18.0.3",
[acool@localhost ~]$
[acool@localhost ~]$ sudo docker inspect slave-mysql |grep \"IPAddress\"
"IPAddress": "",
"IPAddress": "172.18.0.2",
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ mysql -h 172.18.0.3 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 USER 'replication_user'@'%' IDENTIFIED BY 'ru_temp123';
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW MASTER STATUS;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-bin-master.000003 | 763 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ mysql -h 172.18.0.2 -p -u root
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)

mysql> CHANGE MASTER TO MASTER_HOST = '172.18.0.3', MASTER_USER = 'replication_user', MASTER_PASSWORD = 'ru_temp123', MASTER_LOG_FILE = 'mysql-bin-master.000003', MASTER_LOG_POS = 763;
Query OK, 0 rows affected, 1 warning (0.10 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.18.0.3
Master_User: replication_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-master.000003
Read_Master_Log_Pos: 763
Relay_Log_File: mysql-relay-slave-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: mysql-bin-master.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 763
Relay_Log_Space: 540
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 60c1469e-2049-11e9-89ca-0242ac120003
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[acool@localhost ~]$
[acool@localhost ~]$ # create new database in master
[acool@localhost ~]$ mysql -h 172.18.0.3 -p -u root
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 users;
Query OK, 1 row affected (0.01 sec)

mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| users |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit
Bye
[acool@localhost ~]$ # confirm database creation was replicated to slave
[acool@localhost ~]$
[acool@localhost ~]$ mysql -h 172.18.0.2 -p -u root
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| users |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit
Bye
[acool@localhost ~]$
[acool@localhost ~]$ # Exercise finished! SQL statements are being replicated from master to slave :)
[acool@localhost ~]$


[ view entry ] ( 765 views )   |  print article
Docker: CentOS 7 Fun. 
[aesteban@localhost ~]$ 
[aesteban@localhost ~]$
[aesteban@localhost ~]$ cat /etc/redhat-release
Fedora release 24 (Twenty Four)
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[aesteban@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker pull centos:7
Trying to pull repository docker.io/library/centos ...
7: Pulling from docker.io/library/centos

a02a4930cb5d: Pull complete
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for docker.io/centos:7
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 7 1e1148e4cc2c 6 days ago 201.8 MB
[aesteban@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[aesteban@localhost ~]$ sudo docker run -d --privileged -p 80:80 docker.io/centos:7 /sbin/init
f0faf6197fbc696796333bfc81f25d537a1aba170b81f2076010222e84284b36
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker exec -it f0faf6197fbc696796333bfc81f25d537a1aba170b81f2076010222e84284b36 bash
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# yum install epel-release
...
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# yum install nginx
...
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# systemctl start nginx
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2018-12-12 02:55:32 UTC; 4s ago
Process: 2643 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2642 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2641 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 2644 (nginx)
CGroup: /system.slice/docker-f0faf6197fbc696796333bfc81f25d537a1aba170b81f2076010222e84284b36.scope/system.slice/nginx.service
├─2644 nginx: master process /usr/sbin/nginx
├─2645 nginx: worker process
├─2646 nginx: worker process
├─2647 nginx: worker process
└─2648 nginx: worker process

Dec 12 02:55:31 f0faf6197fbc systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 12 02:55:31 f0faf6197fbc nginx[2642]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 12 02:55:31 f0faf6197fbc nginx[2642]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 12 02:55:32 f0faf6197fbc systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# exit
exit
[aesteban@localhost ~]$
[aesteban@localhost ~]$ # localhost should now be accessible in browser
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker exec -it f0faf6197fbc systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2018-12-12 02:55:32 UTC; 1min 57s ago
Process: 2643 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2642 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2641 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 2644 (nginx)
CGroup: /system.slice/docker-f0faf6197fbc696796333bfc81f25d537a1aba170b81f2076010222e84284b36.scope/system.slice/nginx.service
├─2644 nginx: master process /usr/sbin/nginx
├─2645 nginx: worker process
├─2646 nginx: worker process
├─2647 nginx: worker process
└─2648 nginx: worker process

Dec 12 02:55:31 f0faf6197fbc systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 12 02:55:31 f0faf6197fbc nginx[2642]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 12 02:55:31 f0faf6197fbc nginx[2642]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 12 02:55:32 f0faf6197fbc systemd[1]: Started The nginx HTTP and reverse proxy server.
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker exec -it f0faf6197fbc bash
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# systemctl status postfix
Unit postfix.service could not be found.
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# systemctl status memcached
Unit memcached.service could not be found.
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# # We can install postfix and memcached with yum using the same procedure!
[root@f0faf6197fbc /]#
[root@f0faf6197fbc /]# # Exercise Done :) !!


[ view entry ] ( 937 views )   |  print article
PostgreSQL Fun 

#Fedora 23
[acool@localhost ~]$ sudo dnf install postgresql-server.x86_64
[acool@localhost ~]$ sudo postgresql-setup --initdb
[acool@localhost ~]$ sudo systemctl start postgresql
[acool@localhost ~]$ sudo su - postgres
-bash-4.3$
-bash-4.3$
-bash-4.3$ createuser --pwprompt acool
Enter password for new role:
Enter it again:
-bash-4.3$
-bash-4.3$
-bash-4.3$ psql
psql (9.4.9)
Type "help" for help.
postgres=#
postgres=#
postgres=#
postgres-# # list databases
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)

postgres-#
postgres-# #list users (roles)
postgres-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
acool | | {}
postgres | Superuser, Create role, Create DB, Replication | {}

postgres-#
postgres=#
postgres=# ALTER USER acool SUPERUSER;
ALTER ROLE
postgres=# ALTER USER acool CREATEROLE;
ALTER ROLE
postgres=# ALTER USER acool CREATEDB;
ALTER ROLE
postgres=# ALTER USER acool REPLICATION;
ALTER ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
acool | Superuser, Create role, Create DB, Replication | {}
postgres | Superuser, Create role, Create DB, Replication | {}

postgres=#


[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ cat postgresql-test.sql
CREATE TABLE soccer_teams
(
name varchar(250),
city varchar(250)
);
[acool@localhost ~]$
[acool@localhost ~]$ createdb my-db-test
[acool@localhost ~]$
[acool@localhost ~]$ psql -d my-db-test
psql (9.4.9)
Type "help" for help.

my-db-test=#
my-db-test=#
my-db-test=# \i postgresql-test.sql
CREATE TABLE
my-db-test=#
my-db-test=# \dt
List of relations
Schema | Name | Type | Owner
--------+--------------+-------+-------
public | soccer_teams | table | acool
(1 row)

my-db-test=#
my-db-test-#
my-db-test-# \d+ soccer_teams
Table "public.soccer_teams"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------------------+-----------+----------+--------------+-------------
name | character varying(250) | | extended | |
city | character varying(250) | | extended | |

my-db-test=#
my-db-test=# INSERT INTO soccer_teams(name,city) VALUES('LA Galaxy','Los Angeles');
INSERT 0 1
my-db-test=#
my-db-test=# SELECT * FROM soccer_teams;
name | city
-----------+-------------
LA Galaxy | Los Angeles
(1 row)

my-db-test-#
my-db-test-# \q
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ psql -V
psql (PostgreSQL) 9.4.9
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$

// other crap
postgres=# #delete user (without roles?, DROP ROLE acool works too?, objects depend on it?)
postgres=# DROP USER acool;
DROP ROLE


[ view entry ] ( 1181 views )   |  print article
AWS Notes 
S3 crap

#list s3 buckets
[acool@acool2 www]$ aws s3 ls

#list files in bucket
[acool@acool2 www]$ aws s3 ls s3://logs-fastly/www/
...
[aesteban@localhost ~]$ aws s3 ls s3://my-db-backup/all_dbs-2019-10-08/ --human-readable --summarize
...


#downloading a list of files from bucket
[acool@acool2 www]$ for i in x.log y.log z.log; do aws s3 cp "s3://logs-fastly/www/$i" .; done


[ view entry ] ( 907 views )   |  print article
Docker: Tasks 101 
So in my opinion containers are more like chroot-in-steroids isolation than fully virtualized environments. That's how it feels to me so far as of 4/11/2018. - Angel


# installing docker (1.10.3)
[aesteban@localhost ~]$ sudo dnf install docker

# start and enable it
[aesteban@localhost ~]$ sudo systemctl start docker
[aesteban@localhost ~]$ sudo systemctl enable docker

# download and run hello-world image
[aesteban@localhost ~]$ sudo docker run hello-world

# download ubuntu image and run it
[aesteban@localhost ~]$ sudo docker run -it ubuntu bash

# listing images
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest e38bc07ac18e 3 hours ago 1.848 kB
docker.io/ubuntu latest f975c5035748 5 weeks ago 112.4 MB
[aesteban@localhost ~]$

# listing all containers
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
61d35d591f53 ubuntu "bash" 3 minutes ago Exited (0) 3 minutes ago clever_brahmagupta
1c61c7a6f4d7 hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago tiny_boyd
[aesteban@localhost ~]$

# removing a container (container id)
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker rm 61d35d591f53
61d35d591f53
[aesteban@localhost ~]$

# removing an image (image id)
[aesteban@localhost ~]$ sudo docker rmi f975c5035748
Untagged: docker.io/ubuntu:latest
Untagged: docker.io/ubuntu@sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Deleted: sha256:f975c50357489439eb9145dbfa16bb7cd06c02c31aa4df45c77de4d2baa4e232
Deleted: sha256:0bd983fc698ee9453dd7d21f8572ea1016ec9255346ceabb0f9e173b4348644f
Deleted: sha256:08fe90e1a1644431accc00cc80f519f4628dbf06a653c76800b116d3333d2b6d
Deleted: sha256:5dc5eef2b94edd185b4d39586e7beb385a54b6bac05d165c9d47494492448235
Deleted: sha256:14a40a140881d18382e13b37588b3aa70097bb4f3fb44085bc95663bdc68fe20
Deleted: sha256:a94e0d5a7c404d0e6fa15d8cd4010e69663bd8813b5117fbad71365a73656df9
[aesteban@localhost ~]$

# deleting all containers
[aesteban@localhost ~]$ sudo docker rm $(sudo docker ps -a -q)
1c61c7a6f4d7
[aesteban@localhost ~]$

# deleting all images
[aesteban@localhost ~]$
[aesteban@localhost ~]$ sudo docker rmi $(sudo docker images -q)
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:6c88d0eedd6a5e71f0affaf150f8b7b286c7bdc679f23d726d12781803e727d3
Deleted: sha256:e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96
Deleted: sha256:2b8cbd0846c5aeaa7265323e7cf085779eaf244ccbdd982c4931aef9be0d2faf
[aesteban@localhost ~]$

# installing docker-compose
[aesteban@localhost ~]$ sudo dnf install docker-compose

# saving a modified container (eg. after installing RPMs)
[aesteban@localhost ~]$ sudo docker commit "container-id" "image_name" # repo name

#
[aesteban@localhost ~]$ sudo docker start "container-id"

#
[aesteban@localhost ~]$ sudo docker attach "container-id"

# exporting an image to a file
[aesteban@localhost ~]$ sudo docker save "image-id" | gzip -c image_name.tgz

# importing an image from a file
[aesteban@localhost ~]$ sudo docker load < image_name.tgz

# sample docker-compose.yml
[aesteban@localhost docker-practice]$ cat docker-compose.yml
web:
image: nginx:latest
ports:
- "8888:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: cytopia/php-fpm-7.1 # could also be and id (imageid? or containerid? I think the first.)
volumes:
- ./code/:/code/
[aesteban@localhost docker-practice]$

# executing docker-compose.yml
[aesteban@localhost docker-practice]$ sudo docker-compose up
...

# sample docker file
[aesteban@localhost docker-practice]$ cat Dockerfile
FROM miveo/centos-php-fpm:7.1
RUN yum -y install php71u-pecl-memcached.x86_64
[aesteban@localhost docker-practice]$
[aesteban@localhost docker-practice]$

# building a docker file
[aesteban@localhost docker-practice]$ sudo docker build -t ent:ent .
...

# verifying build
[aesteban@localhost docker-practice]$
[aesteban@localhost docker-practice]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ent ent 3d83d51abac9 About a minute ago 429.3 MB
docker.io/nginx latest b175e7467d66 27 hours ago 108.9 MB
docker.io/cytopia/php-fpm-7.1 latest 927ad858fb6a 7 months ago 1.098 GB
docker.io/miveo/centos-php-fpm 7.1 95cae7821f24 15 months ago 287.6 MB
[aesteban@localhost docker-practice]$
[aesteban@localhost docker-practice]$ sudo docker run -it 3d83d51abac9 bash
[root@43bb9a049b54 /]#
[root@43bb9a049b54 /]# rpm -qa | grep php | grep memcached
php71u-pecl-memcached-3.0.4-2.ius.centos7.x86_64
[root@43bb9a049b54 /]#
[root@43bb9a049b54 /]#


12/11/2018

#stop container id
[aesteban@localhost docker-practice-2]$ sudo docker stop 2d900ed18675
2d900ed18675
[aesteban@localhost docker-practice-2]$


[ view entry ] ( 1024 views )   |  print article
Virsh: Moving a VM image to a new machine. 
1.- rsync .img image file to new machine.
2.- Output the xml definition file in the source machine:
[acool@oldmachine ~]$sudo virsh dumpxml VMNAME > VMNAME.xml

3.- Adjust path of .img file image in xml file as needed.
4.- On the destination machine define the new VM:
[acool@newmachine ~]$sudo virsh define VMNAME.xml

5.- Start VM.

Big shout-out to dyasny.

[ view entry ] ( 1046 views )   |  print article
Websocket example with PHP 

[acool@localhost websocket-fun]$
[acool@localhost websocket-fun]$ # start web server
[acool@localhost websocket-fun]$ php -S localhost:8080
PHP 5.6.29 Development Server started at Fri Jul 28 22:21:15 2017
Listening on http://localhost:8080
Document root is /home/acool/websocket-fun
Press Ctrl-C to quit.


[acool@localhost websocket-fun]$ 
[acool@localhost websocket-fun]$ # start websocket application
[acool@localhost websocket-fun]$ php -q server.php


Open two browser windows go to localhost:8080 and start chatting with yourself hehe

Window 1



Window 2



server.php
<?php
$host = 'localhost'; //host
$port = '9000'; //port
$null = NULL; //null var

//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

//bind socket to specified host
socket_bind($socket, 0, $port);

//listen to port
socket_listen($socket);

//create & add listning socket to the list
$clients = array($socket);

//start endless loop, so that our script doesn't stop
while (true)
{
//manage multipal connections
$changed = $clients;
//returns the socket resources in $changed array
socket_select($changed, $null, $null, 0, 10);

//check for new socket
if (in_array($socket, $changed))
{
$socket_new = socket_accept($socket); //accpet new socket
$clients[] = $socket_new; //add socket to client array

$header = socket_read($socket_new, 1024); //read data sent by the socket
perform_handshaking($header, $socket_new, $host, $port); //perform websocket handshake

socket_getpeername($socket_new, $ip); //get ip address of connected socket
$response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' connected'))); //prepare json data
send_message($response); //notify all users about new connection

//make room for new socket
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
}

//loop through all connected sockets
foreach ($changed as $changed_socket)
{

//check for any incomming data
while(socket_recv($changed_socket, $buf, 1024, 0) >= 1)
{
$received_text = unmask($buf); //unmask data
$tst_msg = json_decode($received_text); //json decode
$user_name = $tst_msg->name; //sender name
$user_message = $tst_msg->message; //message text
$user_color = $tst_msg->color; //color

//prepare data to be sent to client
$response_text = mask(json_encode(array('type'=>'usermsg', 'name'=>$user_name, 'message'=>$user_message, 'color'=>$user_color)));
send_message($response_text); //send data
break 2; //exist this loop
}

$buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);
if ($buf === false)
{ // check disconnected client
// remove client for $clients array
$found_socket = array_search($changed_socket, $clients);
socket_getpeername($changed_socket, $ip);
unset($clients[$found_socket]);

//notify all users about disconnected connection
$response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' disconnected')));
send_message($response);
}
}
}
// close the listening socket
socket_close($socket);

function send_message($msg)
{
global $clients;
foreach($clients as $changed_socket)
{
@socket_write($changed_socket,$msg,strlen($msg));
}
return true;
}


//Unmask incoming framed message
function unmask($text)
{
$length = ord($text[1]) & 127;
if($length == 126) {
$masks = substr($text, 4, 4);
$data = substr($text, 8);
}
elseif($length == 127) {
$masks = substr($text, 10, 4);
$data = substr($text, 14);
}
else {
$masks = substr($text, 2, 4);
$data = substr($text, 6);
}
$text = "";
for ($i = 0; $i < strlen($data); ++$i) {
$text .= $data[$i] ^ $masks[$i%4];
}
return $text;
}

//Encode message for transfer to client.
function mask($text)
{
$b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($text);

if($length <= 125)
$header = pack('CC', $b1, $length);
elseif($length > 125 && $length < 65536)
$header = pack('CCn', $b1, 126, $length);
elseif($length >= 65536)
$header = pack('CCNN', $b1, 127, $length);
return $header.$text;
}

//handshake new client.
function perform_handshaking($receved_header,$client_conn, $host, $port)
{
$headers = array();
$lines = preg_split("/\r\n/", $receved_header);
foreach($lines as $line)
{
$line = chop($line);
if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
{
$headers[$matches[1]] = $matches[2];
}
}

$secKey = $headers['Sec-WebSocket-Key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
//hand shaking header
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"WebSocket-Origin: $host\r\n" .
// "WebSocket-Location: ws://$host:$port/demo/shout.php\r\n".
"Sec-WebSocket-Accept:$secAccept\r\n\r\n";
socket_write($client_conn,$upgrade,strlen($upgrade));
}


index.php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">

.panel{


margin-right: 3px;
}

.button {
background-color: #4CAF50;
border: none;
color: white;
margin-right: 30%;
margin-left: 30%;
text-decoration: none;
display: block;
font-size: 16px;
cursor: pointer;
width:30%;
height:40px;
margin-top: 5px;

}
input[type=text]{
width:100%;
margin-top:5px;

}


.chat_wrapper {
width: 70%;
height:472px;
margin-right: auto;
margin-left: auto;
background: #3B5998;
border: 1px solid #999999;
padding: 10px;
font: 14px 'lucida grande',tahoma,verdana,arial,sans-serif;
}
.chat_wrapper .message_box {
background: #F7F7F7;
height:350px;
overflow: auto;
padding: 10px 10px 20px 10px;
border: 1px solid #999999;
}
.chat_wrapper input{
//padding: 2px 2px 2px 5px;
}
.system_msg{color: #BDBDBD;font-style: italic;}
.user_name{font-weight:bold;}
.user_message{color: #88B6E0;}

@media only screen and (max-width: 720px) {
/* For mobile phones: */
.chat_wrapper {
width: 95%;
height: 40%;
}


.button{ width:100%;
margin-right:auto;
margin-left:auto;
height:40px;}






}

</style>
</head>
<body>
<?php
$colours = array('007AFF','FF7000','FF7000','15E25F','CFC700','CFC700','CF1100','CF00BE','F00');
$user_colour = array_rand($colours);
?>


<script src="jquery-3.1.1.js"></script>


<script language="javascript" type="text/javascript">
$(document).ready(function(){
//create a new WebSocket object.
var wsUri = "ws://localhost:9000/demo/server.php";
websocket = new WebSocket(wsUri);

websocket.onopen = function(ev) { // connection is open
$('#message_box').append("<div class=\"system_msg\">Connected!</div>"); //notify user
}

$('#send-btn').click(function(){ //use clicks message send button
var mymessage = $('#message').val(); //get message text
var myname = $('#name').val(); //get user name

if(myname == ""){ //empty name?
alert("Enter your Name please!");
return;
}
if(mymessage == ""){ //emtpy message?
alert("Enter Some message Please!");
return;
}
document.getElementById("name").style.visibility = "hidden";

var objDiv = document.getElementById("message_box");
objDiv.scrollTop = objDiv.scrollHeight;
//prepare json data
var msg = {
message: mymessage,
name: myname,
color : '<?php echo $colours[$user_colour]; ?>'
};
//convert and send data to server
websocket.send(JSON.stringify(msg));
});

//#### Message received from server?
websocket.onmessage = function(ev) {
var msg = JSON.parse(ev.data); //PHP sends Json data
var type = msg.type; //message type
var umsg = msg.message; //message text
var uname = msg.name; //user name
var ucolor = msg.color; //color

if(type == 'usermsg')
{
$('#message_box').append("<div><span class=\"user_name\" style=\"color:#"+ucolor+"\">"+uname+"</span> : <span class=\"user_message\">"+umsg+"</span></div>");
}
if(type == 'system')
{
$('#message_box').append("<div class=\"system_msg\">"+umsg+"</div>");
}

$('#message').val(''); //reset text

var objDiv = document.getElementById("message_box");
objDiv.scrollTop = objDiv.scrollHeight;
};

websocket.onerror = function(ev){$('#message_box').append("<div class=\"system_error\">Error Occurred - "+ev.data+"</div>");};
websocket.onclose = function(ev){$('#message_box').append("<div class=\"system_msg\">Connection Closed</div>");};
});




</script>
<div class="chat_wrapper">
<div class="message_box" id="message_box"></div>
<div class="panel">
<input type="text" name="name" id="name" placeholder="Your Name" maxlength="15" />

<input type="text" name="message" id="message" placeholder="Message" maxlength="80"
onkeydown = "if (event.keyCode == 13)document.getElementById('send-btn').click()" />





</div>

<button id="send-btn" class=button>Send</button>

</div>

</body>
</html>


Thanks to sanwebe.com

[ view entry ] ( 1240 views )   |  print article

<<First <Back | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Next> Last>>


2024 By Angel Cool