#!/bin/bash # Copyright 2014 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. proc launchmaster { if [[ ! -e /redis-master-data ]] { echo "Redis master data doesn't exist, data won't be persistent!" mkdir /redis-master-data } redis-server /redis-master/redis.conf --protected-mode no } proc launchsentinel { while true { global master := $[redis-cli -h $(REDIS_SENTINEL_SERVICE_HOST) -p $(REDIS_SENTINEL_SERVICE_PORT) --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | cut -d' ' -f1] if [[ -n ${master} ]] { global master := $(master//\") } else { global master := $[hostname -i] } redis-cli -h $(master) INFO if [[ "$?" == "0" ]] { break } echo "Connecting to master failed. Waiting..." sleep 10 } global sentinel_conf := 'sentinel.conf' echo "sentinel monitor mymaster $(master) 6379 2" > $(sentinel_conf) echo "sentinel down-after-milliseconds mymaster 60000" >> $(sentinel_conf) echo "sentinel failover-timeout mymaster 180000" >> $(sentinel_conf) echo "sentinel parallel-syncs mymaster 1" >> $(sentinel_conf) echo "bind 0.0.0.0" >> $(sentinel_conf) redis-sentinel $(sentinel_conf) --protected-mode no } proc launchslave { while true { global master := $[redis-cli -h $(REDIS_SENTINEL_SERVICE_HOST) -p $(REDIS_SENTINEL_SERVICE_PORT) --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | cut -d' ' -f1] if [[ -n ${master} ]] { global master := $(master//\") } else { echo "Failed to find master." sleep 60 exit 1 } redis-cli -h $(master) INFO if [[ "$?" == "0" ]] { break } echo "Connecting to master failed. Waiting..." sleep 10 } sed -i "s/%master-ip%/$(master)/" /redis-slave/redis.conf sed -i "s/%master-port%/6379/" /redis-slave/redis.conf redis-server /redis-slave/redis.conf --protected-mode no } if [[ "${MASTER}" == "true" ]] { launchmaster exit 0 } if [[ "${SENTINEL}" == "true" ]] { launchsentinel exit 0 } launchslave (CommandList children: [ (FuncDef name: launchmaster body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_e child:{(/redis-master-data)}) ) ) terminator: ) ] action: [ (C {(echo)} {(DQ ("Redis master data doesn't exist, data won't be persistent!"))}) (C {(mkdir)} {(/redis-master-data)}) ] spids: [-1 66] ) ] spids: [-1 81] ) (C {(redis-server)} {(/redis-master/redis.conf)} {(--protected-mode)} {(no)}) ] spids: [50] ) spids: [44 49] ) (FuncDef name: launchsentinel body: (BraceGroup children: [ (While cond: [(Sentence child:(C {(true)}) terminator:)] body: (DoGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:master) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(redis-cli)} {(-h)} {(${ VSub_Name REDIS_SENTINEL_SERVICE_HOST)} {(-p)} {(${ VSub_Name REDIS_SENTINEL_SERVICE_PORT)} {(--csv)} {(SENTINEL)} {(get-master-addr-by-name)} {(mymaster)} ) (C {(tr)} {(SQ <",">)} {(SQ <" ">)}) (C {(cut)} {(-d) (SQ <" ">)} {(-f1)}) ] negated: False ) ] ) left_token: spids: [113 158] ) } spids: [112] ) ] spids: [112] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id:BoolUnary_n child:{(${ VSub_Name master)}) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:master) op: Equal rhs: { (DQ (BracedVarSub token: suffix_op: (PatSub pat: {(EscapedLiteralPart token:)} do_all: True do_prefix: False do_suffix: False ) spids: [179 184] ) ) } spids: [177] ) ] spids: [177] ) ] spids: [-1 174] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:master) op: Equal rhs: { (CommandSubPart command_list: (CommandList children:[(C {(hostname)} {(-i)})]) left_token: spids: [192 196] ) } spids: [191] ) ] spids: [191] ) ] spids: [188 199] ) (C {(redis-cli)} {(-h)} {(${ VSub_Name master)} {(INFO)}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_QMark "$?"))} right: {(DQ (0))} ) ) terminator: ) ] action: [(ControlFlow token:)] spids: [-1 231] ) ] spids: [-1 237] ) (C {(echo)} {(DQ ("Connecting to master failed. Waiting..."))}) (C {(sleep)} {(10)}) ] spids: [109 252] ) ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:sentinel_conf) op: Equal rhs: {(sentinel.conf)} spids: [256] ) ] spids: [256] ) (SimpleCommand words: [{(echo)} {(DQ ("sentinel monitor mymaster ") (${ VSub_Name master) (" 6379 2"))}] redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(${ VSub_Name sentinel_conf)} spids: [271] ) ] ) (SimpleCommand words: [{(echo)} {(DQ ("sentinel down-after-milliseconds mymaster 60000"))}] redirects: [ (Redir op_id: Redir_DGreat fd: -1 arg_word: {(${ VSub_Name sentinel_conf)} spids: [284] ) ] ) (SimpleCommand words: [{(echo)} {(DQ ("sentinel failover-timeout mymaster 180000"))}] redirects: [ (Redir op_id: Redir_DGreat fd: -1 arg_word: {(${ VSub_Name sentinel_conf)} spids: [297] ) ] ) (SimpleCommand words: [{(echo)} {(DQ ("sentinel parallel-syncs mymaster 1"))}] redirects: [ (Redir op_id: Redir_DGreat fd: -1 arg_word: {(${ VSub_Name sentinel_conf)} spids: [310] ) ] ) (SimpleCommand words: [{(echo)} {(DQ ("bind 0.0.0.0"))}] redirects: [ (Redir op_id: Redir_DGreat fd: -1 arg_word: {(${ VSub_Name sentinel_conf)} spids: [323] ) ] ) (C {(redis-sentinel)} {(${ VSub_Name sentinel_conf)} {(--protected-mode)} {(no)}) ] spids: [101] ) spids: [95 100] ) (FuncDef name: launchslave body: (BraceGroup children: [ (While cond: [(Sentence child:(C {(true)}) terminator:)] body: (DoGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:master) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(redis-cli)} {(-h)} {(${ VSub_Name REDIS_SENTINEL_SERVICE_HOST)} {(-p)} {(${ VSub_Name REDIS_SENTINEL_SERVICE_PORT)} {(--csv)} {(SENTINEL)} {(get-master-addr-by-name)} {(mymaster)} ) (C {(tr)} {(SQ <",">)} {(SQ <" ">)}) (C {(cut)} {(-d) (SQ <" ">)} {(-f1)}) ] negated: False ) ] ) left_token: spids: [362 407] ) } spids: [361] ) ] spids: [361] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id:BoolUnary_n child:{(${ VSub_Name master)}) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:master) op: Equal rhs: { (DQ (BracedVarSub token: suffix_op: (PatSub pat: {(EscapedLiteralPart token:)} do_all: True do_prefix: False do_suffix: False ) spids: [428 433] ) ) } spids: [426] ) ] spids: [426] ) ] spids: [-1 423] ) ] else_action: [ (C {(echo)} {(DQ ("Failed to find master."))}) (C {(sleep)} {(60)}) (C {(exit)} {(1)}) ] spids: [437 457] ) (C {(redis-cli)} {(-h)} {(${ VSub_Name master)} {(INFO)}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_QMark "$?"))} right: {(DQ (0))} ) ) terminator: ) ] action: [(ControlFlow token:)] spids: [-1 489] ) ] spids: [-1 495] ) (C {(echo)} {(DQ ("Connecting to master failed. Waiting..."))}) (C {(sleep)} {(10)}) ] spids: [358 510] ) ) (C {(sed)} {(-i)} {(DQ ("s/%master-ip%/") (${ VSub_Name master) (/))} {(/redis-slave/redis.conf)} ) (C {(sed)} {(-i)} {(DQ ("s/%master-port%/6379/"))} {(/redis-slave/redis.conf)}) (C {(redis-server)} {(/redis-slave/redis.conf)} {(--protected-mode)} {(no)}) ] spids: [350] ) spids: [344 349] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ (${ VSub_Name MASTER))} right: {(DQ (true))} ) ) terminator: ) ] action: [(C {(launchmaster)}) (C {(exit)} {(0)})] spids: [-1 569] ) ] spids: [-1 579] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ (${ VSub_Name SENTINEL))} right: {(DQ (true))} ) ) terminator: ) ] action: [(C {(launchsentinel)}) (C {(exit)} {(0)})] spids: [-1 601] ) ] spids: [-1 611] ) (C {(launchslave)}) ] )