Guest User

Untitled

a guest
Nov 2nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 43.54 KB | None | 0 0
  1. #!/usr/bin/env sh
  2.  
  3. VERSION="@@PACKAGE_VERSION@@"
  4. PROCESS_CONTROLLER_NAME="CloudStorageController"
  5. UNATTENDED_INSTALLATION=false
  6.  
  7. error()
  8. {
  9.     echo "Error: $@" >&2
  10.     exit 1
  11. }
  12.  
  13. case "$(uname -s | tr A-Z a-z)" in
  14. linux)
  15.     PC_PATH="/etc/init.d/$PROCESS_CONTROLLER_NAME"
  16.     ;;
  17. freebsd)
  18.     PC_PATH="/etc/rc.d/$PROCESS_CONTROLLER_NAME"
  19.     ;;
  20. *)
  21.     error "Unsupported platform: $(uname -s)"
  22.     ;;
  23. esac
  24.  
  25. require_root()
  26. {
  27.     if [ "$(id -u)" != "0" ]; then
  28.         error "This action requires superuser access."
  29.     fi
  30. }
  31.  
  32. ends_with_symbol() # string, lastSymbol
  33. {
  34.     if [ ${#1} -eq 0 ]; then
  35.         return 1
  36.     fi
  37.     lastSymbolInString=$(echo "$1" | cut -c ${#1})
  38.     if [ "$lastSymbolInString" = "$2" ]; then
  39.         return 0
  40.     fi
  41.     return 1
  42. }
  43.  
  44. get_normalized_path() # path
  45. {
  46.     if ends_with_symbol "$1" "/" ; then
  47.         echo "$1"
  48.     else
  49.         echo "$1/"
  50.     fi
  51. }
  52.  
  53. starts_with() # string, start
  54. {
  55.     if [ ${#2} -gt ${#1} -o ${#2} -eq 0 ]; then
  56.         return 1
  57.     fi
  58.     firstStringSymbols=$(echo "$1" | cut -c1-${#2})
  59.     if [ "$firstStringSymbols" = "$2" ]; then
  60.         return 0
  61.     fi
  62.     return 1
  63. }
  64.  
  65. is_parent_directory() # parent, child
  66. {
  67.     normalizedParentPath="$(get_normalized_path "$1")"
  68.     normalizedChildPath="$(get_normalized_path "$2")"
  69.     if starts_with "$normalizedChildPath" "$normalizedParentPath"; then
  70.         return 0
  71.     fi
  72.     return 1
  73. }
  74.  
  75. is_livebr_path_interleave() # liveBRLocation, storageLocation
  76. {
  77.     if is_parent_directory "$1" "$2"; then
  78.         echo '"Live BR directory" should not be parent of "Storage directory" or coinside with it'
  79.         return 0
  80.     fi
  81.     if is_parent_directory "$1" "$(getConfigParam StorageReplicationAgentRoot "")"; then
  82.         echo '"Live BR directory" should not be parent of "Replication directory" or coinside with it'
  83.         return 0
  84.     fi
  85.     return 1
  86. }
  87.  
  88. read_value_without_prompt() # variableName, defaultValue
  89. {
  90.     read x
  91.     x="${x:-$2}"
  92.     eval "$1=\"\$x\""
  93.     test -n "$x"
  94. }
  95.  
  96. read_value() # variableName, description, defaultValue
  97. {
  98.     while true; do
  99.         printf "%s" "$2"
  100.         [ -n "$3" ] && printf "%s" " [$3]"
  101.         printf ": "
  102.  
  103.         read_value_without_prompt "$1" "$3" && break
  104.     done
  105. }
  106.  
  107. set_if_not_empty_else_read_without_allow() # commandLineValue, variableName, description, defaultValue
  108. {
  109.     eval "x=\"\$$1\""
  110.  
  111.     if [ -n "$x" ]; then
  112.         eval "$2=\"\$x\""
  113.     else
  114.         if "$UNATTENDED_INSTALLATION"; then
  115.             error "Not specified $3"
  116.         else
  117.             read_value "$2" "$3" "$4"
  118.         fi
  119.     fi
  120. }
  121.  
  122. read_list() # variableName, initialValue, separator, prompt
  123. {
  124.     v="$2"
  125.     while true; do
  126.         printf "%s" "$4"
  127.         read x
  128.         [ -z "$x$v" ] && continue
  129.         [ -z "$x" ] && break
  130.         [ -n "$v" ] && v="$v$3"
  131.         v="$v$x"
  132.     done
  133.  
  134.     eval "$1=\"\$v\""
  135. }
  136.  
  137. read_value_allow_skip() # variableName, description, defaultValue
  138. {
  139.     eval "x=\"\$$1\""
  140.     eval "y=\"\${x:-\$3}\""
  141.  
  142.     if [ $READ_VALUE_ALLOW_SKIP -eq 0 -o -z "$x" ]; then
  143.         if "$UNATTENDED_INSTALLATION"; then
  144.             error "Not specified $2"
  145.         else
  146.             read_value "$1" "$2" "$y"
  147.         fi
  148.     else
  149.         eval "$1=\"\$y\""
  150.     fi
  151. }
  152.  
  153. set_if_not_empty_else_read() # commandLineValue, variableName, description, defaultValue
  154. {
  155.     eval "x=\"\$$1\""
  156.     if [ -z "$x" ]; then
  157.         read_value_allow_skip "$2" "$3" "$4"
  158.     else
  159.         eval "$2=\"\$x\""
  160.     fi
  161. }
  162.  
  163. set_if_not_empty() # commandLineValue, variableName
  164. {
  165.     eval "x=\"\$$1\""
  166.     if [ -n "$x" ]; then
  167.         eval "$2=\"\$x\""
  168.     fi
  169. }
  170.  
  171. ask_question() # variableName, question, choices, defaultChoice
  172. {
  173.     printf "%s (%s) " "$2" "$3"
  174.  
  175.     read_value_without_prompt "$1" "$4"
  176. }
  177.  
  178. guess_program_location() # default
  179. {
  180.     x="$(grep -s '\bprLocation\b' "$PC_PATH" | head -n1 | sed 's|.*prLocation ||')"
  181.     echo "${x:-$1}"
  182. }
  183.  
  184. get_private_config_path() # installDir
  185. {
  186.     echo "$1/etc/config.cfg"
  187. }
  188.  
  189. load_private_config_param() # configFilePath, name, defaultValue
  190. {
  191.     x="$(grep "^$2 " < "$1" | sed -e "s|^$2 ||")"
  192.     x="${x:-$3}"
  193.     eval "$2=\"\$x\""
  194. }
  195.  
  196. load_private_config_param2() # configFilePath, name, name2, defaultValue
  197. {
  198.     x="$(grep "^$2 " < "$1" | sed -e "s|^$2 ||")"
  199.     [ -n "$x" ] || x="$(grep "^$3 " < "$1" | sed -e "s|^$3 ||")"
  200.     x="${x:-$4}"
  201.     eval "$2=\"\$x\""
  202. }
  203.  
  204. load_private_config() # loadAll, filename
  205. {
  206.     [ -e "$2" ] || return 1
  207.  
  208.     load_private_config_param2 "$2" legacyFtpHost legacyServerAddress
  209.     load_private_config_param "$2" legacyStatsUserName "common_user"
  210.     load_private_config_param "$2" legacyStatsUserPassword "04FBA85DD24E260E1BB8"
  211.  
  212.     # load_private_config_param "$2" storageUserId "2001"
  213.     # load_private_config_param "$2" storageUserName "iasouser"
  214.  
  215.     load_private_config_param "$2" partnerName
  216.     load_private_config_param "$2" partnerUserName
  217.     load_private_config_param "$2" partnerUserPassword
  218.     load_private_config_param "$2" storageLocation
  219.     load_private_config_param "$2" liveBRLocation
  220.     load_private_config_param "$2" manServAddress "@@MAN_SERV_ADDRESS@@"
  221.  
  222.     # [ $1 -ne 0 ] || return 0
  223.  
  224.     load_private_config_param "$2" authUser "data"
  225.     load_private_config_param "$2" authPassword
  226.     load_private_config_param "$2" authPasswordHash
  227.     load_private_config_param2 "$2" bindHost bindAddress
  228.     load_private_config_param "$2" bindPort
  229.     load_private_config_param "$2" storageName
  230.     load_private_config_param "$2" storageNodeName
  231.     # load_private_config_param "$2" externalAddress
  232.     load_private_config_param "$2" externalAddressList
  233.     load_private_config_param2 "$2" rcgExtHost rcgExtAddress
  234.     load_private_config_param "$2" rcgExtPort
  235.     load_private_config_param2 "$2" rcgIntHost rcgIntAddress
  236.     load_private_config_param "$2" rcgIntPort
  237.     load_private_config_param2 "$2" webRcgExternalAddressList
  238.     load_private_config_param2 "$2" webRcgIntHost webRcgIntAddress
  239.     load_private_config_param "$2" webRcgIntPort
  240.  
  241.     return 0
  242. }
  243.  
  244. save_private_config_param() # configFilePath, name
  245. {
  246.     eval "x=\"\$$2\""
  247.     echo "$2 $x" >> "$1"
  248. }
  249.  
  250. save_private_config() # saveAll, filename
  251. {
  252.     printf "" > "$2"
  253.  
  254.     [ -e "$2" ] || return
  255.  
  256.     if [ -n "$legacyServerAddress" -o "$legacyStatsUserName" != "common_user" -o "$legacyStatsUserPassword" != "04FBA85DD24E260E1BB8" ]; then
  257.         save_private_config_param "$2" legacyFtpHost
  258.         save_private_config_param "$2" legacyStatsUserName
  259.         save_private_config_param "$2" legacyStatsUserPassword
  260.     fi
  261.  
  262.     # save_private_config_param "$2" storageUserId
  263.     # save_private_config_param "$2" storageUserName
  264.  
  265.     save_private_config_param "$2" partnerName
  266.     save_private_config_param "$2" partnerUserName
  267.     save_private_config_param "$2" partnerUserPassword
  268.     save_private_config_param "$2" storageLocation
  269.     save_private_config_param "$2" liveBRLocation
  270.     save_private_config_param "$2" manServAddress
  271.  
  272.     # [ $1 -ne 0 ] || return
  273.  
  274.     save_private_config_param "$2" authUser
  275.     save_private_config_param "$2" authPassword
  276.     save_private_config_param "$2" authPasswordHash
  277.     save_private_config_param "$2" bindHost
  278.     save_private_config_param "$2" bindPort
  279.     save_private_config_param "$2" storageName
  280.     save_private_config_param "$2" storageNodeName
  281.     # save_private_config_param "$2" externalAddress
  282.     save_private_config_param "$2" externalAddressList
  283.     save_private_config_param "$2" rcgExtHost
  284.     save_private_config_param "$2" rcgExtPort
  285.     save_private_config_param "$2" rcgIntHost
  286.     save_private_config_param "$2" rcgIntPort
  287.     save_private_config_param "$2" webRcgExternalAddressList
  288.     save_private_config_param "$2" webRcgIntHost
  289.     save_private_config_param "$2" webRcgIntPort
  290. }
  291.  
  292. get_list_item() # list, item, separator
  293. {
  294.     echo "$1" | awk -F"$3" "{print \$$2}"
  295. }
  296.  
  297. random_sequence() # length
  298. {
  299.     openssl rand -base64 $(($1 * 2)) | tr -dC 'a-zA-Z0-9' | head -c$1
  300. }
  301.  
  302. get_nginx_status_location()
  303. {
  304.     case "$(uname -s | tr A-Z a-z)" in
  305.     freebsd)
  306.         echo "location = /nginx_status!{!    stub_status on;!    access_log off;!    sla_pass off;!}"
  307.         ;;
  308.     esac
  309. }
  310.  
  311. get_livebr_location() # storageRootPath, liveBRRootPath
  312. {
  313.     if is_parent_directory "$1" "$2"; then
  314.         normalizedStorageRootPath="$(get_normalized_path "$1")"
  315.         normalizedLiveBRRootPath="$(get_normalized_path "$2")"
  316.         liveBRSubDir=$(echo "$normalizedLiveBRRootPath" | cut -c${#normalizedStorageRootPath}-${#normalizedLiveBRRootPath})
  317.         echo "location ^~ "$liveBRSubDir"!{!    deny all;!}"
  318.     else
  319.         echo
  320.     fi
  321. }
  322.  
  323. replace_template_param() # name, value
  324. {
  325.     sed -e "s|@@$1@@|$2|g"
  326. }
  327.  
  328. replace_template_param_with_multi_line() # name, value, oneCharacterNewLineDelimiter
  329. {
  330.     currentIFS=$IFS
  331.     IFS=\n
  332.     while read -r line; do
  333.         if test "${line#*@@$1@@}" != "$line"; then
  334.             echo "$line" | replace_template_param "$1" "$2" | tr "$3" "\n"
  335.         else
  336.             echo "$line"
  337.         fi
  338.     done
  339.     IFS=$currentIFS
  340. }
  341.  
  342. getConfigParam() # name, defaultValue
  343. {
  344.     x=
  345.     [ -r "$D/etc/config.ini" ] && x="$(grep -m 1 "^${1}[ =]" < "$D/etc/config.ini" | sed -e "s|^${1} *= *||")"
  346.     echo "${x:-$2}"
  347. }
  348.  
  349. setConfigParam() # section, key, value, keepEmpty
  350. {
  351.     awk -v section="$1" -v key="$2" -v value="$3" -v keepEmpty="${4:-1}" -f SetConfigParam.awk
  352. }
  353.  
  354. print_spacer() # ...
  355. {
  356.     while [ $# -gt 0 ]; do
  357.         if [ $READ_VALUE_ALLOW_SKIP -eq 0 -o -z "$1" ]; then
  358.             echo ""
  359.             break
  360.         fi
  361.         shift
  362.     done
  363. }
  364.  
  365. getFileUserId() # path, default
  366. {
  367.     x=
  368.     case "$(uname -s | tr A-Z a-z)" in
  369.     linux)
  370.         x="$(stat -c '%u' "$1" 2>/dev/null)"
  371.         ;;
  372.     freebsd)
  373.         x="$(stat -f '%u' "$1" 2>/dev/null)"
  374.         ;;
  375.     esac
  376.     echo "${x:-$2}"
  377. }
  378.  
  379. getUserNameFromId() # uid, default
  380. {
  381.     x="$(getent passwd $1 | awk -F: '{print $1}')"
  382.     echo "${x:-$2}"
  383. }
  384.  
  385. get_nginx_config_param() # name, defaultValue
  386. {
  387.     x=
  388.     [ -r "$D/etc/nginx/nginx.conf" ] && x="$(grep -m 1 '^[ \t]*'$1'[ \t]\+' < "$D/etc/nginx/nginx.conf" | sed -e 's|^[ \t]*'$1'[ \t][ \t]*||;s|[ \t]*;[ \t]*$||')"
  389.     echo "${x:-$2}"
  390. }
  391.  
  392.  
  393. install_nginx_config() # sourceFile, name, dirName
  394. {
  395.     echo "-- Installing $2 NGINX $3 config"
  396.  
  397.     install -d -m 0755 "$D/etc/nginx/$3.d"
  398.  
  399.     install -m 0644 "$1" "$D/etc/nginx/$3.d/$2.conf"
  400. }
  401.  
  402. login_to_cloud() # allowCache
  403. {
  404.     visa=$(cat "$VISA_FILE" 2>/dev/null)
  405.  
  406.     if [ $1 -ne 0 -a -e "$VISA_FILE" ]; then
  407.         x=$(date +%s)
  408.         case "$(uname -s | tr A-Z a-z)" in
  409.         linux)
  410.             y=$(stat -c "%Y" "$VISA_FILE")
  411.             ;;
  412.         freebsd)
  413.             y=$(stat -f "%m" "$VISA_FILE")
  414.             ;;
  415.         esac
  416.  
  417.         [ $((x - y)) -lt $((5 * 60)) ] && return 0
  418.     fi
  419.  
  420.     if ! "$SERVER_TOOL" -machine-readable cloud \
  421.             -server "https://$manServAddress" \
  422.             -partner "$partnerName" \
  423.             -user "$partnerUserName" \
  424.             -password "$partnerUserPassword" \
  425.             -print-visa > "$VISA_FILE"; then
  426.         return 1
  427.     fi
  428.  
  429.     visa=$(cat "$VISA_FILE")
  430. }
  431.  
  432. choose_storage_node_from_list()
  433. {
  434.     if ! login_to_cloud 1 ; then
  435.         echo Failed to login to cloud >&2
  436.  
  437.         return 1
  438.     fi
  439.  
  440.     storagesFilePath="$T/storages.tmp"
  441.     storageNodesFilePath="$T/storage-nodes.tmp"
  442.  
  443.     if ! "$SERVER_TOOL" -machine-readable cloud \
  444.             -server "https://$manServAddress" \
  445.             -visa "$visa" > "$storagesFilePath" \
  446.             cloud.storages.list; then
  447.         exit 1
  448.     fi
  449.     if [ -z "$_storageNodeName" -o -z "$_storageName" ]; then
  450.         echo "Select storage node corresponding to this server:"
  451.  
  452.         count=0
  453.         while read x; do
  454.             echo "  $x"
  455.  
  456.             if ! "$SERVER_TOOL" -machine-readable cloud \
  457.                     -server "https://$manServAddress" \
  458.                     -visa "$visa" \
  459.                     cloud.storage.nodes.list \
  460.                     -storage_name "$x" > "$storageNodesFilePath"; then
  461.                 exit 1
  462.             fi
  463.  
  464.             while read y; do
  465.                 count=$((count + 1))
  466.  
  467.                 echo "    [$count] $y"
  468.  
  469.                 eval "s_$count=\"\$x\""
  470.                 eval "sn_$count=\"\$y\""
  471.             done < "$storageNodesFilePath"
  472.         done < "$storagesFilePath"
  473.  
  474.         read_value i "Enter the number"
  475.         [ "$i" -gt 0 -a "$i" -le $count ] || exit 1
  476.  
  477.         eval "storageName=\"\$s_$i\""
  478.         eval "storageNodeName=\"\$sn_$i\""
  479.         storageNodeName="$(echo "$storageNodeName" | sed -e 's|[ ]*([^)]*)$||')"
  480.     else
  481.         storageName="$_storageName"
  482.         storageNodeName="$_storageNodeName"
  483.     fi
  484. }
  485.  
  486. compose_gateway_host()
  487. {
  488.     if [ -n "$rcgExtHost" ] ; then
  489.         echo "$rcgExtHost:$rcgExtPort"
  490.     fi
  491.     echo ""
  492. }
  493.  
  494. handle_add_node()
  495. {
  496.     _loadFrom="$(get_private_config_path "$D")"
  497.  
  498.     if ! load_private_config 1 "$_loadFrom"; then
  499.         error "Unable to read configuration from '$_loadFrom'"
  500.     fi
  501.  
  502.     login_to_cloud 1
  503.  
  504.     _gatewayHost="$(compose_gateway_host)"
  505.  
  506.     "$SERVER_TOOL" -machine-readable cloud \
  507.         -server "https://$manServAddress" \
  508.         -visa "$visa" \
  509.         cloud.storage.node.add \
  510.         -storage-name "$storageName" \
  511.         -node-name "$storageNodeName" \
  512.         -node-family "WEBDAVS" \
  513.         -node-user "$authUser" \
  514.         -node-password "$authPassword" \
  515.         -node-host "$externalAddressList" \
  516.         -node-path "" \
  517.         -node-gateway-host "$_gatewayHost" \
  518.         -node-http-gateway-host "$webRcgExternalAddressList"
  519. }
  520.  
  521. handle_update_node_info()
  522. {
  523.     _loadFrom="$(get_private_config_path "$D")"
  524.  
  525.     if ! load_private_config 1 "$_loadFrom"; then
  526.         error "Unable to read configuration from '$_loadFrom'"
  527.     fi
  528.  
  529.     login_to_cloud 1
  530.  
  531.     _gatewayHost="$(compose_gateway_host)"
  532.  
  533.     "$SERVER_TOOL" -machine-readable cloud \
  534.         -server "https://$manServAddress" \
  535.         -visa "$visa" \
  536.         cloud.storage.node.info.update \
  537.         -storage-name "$storageName" \
  538.         -node-name "$storageNodeName" \
  539.         -node-family "WEBDAVS" \
  540.         -node-user "$authUser" \
  541.         -node-password "$authPassword" \
  542.         -node-host "$externalAddressList" \
  543.         -node-path "" \
  544.         -node-gateway-host "$_gatewayHost" \
  545.         -node-http-gateway-host "$webRcgExternalAddressList"
  546. }
  547.  
  548. handle_migrate_accounts()
  549. {
  550.     _loadFrom="$(get_private_config_path "$D")"
  551.  
  552.     if ! load_private_config 1 "$_loadFrom"; then
  553.         error "Unable to read configuration from '$_loadFrom'"
  554.     fi
  555.  
  556.     externalAddress="$(get_list_item "$externalAddressList" 1 ",")"
  557.  
  558.     login_to_cloud 1
  559.  
  560.     "$SERVER_TOOL" -machine-readable cloud \
  561.         -server "https://$manServAddress" \
  562.         -visa "$visa" \
  563.         cloud.accounts.migrate \
  564.         -server "$legacyFtpHost" \
  565.         -stats-user "$legacyStatsUserName" \
  566.         -stats-password "$legacyStatsUserPassword" \
  567.         -protocol "FTPS" \
  568.         -from-server "$externalAddress"
  569. }
  570.  
  571. configureRCG()
  572. {
  573.     _rcgConfigured=true
  574.     _defHost="$bindHost"
  575.     _defPort="1999"
  576.  
  577.     while true; do
  578.  
  579.         set_if_not_empty_else_read _rcgIntHost rcgIntHost "RCG bind host" "$_defHost"
  580.         set_if_not_empty_else_read _rcgIntPort rcgIntPort "RCG bind port" "$_defPort"
  581.         _address="$rcgIntHost:$rcgIntPort"
  582.         if [ "$_address" = "$_legacyFtpAddress" ]; then
  583.             _conflictSource="FTP"
  584.         elif [ "$_address" = "$_bindAddress" ]; then
  585.             _conflictSource="NGINX"
  586.         else
  587.             _rcgIntAddress="$_address"
  588.             break
  589.         fi
  590.  
  591.         echo "RCG and $_conflictSource bind addresses ($_address) must be different. Enter another RCG bind address."
  592.         _defHost="$rcgIntHost" && rcgIntHost=
  593.         _defPort="$rcgIntPort" && rcgIntPort=
  594.     done
  595.  
  596.     set_if_not_empty_else_read _rcgExtHost rcgExtHost "RCG external host" "$(get_list_item "$externalAddressList" 1 "," | sed 's|:.*||')"
  597.     set_if_not_empty_else_read _rcgExtPort rcgExtPort "RCG external port" "$rcgIntPort"
  598.     _rcgExtAddress="$rcgExtHost:$rcgExtPort"
  599. }
  600.  
  601. configureWebRCG()
  602. {
  603.     if [ "$UNATTENDED_INSTALLATION" = "true" -a \(\
  604.         -z "$_webRcgIntHost" -a -z "$webRcgIntHost" -o\
  605.         -z "$_webRcgIntPort" -a -z "$webRcgIntPort" -o\
  606.         -z "$_webRcgExternalAddresses" -a -z "$webRcgExternalAddressList" \) ]; then
  607.         return
  608.     fi
  609.  
  610.     _webRcgConfigured=true
  611.     print_spacer "$webRcgIntHost" "$webRcgIntPort" "$swebcgExtHost" "$webRcgExtPort"
  612.  
  613.     echo "Configure Web RCG"
  614.  
  615.     _defHost="$bindHost"
  616.     _defPort="2999"
  617.     while true; do
  618.  
  619.         set_if_not_empty_else_read _webRcgIntHost webRcgIntHost "Web RCG bind host" "$_defHost"
  620.         set_if_not_empty_else_read _webRcgIntPort webRcgIntPort "Web RCG bind port" "$_defPort"
  621.         _address="$webRcgIntHost:$webRcgIntPort"
  622.  
  623.         if [ "$_address" = "$_legacyFtpAddress" ]; then
  624.             _conflictSource="FTP"
  625.         elif [ "$_address" = "$_bindAddress" ]; then
  626.             _conflictSource="NGINX"
  627.         elif [ "$_address" = "$_rcgIntAddress" ]; then
  628.             _conflictSource="RCG"
  629.         else
  630.             _webRcgIntAddress="$_address"
  631.             break
  632.         fi
  633.  
  634.         echo "Web RCG and $_conflictSource bind addresses ($_address) must be different. Enter another Web RCG bind address."
  635.         _defHost="$webRcgIntHost" webRcgIntHost=
  636.         _defPort="$webRcgIntPort" webRcgIntPort=
  637.     done
  638.  
  639.     [ -n "$_webRcgExternalAddresses" ] && webRcgExternalAddressList="$_webRcgExternalAddresses"
  640.  
  641.     # read_value_allow_skip WebRcg externalAddress "WebRcg External address (\"address[:port]\" format)"
  642.  
  643.     if [ $READ_VALUE_ALLOW_SKIP -eq 0 -o -z "$webRcgExternalAddressList" ]; then
  644.         cat <<END
  645.  
  646. Enter a list of WebRcg external addresses (at least one address) your node is
  647. accessible at in "address[:port]" format, one per line.
  648. END
  649.         if [ -n "$webRcgExternalAddressList" ]; then
  650.             echo "Previously provided list: $webRcgExternalAddressList"
  651.         fi
  652.         echo "To finish, enter an empty string:"
  653.         if [ -n "$_webRcgExternalAddresses" ]; then
  654.             webRcgExternalAddressList="$_webRcgExternalAddresses"
  655.         else
  656.             read_list webRcgExternalAddressList "" "," "> "
  657.         fi
  658.     fi
  659. }
  660.  
  661. handle_configure()
  662. {
  663.     require_root
  664.  
  665.     _upgradeMode=0
  666.     _advancedMode=0
  667.     _loadFrom="$(get_private_config_path "$D")"
  668.     _saveTo="$_loadFrom"
  669.     _authUser=
  670.     _authPassword=
  671.  
  672.     _skipQuestions=false
  673.     _cloudPartnerName=
  674.     _cloudPartnerUserName=
  675.     _cloudPartnerPassword=
  676.     _cloudManagementAddress=
  677.     _storageName=
  678.     _storageLocation=
  679.     _storageNodeName=
  680.     _liveBRLocation=
  681.  
  682.  
  683.     _legacyFtpHost=
  684.     _legacyStatusUserName=
  685.     _legacyStatusUserPassword=
  686.  
  687.     _nginxBindHost=
  688.     _nginxBindPort=
  689.     _externalAddresses=
  690.     _rcgIntHost=
  691.     _rcgIntPort=
  692.     _rcgExtHost=
  693.     _rcgExtPort=
  694.     _webRcgIntHost=
  695.     _webRcgIntPort=
  696.     _webRcgExternalAddresses=
  697.     _configureRcg=true
  698.     _configureWebRcg=true
  699.  
  700.     while [ $# -gt 0 ]; do
  701.         case "$1" in
  702.         -skip-questions)
  703.             _skipQuestions=true
  704.             ;;
  705.         -upgrade)
  706.             _upgradeMode=1
  707.             ;;
  708.         -advanced)
  709.             _advancedMode=1
  710.             ;;
  711.         -load-from)
  712.             shift
  713.             _loadFrom="$1"
  714.             ;;
  715.         -save-to)
  716.             shift
  717.             _saveTo="$1"
  718.             ;;
  719.         -auth-user)
  720.             shift
  721.             _authUser="$1"
  722.             ;;
  723.         -auth-password)
  724.             shift
  725.             _authPassword="$1"
  726.             ;;
  727.         -cloud-partner-name)
  728.             shift
  729.             _cloudPartnerName="$1"
  730.             ;;
  731.         -cloud-partner-user-name)
  732.             shift
  733.             _cloudPartnerUserName="$1"
  734.             ;;
  735.         -cloud-partner-user-password)
  736.             shift
  737.             _cloudPartnerPassword="$1"
  738.             ;;
  739.         -cloud-management-address)
  740.             shift
  741.             _cloudManagementAddress="$1"
  742.             ;;
  743.         -storage-name)
  744.             shift
  745.             _storageName="$1"
  746.             ;;
  747.         -storage-directory)
  748.             shift
  749.             _storageLocation="$1"
  750.             ;;
  751.         -storage-node-name)
  752.             shift
  753.             _storageNodeName="$1"
  754.             ;;
  755.         -livebr-directory)
  756.             shift
  757.             _liveBRLocation="$1"
  758.             ;;
  759.         -legacy-ftp-host)
  760.             shift
  761.             _legacyFtpHost="$1"
  762.             ;;
  763.         -legacy-status-user-name)
  764.             shift
  765.             _legacyStatusUserName="$1"
  766.             ;;
  767.         -legacy-status-user-password)
  768.             shift
  769.             _legacyStatusUserPassword="$1"
  770.             ;;
  771.         -nginx-bind-host)
  772.             shift
  773.             _nginxBindHost="$1"
  774.             ;;
  775.         -nginx-bind-port)
  776.             shift
  777.             _nginxBindPort="$1"
  778.             ;;
  779.         -external-addresses)
  780.             shift
  781.             _externalAddresses="$1"
  782.             ;;
  783.         -rcg-bind-host)
  784.             shift
  785.             _rcgIntHost="$1"
  786.             ;;
  787.         -rcg-bind-port)
  788.             shift
  789.             _rcgIntPort="$1"
  790.             ;;
  791.         -rcg-external-host)
  792.             shift
  793.             _rcgExtHost="$1"
  794.             ;;
  795.         -rcg-external-port)
  796.             shift
  797.             _rcgExtPort="$1"
  798.             ;;
  799.         -web-rcg-bind-host)
  800.             shift
  801.             _webRcgIntHost="$1"
  802.             ;;
  803.         -web-rcg-bind-port)
  804.             shift
  805.             _webRcgIntPort="$1"
  806.             ;;
  807.         -web-rcg-external-addresses)
  808.             shift
  809.             _webRcgExternalAddresses="$1"
  810.             ;;
  811.         -no-rcg)
  812.             _configureRcg=false
  813.             ;;
  814.         -no-webrcg)
  815.             _configureWebRcg=false
  816.             ;;
  817.         -unattended_installation)
  818.             UNATTENDED_INSTALLATION=true
  819.             _skipQuestions=true
  820.             ;;
  821.         *)
  822.             error "Unsupported argument: '$1'"
  823.             ;;
  824.         esac
  825.         shift
  826.     done
  827.  
  828.     _updateMode=0
  829.  
  830.     if [ $_upgradeMode -eq 0 ]; then
  831.         if load_private_config 1 "$_loadFrom"; then
  832.             _updateMode=1
  833.         fi
  834.     fi
  835.  
  836.     READ_VALUE_ALLOW_SKIP=$_updateMode
  837.  
  838.     _should_scramble_partner_user_password=false
  839.  
  840.     while true; do
  841.         if ! "$UNATTENDED_INSTALLATION"; then
  842.             partnerUserPassword=""
  843.             _should_scramble_partner_user_password=true
  844.         fi
  845.  
  846.         if [ $_upgradeMode -eq 1 ]; then
  847.             set_if_not_empty_else_read_without_allow _storageLocation storageLocation "Storage directory" "$storageLocation"
  848.             set_if_not_empty_else_read_without_allow _legacyFtpHost legacyFtpHost "FTP Server address" "${legacyFtpHost:-127.0.0.1}"
  849.             if [ $_advancedMode -eq 1 ]; then
  850.                 set_if_not_empty_else_read_without_allow _legacyStatusUserName legacyStatsUserName "Legacy statistics user name" "$legacyStatsUserName"
  851.                 set_if_not_empty_else_read_without_allow _legacyStatusUserPassword legacyStatsUserPassword "Legacy statistics user password" "$legacyStatsUserPassword"
  852.             fi
  853.         fi
  854.  
  855.         _legacyFtpAddress="$legacyFtpHost:443"
  856.  
  857.         _save_READ_VALUE_ALLOW_SKIP=$READ_VALUE_ALLOW_SKIP
  858.  
  859.         while true; do
  860.             if [ $_advancedMode -eq 1 ]; then
  861.                 set_if_not_empty_else_read _cloudManagementAddress manServAddress "Cloud management address" "@@MAN_SERV_ADDRESS@@"
  862.             else
  863.                 if [ -n "$_cloudManagementAddress" ]; then
  864.                     manServAddress="$_cloudManagementAddress"
  865.                 else
  866.                     manServAddress="${manServAddress:-@@MAN_SERV_ADDRESS@@}"
  867.                 fi
  868.             fi
  869.  
  870.             set_if_not_empty_else_read _cloudPartnerName partnerName "Cloud partner name"
  871.             set_if_not_empty_else_read _cloudPartnerUserName partnerUserName "Cloud user"
  872.  
  873.             set_if_not_empty_else_read _cloudPartnerPassword partnerUserPassword "Cloud password for '$partnerUserName' user"
  874.  
  875.             if [ "$_should_scramble_partner_user_password" = "true" ]; then
  876.                 partnerUserPassword="$("$SERVER_TOOL" -machine-readable scramble -data "$partnerUserPassword")"
  877.             fi
  878.  
  879.             printf "Attempting to authenticate at $manServAddress... "
  880.             if login_to_cloud 0; then
  881.                 echo "ok"
  882.                 break
  883.             fi
  884.  
  885.             if [ "$UNATTENDED_INSTALLATION" = "false" \
  886.                 -a \( -z "$_cloudPartnerName" -o -z "$_cloudPartnerUserName" -o -z "$_cloudPartnerPassword" \) ]; then
  887.  
  888.                 ask_question yn "Would you like to re-enter cloud credentials?" "Y/n" "y"
  889.  
  890.                 if [ "$yn" = "y" -o "$yn" = "Y" ]; then
  891.                     READ_VALUE_ALLOW_SKIP=0
  892.                     partnerUserPassword=""
  893.                     echo ""
  894.                     continue
  895.                 fi
  896.             fi
  897.  
  898.             echo "Configuration was not finished."
  899.             exit 1
  900.         done
  901.  
  902.         READ_VALUE_ALLOW_SKIP=$_save_READ_VALUE_ALLOW_SKIP
  903.  
  904.         if [ $_upgradeMode -eq 0 ]; then
  905.             print_spacer "$storageLocation"
  906.             set_if_not_empty_else_read _storageLocation storageLocation "Storage directory" "$D/storage"
  907.         fi
  908.  
  909.         [ -n "$_liveBRLocation" ] && liveBRLocation="$_liveBRLocation"
  910.         [ -z "$liveBRLocation" ] && liveBRLocation="$storageLocation/br_21cc71bd-69fa-4140-82cd-b4c9983fa928"
  911.  
  912.         if is_livebr_path_interleave "$liveBRLocation" "$storageLocation"; then
  913.             echo "Configuration was not finished."
  914.             exit 1
  915.         fi
  916.  
  917.         storageLocation="$(echo "$storageLocation" | sed -e 's|/*$||')"
  918.  
  919.         [ -n "$_storageName" ] && storageName="$_storageName"
  920.         [ -n "$_storageNodeName" ] && storageNodeName="$_storageNodeName"
  921.  
  922.         print_spacer "$storageName" "$storageNodeName"
  923.         if [ $_upgradeMode -eq 1 ]; then
  924.             choose_storage_node_from_list
  925.         else
  926.             [ -n "$storageName" ] || set_if_not_empty_else_read _storageName storageName "Storage name" "Primary"
  927.             [ -n "$storageNodeName" ] || set_if_not_empty_else_read _storageNodeName storageNodeName "Storage node name"
  928.         fi
  929.  
  930.         [ -n "$_nginxBindHost" ] && bindHost="$_nginxBindHost"
  931.         [ -n "$_nginxBindPort" ] && bindPort="$_nginxBindPort"
  932.  
  933.         print_spacer "$bindHost" "$bindPort"
  934.  
  935.         _defHost="0.0.0.0"
  936.         _defPort="443"
  937.         while true; do
  938.             set_if_not_empty_else_read _nginxBindHost bindHost "NGINX bind host" "$_defHost"
  939.             set_if_not_empty_else_read _nginxBindPort bindPort "NGINX bind port" "$_defPort"
  940.             _address="$bindHost:$bindPort"
  941.  
  942.             if [ "$_address" = "$_legacyFtpAddress" ]; then
  943.                 _conflictSource="FTP"
  944.             else
  945.                 _bindAddress="$_address"
  946.                 break
  947.             fi
  948.  
  949.             echo "NGINX and $_conflictSource bind addresses ($_address) must be different. Enter another NGINX bind address."
  950.             _defHost="$bindHost" && bindHost=
  951.             _defPort="$bindPort" && bindPort=
  952.         done
  953.  
  954.         [ -n "$_externalAddresses" ] && externalAddressList="$_externalAddresses"
  955.  
  956.         # read_value_allow_skip externalAddress "External address (\"address[:port]\" format)"
  957.  
  958.         if [ $READ_VALUE_ALLOW_SKIP -eq 0 -o -z "$externalAddressList" ]; then
  959.             cat <<END
  960.  
  961. Enter a list of external addresses (at least one address) your node is
  962. accessible at in "address[:port]" format, one per line.
  963. END
  964.             if [ -n "$externalAddressList" ]; then
  965.                 echo "Previously provided list: $externalAddressList"
  966.             fi
  967.             echo "To finish, enter an empty string:"
  968.             if [ -n "$_externalAddresses" ]; then
  969.                 externalAddressList="$_externalAddresses"
  970.             else
  971.                 read_list externalAddressList "" "," "> "
  972.             fi
  973.         fi
  974.  
  975.         if [ -n "$_authUser"  ]; then
  976.             authUser="$_authUser"
  977.         fi
  978.  
  979.         if [ -z "$authUser" ]; then
  980.             authUser="$(random_sequence 32)"
  981.         fi
  982.  
  983.         if [ -n "$_authPassword" ]; then
  984.             authPassword="$_authPassword"
  985.             authPasswordHash="$(openssl passwd -apr1 "$authPassword")"
  986.             authPassword="$("$SERVER_TOOL" -machine-readable scramble -data "$authPassword")"
  987.         fi
  988.  
  989.         if [ -z "$authPassword" ]; then
  990.             authPassword="$(random_sequence 32)"
  991.             authPasswordHash="$(openssl passwd -apr1 "$authPassword")"
  992.             authPassword="$("$SERVER_TOOL" -machine-readable scramble -data "$authPassword")"
  993.         fi
  994.  
  995.         _webRcgConfigured=false
  996.  
  997.         if $_configureWebRcg ; then
  998.             configureWebRCG
  999.         else
  1000.             webRcgIntHost=
  1001.             webRcgIntPort=
  1002.             webRcgExternalAddressList=
  1003.         fi
  1004.  
  1005.         _rcgConfigured=false
  1006.  
  1007.         if $_configureRcg ; then
  1008.  
  1009.             print_spacer "$rcgIntHost" "$rcgIntPort" "$rcgExtHost" "$rcgExtPort"
  1010.  
  1011.             set_if_not_empty _rcgIntHost rcgIntHost
  1012.  
  1013.             if [ -n "$rcgIntHost" -o \( "$_skipQuestions" = "true" -a "$UNATTENDED_INSTALLATION" = "false" \) ] ; then
  1014.                 configureRCG
  1015.             else
  1016.                 if ! "$UNATTENDED_INSTALLATION"; then
  1017.                     ask_question yn "Configure RCG? (Actual if your node hosts backup clients of version 13.6.1 and older)" "Y/n" "y"
  1018.  
  1019.                     if [ "$yn" = "y" -o "$yn" = "Y" ]; then
  1020.                         configureRCG
  1021.                     fi
  1022.                 fi
  1023.             fi
  1024.         else
  1025.             rcgIntHost=
  1026.             rcgIntPort=
  1027.             rcgExtHost=
  1028.             rcgExtPort=
  1029.         fi
  1030.         echo ""
  1031.         echo "Please review all the information once again:"
  1032.         echo "============================================================"
  1033.         if [ $_upgradeMode -eq 1 ]; then
  1034.             echo "FTP server address:       $legacyFtpAddress"
  1035.             if [ $_advancedMode -eq 1 ]; then
  1036.                 echo "Statistics user name:     $legacyStatsUserName"
  1037.                 echo "Statistics user password: $legacyStatsUserPassword"
  1038.             fi
  1039.             echo "------------------------------------------------------------"
  1040.         fi
  1041.         echo "Storage directory:        $storageLocation"
  1042.         echo "------------------------------------------------------------"
  1043.         echo "Cloud address:            $manServAddress"
  1044.         echo "Cloud partner name:       $partnerName"
  1045.         echo "Cloud user:               $partnerUserName"
  1046.         echo "Cloud password:           *****"
  1047.         echo "------------------------------------------------------------"
  1048.         echo "Storage name:             $storageName"
  1049.         echo "Storage node name:        $storageNodeName"
  1050.         echo "------------------------------------------------------------"
  1051.         echo "NGINX bind address:       $_bindAddress"
  1052.         echo "External addresses:       $externalAddressList"
  1053.  
  1054.         if $_rcgConfigured ; then
  1055.             echo "RCG bind address:         $_rcgIntAddress"
  1056.             echo "RCG external address:     $_rcgExtAddress"
  1057.         fi
  1058.  
  1059.         if $_webRcgConfigured ; then
  1060.             echo "Web RCG bind address:     $_webRcgIntAddress"
  1061.             echo "Web RCG external addresses: $webRcgExternalAddressList"
  1062.         fi
  1063.  
  1064.         echo "============================================================"
  1065.  
  1066.         if $_skipQuestions ; then
  1067.             break
  1068.         fi
  1069.  
  1070.         ask_question yn "Is everything correct?" "Y/n" "y"
  1071.         if [ "$yn" = "y" -o "$yn" = "Y" ]; then
  1072.             break
  1073.         fi
  1074.  
  1075.         echo ""
  1076.         ask_question yn "Would you like to start over?" "Y/n" "y"
  1077.         if [ "$yn" = "y" -o "$yn" = "Y" ]; then
  1078.             READ_VALUE_ALLOW_SKIP=0
  1079.             echo ""
  1080.             continue
  1081.         fi
  1082.  
  1083.         echo ""
  1084.         echo "Configuration was not finished."
  1085.         exit 1
  1086.     done
  1087.  
  1088.     save_private_config 1 "$_saveTo"
  1089. }
  1090.  
  1091. handle_get()
  1092. {
  1093.     _name=""
  1094.     _loadFrom="$(get_private_config_path "$D")"
  1095.  
  1096.     while [ $# -gt 0 ]; do
  1097.         case "$1" in
  1098.         -name)
  1099.             shift
  1100.             _name="$1"
  1101.             ;;
  1102.         -load-from)
  1103.             shift
  1104.             _loadFrom="$1"
  1105.             ;;
  1106.         *)
  1107.             error "Unsupported argument: '$1'"
  1108.             ;;
  1109.         esac
  1110.         shift
  1111.     done
  1112.  
  1113.     load_private_config_param "$_loadFrom" "$_name"
  1114.  
  1115.     eval "x=\"\$$_name\""
  1116.     echo "$x"
  1117. }
  1118.  
  1119. handle_get_public_config_param()
  1120. {
  1121.     _name=""
  1122.     _defaultValue=""
  1123.  
  1124.     while [ $# -gt 0 ]; do
  1125.         case "$1" in
  1126.         -name)
  1127.             shift
  1128.             _name="$1"
  1129.             ;;
  1130.         -default-value)
  1131.             shift
  1132.             _defaultValue="$1"
  1133.             ;;
  1134.         *)
  1135.             error "Unsupported argument: '$1'"
  1136.             ;;
  1137.         esac
  1138.         shift
  1139.     done
  1140.  
  1141.     configParam="$(getConfigParam "$_name" "$_defaultValue")"
  1142.     echo "$configParam"
  1143. }
  1144.  
  1145. handle_patch()
  1146. {
  1147.     require_root
  1148.  
  1149.     while [ $# -gt 0 ]; do
  1150.         case "$1" in
  1151.         -cold-node-configured)
  1152.             shift
  1153.             _coldNodeConfigured="$1"
  1154.             ;;
  1155.         -replication-master-configured)
  1156.             shift
  1157.             _replicationMasterConfigured="$1"
  1158.             ;;
  1159.         *)
  1160.             error "Unsupported argument: '$1'"
  1161.             ;;
  1162.         esac
  1163.         shift
  1164.     done
  1165.  
  1166.     _loadFrom="$(get_private_config_path "$D")"
  1167.  
  1168.     if ! load_private_config 1 "$_loadFrom"; then
  1169.         error "Unable to read configuration from '$_loadFrom'"
  1170.     fi
  1171.  
  1172.     tplDir="$D/share/cloud-sn"
  1173.  
  1174.     groupId=$(getent group iasouser | cut -d: -f3)
  1175.     userId=$(id -u iasouser 2>/dev/null || true)
  1176.  
  1177.     [ -z "$groupId" ] && groupId="2001"
  1178.     [ -z "$userId" ] && userId="2001"
  1179.  
  1180.     storageUserId=$(getFileUserId "$storageLocation" $userId)
  1181.     storageUserName=$(getUserNameFromId "$storageUserId" iasouser)
  1182.  
  1183.     rcgSessionExpirationTimeout="$(getConfigParam RCGSessionExpirationTimeout "300")"
  1184.  
  1185.     webRcgSslPrivateKeyPath="$(getConfigParam WebRcgSslPrivateKeyPath "$D/etc/nginx/ssl/server.key")"
  1186.     webRcgSslCertificatePath="$(getConfigParam WebRcgSslCertificatePath "$D/etc/nginx/ssl/server.crt")"
  1187.     webRcgSslDhParamsPath="$(getConfigParam WebRcgSslDhParamsPath "$D/etc/nginx/ssl/dh2048.pem")"
  1188.     if [ "$webRcgSslDhParamsPath" = "$D/etc/nginx/ssl/dh512.pem" ]; then
  1189.         webRcgSslDhParamsPath="$D/etc/nginx/ssl/dh2048.pem"
  1190.     fi
  1191.  
  1192.     webRcgTemplatesPath="$(getConfigParam WebRcgTemplatesPath "$D/share/webrcg/templates")"
  1193.     webRcgErrorTemplate="$(getConfigParam WebRcgErrorTemplate "error")"
  1194.  
  1195.     nginxPidFile="$(get_nginx_config_param pid "$D/var/run/nginx.pid")"
  1196.     nginxErrorLogFile="$(get_nginx_config_param error_log "$D/var/log/nginx/nginx-error.log")"
  1197.     nginxAccessLogFile="$(get_nginx_config_param access_log "$D/var/log/nginx/nginx-access.log main buffer=128k")"
  1198.     nginxClientBodyTempDir="$(get_nginx_config_param client_body_temp_path "$D/var/tmp/nginx/client_body_temp")"
  1199.  
  1200.     invalidCabinetsPocketPath="$(getConfigParam InvalidCabinetsPocketPath "")"
  1201.  
  1202.     storageReplicationAgentRoot="$(getConfigParam StorageReplicationAgentRoot "")"
  1203.     replicationServerAddress="$(getConfigParam ReplicationServerAddress "")"
  1204.     replicationServerUser="$(getConfigParam ReplicationServerUser "")"
  1205.     replicationServerPassword="$(getConfigParam ReplicationServerPassword "")"
  1206.     localHost="127.0.0.1"
  1207.  
  1208.     databaseCacheSize="$(getConfigParam CacheSize 130000)"
  1209.  
  1210.     nginxSlaConfSupported="$("$SERVER_TOOL" storage.node.nginx.sla.supported)"
  1211.     nginxSlaConf=""
  1212.     if [ "$nginxSlaConfSupported" = "1" ] ; then
  1213.         nginxSlaConf="include nginx_sla.conf;"
  1214.     fi
  1215.  
  1216.     touch "$T/config.ini"
  1217.     [ -f "$D/etc/config.ini" ] && cp "$D/etc/config.ini" "$T/config.ini"
  1218.  
  1219.     cat "$T/config.ini" |
  1220.         setConfigParam General RootStoragePath "$storageLocation" |
  1221.         setConfigParam General RootLiveBRPath "$liveBRLocation" |
  1222.         setConfigParam General InvalidCabinetsPocketPath "$invalidCabinetsPocketPath" 0 |
  1223.         setConfigParam General "InternalHttpApiAddress" "http://$localHost:$bindPort" |
  1224.         setConfigParam RemoteConnectionGateway "RCGAddress" "$rcgIntHost" |
  1225.         setConfigParam RemoteConnectionGateway "RCGPort" "$rcgIntPort" |
  1226.         setConfigParam RemoteConnectionGateway "RCGSessionExpirationTimeout" "$rcgSessionExpirationTimeout" |
  1227.         setConfigParam WebRemoteConnectionGateway "WebRcgAddress" "$webRcgIntHost" |
  1228.         setConfigParam WebRemoteConnectionGateway "WebRcgPort" "$webRcgIntPort" |
  1229.         setConfigParam WebRemoteConnectionGateway "WebRcgSslPrivateKeyPath" "$webRcgSslPrivateKeyPath" |
  1230.         setConfigParam WebRemoteConnectionGateway "WebRcgSslCertificatePath" "$webRcgSslCertificatePath" |
  1231.         setConfigParam WebRemoteConnectionGateway "WebRcgSslDhParamsPath" "$webRcgSslDhParamsPath" |
  1232.         setConfigParam WebRemoteConnectionGateway "WebRcgTemplatesPath" "$webRcgTemplatesPath" |
  1233.         setConfigParam WebRemoteConnectionGateway "WebRcgErrorTemplate" "$webRcgErrorTemplate" |
  1234.         setConfigParam StorageReplication "StorageReplicationAgentRoot" "$storageReplicationAgentRoot" |
  1235.         setConfigParam StorageReplication "ReplicationServerAddress" "$replicationServerAddress" |
  1236.         setConfigParam StorageReplication "ReplicationServerUser" "$replicationServerUser" |
  1237.         setConfigParam StorageReplication "ReplicationServerPassword" "$replicationServerPassword" |
  1238.         setConfigParam Database CacheSize "$databaseCacheSize" > "$T/config.ini.tmp"
  1239.  
  1240.     mv -f "$T/config.ini.tmp" "$T/config.ini"
  1241.  
  1242.     cat "$tplDir/../nginx/nginx.conf.in" |
  1243.         replace_template_param "NGINX_PID_FILE" "$nginxPidFile" |
  1244.         replace_template_param "NGINX_ERROR_LOG_FILE" "$nginxErrorLogFile" |
  1245.         replace_template_param "NGINX_ACCESS_LOG_FILE" "$nginxAccessLogFile" |
  1246.         replace_template_param "NGINX_CLIENT_BODY_TEMP_DIR" "$nginxClientBodyTempDir" |
  1247.         replace_template_param "NGINX_USER_NAME" "$storageUserName" |
  1248.         replace_template_param "NGINX_GROUP_NAME" "$(id -ng "$storageUserName")" |
  1249.         replace_template_param "BIND_ADDRESS" "$bindHost:$bindPort" |
  1250.         replace_template_param "LOCAL_BIND_ADDRESS" "$localHost:$bindPort" |
  1251.         replace_template_param "NGINX_SLA_CONF" "$nginxSlaConf" > "$T/nginx.conf"
  1252.  
  1253.     cat "$tplDir/../nginx/storage.location.in" |
  1254.         replace_template_param "STORAGE_DIR" "$storageLocation" > "$T/storage.location"
  1255.  
  1256.     cp "$tplDir/nginx.location.00_high_priority_regex.conf.in" "$T/nginx.location.00_high_priority_regex.conf"
  1257.     if [ "$_replicationMasterConfigured" = "1" ]; then
  1258.         cat "$tplDir/nginx.location.40_cloud_storage_node.conf.in" |
  1259.             replace_template_param "STORAGE_DIR" "$storageLocation" > "$T/nginx.location.40_cloud_storage_node.conf"
  1260.         cat "$tplDir/../nginx/replication.location.in" |
  1261.             replace_template_param "STORAGE_DIR" "$storageLocation" > "$T/replication.location"
  1262.     fi
  1263.     if [ "$_coldNodeConfigured" = "1" ]; then
  1264.         dnsResolverList="$("$SERVER_TOOL" branding-config.parameter.get -key "DnsResolverList")"
  1265.         dnsResolverList="resolver $(echo $dnsResolverList | sed "s/;/ /g");"
  1266.         cat "$tplDir/nginx.location.45_cold_storage.conf.in" |
  1267.             replace_template_param "DNS_RESOLVER" "$dnsResolverList" > "$T/nginx.location.45_cold_storage.conf"
  1268.     fi
  1269.     cat "$tplDir/nginx.location.conf.in" |
  1270.         replace_template_param "STORAGE_DIR" "$storageLocation" |
  1271.         replace_template_param "INSTALL_DIR" "$D" |
  1272.         replace_template_param "WSDL_LOCATION" "$(random_sequence 64)" |
  1273.         replace_template_param_with_multi_line "NGINX_STATUS_LOCATION" "$(get_nginx_status_location)" "!" |
  1274.         replace_template_param_with_multi_line "LIVEBR_LOCATION" "$(get_livebr_location "$storageLocation" "$liveBRLocation")" "!" > "$T/cloud-sn.location.conf"
  1275.  
  1276.     cp "$tplDir/nginx.upstream.50_cloud_storage_node.conf.in" "$T/nginx.upstream.50_cloud_storage_node.conf"
  1277.  
  1278.     echo "$authUser:$authPasswordHash" > "$T/htpasswd.cloud-sn"
  1279.  
  1280.     install -d -m 0755 "$D/etc"
  1281.     install -d -m 0755 "$D/etc/nginx"
  1282.  
  1283.     install -m 0644 "$T/config.ini" "$D/etc/"
  1284.     install -o "$storageUserId" -m 0644 "$T/htpasswd.cloud-sn" "$D/etc/nginx/"
  1285.     install -m 0644 "$T/nginx.conf" "$D/etc/nginx/"
  1286.  
  1287.     [ -f "$D/etc/nginx/htpasswd" ] && mv "$D/etc/nginx/htpasswd" "$D/etc/nginx/htpasswd.bak"
  1288.     rm -f "$D/etc/nginx/ssl/dh512.pem"
  1289.     [ -f "$D/etc/nginx/ssl/dh2048.pem" ] || openssl dhparam -out "$D/etc/nginx/ssl/dh2048.pem" 2048
  1290.  
  1291.     externalAddress="$(get_list_item "$externalAddressList" 1 ",")"
  1292.  
  1293.     install_nginx_config "$T/nginx.location.00_high_priority_regex.conf" "00_high_priority_regex" location
  1294.     if [ "$_replicationMasterConfigured" = "1" ]; then
  1295.         install_nginx_config "$T/nginx.location.40_cloud_storage_node.conf" "40_cloud_storage_node" location
  1296.     fi
  1297.     if [ "$_coldNodeConfigured" = "1" ]; then
  1298.         install_nginx_config "$T/nginx.location.45_cold_storage.conf" "45_cold_storage" location
  1299.     fi
  1300.     install_nginx_config "$T/cloud-sn.location.conf" "50_cloud_storage_node" location
  1301.  
  1302.     install_nginx_config "$T/nginx.upstream.50_cloud_storage_node.conf" "50_cloud_storage_node" upstream
  1303.  
  1304.     echo "-- Installing storage.location NGINX config"
  1305.     install -m 0644 "$T/storage.location" "$D/etc/nginx/"
  1306.  
  1307.     if [ "$_replicationMasterConfigured" = "1" ]; then
  1308.         echo "-- Installing replication.location NGINX config"
  1309.         install -m 0644 "$T/replication.location" "$D/etc/nginx/"
  1310.     fi
  1311.  
  1312.     chown $storageUserId:$groupId \
  1313.         "$D/etc/nginx/location.d" \
  1314.         "$D/etc/nginx/upstream.d"
  1315. }
  1316.  
  1317. handle_reconfigure()
  1318. {
  1319.     require_root
  1320.  
  1321.     _tempConfigPath="$T/config.cfg"
  1322.  
  1323.     handle_configure -save-to "$_tempConfigPath"
  1324.  
  1325.     "$PC_PATH" stop
  1326.  
  1327.     install -m 0644 "$_tempConfigPath" "$D/etc/"
  1328.  
  1329.     handle_patch
  1330.  
  1331.     "$PC_PATH" stop
  1332.  
  1333.     handle_update_node_info
  1334.  
  1335.     [ -x "$D/scripts/update_storage_node.sh" ] && "$D/scripts/update_storage_node.sh"
  1336. }
  1337.  
  1338. D="$(guess_program_location "/opt/@@INSTALL_DIR@@")"
  1339. T="/tmp/cloud-sn-ctl-$$"
  1340.  
  1341. if [ $# -eq 0 ]; then
  1342.     cat <<END
  1343. Cloud storage node management script, version $VERSION
  1344. Copyright (c) 2004-2017 LogicNow Holdings Limited
  1345. All rights reserved.
  1346.  
  1347. Usage:
  1348.     $(basename "$0") [global arguments] command [arguments]
  1349.  
  1350. Commands:
  1351.     add-node          Add node to cloud.
  1352.     configure         Start interactive configuration wizard.
  1353.     get               Get configuration parameter.
  1354.     migrate-accounts  Migrate accounts to cloud.
  1355.     patch             Patch configuration files and scripts.
  1356.     reconfigure       Run configure, then patch, then update-node-info.
  1357.     update-node-info  Update node information.
  1358.  
  1359. Global arguments:
  1360.    -install-path
  1361.         Override default installation path.
  1362. END
  1363.     exit 1
  1364. fi
  1365.  
  1366. CMD=""
  1367. while [ -z "$CMD" -a $# -gt 0 ]; do
  1368.     case "$1" in
  1369.     -install-path)
  1370.         shift
  1371.         D="$1"
  1372.         ;;
  1373.     -*)
  1374.         error "Unsupported argument: '$1'"
  1375.         ;;
  1376.     *)
  1377.         CMD="$1"
  1378.         ;;
  1379.     esac
  1380.     shift
  1381. done
  1382.  
  1383. D="$(echo "$D" | sed -e 's|/*$||')"
  1384.  
  1385. SERVER_TOOL="bin/ServerTool"
  1386. if [ ! -x "$SERVER_TOOL" ]; then
  1387.     SERVER_TOOL="$D/bin/ServerTool"
  1388. fi
  1389.  
  1390. mkdir -p "$T"
  1391.  
  1392. trap "rm -rf '$T'" EXIT
  1393.  
  1394. VISA_FILE="$T/mansvc-visa-$$.tmp"
  1395.  
  1396. case "$CMD" in
  1397. add-node|configure|get|migrate-accounts|patch|reconfigure|update-node-info|get_public_config_param)
  1398.     handler_postfix=$(echo $CMD | sed 's/-/_/g')
  1399.     handle_$handler_postfix "$@"
  1400.     ;;
  1401. *)
  1402.     error "Unsupported command: '$CMD'"
  1403.     ;;
  1404. esac
Add Comment
Please, Sign In to add comment