Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo 3.4.5 fix: update tag value in replicaset configuration when restore from backups #20

Open
wants to merge 5 commits into
base: mongo-3.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hosts
files/tmp
debug-*.yml
*.tar.gz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个应该在 ansible/ 目录下吧

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请忽略这个文件,不小心加进去的

2 changes: 0 additions & 2 deletions ansible/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion ansible/make.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: test all roles
hosts: all
hosts: image
gather_facts: no
vars:
caddy_version: 1.0.4
Expand Down
2 changes: 2 additions & 0 deletions ansible/roles/replica/files/mongo-trib.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def reconfig_host(self):
for member in members:
if member_id == self.get_member_id(member):
member_in_db['host'] = self.get_member_host(member, port)
member_in_db['tags']['qc_sid'] = member['sid']
member_in_db['tags']['qc_node_id'] = member['node_id']
ret = db.system.replset.update({'_id': DEFAULT_REPL_SET_NAME}, cfg)
self.logger.info('cfg=%s, ret=%s', json_dumps(cfg), json_dumps(ret))

Expand Down
2 changes: 1 addition & 1 deletion ansible/roles/replica/files/templates/authorized_keys.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range gets "/hosts/*/*/pub_key"}}
{{.Value}}{{end}}

ssh-rsa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rE/lsJ1DLVhZTQUyEwPpbeRtlphsn+CnUQezDLGIiC1DF85m6WA3fioqCmE2Ofhgv2j8fSfMCUyOyD393fovB87hWvTt4quJnelFG0OkK7wfRwDU4xkQMr5kAk3PrG0nKHaQjtPTem25GA7gsIdQsHYY2jHUij+ai615+ZN6uL5RW8bCxRirEnkqTnfjAjXTy8RgimygBHcR1sqa/fLY4tgLHl0wdSgYvVPXEuo45p73gnf+Goqc7FlSRxZwDvVyz4/PAEMNJNel0sOxCKlMJfNrJAd3XVtqLlmK2LcQsUdlVt9iBPVH7oRkVNakYpXXgIbzyqf17jlK2w/fqBTW6TlOPZPNFil3g67K4I9w== [email protected]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

内容应该没什么变化吧?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1,2行保证集群之间相互ssh
4 行为多余行,疑似代码贡献者为方便调试而添加。此行删除或保留,不影响集群之间ssh通信

7 changes: 2 additions & 5 deletions ansible/roles/replica/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
copy: src=bashrc.sh dest=/root/.bashrc

- get_url:
url: https://fastdl.mongodb.org/linux/{{item.mongodb_version}}.tgz
url: http://downloads.mongodb.org/linux/{{item.mongodb_version}}.tgz
dest: /tmp/{{item.mongodb_version}}.tgz
checksum: sha256:{{item.mongodb_sha}}
with_items:
- {mongodb_version: "{{mongodb_version_34}}",mongodb_sha: "{{mongodb_sha_34}}"}
- {mongodb_version: "{{mongodb_version_36}}",mongodb_sha: "{{mongodb_sha_36}}"}

- get_url:
url: http://appcenter-docs.qingcloud.com/developer-guide/scripts/app-agent-linux-amd64.tar.gz
Expand All @@ -56,7 +55,6 @@
group: root
state: directory
with_items:
- "{{mongo_dir_34}}"
- "app/bin"
- "mongodb"

Expand All @@ -67,8 +65,7 @@
creates: "/opt/{{item.mongodb_dir}}/bin"
extra_opts: [ '--strip-components=1' ]
with_items:
- {mongodb_version: "{{mongodb_version_34}}",mongodb_dir: "{{mongo_dir_34}}"}
- {mongodb_version: "{{mongodb_version_36}}",mongodb_dir: "mongodb"}
- {mongodb_version: "{{mongodb_version_34}}",mongodb_dir: "mongodb"}


- unarchive:
Expand Down
2 changes: 1 addition & 1 deletion app/cluster.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"container":{
"type":"kvm",
"zone":"pek3",
"image":"img-i2o7whhf"
"image":"img-odl8dyxo"
},
"instance_class":{{cluster.replica.instance_class}},
"count":{{cluster.replica.count}},
Expand Down
110 changes: 110 additions & 0 deletions patches/p001/helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env bash

set -eo pipefail

command=$1
args="${@:2}"

initNode() {
if [ ! -d $appctlLogDir ]; then mkdir -p $appctlLogDir; fi
}

readonly appctlLogDir=/data/appctl/logs
readonly appctlLogFile=$appctlLogDir/appctl.log
log() {
echo "$@" >> $appctlLogFile
}

execute() {
local cmd=$1
[ "$(type -t $cmd)" = "function" ] || cmd=_$cmd
$cmd ${@:2}
}

getMongoPort() {
awk '$1=="port:" {print $2}' /etc/mongod.conf
}

# runMongoCmd
# desc run mongo shell
# $1: script string
# $2: ip (option)
runMongoCmd() {
local passwd="$(cat /data/pitrix.pwd)"
local port=$(getMongoPort)

local cmd="/opt/mongodb/bin/mongo --quiet --port $port --authenticationDatabase admin --username qc_master --password $passwd"
local jsstr="$1"

shift
if [ $# -gt 0 ] && [ -n "$1" ]; then
cmd="$cmd --host $1"
fi

timeout --preserve-status 5 echo "$jsstr" | $cmd
}

# isMaster
# desc: judge if the node is a primary node
# $1: node's ip (option)
isMaster() {
runMongoCmd "db.isMaster().ismaster == true || quit(1)" "$1" > /dev/null
}

getIp() {
echo $(echo $1 | cut -d'|' -f2)
}

getNodeId() {
echo $(echo $1 | cut -d'|' -f1)
}

# getOrder
# desc: get upgrade order
# intput: none
# output: like cli-xxx,cli-yyy,cli-zzz
# test cmd: upgrade getorder
getOrder() {
local meta=$(curl -s http://metadata/self/hosts)
local nodes=$(echo "$meta" | grep -o '^/replica/[[:alnum:]-]\+' | uniq | cut -d'/' -f3 | sort)
local tmpstr=''
nodes=($(echo $nodes))
for((i=0;i<${#nodes[@]};i++)); do
tmpstr="$tmpstr $(echo "$meta" | grep "${nodes[i]}/node_id" | cut -f2)"
tmpstr="$tmpstr|$(echo "$meta" | grep "${nodes[i]}/ip" | cut -f2) "
done
tmpstr=($(echo $tmpstr))
local res=''
local mas=''
for((i=0;i<${#tmpstr[@]};i++)); do
if isMaster $(getIp ${tmpstr[i]}); then
mas=$(getNodeId ${tmpstr[i]})
else
res="$res$(getNodeId ${tmpstr[i]}),"
fi
done
res="$res$mas"
echo "$res"
}

doPatch() {
log "patch begin ..."
log "backup files"
mv /opt/mongodb/bin/stop-mongod-server.sh /opt/mongodb/bin/stop-mongod-server.sh.back
log "copy files"
cp /patch/helper.sh /opt/mongodb/bin/
cp /patch/stop-mongod-server.sh /opt/mongodb/bin/
chmod +x /opt/mongodb/bin/helper.sh /opt/mongodb/bin/stop-mongod-server.sh
log "patch done!"
}

doRollback() {
log "rollback begin ..."
log "recover files"
mv /opt/mongodb/bin/stop-mongod-server.sh /opt/mongodb/bin/stop-mongod-server.sh.patch
mv /opt/mongodb/bin/stop-mongod-server.sh.back /opt/mongodb/bin/stop-mongod-server.sh
log "rollback done!"
}

initNode
execute $command $args
205 changes: 205 additions & 0 deletions patches/p001/packages/cluster.json.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"name":{{cluster.name}},
"description":{{cluster.description}},
"vxnet":{{cluster.vxnet}},
"backup_policy": "device",
"incremental_backup_supported": true,
"multi_zone_policy": "round_robin",
"advanced_actions": ["change_vxnet", "scale_horizontal"],
"nodes":[
{
"role":"replica",
"container":{
"type":"kvm",
"zone":"pek3a",
"image":"img-i2o7whhf"
},
"instance_class":{{cluster.replica.instance_class}},
"count":{{cluster.replica.count}},
"cpu":{{cluster.replica.cpu}},
"memory":{{cluster.replica.memory}},
"volume":{
"size":{{cluster.replica.volume_size}},
"mount_point":"/data",
"filesystem":"ext4"
},
"passphraseless":"ssh-rsa",
"vertical_scaling_policy":"sequential",
"services":{
"backup": {
"nodes_to_execute_on": 1,
"order": 1
},
"init":{
"post_start_service":true,
"order":1,
"cmd":"/opt/mongodb/bin/mongo-trib.py init"
},
"start":{
"cmd":"mkdir -p /data/mongodb;/opt/mongodb/bin/start-mongod-server.sh"
},
"stop":{
"cmd":"/opt/mongodb/bin/stop-mongod-server.sh"
},
"restart":{
"cmd":"/opt/mongodb/bin/restart-mongod-server.sh"
},
"scale_out":{
"nodes_to_execute_on":1,
"order":2,
"cmd":"/opt/mongodb/bin/mongo-trib.py reconfig"
},
"scale_in":{
"nodes_to_execute_on":1,
"order":3,
"cmd":"/opt/mongodb/bin/mongo-trib.py reconfig"
},
"destroy":{
"nodes_to_execute_on":1,
"order":2,
"cmd":"/opt/mongodb/bin/mongo-trib.py reconfig"
},
"copy_log": {
"type": "custom",
"cmd": "/opt/mongodb/bin/mongo-trib.py copy_log && echo finish",
"timeout": 1800,
"service_params": {
"copy_log": {{service_params.copy_log}}
}
},
"clean_log": {
"type": "custom",
"cmd": "/opt/mongodb/bin/mongo-trib.py clean_log",
"timeout": 1800
},
"get_nodes_order": {
"event": ["upgrade", "rollback", "scale_vertical"],
"cmd": "/opt/mongodb/bin/helper.sh getOrder"
}
}
}
],
"env": {
"user": {{env.user}},
"passwd": {{env.passwd}},
"maxConns": {{env.maxConns}},
"oplogSize": {{env.oplogSize}},
"port": {{env.port}}
},
"advanced_services": {
"update_nodes_names": {
"cmd": "/opt/mongodb/bin/mongo-trib.py get_nodes_names",
"timeout": 10
}
},
"endpoints": {
"mongod": {
"port": {{env.port}},
"protocol": "TCP"
}
},
"health_check":{
"enable": true,
"interval_sec": 60,
"timeout_sec": 30,
"action_timeout_sec": 30,
"healthy_threshold": 2,
"unhealthy_threshold": 2,
"check_cmd": "/opt/mongodb/bin/mongo-trib.py health_check",
"action_cmd": "/opt/mongodb/bin/mongo-trib.py tackle"
},
"monitor":{
"enable":true,
"cmd":"/opt/mongodb/bin/mongo-trib.py monitor",
"items":{
"opcounters-query":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcounters-update":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcountersRepl-query":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcounters-insert":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcounters-total":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcountersRepl-total":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcountersRepl-delete":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"connections":{
"unit":"times",
"value_type":"int",
"statistics_type":"avg",
"scale_factor_when_display":1
},
"opcountersRepl-insert":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcountersRepl-update":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
},
"opcounters-delete":{
"unit":"times",
"value_type":"int",
"statistics_type":"delta",
"scale_factor_when_display":1
}
},
"groups":{
"opcountersRepl":[
"opcountersRepl-insert",
"opcountersRepl-query",
"opcountersRepl-update",
"opcountersRepl-delete"
],
"opcounters":[
"opcounters-insert",
"opcounters-query",
"opcounters-update",
"opcounters-delete"
]
},
"display":[
"opcounters",
"opcountersRepl",
"connections"
],
"alarm":[
"connections"
]
}
}
Loading