昨天,今天,明天,每天的每天,你是否都多懂得一点点...

星期三, 二月 14, 2018

Get your dev env ready in a second

I have been using cmder for quite a while. But I never setup task group before. Before I start my development, I need to start the API backend, the database server, and the front end service. It doesn' t take a lot of time. But it requires me to think about what commands to run every time. 

Setting up cmder to automatically start the env is not hard. 

Here is how you do it.





cmd /k "%ConEmuDir%\..\init.bat && docker start mongo mongo-exp "  -new_console:d:%userprofile%

cmd /k "%ConEmuDir%\..\init.bat && dotnet run"  -new_console:d:c:\Users\dengy\gitLocal\amoeba-api\AmoebaAPI

cmd /k "%ConEmuDir%\..\init.bat && ng serve"  -new_console:d:c:\Users\dengy\gitLocal\tarantula\Tarantula




​​



--
Feng

星期二, 二月 13, 2018

Migrate your local mysql db to Azure mysql

Last time, I introduced deploying a new wordpress on azure. But in the real case, you may already have a wordpress running on your local machine with a lot of fancy plugins and some cool pages.

Instead of let wordpress to init an empty database, you may would like to migrate your local mysql database to azure.

I will show you how this can be done.

The first thing is dumping your database to a file. This can be done by using mysqldump command or any other mysql tools such as phpmyadmin.

If you are running mysql in a docker container like what I am doing, use the following command:

docker exec container-name /usr/bin/mysqldump -u root --password=yourpassword wordpress > backup.sql

Or alternatively, you can use phpmyadmin to connect to mysql and export from phpmyadmin

docker run --name tempmyadmin --rm -d --network=wp-net --link wp-mysql:db -p 8060:80 phpmyadmin/phpmyadmin

Then you will need to import your dumped file to azure mysql server. I use phpmyadmin to do this.

docker run --name myadmin -d -e PMA_HOST=azure-dbhost -p 8080:80 phpmyadmin/phpmyadmin

Before importing, you may need to drop the old db and recreate an empty one with phpmyadmin. Once imported, you will need to update the URLs in options table to match the new azure wordpress domain.

Now, back to your wordpress website, you will notice that all the posts and pages appear.

If you have customized your wordpress locally, you may notice that your plugins and customized themes are gone. Because these settings are not in database. You will need to migrate your whole wordpress in your localhost to Azure, I will cover this bit later.


Pass environment variables to Azure container instance

In my last post, I created a wordpress on Azure container instance. And I used Azure database for mysql as the database.

The reason I chose to use Azure database is that it's cheaper than docker container.

But it doesn't stop you to use a docker container as a database server.

Here is how you can create a mysql server in docker. 

az container create --name tempmysql --image mysql -e MYSQL_ROOT_PASSWORD=yourpassword --resource-group testACI --ip-address public --port 3306 

environment variables and start up command are well supported in  Azure container instance.

fengnz@mac:~ > az container create -h

Command
    az container create: Create a container group.

Arguments
    --image               [Required]: The container image name.
    --name -n             [Required]: The name of the container group.
    --resource-group -g   [Required]: Name of resource group. You can configure the default group
                                      using `az configure --defaults group=<name>`.
    --command-line                  : The command line to run when the container is started, e.g.
                                      '/bin/bash -c myscript.sh'.
    --cpu                           : The required number of CPU cores of the containers.  Default:
                                      1.
    --environment-variables -e      : A list of environment variable for the container. Space
                                      separated values in 'key=value' format.
    --ip-address                    : The IP address type of the container group.  Allowed values:
                                      Public.
    --location -l                   : Location. You can configure the default location using `az
                                      configure --defaults location=<location>`.
    --memory                        : The required memory of the containers in GB.  Default: 1.5.
    --os-type                       : The OS type of the containers.  Allowed values: Linux,
                                      Windows.  Default: Linux.
    --ports                         : The ports to open.  Default: [80].
    --restart-policy                : Restart policy for all containers within the container group.
                                      Allowed values: Always, Never, OnFailure.  Default: Always.

Azure File Volume Arguments
    --azure-file-volume-account-key : The storage account access key used to access the Azure File
                                      share.
    --azure-file-volume-account-name: The name of the storage account that contains the Azure File
                                      share.
    --azure-file-volume-mount-path  : The path within the container where the volume should be
                                      mounted. Must not contain colon (:).
    --azure-file-volume-share-name  : The name of the Azure File share to be mounted as a volume.

Image Registry Arguments
    --registry-login-server         : The container image registry login server.
    --registry-password             : The password to log in container image registry server.
    --registry-username             : The username to log in container image registry server.

Global Arguments
    --debug                         : Increase logging verbosity to show all debug logs.
    --help -h                       : Show this help message and exit.
    --output -o                     : Output format.  Allowed values: json, jsonc, table, tsv.
                                      Default: json.
    --query                         : JMESPath query string. See http://jmespath.org/ for more
                                      information and examples.
    --verbose                       : Increase logging verbosity. Use --debug for full debug logs.

Examples
    Create a container in a container group with 1 core and 1Gb of memory.
        az container create -g MyResourceGroup --name myalpine --image alpine:latest --cpu 1
        --memory 1

    Create a container in a container group that runs Windows, with 2 cores and 3.5Gb of memory.
        az container create -g MyResourceGroup --name mywinapp --image winappimage:latest --os-type
        Windows --cpu 2 --memory 3.5

    Create a container in a container group with public IP address and ports.
        az container create -g MyResourceGroup --name myalpine --image alpine:latest --ip-address
        public --ports 80 443

    Create a container in a container group that invokes a script upon start.
        az container create -g MyResourceGroup --name myalpine --image alpine:latest --command-line
        "/bin/sh -c '/path to/myscript.sh'"

    Create a container in a container group that runs a command and stop the container afterwards.
        az container create -g MyResourceGroup --name myalpine --image alpine:latest --command-line
        "echo hello" --restart-policy Never

    Create a container in a container group with environment variables.
        az container create -g MyResourceGroup --name myalpine --image alpine:latest -e key1=value1
        key2=value2

    Create a container in a container group using container image from Azure Container Registry.
        az container create -g MyResourceGroup --name myalpine --image
        myAcrRegistry.azurecr.io/alpine:latest --registry-password password

    Create a container in a container group using container image from another private container
    image registry.
        az container create -g MyResourceGroup --name myapp --image myimage:latest --cpu 1 --memory
        1.5 --registry-login-server myregistry.com --registry-username username --registry-password
        password

    Create a container in a container group that mounts an Azure File share as volume.
        az container create -g MyResourceGroup --name myapp --image alpine:latest --command-line
        "cat /mnt/azfile/myfile" --azure-file-volume-share-name myshare --azure-file-volume-account-
        name mystorageaccount --azure-file-volume-account-key mystoragekey --azure-file-volume-
        mount-path /mnt/azfile

fengnz@mac:~ >


--
Feng

星期一, 二月 12, 2018

Wordpress on Azure Container Instance

In my last post, I tried out using Azure Container Instance to host a simple HTTP service. Today, I am going to host wordpress on it. 

Once loginned, run the following command:

az container create --name wp --image wordpress --resource-group testACI --ip-address public --port 80



You can find the full image name from docker hub: https://hub.docker.com


Use the ip address and port number from the output

Navigate it from a browser:



Choose the language and next



Now you will need to connect to a database



Let's create one, in this example, I will use a mysql database



Enter database details



Then wait



Once finished, you can see all the info of this sql server





Telnet to test the server 




To make things simple 

I disabled SSL and enable Azure service



Before connecting from wordpress to mysql, we need to create a database for wordpress. I use phpmyadmin in docker to do this:

docker run --name myadmin1 -d -e PMA_HOST=feng-test-mysql.mysql.database.azure.com -p 8070:80 phpmyadmin/phpmyadmin










Now enter the database detail to wordpress














Now my wordpress is ready to use, remember to update the URLs when a domain name is used.



--
Feng

星期六, 二月 10, 2018

Azure Container Instance Play Around

To start with Azure Container Instance, we will need to install Azure CLI tool, which is a tool to manage Azure from the command line.


I installed Azure CLI on my Mac OS EI Capitan 

brew update && brew install azure-cli

then encountered this error:



Follow the instruction to install Xcode Command Line tools



Click on Install and agree with the agreement



It will take a while to download




Run again 

brew update && brew install azure-cli

The following dependencies will be installed:
openssl, sqlite, gdbm, python3

Once finished, use the following command to login to your azure account.

az login



Follow the instruction to login from browser



Once done, you can close the browser window


Then you will be able to see your subscription from the command line



Then run the following command to create a new resource group

az group create --name testACI --location eastus



Then run 
az container create --name simpleservice --image magneticio/simpleservice:1.0.0 --resource-group testACI --ip-address public --port 3000


to create a service



You can always run the following command to see the service detail

 az container show --name simpleservice --resource-group testACI

Find ip address and port number from the output

Now try to browse it from the browser



How to host my own stuff? Leave it next time!



--
Feng

星期五, 二月 09, 2018

Kiwi Saver 的回报率到底是多少?让我来告诉你

NZ 的 Kiwi Saver 到底怎么计算的? 相信很多朋友跟我之前一样,搞不清,也不想搞清。 

今天有个同事问我这个问题,我发现我不懂, 于是上网了解了一下。 

在2012政策改变以前,Kiwi Saver 的回报率是100%. 非常高,因为你只要往你的Kiwi Saver 帐户里放一块钱,你的公司也要放一块钱进去。 所以你的回报率是100%, 而且是即时的。当然,公司在最少的情况下,只需要交你税钱工资的3%. 交满这么多,那自己放多少, 公司就不用跟着放了。 

举个例子,你的月工资是税前1万, 每个月往kiwi saver 里面放%3,也就是300块。 你的公司也得跟着放300块进去,你的回报率是100%. 但如果你放500块进去, 你的公司也只需要放300块。 这时候你的回报率就不足100%了。 有的公司会match 你放的数额, 有些公司只付minimum 的3%。

可惜的是,2012年,政策改变,当你的公司放这300块进你的kiwi saver 帐户的时候, 政府过来插一脚, 这个钱先进政府扣税, 用的ESCT来计算, 以我们上面的例子来算,这个税率是33%。 也就是,公司给的这300块,你只能收到200。所以,你每个月进kiwi saver的钱是你自己的300加公司的200, 是500块。

我同事问我,这300块是税前扣的还是税后扣的? 

答案是税后扣的。 

有些朋友就纳闷了,不是说3% of gross salary 吗? 怎么会是税后扣的?

从以下引用看的确是这样。


If you're employed, you can choose to contribute either 3%, 4% or 8% of your before-tax pay. Otherwise you can agree your contribution rate with your provider.


The employer must make KiwiSaver deductions at the default rate of 3% of the new employee's total salary or wages, unless the employee has given them notice that their contribution rate is 4% or 8%.


但是,这句话实际上只是用来说明你可以存多少钱进你的kiwi saver 帐户,而不是说这些钱是从gross salary 里面扣的。 

再举上面的例子,你一个月1万块,那你就可以最高存800进你的kiwi saver account, 而你的公司得最少付3%, 也就是300进你的kiwi saver account, 这300会被税。而如果你一个月工资只有1千,你就最多只能存80, 而你只能从你公司白拿30块。 看到差距了吧,gross salary越高,可以白拿的越多。

但你自己存进kiwi saver 帐户的钱其实是被税了吧。 因为你付的PAYE是按你总的gross salary 里面计算的, 而不是减去3%以后才计算的。 

如果你月入是1万,那年入就是12万,可以计算出你的net income 是 73.18%. 



也就是说,如果kiki saver 没有最高8%的限制,你也只能存73.18% 的 gross income 到你的kiwi saver account, 而不是100%. 因为这些钱是从你的net income里扣的。 重复一次, 这个3% of gross income 只是用来计算你想放多少钱进kiwi saver account 而已。

这3%的gross income 其实占 net income的比就不只3%了。 而是 3/73.18 = 4.1%, case by case.

所以, 不要幻想你存进kiwi saver 的钱是免税的,你存进8%的钱也不能省更多的税。因为税已经扣过了。

那到底你税前的多少钱进入了kiwi saver 呢?以月入1万为例,如果你选择的是3%的话,那你进Kiwi saver account 的钱就是300块,而这300块的净收入,其实是从 300/0.07318 = 410 块的毛收入里来的。

搞笑的是,你是用税前410块存进你的kiwi saver的(变成税后300块),但公司却只是match 你税后交的,也就是说, 公司只会存300块到你的kiwi saver account. 而且这300块还要被税,也就是变成200块。所以有些朋友会觉得公司给的这份钱是不是被税了两次。不是的, 就是这么算的。

好了,这就是kiwi saver 的各种计算方法了,想了解ESCT算法的话可以看这里:

 

--
Feng

星期四, 二月 08, 2018

用手机上的Youtube APP投屏到小米盒子上的Youtube TV, 可遥控

用手机上的Youtube控制小米盒子上的Youtube, 这个功能真的很好, 一这下要分享. 这个功能其实早就存在了, 但我好久后才注意到.


--
Feng

庆祝Youtube Channel 获得首次100个订阅

哈哈, 昨天收到谷歌邮件通知得到100个订阅, 非常开心. 

这个月发了十几个视频, 虽然每个视频的制作都要花我好几个小时的时间, 但是看到我的视频被看了几千次, 网友看视频的时候远远超过我制作它们的时间的时候,我就觉得我花的每一分钟都非常值得.



一个月得到100个订阅, 感觉还是很有成就感的. 

这一百个是一个里程碑, 代表了网友对我的支持, 来之不易, 是对我今后继续分享知识最大的动力.

昨天刚过100, 今天就到153了, 后面这53来得有点容易 :) 
last 28 days 的曲线也非常漂亮, 基本是稳步增长.





--
Feng

Use Local network to access internet when VPN is connected

I recently worked a few times at home using company VPN. Once connected with VPN, I remote desktop to my computer in my office to work. The VPN speed is unstable, sometimes slow, sometimes all right. I would like to only use remote desktop through VPN. All other internet access from my local network. There are problems on both Mac os and Windows, and their problems are different. 

I first tried with mac os. I have mac os EI Capitan, which is not the latest OS Sierra. 

The connection to the VPN is totally fine. But after connecting, I still couldn't access my remote desktop. I couldn't even ping my PC in the office. 

I checked my VPN IP address and found that it's different from my office LAN IP. The VPN adapter has an IP address of 172.16.1.118, but my office computer has an IP address of 192.168.0.50. But this wasn't the problem. If you are trying to fix your VPN IP, stop it.

I checked my route table with the command netstat -nr and noticed that there was no routing route to the VPN network. 

I then added a static route manually using the following command:

sudo route -n add 192.168.0.0/24 172.16.1.1

This did all the trick for me. 

Now I can remote desktop to my office PC, and my local network is used to access the internet.


whereas on Windows 10, it's a different situation. VPN connection on Windows 10 is straightforward. Windows can detect the VPN protocol type, you don't have to enter if you don't know. 

Once connected, I could remote into my office computer without any problem. But my internet became very slow, as all my traffic went to the VPN.  I found out this by trace route.


And interestingly, there are two routes for defaults. Obviously, the second one is used.


This is not what I want, I need to change my routing table to route all other traffic (outside 192.168.0.1 - 192.168.0.254 range) to my local gateway.

I did this by using the following command:

route delete 0.0.0.0
route add 192.168.0.0 mask 255.255.255.0 172.16.1.118
route add 0.0.0.0 mask 0.0.0.0 172.16.1.118

In theory, the above command should resolve the problem. But It didn't. I couldn't access any internet anymore. I could still access the VPN network.

The default route was not working. 

Now my routing table looked like:


The routing table is perfect, but it didn't work as expected.

The one more trick to do is to disable  Use default gateway on remote network






You need to do this for both ipv4 and ipv6. 

Once this is done, reconnect to the VPN. This time, your routing table will not be changed by VPN. 

But the behavior will become the same as mac os.  I couldn't access my office network by default. I needed to add a static route to access my office network.

I did these commands again

route add 192.168.0.0 mask 255.255.255.0 172.16.1.118


Now, all done. I can access my office PC through remote desktop. And all my other traffic to the internet will still go through my local network.


-
Feng

星期二, 二月 06, 2018

被微软One Drive 坑了

One Drive 其实好久没用了. 因为大多数照片在Google Photo里面, 只有一年左右的照片在OneDrive 里. 今天突然想找那一年的照片, 于是登于一看,吓了一跳...



忘了截图了, 上面那张图是网上找的, 日期不对, 反正我的就是显示明天就过期, 我X. 连一点思考的时间都不给我.

因为我之前Unfroze过, 当初登录的时候没有太认真看, 直接点unfroze 就不理了. 现在微软不给机会了, 除了花钱买它的服务, 不买? 明天就删你的照片. 我哭!!

好吧, 大丈夫能屈更能缩. 付款.

才不! 


这里有个office 365 的 plan, 可以试用一个月.  试用就试用吧, 被逼试用... 需要信用卡


注册完后, 得到 1T空间



空间容量比百度云差远了, 虽然我也不用百度云放照片. 

然后, 赶紧的, 取消, 取消后还是可以试用了一个月的. 不会自动续费就是.




哈, 还能得到一个office的key, 能用多久不知道, 还没试. 

然后下载one drive 开始同步






等我同步完了我就跟one drive say goodbye



下一次就是得想办法把这些照片搞到 Google Photo 上去, 还没想好怎么搞. Google Photo好像没有mac 版. 以前的picaza 肯定是不能再用了. 

应该可以用google drive client 搞定, 等我onedrive 同步完再说吧.











--
Feng

其它博客地址

此博客的同步博客地址: http://fengnz.wordpress.com
这里进入我的MSN SPACE.