Set up juju development env (by quqi99)

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99)

Install GO

It can’t find context package when using go-1.6 (usr/lib/go-1.6/src/context (from $GOROOT)), so switch to go > 1.8.

#sudo apt upgrade golang
wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
sudo rm -rf /usr/lib/go && sudo tar -C /usr/lib -xzf go1.10.3.linux-amd64.tar.gz
#注意:系统包更新后,如更新为go-1.10,那么GOROOT也要相应更新,否则报很多错
export GOROOT=/usr/lib/go
export GOPATH=/bak/golang
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH

Install juju from source code

go get -d -v github.com/juju/juju/...
cd $GOPATH/src/github.com/juju/juju
make install-dependencies
# may fail because of the lack of some packages caused by gfw, you can fix it by 'go get ...' one by one
go install -v github.com/juju/juju/...

go get github.com/rogpeppe/godeps
cd $GOPATH/src/github.com/juju/juju
godeps -u dependencies.tsv

Prepare LXD

sudo rm -rf /var/lib/lxd
sudo rm -rf /home/hua/.config/lxc/
sudo apt install lxd zfsutils-linux bridge-utils squid-deb-proxy
sudo mkdir -p /images/lxd && sudo ln -s /images/lxd /var/lib/lxd/containers
sudo systemctl enable lxd
sudo systemctl start lxd
newgrp lxd
sudo usermod -a -G lxd hua
sudo lxd init
sudo chown -R root:lxd /var/lib/lxd

Create juju controller

#https://jujucharms.com/docs/2.0/controllers-creating
#https://api.jujucharms.com/charmstore/v5/~james-page/openstack-on-lxd/archive
sudo lxc profile create juju-controller
cat /bak/golang/jujudata/lxd-profile.yaml |sudo lxc profile edit juju-controller
$ cat /bak/golang/jujudata/lxd-profile.yaml
name: juju-controller
config:
  boot.autostart: "true"
  security.nesting: "true"
  security.privileged: "true"
  linux.kernel_modules: openvswitch,nbd,ip_tables,ip6_tables
devices:
  eth0:
    mtu: "9000"
    name: eth0
    nictype: bridged
    parent: lxdbr0
    type: nic
  eth1:
    mtu: "9000"
    name: eth1
    nictype: bridged
    parent: lxdbr0
    type: nic
  kvm:
    path: /dev/kvm
    type: unix-char
  mem:
    path: /dev/mem
    type: unix-char
  root:
    path: /
    type: disk
  tun:
    path: /dev/net/tun
    type: unix-char
juju bootstrap --debug --config bootstrap-series=xenial --config agent-stream=devel localhost juju-controller
sudo lxc exec juju-bf76c2-0 bash

Set up development env

cd $GOPATH/src/github.com/juju/juju
git config --global user.name "xxx"
git config --global user.email "xxx"
ln -s ../../scripts/pre-push.bash .git/hooks/pre-push
#https://segmentfault.com/a/1190000002783245
git remote -v
git pull upstream master
git checkout -b new_feature
go test github.com/juju/juju/...
go fmt
#JUJU_NOTEST_MONGOJS=1 go test github.com/juju/juju/...
git config --global push.default 'nothing'
git rebase master
#git push origin new_feature
$ cat .git/config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = [email protected]:/juju.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = upstream
        merge = refs/heads/master
[remote "upstream"]
        url = https://github.com/juju/juju.git
        fetch = +refs/heads/*:refs/remotes/upstream/*

这是一个典型的使用github pull request模式开发的例子,如:

# First click 'Fork' button to fork - https://github.com/zhhuabj/openstack_understand_Neutron
git clone [email protected]:zhhuabj/openstack_understand_Neutron.git
cd openstack_understand_Neutron
git config user.name "zhhuabj"
git config user.email [email protected]

#do some change on the content
git commit -am "Fix issue #1: change helo to hello"
git push

#pull request

#Update own fork
git remote add upstream https://github.com/yeasy/openstack_understand_Neutron
git remote set-url --push upstream no_push
git fetch upstream                 #git fetch upstream master  
git checkout master              #switch to local master
git rebase upstream/master #update master
git push -f origin master       #push to orign's master

# develop code
git checkout -b myfeature
git add -a
git commit
git push origin myfeature  #git push origin myfeature:myfeature, this will by default create a myfeature on the orign repo

# PR
Can directly visit the this page to create PR - https://github.com/zhhuabj/openstack_understand_Neutron/pull/new/test
or you should now be able to see your branch in https://github.com/zhhuabj/openstack_understand_Neutron/branches. Click on your branch name. Then, click the "New pull request" button, either on the top right of the screen, or in the center of the page under the label "Your recently pushed branches:". Click the Send Pull request button. Await someone's code review.

# Review other's code
Visit https://github.com/yeasy/openstack_understand_Neutron/pulls, or URL in the email you received and go to the person's pull request. If you like-stamp the code, and have judged it suitable, press the big green Merge button

Help! There is no big green button, only a gray bar saying "This pull request cannot be automatically merged.", need to update the code
git fetch origin
git checkout myfeature
git merge origin/master
git commit -a
git push origin myfeature

也可使用Shared Repository Model提交PR,见:https://gist.github.com/seshness/3943237

附录 - 如何创建Go开发环境

1, GOPATH不需要配置多个(它可以配置多个,但不需要),只配置一个即可。如:

export GOROOT=/usr/lib/go-1.8
export GOPATH=/bak/golang

2, 下面开始配置我的第一个Go工程ss,将它放在$GOPATH/src/github.com/zhhuabj目录下

cd /bak/golang/src/github.com/zhhuabj/ && mkdir ss

3, 使用govendor配置包依赖,使用govendor之后,优先从 G O P A T H / s r c / g i t h u b . c o m / z h h u a b j / s s / v e n d o r 目 录 下 找 包 , 如 果 没 有 , 才 到 GOPATH/src/github.com/zhhuabj/ss/vendor目录下找包,如果没有,才到 GOPATH/src/github.com/zhhuabj/ss/vendorGOPATH/src目录下去找。‘govendor init’命令需在$GOPATH/src/github.com/zhhuabj/ss目录下执行,故:

go get -u -v github.com/kardianos/govendor
cd ss && govendor init

4, 使用govendor配置本工程需要用到的其他包:

go get github.com/mitchellh/go-homedir
go get github.com/phayes/freeport
govendor add github.com/mitchellh/go-homedir
govendor add github.com/phayes/freeport

hua@t440p:/bak/golang/src/github.com/zhhuabj/ss$ govendor list -v
 vu github.com/mitchellh/go-homedir ::/vendor/github.com/mitchellh/go-homedir    
 vu github.com/phayes/freeport ::/vendor/github.com/phayes/freeport

5, 编译并运行测试:

#/usr/lib/go-1.8/bin/go install -v -gcflags "-N -l" github.com/zhhuabj/ss/...
GOPATH=/bak/golang go install -v github.com/zhhuabj/ss/...
ll $GOPATH/bin/ss*
go test -cover -bench=. -run=. github.com/zhhuabj/ss/...

6, 配置eclipse + goclipse环境,首先安装goclipse插件(略), 然后运行下列命令安装一些下面要用到的库:

go get -u golang.org/x/tools/cmd/goimports
go get -u golang.org/x/tools/cmd/gorename
go get -u github.com/sqs/goreturns
go get -u github.com/nsf/gocode
go get -u github.com/alecthomas/gometalinter
go get -u github.com/zmb3/gogetdoc
go get -u github.com/rogpeppe/godef
go get -u golang.org/x/tools/cmd/guru

最后配置’Preferences -> Go’与’Preferences -> Go -> Tools‘中的下列信息即可:
go installation directory = /usr/lib/go-1.8
gocode = /bak/golang/bin/gocode
guru = /bak/golang/bin/guru
godef = /bak/golang/bin/godef
gofmt = /usr/lib/go-1.8/bin/gofmt
最后创建eclipse工程,workspace可以选择在如/bak/workspace_go目录,但是工程目录设置到$GOPATH/src/github.com/zhhuabj/ss即可。所以不需要有多个GOPATH路径。
注意:eclipse中对go工程的是’Build All’,因为之前我将/bak/golang为了方便看代码也链了一个工程,所以Build All时出错。去掉该工程即可。
7, govendor没有版本控制,可使用godep添加版本控制

go get github.com/tools/godep
ls /bak/golang/src/github.com/tools/godep/
cd $GOPATH/src/github.com/zhhuabj/ss && godep save -v ./...

$ cat $GOPATH/src/github.com/zhhuabj/ss/Godeps/Godeps.json
{
	"ImportPath": "github.com/zhhuabj/ss",
	"GoVersion": "go1.8",
	"GodepVersion": "v79",
	"Packages": [
		"./..."
	],
	"Deps": [
		{
			"ImportPath": "github.com/mitchellh/go-homedir",
			"Rev": "b8bc1bf767474819792c23f32d8286a45736f1c6"
		},
		{
			"ImportPath": "github.com/phayes/freeport",
			"Comment": "1.0.2-7-ge27662a",
			"Rev": "e27662a4a9d6b2083dfd7e7b5d0e30985daca925"
		},
		{
			"ImportPath": "golang.org/x/net/proxy",
			"Rev": "2fb46b16b8dda405028c50f7c7f0f9dd1fa6bfb1"
		}
	]
}

rm -rf /bak/golang/src/github.com/mitchellh/go-homedir/
cd /bak/golang/src/github.com/zhhuabj/ss/ && godep go install -v github.com/zhhuabj/ss/...

8, godep仅针对git, 如果想git和bzr同时用可使用godebs,就和上面juju中使用的一样。

go get github.com/rogpeppe/godeps

# need to write dependencies.tsv according to the following format
https://github.com/rogpeppe/godeps/blob/master/dependencies.tsv
github.com/kisielk/gotool	git	d6ce6262d87e3a4e153e86023ff56ae771554a41	2017-08-28T04:23:10Z
launchpad.net/gocheck	bzr	[email protected]	87

cd /bak/golang/src/github.com/zhhuabj/ss/ && godeps -u dependencies.tsv

9, git版本控制

cd /bak/golang/src/github.com/zhhuabj/ss
git init 
git add .
git commit -m 'initial version'
git remote add origin [email protected]:xxxx/xx.git
git push origin master

10, Debug go in VIM
https://michaelthessel.com/go-vim-debugging-with-gdb/

Add the following two plugin into plugin vundle position in ~/.vimrc
Plugin ‘fatih/vim-go’
Plugin ‘vim-scripts/Conque-GDB’

Then run ‘vim +BundleInstall +qall’ to install those plugins

Finally append the following configurations in ~/.vimrc

let g:ConqueTerm_Color = 2
let g:ConqueTerm_CloseOnEnd = 1
let g:ConqueTerm_StartMessages = 0

function DebugSession()
silent make -o vimgdb -gcflags “-N -l”
redraw!
if (filereadable(“vimgdb”))
ConqueGdb vimgdb
else
echom “Couldn’t find debug file”
endif
endfunction
function DebugSessionCleanup(term)
if (filereadable(“vimgdb”))
let ds=delete(“vimgdb”)
endif
endfunction
call conque_term#register_function(“after_close”, “DebugSessionCleanup”)
nmap d :call DebugSession();

and run ‘:GoInstallBinaries’ in vim to install some dependencies binarys

Press ,d to start debug, eg:
(gdb) b main
(gdb) set args -config server.ini -debug
(gdb) show args
Argument list to give program being debugged when it is started is “-config server.ini -debug”.
(gdb) r

注:如果go里有greenthread,那么gdb将无法调试,这时得用Delve (https://yq.aliyun.com/articles/57578), 使用’dlv debug’启动后剩下的命令与gdb同。
go get github.com/derekparker/delve/cmd/dlv
dlv debug test.go
dlv attach existing-pid

11, Debug C/C++ in VIM - ConqueGDB
https://gist.github.com/RobinCPC/228eceed32dea10f32e2b3d41ad930c8
Compiler your c/c++ code with -g flag. if not, gdb may not find symbol, eg: gcc -g hello.c -o hello
Start debug by ‘:ConqueGdbSplit hello’ in VIM

12, Debug Python by pudb
sudo pip install pudb
from pudb import set_trace; set_trace()
Press ‘?’ for hinting

Reference

[1] https://github.com/juju/juju/blob/staging/CONTRIBUTING.md
[2] https://launchpad.net/juju-core
[3] https://blog.labix.org/2013/06/25/the-heart-of-juju
[4] https://jujucharms.com/community
[5] https://lists.ubuntu.com/mailman/listinfo/juju
[6] https://lists.ubuntu.com/mailman/listinfo/juju-dev
[7] https://github.com/juju/juju/tree/staging/doc

你可能感兴趣的:(Juju,Go,k8s)