版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99)
Install GO
参考此篇博客创建 - https://blog.csdn.net/quqi99/article/details/121428987
Install juju from source code
go get -u -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
another example
#using 'go get' instead of 'git clone' can download dependency packages as well
#git clone https://github.com/kubernetes/cloud-provider-openstack.git
go get -u -d -v github.com/kubernetes/cloud-provider-openstack/...
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 = git@github.com:<launchpad-id>/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 git@github.com:zhhuabj/openstack_understand_Neutron.git
cd openstack_understand_Neutron
git config user.name "zhhuabj"
git config user.email veryhua2006@gmail.com
#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 -i upstream/master #update master
git merge upstream/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 -f #we need -f if we have rebase
也可使用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/vendor目录下找包,如果没有,才到GOPATH/src目录下去找。‘govendor init’命令需在$GOPATH/src/github.com/zhhuabj/ss目录下执行,故:
20211111更新 - govnedor是第三方的包管理工具,现在go语言支持自己的go.mod来做包管理,当GO111MODULE=on有模块支持时go会忽略 GOPATH 和vendor文件夹只根据 go.mod下载依赖,见:https://www.cnblogs.com/embedded-linux/p/11616183.html
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 gustavo@niemeyer.net-20140225173054-xu9zlkf9kxhvow02 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 git@github.com: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
20201110更新 - Change controller’s IP
refer - https://cloud.garr.it/support/kb/juju/juju_recovery_from_cloned_VM/
juju show-controller zhhuabj |grep api-endpoints
nc -vz 10.5.1.217 22
nc -vz 10.5.1.217 17070
ssh -i ~/.local/share/juju/ssh/juju_id_rsa ubuntu@10.5.1.217
juju models |grep controller && juju show-controller zhhuabj |grep controller-machines && juju ssh -m controller 0
sudo apt install mongodb-clients net-tools -y
#sudo systemctl status juju-db.service
sudo systemctl status snap.juju-db.daemon
sudo mongo --sslAllowInvalidHostnames --sslAllowInvalidCertificates localhost:37017/juju --ssl -u "$(ls /var/lib/juju/agents/)" -p "$(sudo grep statepassword /var/lib/juju/agents/$(ls /var/lib/juju/agents/)/agent.conf|awk '{print $2}')" --authenticationDatabase admin --eval "rs.status()"
# or login into mongodb shell
sudo mongo --sslAllowInvalidHostnames --sslAllowInvalidCertificates localhost:37017/juju --ssl -u "$(ls /var/lib/juju/agents/)" -p "$(sudo grep statepassword /var/lib/juju/agents/$(ls /var/lib/juju/agents/)/agent.conf|awk '{print $2}')" --authenticationDatabase admin
juju:PRIMARY> rs.config()
{
"_id" : "juju",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 1,
"host" : "10.5.1.217:37017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
"juju-machine-id" : "0"
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5f992f1d593bf044e9ef5473")
}
}
# change controller's IP
juju:PRIMARY> cfg = rs.conf()
juju:PRIMARY> cfg.members[0].host = "$NEW_IP_ADDRESS:37017"
juju:PRIMARY> cfg.members = [cfg.members[0]]
juju:PRIMARY> rs.reconfig(cfg, {force : true})
# also need to change controller's IP for juju client in .local/share/juju/controller.yaml
If it's master/slave
remove '--replSet juju' first - sudo EDITOR=vi systemctl edit --full juju-db
sudo systemctl daemon-reload & sudo systemctl restart juju-db
show databases;
use local
help
db.help()
# show dbs pagination
# https://mongodb.github.io/node-mongodb-native/markdown-docs/collections.html
sudo mongo --sslAllowInvalidHostnames --sslAllowInvalidCertificates localhost:37017/juju --ssl -u "$(ls /var/lib/juju/agents/)" -p "$(sudo grep statepassword /var/lib/juju/agents/$(ls /var/lib/juju/agents/)/agent.conf|awk '{print $2}')" --authenticationDatabase admin --eval "db.getCollectionNames()" | more
show tables
db.volumes.help()
db.volumes.stats() #table structure
mongdb ... juju --quiet --eval "db.volumes.find().pretty()" | more
db.machines.find({"model-uuid":"e9932e84-9288-423f-8223-54582e823299"},{"volumes": 0})
db.dropDatabase()
change '--replSet juju' back again - sudo EDITOR=vi systemctl edit --full juju-db
sudo systemctl daemon-reload & sudo systemctl restart juju-db
rs.initiate()
# Before proceeding just for safety, make a backup of your controller.
sudo systemctl stop jujud-machine-0.service
sudo systemctl stop juju-db.service
sudo tar -cvf /home/ubuntu/bkp.tar /var/lib/juju/
sudo systemctl start juju-db.service
sudo mongo --sslAllowInvalidHostnames --sslAllowInvalidCertificates localhost:37017/juju --ssl -u "$(ls /var/lib/juju/agents/)" -p "$(sudo grep statepassword /var/lib/juju/agents/$(ls /var/lib/juju/agents/)/agent.conf|awk '{print $2}')" --authenticationDatabase admin
#other basic usage
show databases;
use juju;
show tables;
db.getName()
agent=$(cd /var/lib/juju/agents; echo machine-*)
pw=$(sudo grep statepassword /var/lib/juju/agents/${agent}/agent.conf|awk '{print $2}')
sudo mongodump -h 127.0.0.1 --port 37017 --ssl -u $agent -p $pw --authenticationDatabase admin --db juju --sslAllowInvalidCertificates --forceTableScan --gzip
#tar -czf mongodump-`date '+%F_%T'| tr -s ':' '-'`.tgz dump
sudo bsondump --pretty dump/juju/controllers.bson --outFile dump/juju/controllers.json
find . -name '*.bson' |xargs -i bsondump --pretty {} --outFile {}.json
find . -name '*.bson.json' |xargs -i grep --color -H 'htb' {}
20211018 - cross-compiling
# for armv7l
mv vendor vendor_bak
...
GOARCH=arm GOARM=5 go get golang.org/x/crypto/ssh/terminal
GOARCH=arm GOARM=5 go build -v -o ./bin/ ...
20220609 - go module
# cd to /bak/golnag/src because go.mod is there
cd /bak/golang/src/
# it doesn't work, don't know how to unset GOPROXY
#go env unset GOPROXY=https://proxy.golang.org,direct
go env -w GOPROXY="direct"
proxychains go get -u -d -v github.com/things-go/go-socks5/...
ls /bak/golang/src/github.com/jpillora/chisel/vendor/github.com/armon/go-socks5
proxychains go install -v github.com/things-go/go-socks5/...
ls /bak/golang/pkg/mod/cache/download/github.com/things-go
go env -w GO111MODULE=off
#unset GO111MODULE && go env -w GO111MODULE=on
proxychains go get -u -d -v github.com/things-go/go-socks5/...
ls /bak/golang/src/github.com/things-go/go-socks5
go build -v -o ./bin/ github.com/things-go/go-socks5/...
20230425 - debug juju code
测试这个代码: https://github.com/juju/juju/blob/juju-2.9.42/provider/vsphere/provider.go#L126
hua@node1:/bak/golang/src/github.com/juju/juju/provider/vsphere/test$ cat main.go
package main
//go build -gcflags="-N -l" main.go
import (
stdcontext "context"
"golang.org/x/net/context"
"net/url"
"fmt"
"github.com/juju/loggo"
"github.com/juju/juju/provider/vsphere"
"github.com/juju/juju/provider/vsphere/internal/vsphereclient"
)
const failedLoginMsg = "ServerFaultCode: Cannot complete login due to an incorrect user name or password."
func main() {
var logger = loggo.GetLogger("juju.provider.vmware")
dial := func(ctx stdcontext.Context, u *url.URL, dc string) (vsphere.Client, error) {
return vsphereclient.Dial(ctx, u, dc, logger)
}
endpoint := "192.168.10.4"
u, err := url.Parse(endpoint)
u, err = url.Parse("https://" + endpoint + "/sdk")
//u.User = url.User("juju")
u.User = url.UserPassword("administrator@vsphere.local", "xxxx")
ctx := context.Background()
client, err := dial(ctx, u, "")
if err != nil {
if err.Error() == failedLoginMsg {
fmt.Println(err)
}
logger.Errorf("Unexpected error dialing vSphere connection: %v", err)
return
}
defer client.Close(ctx)
fmt.Println("Ping succeeded")
}
20230425 - use dlv to debug juju
https://blog.csdn.net/quqi99/article/details/81032421
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