Ming's blog

一個軟體工程師的旅程 :)


  • Home

  • Archives

暫時使用 Notion 作為 Blog

Posted on 2019-09-09

使用 Codenvy 遇到了一些問題,新的文章暫時先更新在 Notion 上。

Notion 網址:https://www.notion.so/Ming-s-Blog-95642a43c33448ee9e293d0bd9b838ea

grafana-reset-user-password

Posted on 2019-07-15 Comments:

早上 Grafana dashboard 的 session 過期提示我重新登入

然後發現 .. 我忘記 grafana dashboard 的密碼了 Orz


我的 grafana-server 版本是:

1
2
~$ grafana-server -v
Version 6.2.5 (commit: 6082d19, branch: HEAD)

解決方法

1
2
3
~$ sudo sqlite3 /var/lib/grafana/grafana.db
~$ update user set password = '59acf18b94d7eb0694c61e60ce44c110c7a683ac6a8f09580d626f90f4a242000746579358d77dd9e570e83fa24faa88a8a6', salt = 'F3FAxVm33R' where login = 'username';
~$ .exit

這樣就可以重置 username 的 password

還蠻 .. 暴力的 XD

Reference

  • https://community.grafana.com/t/how-do-i-reset-admin-password/23

Lua on Ubuntu18.04

Posted on 2019-07-08 Comments:

最近工作上需要在 Redis 上面寫 Script,Redis 從 2.6.0 版之後包了 Lua interpreter 進去開始支援用 Lua 語言寫 Script,
所以花一些時間去熟悉久仰的 Lua 語言。

筆記一下在 Ubuntu 18.04 上手 Lua 的過程。

Install

apt

Ubuntu 18.04 可以安裝到最新的 Lua 版本是 Lua 5.3

1
2
3
4
~$ sudo apt install lua5.3
~$ lua5.3
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
>

不過要注意的是 Redis 中的 EVAL 版本是 5.1[1]

Building from source

1
2
3
4
5
6
7
8
~$ sudo apt install libreadline-dev
~$ git clone https://github.com/lua/lua
~$ cd ./lua
~$ make all
~$ mv ./lua ./lua /usr/local/bin
~$ lua
Lua 5.4.0 Copyright (C) 1994-2019 Lua.org, PUC-Rio
>

Example: Hello, world!

Interactive Mode

1
2
3
4
5
6
~$ lua
lua
Lua 5.4.0 Copyright (C) 1994-2019 Lua.org, PUC-Rio
> print("Hello, world!")
Hello, world!
>

helloworld.lua

1
~$ vim helloworld.lua
1
2
3
4
5
6
function helloWorld(name)
assert(type(name) == "string", "name expects a string")
return string.format("Hello, %s!", name)
end

print(helloWorld("world"))
1
2
~$ lua helloworld.lua
Hello, world!

Unit Test

要在 Lua 寫 Unit Test 的話,根據 Unit Testing - lua-users wiki 的範例是使用 bluebird75/luaunit。

安裝 luaunit module 以 Lua 5.4 為例

1
2
3
4
~$ lua -v | awk '{print $2}'
5.4.0
~$ sudo mkdir -p /usr/local/share/lua/5.4/luaunit/
~$ sudo curl -o /usr/local/share/lua/5.4/luaunit/init.lua https://raw.githubusercontent.com/bluebird75/luaunit/master/luaunit.lua

安裝 luaunit module 以 5.3 為例

1
2
3
4
lua5.3 -v | awk '{print $2}'
5.3.3
~$ sudo mkdir -p /usr/share/lua/5.3
~$ sudo curl -o /usr/share/lua/5.3/luaunit.lua https://raw.githubusercontent.com/bluebird75/luaunit/master/luaunit.lua

Example: helloWorld module

helloworld.lua

1
2
3
4
5
6
7
8
module = {}

function module.helloWorld(name)
assert(type(name) == "string", "name expects a string")
return string.format("Hello, %s!", name)
end

return module

test_helloworld.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
luaUnit = require("luaunit")
testModule = require("helloworld")

function testHelloWorld()
local ret = testModule.helloWorld("world")
luaUnit.assertEquals(type(ret), "string")
luaUnit.assertEquals(ret, "Hello, world!")

ret = testModule.helloWorld("ming")
luaUnit.assertEquals(type(ret), "string")
luaUnit.assertEquals(ret, "Hello, ming!")

ret = testModule.helloWorld(integer)
luaUnit.assertEquals(type(ret), "string")
luaUnit.assertEquals(ret, "Hello, ming!")
end

os.exit(luaUnit.LuaUnit.run())

Run LuaUnit

1
2
3
4
5
6
~$ lua test_helloworld.lua -v
Started on Mon Jul 8 15:06:09 2019
testHelloWorld ... Ok
=========================================================
Ran 1 tests in 0.000 seconds, 1 success, 0 failures
OK

Error Handling

helloworld.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module = {}

local function helloWorld(name)
-- assert(type(name) == "string", {message="name expects a string"})
if type(name) ~= "string" then
error({message="name expects a string"})
end

return string.format("Hello, %s!", name)
end

function module.HelloWorld(name)
-- Error Handling
-- Reference: https://blog.golang.org/error-handling-and-go
local success, result = pcall(helloWorld, name)
if not success then
return "", result.message
end

return result, nil
end

return module

test_helloworld.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
luaUnit = require("luaunit")
testModule = require("helloworld")

function testHelloWorld()
local ret, err = testModule.HelloWorld("world")
luaUnit.assertNil(err)
luaUnit.assertEquals(type(ret), "string")
luaUnit.assertEquals(ret, "Hello, world!")

ret, err = testModule.HelloWorld("ming")
luaUnit.assertNil(err)
luaUnit.assertEquals(type(ret), "string")
luaUnit.assertEquals(ret, "Hello, ming!")
end

function testHelloWorldWrongCase()
local ret, err = testModule.HelloWorld(123)
luaUnit.assertNotNil(err)
luaUnit.assertEquals(ret, "")
luaUnit.assertEquals(err, "name expects a string")
end

os.exit(luaUnit.LuaUnit.run())
1
2
3
4
5
6
7
~$ lua test_helloworld.lua -v  
Started on Mon Jul 8 15:42:30 2019
testHelloWorld ... Ok
testHelloWorldWrongCase ... Ok
=========================================================
Ran 2 tests in 0.000 seconds, 2 successes, 0 failures
OK

備註

  1. “EVAL is a Lua 5.1 script.” redis.io

Reference & Resource

  • Programming in Lua
  • lua-users wiki
  • Lua Tutorial - tutorialspoint
  • Learn Lua in 15 Minutes

microk8s-notes

Posted on 2019-07-01 Edited on 2019-07-02 Comments:

學習 Kubernetes、MicroK8s 的筆記

  • MicroK8s - Fast, Light, Upstream Developer Kubernetes
  • MicroK8s docs
1
snap install microk8s --classic
1
2
microk8s.start
microk8s.stop
1
~$ sudo snap alias microk8s.kubectl kubectl
1
2
~$ source <(kubectl completion zsh)
~$ source <(kubectl completion bash)

##

~$ microk8s.kubectl config view --raw > $HOME/.kube/config
~$ microk8s.config

## kubernetes dashboard: Not enough data to create auth info structure.

> kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

Alternatives to cloud9 (cloud IDE)

Posted on 2019-06-16 Edited on 2019-07-01 Comments:

為了可以方便隨時寫作,blog.alone.tw 的 hexo 環境,一直都是放在 Cloud 9。

直到 Cloud 9 被 AWS 收購之後,免費方案仍然佛心的維持了一段時間,不過該來的總是要來,最近 c9.io 宣佈將會在 2019 年底停止 c9.io 的服務 :

Cloud9 workspaces will be disabled in 13 days (June 30th, 2019). You will still be able to download and migrate your workspaces until December 31st, 2019.

we plan to discontinue the ability to create new or to use existing workspaces on c9.io on June 30, 2019 and to discontinue all access on December 31, 2019

除了感謝 c9.io 提供這麼好的 cloud IDE 之外,也物色了 c9.io 的替代方案,最終決定落腳 - Codenvy。

Codenvy 是由大名鼎鼎的 Red Hat 提供的 cloud IDE 服務,他是基於 Eclipse Che建構的 SaaS 服務。

平心而論還是 c9.io 用的比較順手、提供的機器運算速度也比較快,不過好消息是 Red Hat 很大方的提供 Codenvy 免費方案 (HOSTED @ CODENVY.IO for DEVELOPER) 總共 3GB RAM (所有建立的 workspace 共享)。

煩惱 c9.io 即將停止服務可以參考 Codenvy :)

或者 AWS Cloud9,同時 c9.io 也有 open source 他們的 core,有動手能力的可以嘗試 c9/core - GitHub

最後也感謝 c9.io 以及 Red Hat 提供的如此優質的 cloud IDE 服務 :)

kde-neon-keep-using-apt-package-manager

Posted on 2019-06-04 Comments:

KDE neon

1
2
3
4
~$ sudo /usr/bin/apt update
~$ sudo /usr/bin/apt upgrade
~$ sudo /usr/bin/apt dist-upgrade
~$ sudo /usr/bin/apt autoremove

https://neon.kde.org/faq#command-to-update

cpp-googletest-gtest-unit-test

Posted on 2019-06-03 Edited on 2019-06-04 Comments:

Install

My environment is Ubuntu 18.04 (bionic)

1
2
3
4
5
6
7
8
9
10
11
12
13
~$ sudo apt install libgtest-dev
~$ sudo apt install cmake
~$ cd /usr/src/googletest/googletest
~$ sudo makir build
~$ cd build
~$ sudo cmake ..
~$ sudo make
~$ sudo cp libgtest*.a /usr/lib/
~$ cd ..
~$ sudo rm -rf ./build
~$ sudo mkdir -p /usr/local/lib/googletest/
~$ sudo ln -s /usr/lib/libgtest.a /usr/local/lib/googletest/libgtest.a
~$ sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/googletest/libgtest_main.a

Sample

Please refer to the following link:
https://github.com/iwdmb/maxmin-googletest-sample

Reference

  • Why no library files installed for google test?
  • google/googletest/googletest/samples/ - GitHub

display-Golang-version-on-bash-prompt

Posted on 2019-01-19 Comments:
1
2
3
4
5
version=$(go version)
regex="([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})"
if [[ $version =~ $regex ]]; then
echo ${BASH_REMATCH[1]}
fi
1
2
3
4
5
6
7
8
9
10
function funcGoVersion {
version=$(go version)
regex="([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})"
if [[ $version =~ $regex ]]; then
echo ${BASH_REMATCH[1]}
fi
}

goVersion=$(funcGoVersion)
echo $goVersion

Reference

  • https://gist.github.com/pyk/ab63cfbd53668display3eed50

Setting-up-an-vpn-server-on-ubuntu-with-docker-pptp-ipsec

Posted on 2018-07-21 Comments:

PPTP mobtitude/vpn-pptp

1
~$ touch chapsecrets
1
2
3
# Secrets for authentication using PAP
# client server secret acceptable local IP addresses
username * password *
1
2
~$ sudo docker pull mobtitude/vpn-pptp
~$ sudo docker run --net=host --name pptp-vpn-server -d --privileged -p 1723:1723 -v /home/chapsecrets:/etc/ppp/chap-secrets mobtitude/vpn-pptp

L2TP/IPSec PSK hwdsl2/ipsec-vpn-server

1
touch vpn.env

vpn.env reference: https://github.com/hwdsl2/docker-ipsec-vpn-server/blob/master/vpn.env.example

1
2
3
~$ sudo modprobe af_key
~$ sudo docker pull hwdsl2/ipsec-vpn-server
~$ sudo docker run --name ipsec-vpn-server --env-file ./vpn.env --restart=always -p 500:500/udp -p 4500:4500/udp -v /lib/modules:/lib/modules:ro -d --privileged hwdsl2/ipsec-vpn-server

Reference

  • IPsec VPN Server on Docker
  • Configure IPsec/L2TP VPN Clients

Python package publish to pypi

Posted on 2018-07-15 Comments:
1
~$ vim setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3

#from distutils.core import setup
import platform
import setuptools

def build_params():
params = {
'name':'copycat-clipboard3',
'version':'1.1',
'description':'easy way let use clip on command line with system clip (support python 3)',
'author':'Ming',
'author_email':'ming@alone.tw',
'url':'https://github.com/iwdmb/copycat',
'py_modules':['copycat3'],
'license':'MIT',
'install_requires': ['clime', 'pyclip-copycat'],
}
if platform.system() == 'Windows':
params['scripts'] = ['copycat3.bat']
else:
params['scripts'] = ['copycat3']

return params

setuptools.setup (
**build_params()
)
1
~$ vim ~/.pypirc
1
2
3
4
5
6
7
8
[distutils]
index-servers =
pypi

[pypi]
repository=https://pypi.python.org/pypi
username=*username*
password=*password*
1
2
3
4
5
6
~$ python3 -m venv ./venv
~$ source ./venv/bin/active
~$ pip install --upgrade pip
~$ pip install --upgrade setuptools wheel
~$ python3 setup.py sdist bdist_wheel
~$ python setup.py sdist upload -r pypi

Reference

  • Packaging Python Projects

解決 gcin 在 KDE 無法輸入中文 (Ubuntu 16.04)

Posted on 2018-07-14 Comments:

今天把系統從 Kubuntu 換成 KDE neon User Edition 並裝完 gcin 之後,發現 gcin 不能在 Konsole 中輸入中文。

解決方法很簡單也很困難,簡單之處在於安裝 gcin-qt5-immodule,難也難在安裝 gcin-qt5-immodule

1
~$ sudo apt-get install gcin-qt5-immodule

輸入之後,會出現錯誤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
~$ sudo apt-get install gcin-qt5-immodule 
Reading package lists... Done
Building dependency tree
Reading state information... Done
Starting pkgProblemResolver with broken count: 1
Starting 2 pkgProblemResolver with broken count: 1
Investigating (0) gcin-qt5-immodule [ amd64 ] < 2.8.6+eliu-0 > ( utils )
Broken gcin-qt5-immodule:amd64 Depends on qtbase-abi-5-5-1 [ amd64 ] < none -> > ( none )
Considering libqt5core5a:amd64 2904 as a solution to gcin-qt5-immodule:amd64 10000
Considering libqt5core5a:amd64 2904 as a solution to gcin-qt5-immodule:amd64 10000
Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
gcin-qt5-immodule : Depends: qtbase-abi-5-5-1
E: Unable to correct problems, you have held broken packages.

之前裝 LinuxMint 18 的時候有解過一次這個問題。

解決方法如下

1
2
3
sudo apt download gcin-qt5-immodule
ar x gcin-qt5-immodule_2.8.6+eliu-0_amd64.deb
vim control.tar.gz

將 tarfile::./control 中的 Depends 由

Depends: gcin-im-client (>= 2.8.6+eliu-0), libc6 (>= 2.4), libgcc1 (>= 1:3.0), libqt5core5a (>= 5.0.2), libqt5gui5 (>= 5.4.1) | libqt5gui5-gles (>= 5.4.1), qtbase-abi-5-5-1

改為:

Depends: gcin-im-client (>= 2.8.6+eliu-0), libc6 (>= 2.4), libgcc1 (>= 1:3.0), libqt5core5a (>= 5.0.2), libqt5gui5 (>= 5.4.1) | libqt5gui5-gles (>= 5.4.1)

wq 儲存

1
2
ar r gcin-qt5-immodule_2.8.6+eliu-0_amd64.deb control.tar.gz
sudo dpkg -i gcin-qt5-immodule_2.8.6+eliu-0_amd64.deb

重新執行 gcin 即可。

也可以直接下載我包好的:Download

1
2
3
4
~$ md5sum gcin-qt5-immodule_2.8.6+eliu-0_amd64.deb 
55ccc5e54dad0665f81b8885c920a00d gcin-qt5-immodule_2.8.6+eliu-0_amd64.deb
~$ sha256sum gcin-qt5-immodule_2.8.6+eliu-0_amd64.deb
87c7edc510f0de68c47a9ababc16ec36c7a765a61343e474e5d6a30f6b1570a3 gcin-qt5-immodule_2.8.6+eliu-0_amd64.deb
12…6

Ming

一個軟體工程師的旅程 :)
58 posts
12 categories
50 tags
RSS
© 2019 Ming
Powered by Hexo v3.9.0
|
Theme – NexT.Muse v7.2.0