Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to create a simple Chrome extension?

First of all you need to enable developer mode in Chrome in order to debug your extension before publishing. Head over to chrome:// extensions in your browser, or simply click “More Tools” and “Extensions” on the Chrome menu. This should lead you to the Extension Management page, where you can turn on Developer Mode (it should be in the top right corner).

OK! Create a directory and put a file called manifest.json into it with the below content:

{
"name": "Name of the extension",
"description": "Description of the extension",
"version": "1.0",
"background": {
"scripts": [ "background.js" ]
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "Your Extension Title"
},
"icons": {
"128": "icon_128.png"
},
"manifest_version": 2,
"permissions": ["activeTab", "tabs", "contextMenus"]
}

background section is used to link a javascript file to your extension (You need to create a file background.js in your newly created directory).

permissions section is used when you want specific permissions in your js file. contextMenus is used to work on menu items related to your extension. tabs is used to open a new tab on chrome browser.

In the next post I will explain the JS section.

#chrome #extension
Now let's write the main JS code in order to add menu items into Chrome extension. Create background.js file in the same directory as manifest.json with the content below:

if (chrome && chrome.browserAction){
var baseUrl = "https://www.example.com"
chrome.runtime.onInstalled.addListener(function() {
chrome.tabs.create({ url: baseUrl + "/extension-installed/" });
});

chrome.runtime.setUninstallURL(baseUrl + "/extension-uninstalled/");

// open a new page on left clicking on the extension itself
chrome.browserAction.onClicked.addListener(function(activeTab) {
chrome.tabs.create({ url: baseUrl + "/my-home-page" });
});

chrome.contextMenus.create({
"id": "first_item",
"title": "This is the first item",
"contexts": ["browser_action"]
});

chrome.contextMenus.create({
"id": "second_item",
"title": "This is the second item",
"contexts": ["browser_action"]
});

chrome.contextMenus.onClicked.addListener(function(info)
{
urls = {
first_item: baseUrl + "/first-item",
second_item: baseUrl + "/second-item"
};
chrome.tabs.create({url: urls[info.menuItemId]});
});
}

There are many events that based on that event you can do an action. Let's review some of the codes above.

chrome.runtime.onInstalled.addListener: on extension installation open a url using chrome.tabs. As you recall from the previous post we have added tabs to the permission section of manifest.

chrome.runtime.setUninstallURL: open a url on extension uninstallation.

chrome.browserAction.onClicked.addListener: open a url when your extension in toolbar is clicked (at the right of the address bar).

chrome.contextMenus.create: create an item with specific id and title. contexts sections tells the browser to show the menu just when user right click on the extension in toolbar. You can add page to have the extension menu item on the main page of browser itself.

chrome.contextMenus.onClicked.addListener: When one of the menu items is clicked open the corresponding URL.

Now to test it go to chrome://extensions/. Make sure developer mode is enabled. Click on Load unpacked and select your extension folder and open it. It will be installed by now.

Enjoy working with Chrome Extension! :)

#chrome #xtension
In order to list all logical volumes (LVM) in Linux you can issue the below command:

$ lvscan
ACTIVE '/dev/mysql-vg/swap_1' [31.88 GiB] inherit
ACTIVE '/dev/mysql-vg/root' [27.94 GiB] inherit
ACTIVE '/dev/mysql-vg/var' [833.64 GiB] inherit

#linux #lvscan #lvm #logical_volume
9781785881114.epub
4 MB
Python Microservices Developement

#python #ebook #epub #pdf #microservice #flask
How to configure a Delayed Replica Set Member?

Let's assume that our member is third in the array of replica members:

cfg = rs.conf()
cfg.members[2].priority = 0
cfg.members[2].hidden = true
cfg.members[2].slaveDelay = 3600
rs.reconfig(cfg)

The priority is set to 0 (preventing to be elected as primary).

The hidden to true in order to hide the node from clients querying the database.

And finally slaveDelay to number of seconds that we want it to get behind of Primary Node.

The use case for this is to have a replication that is used for analytical purposes or used for backup and so on.

#mongodb #mongo #replica #replication #primary #delayed_replica_set #slaveDelay
Tech C**P
How to configure a Delayed Replica Set Member? Let's assume that our member is third in the array of replica members: cfg = rs.conf() cfg.members[2].priority = 0 cfg.members[2].hidden = true cfg.members[2].slaveDelay = 3600 rs.reconfig(cfg) The priority…
IMPORTANT:
The length of the secondary members[n].slaveDelay must fit within the window of the oplog. If the oplog is shorter than the members[n].slaveDelay window, the delayed member cannot successfully replicate operations.
In order to see how much time your mongoDB slave is behind the primary node:

rs0:SECONDARY> db.printSlaveReplicationInfo()
source: mongo.mongo.com:27017
syncedTo: Mon Nov 12 2018 06:33:40 GMT+0000 (UTC)
-4 secs (0 hrs) behind the primary

#mongodb #mongo #slave #printSlaveReplicationInfo #replica #replication
How to check MongoDB replication lag in Icinga2 and get notified when it is over 15 seconds?

We assume here that you have a replica set in place. First download the python script for our nagios plugin:

cd /usr/lib/nagios/plugins
git clone git://github.com/mzupan/nagios-plugin-mongodb.git

Now the Icinga2 part. You first need to create a command for replication lag check:

cd /etc/icinga2/conf.d/commands

Create a new file replication_lag.conf:

object CheckCommand "check_replication_lag" {
import "plugin-check-command"
command = [ PluginDir + "/nagios-plugin-mongodb/check_mongodb.py", "-A", "replication_lag" ]
arguments = {
"-H" = "$mongo_host$"
"-P" = "$mongo_port$"
}
}


Create a new file in services folder called replication_lag.conf:

apply Service for (display_name => config in host.vars.replication) {
import "generic-service"
check_command = "check_replication_lag"
vars += config
assign where host.vars.replication
}


This service gets enabled where it finds replication in host config. Now in secondary mongoDB hosts configuration add the below part:

vars.replication["Secondary DB"] = {
mongo_host = "slave.example.com"
mongo_port = 27017
}

#sysadmin #icinga2 #mongodb #replication #replication_lag #nagios_plugin
Why Axigen Mail Server log gives the error of Unable to perform STARTTLS and how to solve it?

Axigen by default initiate a TLS connection with the target mail server. When the target mail server does not accept TLS, Axigen will mark the mail message as Relay error with the error of Unable to perform STARTTLS.

It is suggest to leave this behaviour as is, but in case there is an exception that you want to send mail to that mail server you need to ignore TLS for the target. In order to do so login to Axigen webadmin interface and:

1- Navigate to Security & Filtering -> Acceptance & Routing -> Advanced Settings

2- Click the Add Acceptance / Routing Rule button

3- Write a suggestive name for the rule like disable_tls_example_com

4- In the Conditions section add the following two conditions:
- Recipient -> Domain -> add the condition -> select 'Is' from the combo box -> write Name_of_the_domain (example.com) in the combo box.
- Delivery -> Relaying mail -> click add the condition

5- Select at the top of the Conditions section For incoming messages that match -> ALL of the conditions below (instead of the default `ANY of the conditions below`)

6- in the Actions section select Settings -> Allow StartTLS -> add the action. DO NOT tick the check-box next to Allow StartTLS.

Now click SAVE CONFIGURATION button. Your're done and you should see something like below in your axigen log file:

Set recipient <info@example.com> state to SENT

#mailserver #axigen #TLS #STARTTLS
DevOps with Kubernetes
#devops #kubernetes
How to add comma to end of multiple lines in VIM? Yeah, that's tricky boy :))))

1- First go to Visual Block mode by pressing ^v (CTRL+V)

2- Now select your mutiple lines by going down using down arrow button.

3- Go to end of line by pressing end command in your keyboard in linux and windows or if you're using MAC by pressing fn+right arrow.

4- Now press shift+A (It worked in MAC in linux we needn't that)

5- type comma

6- press ESC (wait one second or so)

Voila! We're done.

#vim #tricks #VB #visual_block
Cannot umount a busy driver.

If you haven't tried to force umount by -f try it:

umount -f /mnt/your-mounted-drive


Now in case using -f it still gives error. Try this:

apt-get install psmisc


Now user fuser command like below:

fuser -c /mnt/your-mounted-drive
/mnt/your-mounted-drive/: 2510c 11086


It outputs the pid of the processes using this volume. The extra character at the end of pid will give some extra info. ( c in 2510c)

c - the process is using the file as its current working directory

m - the file is mapped with mmap

o - the process is using it as an open file

r - the file is the root directory of the process

t - the process is accessing the file as a text file

y - this file is the controlling terminal for the process

Kill these processes:

kill -9 2510 11086


Now umount it:

umount /mnt/your-mounted-drive

#umount #NFS #fuser #psmisc
Tech C**P
Cannot umount a busy driver. If you haven't tried to force umount by -f try it: umount -f /mnt/your-mounted-drive Now in case using -f it still gives error. Try this: apt-get install psmisc Now user fuser command like below: fuser -c /mnt/your…
You can use the below command to do it all:

fuser -ickv /mnt/your-mounted-drive

Where:
- parameter k kills the offending process,

- while v shows in advance the process and its user

- i asks you for confirmation.

NOTE: if some process resists, then try again with fuser -ickv -9

#linux #fuser #umount