Thursday, October 8, 2020

Simple Javascript Log Function

Simple log function with date/time prefix


1
2
3
4
5
const log = function(data)
{
  var dateTime = '[' + new Date().toUTCString() + '] ';
  console.log(dateTime, data);
};

Saturday, March 28, 2020

Custom Pytyhon Lock with statement

class TimeoutLock(object):
  """Customized threading.Lock with timeout functionality for with statement."""

  def __init__(self):
    self.lock = threading.Lock()

  @contextlib2.contextmanager
  def acquire(self, timeout_ms: int):
    """Tries to acquires lock within specified timeout.

    Args:
      timeout_ms (int): timeout for acquiring lock.

    Yields:
      The lock acquiring result.
    """
    ok = self.lock.acquire(blocking=True, timeout=timeout_ms/1000)
    logging.info('****** LOCK acquired ********')

    yield ok
    if ok:
      self.lock.release()
      logging.info('********* LOCK released ************')
    else:
      logging.warning('couldn\'t acquire lock, likely timed out.')

  def release(self):
    """Wrapper for threading lock."""
    if self.lock.locked():
      self.lock.release()

Tuesday, November 19, 2019

Disable LAN/WLAN Power Saving Mode in RaspberryPi

Raspberry pi connection may be lost as in raspbian or maybe other linux, the power saving mode for network interfaces is enabled by default.  They supposed to wake up by external events but if they got IP from dhcp and it get expired then no route can be made to them.

Here is some way to disable power saving on LAN (eth0) and WiFi (wlan0) interface.

LAN:
Add the following parameter to /boot/config.txt

dtparam=eee=off


WiFi:
Add the following command in /etc/rc.local

/sbin/iw dev wlan0 set power_save off

Wednesday, October 30, 2019

Arduino + Tinygo + Docker + Avrdude

Using golang to program arduino uni board

1- Code
package main

import (
    "machine"
    "time"
    // "fmt"
)

const led = machine.LED

func main() {
    // fmt.Println("Test")
    println("Hello, TinyGo")
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})
    for {
        led.Low()
        time.Sleep(time.Second)

        led.High()
        time.Sleep(time.Second)
    }
}
* fmt is not supported, cannot be used.

2- Compile using tinygo docker image 
docker run --rm -v $(pwd):/src tinygo/tinygo:0.9.0 
tinygo build -o /src/test1.hex -size=short -target=arduino /src/tinygo/tinygo1.go
* The source is in $(pwd)/tinygo/tinygo1.go

3- Upload by avrdude in command line

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude 
-C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf 
-v -patmega328p -carduino -P/dev/cu.usbserial-1440 -b115200 -D -Uflash:w:$(pwd)/tinygo/test1.hex:i
* I got command from Arduino IDE, by enabling verbose mode for upload and run a sample upload

Saturday, October 26, 2019

Raspberry Pi Testing/Unstable Repository

Add the following one to /etc/apt/sources.list

deb http://mirrordirector.raspbian.org/raspbian/ testing main contrib non-free rpi

Wednesday, October 10, 2018

Test DHCP Server

Testing dhcp server works as expected, like after configuring MAAS dhcp

sudo nmap --script broadcast-dhcp-discover -e cbr0

Tuesday, June 6, 2017

Trasnfer Docker Image Offline

Export docker image:
docker save -o export-docker-image.tar my_image:latest

Import docker image in destination:
docker load -i export-docker-image.tar

Friday, January 20, 2017

Local Insecure Docker Registry

Start the local registry:
docker run -d -p 5000:5000 --restart=always --name registry registry:2

Get image:
docker pull ubuntu && docker tag ubuntu localhost:5000/ubuntu

Push to the local registry:
docker push localhost:5000/ubuntu

Enable insecure docker registry for engine in /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry 10.0.1.11:5000

Pull image
docker pull localhost:5000/ubuntu


Thursday, December 15, 2016

Enabling jQuery to Lookup in Simple Page (in console)

Assuming the loaded page is simple page like the apache directory list as known as "index of /..."
No jquery is there, just the core javascript by browser

Loading jQuery:
var s = document.createElement('script');
s.setAttribute('src', 'http://code.jquery.com/jquery-latest.min.js');
document.body.appendChild(s)

Now using jquery to do your stuff like finding the links
var links = $('a[href*="1080"]')
for(var i = 0; i < links.length; i++) {
  if(links[i].href.indexOf('x265') > 0)
    console.log(links[i].href);
}

Tuesday, December 6, 2016

Handling One or Multiple Selection in Grails

Scenario:
We have a select in the form with id="book".
The user may choose none, 1 or multiple in select and submit the form.
There is an each loop in the controller/service to process each user.

What happen:
For each selected book in the select one book=N parameter is posted to the controller.

Grails controller which creates params automatically brings a little intelligence and convert the params to a list if there are multiple instances of that parameters, so in the case of multiple book selection the params will be something like this:

params.book = [ N, M, ...]
It is great and we can loop on it and do the operation on each of them.

But..., if just one user is selected, the there will be no list by default and the param contains just a simple string

params.book = "N"

The bad:

  • The loop is not failed, it is a string and loop iterate over each char of the string
  • In the development and simple test cases, it works perfectly. For example, if it is the id like 5, then it is just a char same as the original. But if the id grows and become something like "58", then your loop process object id 5 and 8.
Solution:
Use params.list method to create a list for that parameter, even if it is single.
def bookIds = params.list("book")

Friday, November 25, 2016

Add to Local Maven Repo, then use it Gradle

Install downloaded jar file (here sqlserver jdbc driver) in local maven repository

mvn install:install-file -Dfile=sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.1 -Dpackaging=jar

In gradle file add mavenLocal() and use the lib as normal maven dependency.


Tuesday, November 8, 2016

Rails Regenerate Scaffolding Views

rails g erb:scaffold ModelName

The scaffolds can be customized by copying originals to lib/templates/erb/scaffold/templates


The original templates and generators can be found in railties gem directory like

lib\ruby\gems\2.2.0\gems\railties-5.0.0.1\lib\rails\generators


Sunday, November 6, 2016

Workaround for Ruby SSL Certificate Issue

The good and complete root certificates can be found at curl site here.

For gem download issue it should be placed in rubygems SSL certificate directory like the following path:

C:\Programs\Ruby22-x64\lib\ruby\site_ruby\2.2.0\rubygems\ssl_certs

For ruby, the root cert file path should be set in environment variable SSL_CERT_FILE like

SSL_CERT_FILE=C:\Programs\Ruby22-x64\lib\ruby\site_ruby\2.2.0\rubygems\ssl_certs\cacert.pem rails new testapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb

ref: https://gist.github.com/fnichol/867550

Thursday, October 13, 2016

Add Base URLs to Grails Page and Scripts

Sometimes we are note sure about final deployment context root address, so the root of the application can be something like mydomain.com/ or mydomain/myapp/ .

Grails or most of the other web app frameworks handle the link creation for such situation with some link facility in pages, but the problem is links in JavaScript, like base url for ajax call or resource loaders.

One possible solution is finding the base urls using the same facility of the framework and set them in global javascript variables.

The best location to add them and complying DRY is the layout page, where all pages have access.

The following is a sample for grails framework to have the url of the root of the app and the assets as well.


window.grails = {
    baseUrl: '${raw(g.createLink(absolute:true, uri:"/"))}'
    assetsUrl : '${ raw(asset.assetPath(src: '')) }',
};


It can be used like this:

$.ajax({
    type: 'GET',
    url: window.grails.baseUrl + 'controller/show/' + id +'.json',
    contentType: 'application/json',
    success: function( data, textStatus, jqXHR )
    {
    },
    error: function( jqXHR, textStatus, errorThrown )
    {
    }
});

First Swagger

Start by editing sample or own simple one

http://editor.swagger.io/

Generate client or server code online in editor or get more customizable package by online generator

curl -X POST -H "content-type:application/json" -d '{"options": {"packageName": "rira_api"},"swaggerUrl":"https://raw.githubusercontent.com/omidmt/rira/0.7.x/api/swagger-api.yaml"}' http://generator.swagger.io/api/gen/clients/python

Response:

{"code":"55b0eb81-87ba-48ff-91f5-3a4444a95462","link":"https://generator.swagger.io/api/gen/download/55b0eb81-87ba-48ff-91f5-3a4444a95462

Sample security definition by 2 ways of using it for client (query/header)

securityDefinitions:
  apikeyHeader:
    type: apiKey
    name: apiKey
    in: header
  apikeyQuery:
    type: apiKey
    name: apiKey
    in: query
security:
  - apikeyHeader: []
  - apikeyQuery: []

[] means apply to all operations, but different definition can be applied per path/operation as well.

Enabe debug mode:
rira_api.configuration.debug = True

Friday, August 12, 2016

Reverse Range of IP to Domain Name

for i in $(seq 1 254); do dig -x 127.0.0.${i} +short; done

Tuesday, July 12, 2016

Control Pagination of Restful Controller

Problem: The default index action (scaffolded) paginate the result, even for xml and json request *that may not be needed for many cases and the result doesn't indicate it is paginated)

Solution: Using withFormat (response not request) and indicating each format (including html) individually. Using '*' overwrite all formats and results in unexpected format.

params.max = Math.min(max ?: 10, 100)
withFormat {
    'html' {
        respond UnknownNode.list(params), model: [unknownNodeInstanceCount: UnknownNode.count()]
    }
    'json' { respond UnknownNode.list(), [status: CREATED] }
    'xml' { respond UnknownNode.list(), [status: CREATED] }
}

Thursday, June 16, 2016

Install JBoss as Windows Service

Env: JBoss EAP 6.4
Windows 7, 10

1- Get the native utility of JBoss. It is available if using JBoss jar installer. If you don't have it get the jar and install somewhere and copy the "native" directory from the following path to the same path of your JBoss:


EAP-6.4.0\modules\system\layers\base\

2- Use the following script and put it in a bat file on the same directory as JBoss directory, then run it.

set BASE=%~dp0

setx /M JBOSS_HOME %BASE%\jboss-eap
setx /M NOPAUSE 1
echo %BASE%

%BASE:~0,2%

cd %BASE%\jboss-eap\modules\system\layers\base\native\sbin

service.bat install /startup /name JBossAppSrv /jbossuser admin /jbosspass 'adminPass'

Just for clarification, the mandatory parts are:
Set system env variable JBOSS_HOME and NOPAUSE=1
Run the "service.bat install "command.

Ref: JBoss 6.4 Installation Guide

Saturday, June 11, 2016

Some Grails Tricks

Set log level in running instance by console

import org.apache.log4j.*
Logger.getLogger("mt.omid.rira.DataService").level = Level.DEBUG

Set grails environment in app server (using same war file for test/release), great for testing in JBoss

Environment variable grails.env can be set to proper environment like development or production.
or

CATALINA_OPTS=-Dgrails.env=development

In JBoss System properties part in Configuration section accept key/value pair that is used for the same purpose.

Grails (Hibernate) Batch Insert/Update

To prevent memory issue and increase the performance of batch insert or update it can be divided into the smaller bunch data, e.g. 1000 records, and flush and clear the transaction after saving each bunch.

def batch = []  // temp batch list
dataList.each { d ->
    d.value = 'blah blah blah'
    batch.add(d)
    if(batch.size() > 1000)
       runBatch(batch)
}


And the runBatch can be a piece of code like this:
        log.debug("Start Data batch saving")
        Data.withTransaction {
            for(Data d in batch){
                log.debug "Saving datum: ${d} ${d.isDirty('value')}"
                // d.save(flush: true) // not required to flush here, since session will flush before clear
                d.save()
            }
        }
        batch.clear()
        def session = sessionFactory.getCurrentSession()
        session.flush()
        session.clear()
        log.debug("Batch saving transaction & clearing hbn session finished")

The hibernate details can be found here.