owned this note
owned this note
Published
Linked with GitHub
# Wherehows Build Code with Windows
Date: 2017/11/27
## 前置作業
請優先參考 [官網](https://github.com/linkedin/WhereHows/blob/master/wherehows-docs/getting-started.md)
1. java sdk 1.8
2. 安裝 docker, docker-compose
3. git bash for windows
4. 設定JAVA_HOME的環境變數
5. 設定HADOOP_HOME的環境變數
6. 確認網路環境是否不會限制存取特定網域資訊
7. (Optional) 建議不要在已經下載好的模組下 rebuild module,不適用於此case
8. (Optional) 盡可能不要在本機也裝不同版本的 nodejs,有時候會有問題
9. (Better) 給予Docker Default VM 4GB RAM以上的空間
### 設定環境變數
1. JAVA_HOME
(1). Wherehows要用java 1.8 build, 設定方式如下
![](https://i.imgur.com/FMwVKYw.png)
(2). (Optional) 若已經build test failed,要先清除因test fail產生但未清除的測試檔案
目錄應是: /c/Users/<USER-NAME>/AppData/Local/Temp/
![](https://i.imgur.com/iDP8j30.png)
若裡面沒有但是還是在common-test fail
可參考[這裡](https://hackmd.io/KYMwLAHMlgtARgVnvWYBsYAmsLYEywDsAnAMwkjoAM1AhhPCUA==#2-build-test-fail的原因),修改程式以顯示temp***.job的所在路徑
清除指令 : `rm temp*.job`
2. HADOOP_HOME and system path (Windows Only)
參考[此篇](https://stackoverflow.com/questions/19620642/failed-to-locate-the-winutils-binary-in-the-hadoop-binary-path)下載指定[檔案](https://github.com/srccodes/hadoop-common-2.2.0-bin/archive/master.zip)解壓縮到任意目錄後,環境變數設定方式如下
(1). 設定使用者的環境變數
**HADOOP_HOME = <PATH-TO>\hadoop-common-2.2.0-bin-master**
![](https://i.imgur.com/eAdfZun.png)
(2). 在系統path加上 **${HADOOP_HOME}\bin**
![](https://i.imgur.com/U0ntaYp.png)
:::warning
若不設定, `./gradlew build` 會失敗
可用 `./gradlew build -x test` 跳過但不建議
:::
## Build code 方式
一般權限開啟 git bash 即可
1. 於 Project根目錄 WhereHows/ , 輸入以下指令
```shell
./gradlew build
```
2. (推薦) WhereHows/wherehows-docker/ , 輸入以下指令
```shell
# ./build.sh <image-version>
./build.sh 1
# if success
docker-compose up
```
## Build fail Root Cause file @ wherehows-web/
:::danger
node_modules/broccoli-eyeglass/lib/broccoli_sass_compiler.js
:::
Windows 無法順利build code的問題摘要:
* wherehows-web 中的 Ember build failed
* Error Log https://github.com/linkedin/WhereHows/issues/855
### 問題描述
sass-eyeglass/broccoli-eyeglass 模組尚未處理windows路徑,
加上nodejs對不同作業系統有各自處理路徑的機制。因此:
1. 直接給windows路徑會無法正確讀取檔案。
::: info
需修改 scopedFileName & filesInTree function。
:::
2. 沒特別將來源的css檔案路徑切分css根目錄 (例如: app/styles/),導致會在wherehows-web/dist/assets/ 產生包含css根目錄的css file (例如: dist/assets/app/styles/),因此網頁會找不到應該在預設目錄下(assets/)的wherehows-web-FINGERPINRT.css。
::: info
需修改 parsePath function,針對windows os特別處理
:::
:::warning
注意: 這是針對emberApp做的修改,其他framework要再看看
:::
broccoli_sass_compiler.js 的 [原始碼](https://github.com/sass-eyeglass/broccoli-eyeglass/blob/master/lib/broccoli_sass_compiler.js)
### Fast-solution
可參考自行修改的範例檔案 (last updated: 2017/11/28):
1. [broccoli_sass_compiler.js](https://drive.google.com/open?id=1iBSTN0AH3cKttuWGzA66MzrjwAVEsN4V) 解壓縮到 WhereHows/wherehows-web/
2. [ember.gradle](https://drive.google.com/open?id=16ScDQMDwaqCQgctOvLtAzff58iw_aHcc) 覆蓋掉 WhereHows/gradle/scripts/
### Detail Solution
*由於node_module會在yarnInstall與emberInstall時才開始下載模組,需在intstall後覆蓋
(1). 修改以下程式碼,並把修改好的broccoli_sass_compiler.js
放在 WhereHows/wherehows-web/<PATH-TO>/ folder
**broccoli_sass_compiler.js**
``` javascript
// Support for older versions of node.
function parsePath(pathname) {
if (os.platform() === 'win32') {
/* Remove emberApp default css's root folder.
In linux, before first '/' would be regard as root folder,
implies app/styles will be root folder after path.parse
In windows, root folder always is C:\\,
therefore app\\styles is dir folder not root folder
however, 'app\\styles' logically regarded as css's root in emberApp
*/
pathname = pathname.replace("app\\styles","")
}
/* 省略 */
}
/* 省略 */
module.exports = class BroccoliSassCompiler extends BroccoliPlugin {
/* 省略 */
scopedFileName(file) {
file = this.relativize(file);
if (this.treeName) {
return path.join(this.treeName, file);
} else {
return file;
}
}
/* 省略 */
filesInTree(srcPath) {
let sassDir = this.sassDir || "";
let files = [];
if (this.discover) {
let pattern = path.join(srcPath, sassDir, "**", "[^_]*.scss");
files = glob.sync(pattern);
}
this.sourceFiles.forEach(sourceFile => {
let pattern = path.join(srcPath, sassDir, sourceFile);
files = files.concat(glob.sync(pattern));
});
files = files.map(function(rawPath){
return path.normalize(rawPath);
});
return unique(files);
}
/* 省略 */
```
(2). 在 WhereHows/gradle/scripts/ 修改 ember.gradle
**ember.gradle**
```groovy
/* 省略 */
task emberBuild(type: NodeTask, dependsOn: bowerInstall) {
doFirst {
copy {
// Copy the output of copyTask
from '<PATH-TO>/broccoli_sass_compiler.js'
into 'node_modules/broccoli-eyeglass/lib'
}
}
script = file('node_modules/ember-cli/bin/ember')
args = ['build', '--environment', 'production']
}
/* 省略 */
```
完成之後,確認沒有權限問題即可build
## Develop with intellij IDEA
1. WhereHows/ 輸入以下指令:
```
./gradlew idea
```
2. open -> WhereHows 目錄打開即可,**建議不要選擇 gradle.build**
若照[官網的建議](https://github.com/linkedin/WhereHows/blob/master/wherehows-docs/getting-started.md#edit-the-source-code-in-your-ide)選完之後,會有一些選項,一打開他就是無條件先gradle.build一番,但用intellij的gradle build目前會有問題。
Note: 不要用intellij的 gradle wrapper,也不在那build
## Docker in new git bash windows
1. `docker-machine start` (如果Docker那台VM還沒開)
2. `docker-machine env`
3. 把env輸出結果要求你複製貼上的貼上 e.g., 我的是
` eval $("C:\Program Files\Docker Toolbox\docker-machine.exe" env)`
4. 在Wherehows-docker目錄下,下 `docker-compose up`
## Docker elasticsearch memory too low
如果Elasticsearch開啟時會有 vm.max_map_count [65530] too low的問題
透過 `docker-machine ssh default` 指令進入docker-machine後,輸入以下指令 :
```
sudo sysctl -w vm.max_map_count=262144
```
## Change docker machine location
參考[這裡](https://stackoverflow.com/questions/33933107/change-docker-machine-location-windows/33934211?noredirect=1)新增兩個環境變數 `MACHINE_STORAGE_PATH`、`DOCKER_CERT_PATH`,指向欲使用的目錄
![](https://i.imgur.com/xNtCcYO.png =350x388)![](https://i.imgur.com/785TxYf.png =350x388)
:::info
做任何環境變數的修改,記得bash要重開
:::
## Customized docker machine size
:::danger
以下操作,用Kitematic開啟時,可能會自動移除重建default vm
Toolbox的Kitematic 已被Docker官方列為Deprecated,因此建議以git bash操作為主
:::
參考[Kitematic官方建議](https://github.com/docker/kitematic/wiki/Common-Issues-and-Fixes#running-out-of-space)
使用 `--virtualbox-disk-size` 參數來指定VM大小,單位是MB。以100GB空間為例:
```
docker-machine create -d virtualbox --virtualbox-disk-size "100000" default
```
*記憶體 --virtualbox-memory "4096" 忘記設定,在Virtualbox可以修改
## Others about Docker
1. Mysql 開啟速度過慢:
若電腦運算效能不佳或記憶體不足可能會發生
等到Mysql完全開啟後,再重啟Frontend跟Backend
2. 從容器拿出檔案 or 複製檔案至容器 (資料夾亦可)
```shell
# docker cp <container_name>:<absolute_path> <your_path>
docker cp wherehowsdocker_wherehows-backend_1:/application/logs/etl/foo.txt .
# docker cp <your_path> <container_name>:<absolute_path>
docker cp foo.txt wherehowsdocker_wherehows-backend_1:/application/logs/etl/
```
3. "Unable to use system certificate pool: crypto/x509: system root pool is not available on Windows" : 重新到[官網](https://docs.docker.com/toolbox/toolbox_install_windows/)下載Toolbox安裝更新即可
4. Docker Toolbox 是給 Win 10 標準版以前的Windows OS使用
5. [Docker Community Edition for Windows](https://store.docker.com/editions/community/docker-ce-desktop-windows) 是給Win10 企業版以上的使用
6. [Toolbox的Kitematic is deprecated](https://github.com/docker/kitematic/issues/2817) , toolbox的Kitematic不建議使用
官方目前建議用 [Docker for Mac](https://docs.docker.com/docker-for-mac/) or [Docker for Windows](https://docs.docker.com/docker-for-windows/)
7. 清除Untouched的volume
`docker volume rm $(docker volume ls -qf dangling=true)`
## References
1. [Using path.normalize and path.sep to overcome windows issues](https://github.com/sass-eyeglass/broccoli-eyeglass/pull/38)
2. [Nodejs path](https://nodejs.org/api/path.html#path_path_normalize_path)
3. [Detail solution](https://hackmd.io/KYVgzGAsBGAmwFoDGBDEIGRUsCAcSW+AjLGMYdgOyTFA?both#detail-solution) 的 Github issue [討論串](https://github.com/sass-eyeglass/broccoli-eyeglass/issues/37)
4. [Path issue with different os](https://shapeshed.com/writing-cross-platform-node/)