Tuesday, June 28, 2016

How to install Flask in CentOs 5.3

When you try to install Flask, you might encounter the following error:

$ sudo pip install Flask
Traceback (most recent call last):
  File "/usr/bin/pip", line 7, in ?
    sys.exit(
  File "/usr/lib/python2.4/site-packages/pkg_resources.py", line 236, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.4/site-packages/pkg_resources.py", line 2097, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.4/site-packages/pkg_resources.py", line 1830, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/lib/python2.4/site-packages/pip-8.1.2-py2.4.egg/pip/__init__.py", line 208
    except PipError as exc:
                     ^
SyntaxError: invalid syntax
$

It's a Python version problem.

You can check with the following code:

try:
  print 'a'
except PipError as exc:
  print 'b'

With Python 2.4.3, you will get the following error:

$ python -V                                                                                      
Python 2.4.3
$ python try_except.py
  File "try_except.py", line 3
    except PipError as exc:
                     ^
SyntaxError: invalid syntax
$

With Python 2.6.6, you will get no error as follows:

$ python -V                                                                                      
Python 2.6.6
$
$ python try_except.py
a
$

To install the latest Python, do as follows:

sudo yum install zlib-devel
sudo yum install openssl-devel

cd /home/izeye/programs
wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz
tar zxvf Python-2.7.12.tgz
cd Python-2.7.12
./configure --prefix=/home/izeye/programs/python
make
make install

cd ..
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
./python/bin/python get-pip.py
./python/bin/pip install Flask

Now you can check Flask is working as follows:

$ ./python/bin/python
Python 2.7.12 (default, Jun 28 2016, 23:29:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>>

ImportError: cannot import name HTTPSHandler

If you encounter the following error:

$ python get-pip.py
Traceback (most recent call last):
  File "get-pip.py", line 19177, in <module>
    main()
  File "get-pip.py", line 194, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip
  File "/tmp/tmpI4QU87/pip.zip/pip/__init__.py", line 16, in <module>
  File "/tmp/tmpI4QU87/pip.zip/pip/vcs/subversion.py", line 9, in <module>
  File "/tmp/tmpI4QU87/pip.zip/pip/index.py", line 30, in <module>
  File "/tmp/tmpI4QU87/pip.zip/pip/wheel.py", line 39, in <module>
  File "/tmp/tmpI4QU87/pip.zip/pip/_vendor/distlib/scripts.py", line 14, in <module>
  File "/tmp/tmpI4QU87/pip.zip/pip/_vendor/distlib/compat.py", line 31, in <module>
ImportError: cannot import name HTTPSHandler
$

install `openssl-devel` as follows:

sudo yum install openssl-devel

and rebuild and install Python as follows:

./configure --prefix=/home/izeye/programs/python
make
make install

Reference:
http://stackoverflow.com/questions/20688034/importerror-cannot-import-name-httpshandler-using-pip

zipimport.ZipImportError: can't decompress data; zlib not available

If you encounter the following error:

$ python get-pip.py
Traceback (most recent call last):
  File "get-pip.py", line 19177, in <module>
    main()
  File "get-pip.py", line 194, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip
zipimport.ZipImportError: can't decompress data; zlib not available
$

install `zlib-devel` as follows:

sudo yum install zlib-devel

and rebuild and install Python as follows:

./configure --prefix=/home/izeye/programs/python
make
make install

Reference:
http://stackoverflow.com/questions/6169522/no-module-named-zlib

404 when executing `yum list`

When you're executing `yum list`, you may encounter the following error:

$ sudo yum list
Gathering header information file(s) from server(s)
Server: Red Hat Linux 4AS - x86_64 - Base
retrygrab() failed for:
  http://centos.ustc.edu.cn/centos/4/os/i386/headers/header.info
  Executing failover method
failover: out of servers to try
Error getting file http://centos.ustc.edu.cn/centos/4/os/i386/headers/header.info
[Errno 4] IOError: HTTP Error 404: Not Found
$

Opening the URL in a web browser will get 404, too: http://centos.ustc.edu.cn/centos/4/os/i386/headers/header.info

You can see `readme` in http://centos.ustc.edu.cn/centos/4/

and it includes the following:

```
This directory (and version of CentOS) is depreciated.

CentOS-4 is now past EOL

You can get the last released version of centos 4.9 here:

http://vault.centos.org/4.9/
```

Modify `/etc/yum.conf` as follows:

[base]
name=Red Hat Linux $releasever - $basearch - Base
#baseurl=http://centos.ustc.edu.cn/centos/4/os/i386/
baseurl=http://vault.centos.org/4.0/os/i386/

[updates]
name=Red Hat Linux $releasever - Updates
#baseurl=http://centos.ustc.edu.cn/centos/4/updates/i386/
baseurl=http://vault.centos.org/4.0/updates/i386/

and try `yum list` again.

Wednesday, June 22, 2016

How to convert URL to punycode in Java

To convert a URL to punycode in Java, you can use the following code if you're using Spring framework:

  public static String getPunycodeUrl(String url) {
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
    String host = uriComponentsBuilder.build().getHost();
    return uriComponentsBuilder.host(IDN.toASCII(host)).toUriString();
  }

Friday, June 17, 2016

Spring Boot heap dump script

If you're using Spring Boot and creating an `application.pid`, you can create the following script to dump heap (live objects only):

jmap -dump:live,format=b,file=heap_dump.`date +%Y%m%d_%H%M%S`.hprof `cat application.pid`

Spring Boot thread dump script

If you're using Spring Boot and creating an `application.pid`, you can create the following script to dump threads:

jstack `cat application.pid` > thread_dump.`date +%Y%m%d_%H%M%S`

Monday, June 13, 2016

How to pass parameters for Java compiler like `-Xlint:unchecked` in Gradle

If you encounter the following warning in Gradle:

Note: /Users/izeye/IdeaProjects/trust/src/test/java/com/ctb/trust/SpringBootActuatorTests.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

you can add the following configuration:

tasks.withType(JavaCompile) {
    options.compilerArgs << '-Xlint:unchecked'
}

And then you will see the following detail:

Map<String, Object> health = response.getBody();
                                                             ^
  required: Map<String,Object>
  found:    Map
1 warning

Reference:
https://discuss.gradle.org/t/it-seems-that-javac-compiler-options-is-not-passed-to-compilejava-task-on-gradle2-6-with-jdk8u60/11271

Thursday, June 9, 2016

Use a specific value of enum for a column value in JPA

If you use JPA 2.1, you can use JPA converter as follows:

@Converter(autoApply = true)
public class RatingScoreConverter implements AttributeConverter<RatingScore, Integer> {

@Override
public Integer convertToDatabaseColumn(RatingScore attribute) {
return attribute.getScore();
}

@Override
public RatingScore convertToEntityAttribute(Integer dbData) {
return RatingScore.getValueByScore(dbData);
}

}

Reference:
https://dzone.com/articles/mapping-enums-done-right

org.hibernate.TransientObjectException: object references an unsaved transient instance

When saving an entity having the following property:

@ManyToMany
private Set<Landmark> landmarks;

the following exception might occur:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.ctb.trust.core.restaurant.domain.Landmark

Set `cascade` as follows:

@ManyToMany(cascade = CascadeType.ALL)
private Set<Landmark> landmarks;

ONLY IF its behavior is appropriate for you.

Otherwise, you can save them manually.

Reference:
http://stackoverflow.com/questions/2302802/object-references-an-unsaved-transient-instance-save-the-transient-instance-be

Wednesday, June 8, 2016

How to check errors in logrotate

To check errors in logrotate, run the logrotate cron script as follows:

$ sudo /etc/cron.daily/logrotate
error: skipping "/home/izeye/programs/nginx/logs/access.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
error: skipping "/home/izeye/programs/nginx/logs/error.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
$

If you encounter the above errors, you can change the owner and the group of the `logs` directory to the `root` in this case as follows:

sudo chown -R root:root logs

I can't see any security effect with this but I'm not sure because I'm not an expert on security.

Reference:
http://serverfault.com/questions/381081/where-does-logrotate-save-their-own-log

How to get the last rotation time with logrotate

To get the last rotation time with logrotate, use the following command:

cat /var/lib/logrotate.status

Reference:
http://serverfault.com/questions/189320/how-can-i-monitor-what-logrotate-is-doing

Tuesday, June 7, 2016

Change the number of partitions of a specific topic in Kafka

Check the number of partitions of a specific topic with the following command:

./bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic event

and change the number of partitions of the topic with the following command:

./bin/kafka-topics.sh --zookeeper localhost:2181 --topic event --alter --partitions 10

Sunday, June 5, 2016

Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.

If you see the following error:

There was an unexpected error (type=Forbidden, status=403).
Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.

Check if you're trying to sign out (log out, logout) in security-ignored path.

`CsrfToken` will be `null` in security-ignored path.

Saturday, June 4, 2016

Why aren't my all `*.log` files committed into Git?

There's no entry like `*.log` in the `.gitignore` file of my project.

But I can't add `*.log` with the following message:

C:\Users\izeye\IdeaProjects\test>git addsrc\test\resources\test.log
The following paths are ignored by one of your .gitignore files:
src\test\resources\test.log
Use -f if you really want to add them.
fatal: no files added

C:\Users\izeye\IdeaProjects\test>

I can add it forcefully:

C:\Users\izeye\IdeaProjects\test>git add src\test\resources\test.log -f
warning: LF will be replaced by CRLF in src/test/resources/test.log.
The file will have its original line endings in your working directory.

C:\Users\izeye\IdeaProjects\test>

but why?

Finally I found the following configuration in `.gitconfig`:

[core]
autocrlf = true
excludesfile = C:\\Users\\izeye\\Documents\\gitignore_global.txt

and `gitignore_global.txt` has the following configuration:

#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
*.dll
*.lib
*.sbr

That's why I can't add `*.log`.

Wednesday, June 1, 2016

Limit `kafka-logs` directory disk size

You can limit `kafka-logs` directory disk size through log retension hours.

Open `config/server.properties` and change the following property:

log.retention.hours=168