构建Grafana的windows编译环境

Grafana是用进行编写的,我们如果想在windows中编译其源码,那可以follow以下的步骤:

编译环境搭建

go环境搭建

Go环境搭建,可以参考前一篇笔记内容,进行环境搭建和配置。

gcc环境安装

go编译grafana时需要用到gcc, 在Windows中我们可以安装win-builds工具,具体下载地址如下:

1
http://win-builds.org/doku.php/download_and_installation_from_windows

下载安装,然后运行安装程序。

这里选择Native Windows和x86_64,然后再选择或新建一个空文件夹作为安装目录,我这里随便起了一个F:\wingw-w64。直接点击右上角Process开始安装。(这样会安装一些额外的工具,可以根据需要自行去掉)

安装完后在系统变量的Path里新建一个条目,值为我们在win-builds里选择的安装目录下的bin, 例如我刚才安装在F:\wingw-w64下,那么系统变量Path应该为F:\wingw-w64\bin

安装完成后,在cmd或powershell中输入$ gcc -v,如果安装成功应该显示类似的提示信息。

至此gcc已经安装配置完成

配置编译

在cmd或powershell中直接运行以下命令(需要安装git,如果没有安装git请自行搜索下载)

1
go get github.com/grafana/grafana

接下来到你的go工作空间中创建如下目录src/golang.org/x/
例如我的是F:\go\src\golang.org\x
然后运行$ git clone git@github.com:golang/sync.git --depth 1
等待git clone完成后, 再运行$ go get github.com/Unknwon/bra,(这个包没有提示,命令执行完毕后到相应目录下确认文件)
进入bra的目录($GOPATH\src\github.com\Unknwon\bra),运行以下命令编译bra
$ go run bra.go run

进入grafana的目录依次运行以下命令:

构建后端

1
2
$ go run build.go setup
$ go run build.go build

构建前端

1
2
3
$ npm install -g yarn
$ yarn install --pure-lockfile
$ npm run build

启动grafana

1
2
3
grafana-server.exe -homepath=F:\go\src\github.com\grafana\grafana
或者
grafana-server.exe -config=F:\go\src\github.com\grafana\grafana\conf\defaults.ini

打开http://localhost:3000,用用户名/密码 = admin/admin登录(默认账户密码)

如果grafana正常工作,则基本的开发环境已经构建完成。

前端编译时,可能会出现无法安装某个包的问题,这个时候我们可以重试几次,如果还是无法解决,比如出现如下问题,

1
2
3
4
5
6
7
info There appears to be trouble with your network connection. Retrying...
error An unexpected error occurred: "https://registry.yarnpkg.com/es6-shim/-/es6-shim-0.35.3.tgz: ETIMEDOUT".
info If you think this is a bug, please open a bug report with the information provided in "/home/gopath/src/github.com/grafana/grafana/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
info There appears to be trouble with your network connection. Retrying...
error https://registry.yarnpkg.com/fsevents/-/fsevents-1.2.4.tgz: ETIMEDOUT
info There appears to be trouble with your network connection. Retrying...

我们可以尝试将yarn的镜像设置为淘宝的镜像即可:

1
2
3
4
5
6
7
yarn config get registry
# -> https://registry.yarnpkg.com

yarn config set registry https://registry.npm.taobao.org
# -> yarn config v0.15.0
# -> success Set "registry" to "https://registry.npm.taobao.org".
# -> Done in 0.04s.