GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — devstack/functions-common at master · preetiq/devstack Expand file tree/ Copy pathfunctions-commonMore file actions 1700 lines (1513 loc) · 52.7 KB / Copy pathfunctions-commonFile metadata and controls1700 lines (1513 loc) · 52.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 # functions-common - Common functions used by DevStack components # # The canonical copy of this file is maintained in the DevStack repo. # All modifications should be made there and then sync'ed to other repos # as required. # # This file is sorted alphabetically within the function groups. # # - Config Functions # - Control Functions # - Distro Functions # - Git Functions # - OpenStack Functions # - Package Functions # - Process Functions # - Python Functions # - Service Functions # - System Functions # # The following variables are assumed to be defined by certain functions: # # - ``ENABLED_SERVICES`` # - ``ERROR_ON_CLONE`` # - ``FILES`` # - ``OFFLINE`` # - ``PIP_DOWNLOAD_CACHE`` # - ``PIP_USE_MIRRORS`` # - ``RECLONE`` # - ``REQUIREMENTS_DIR`` # - ``STACK_USER`` # - ``TRACK_DEPENDS`` # - ``UNDO_REQUIREMENTS`` # - ``http_proxy``, ``https_proxy``, ``no_proxy`` # Save trace setting XTRACE=$(set +o | grep xtrace) set +o xtrace # Config Functions # ================ # Append a new option in an ini file without replacing the old value # iniadd config-file section option value1 value2 value3 ... function iniadd { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 shift 3 local values="$(iniget_multiline $file $section $option) $@" iniset_multiline $file $section $option $values $xtrace } # Comment an option in an INI file # inicomment config-file section option function inicomment { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file" $xtrace } # Get an option from an INI file # iniget config-file section option function iniget { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 local line line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") echo ${line#*=} $xtrace } # Get a multiple line option from an INI file # iniget_multiline config-file section option function iniget_multiline { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 local values values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file") echo ${values} $xtrace } # Determinate is the given option present in the INI file # ini_has_option config-file section option function ini_has_option { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 local line line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") $xtrace [ -n "$line" ] } # Set an option in an INI file # iniset config-file section option value function iniset { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 local value=$4 [[ -z $section || -z $option ]] && return if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then # Add section at the end echo -e "\n[$section]" >>"$file" fi if ! ini_has_option "$file" "$section" "$option"; then # Add it sed -i -e "/^\[$section\]/ a\\ $option = $value " "$file" else local sep=$(echo -ne "\x01") # Replace it sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file" fi $xtrace } # Set a multiple line option in an INI file # iniset_multiline config-file section option value1 value2 valu3 ... function iniset_multiline { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 shift 3 local values for v in $@; do # The later sed command inserts each new value in the line next to # the section identifier, which causes the values to be inserted in # the reverse order. Do a reverse here to keep the original order. values="$v ${values}" done if ! grep -q "^\[$section\]" "$file"; then # Add section at the end echo -e "\n[$section]" >>"$file" else # Remove old values sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" fi # Add new ones for v in $values; do sed -i -e "/^\[$section\]/ a\\ $option = $v " "$file" done $xtrace } # Uncomment an option in an INI file # iniuncomment config-file section option function iniuncomment { local xtrace=$(set +o | grep xtrace) set +o xtrace local file=$1 local section=$2 local option=$3 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file" $xtrace } # Normalize config values to True or False # Accepts as False: 0 no No NO false False FALSE # Accepts as True: 1 yes Yes YES true True TRUE # VAR=$(trueorfalse default-value test-value) function trueorfalse { local xtrace=$(set +o | grep xtrace) set +o xtrace local default=$1 local testval=$2 [[ -z "$testval" ]] && { echo "$default"; return; } [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; } [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; } echo "$default" $xtrace } # Control Functions # ================= # Prints backtrace info # filename:lineno:function # backtrace level function backtrace { local level=$1 local deep=$((${#BASH_SOURCE[@]} - 1)) echo "[Call Trace]" while [ $level -le $deep ]; do echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}" deep=$((deep - 1)) done } # Prints line number and "message" then exits # die $LINENO "message" function die { local exitcode=$? set +o xtrace local line=$1; shift if [ $exitcode == 0 ]; then exitcode=1 fi backtrace 2 err $line "$*" # Give buffers a second to flush sleep 1 exit $exitcode } # Checks an environment variable is not set or has length 0 OR if the # exit code is non-zero and prints "message" and exits # NOTE: env-var is the variable name without a '$' # die_if_not_set $LINENO env-var "message" function die_if_not_set { local exitcode=$? FXTRACE=$(set +o | grep xtrace) set +o xtrace local line=$1; shift local evar=$1; shift if ! is_set $evar || [ $exitcode != 0 ]; then die $line "$*" fi $FXTRACE } # Prints line number and "message" in error format # err $LINENO "message" function err { local exitcode=$? errXTRACE=$(set +o | grep xtrace) set +o xtrace local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2" echo $msg 1>&2; if [[ -n ${SCREEN_LOGDIR} ]]; then echo $msg >> "${SCREEN_LOGDIR}/error.log" fi $errXTRACE return $exitcode } # Checks an environment variable is not set or has length 0 OR if the # exit code is non-zero and prints "message" # NOTE: env-var is the variable name without a '$' # err_if_not_set $LINENO env-var "message" function err_if_not_set { local exitcode=$? errinsXTRACE=$(set +o | grep xtrace) set +o xtrace local line=$1; shift local evar=$1; shift if ! is_set $evar || [ $exitcode != 0 ]; then err $line "$*" fi $errinsXTRACE return $exitcode } # Exit after outputting a message about the distribution not being supported. # exit_distro_not_supported [optional-string-telling-what-is-missing] function exit_distro_not_supported { if [[ -z "$DISTRO" ]]; then GetDistro fi if [ $# -gt 0 ]; then die $LINENO "Support for $DISTRO is incomplete: no support for $@" else die $LINENO "Support for $DISTRO is incomplete." fi } # Test if the named environment variable is set and not zero length # is_set env-var function is_set { local var=\$"$1" eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this } # Prints line number and "message" in warning format # warn $LINENO "message" function warn { local exitcode=$? errXTRACE=$(set +o | grep xtrace) set +o xtrace local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2" echo $msg 1>&2; if [[ -n ${SCREEN_LOGDIR} ]]; then echo $msg >> "${SCREEN_LOGDIR}/error.log" fi $errXTRACE return $exitcode } # Distro Functions # ================ # Determine OS Vendor, Release and Update # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora # Returns results in global variables: # os_VENDOR - vendor name # os_RELEASE - release # os_UPDATE - update # os_PACKAGE - package type # os_CODENAME - vendor's codename for release # GetOSVersion function GetOSVersion { # Figure out which vendor we are if [[ -x "`which sw_vers 2>/dev/null`" ]]; then # OS/X os_VENDOR=`sw_vers -productName` os_RELEASE=`sw_vers -productVersion` os_UPDATE=${os_RELEASE##*.} os_RELEASE=${os_RELEASE%.*} os_PACKAGE="" if [[ "$os_RELEASE" =~ "10.7" ]]; then os_CODENAME="lion" elif [[ "$os_RELEASE" =~ "10.6" ]]; then os_CODENAME="snow leopard" elif [[ "$os_RELEASE" =~ "10.5" ]]; then os_CODENAME="leopard" elif [[ "$os_RELEASE" =~ "10.4" ]]; then os_CODENAME="tiger" elif [[ "$os_RELEASE" =~ "10.3" ]]; then os_CODENAME="panther" else os_CODENAME="" fi elif [[ -x $(which lsb_release 2>/dev/null) ]]; then os_VENDOR=$(lsb_release -i -s) os_RELEASE=$(lsb_release -r -s) os_UPDATE="" os_PACKAGE="rpm" if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then os_PACKAGE="deb" elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then lsb_release -d -s | grep -q openSUSE if [[ $? -eq 0 ]]; then os_VENDOR="openSUSE" fi elif [[ $os_VENDOR == "openSUSE project" ]]; then os_VENDOR="openSUSE" elif [[ $os_VENDOR =~ Red.*Hat ]]; then os_VENDOR="Red Hat" fi os_CODENAME=$(lsb_release -c -s) elif [[ -r /etc/redhat-release ]]; then # Red Hat Enterprise Linux Server release 5.5 (Tikanga) # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo) # CentOS release 5.5 (Final) # CentOS Linux release 6.0 (Final) # Fedora release 16 (Verne) # XenServer release 6.2.0-70446c (xenenterprise) os_CODENAME="" for r in "Red Hat" CentOS Fedora XenServer; do os_VENDOR=$r if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` os_CODENAME=${ver#*|} os_RELEASE=${ver%|*} os_UPDATE=${os_RELEASE##*.} os_RELEASE=${os_RELEASE%.*} break fi os_VENDOR="" done os_PACKAGE="rpm" elif [[ -r /etc/SuSE-release ]]; then for r in openSUSE "SUSE Linux"; do if [[ "$r" = "SUSE Linux" ]]; then os_VENDOR="SUSE LINUX" else os_VENDOR=$r fi if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'` os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'` os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'` break fi os_VENDOR="" done os_PACKAGE="rpm" # If lsb_release is not installed, we should be able to detect Debian OS elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then os_VENDOR="Debian" os_PACKAGE="deb" os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}') os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g') fi export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME } # Translate the OS version values into common nomenclature # Sets global ``DISTRO`` from the ``os_*`` values function GetDistro { GetOSVersion if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective DISTRO=$os_CODENAME elif [[ "$os_VENDOR" =~ (Fedora) ]]; then # For Fedora, just use 'f' and the release DISTRO="f$os_RELEASE" elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then DISTRO="opensuse-$os_RELEASE" elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then # For SLE, also use the service pack if [[ -z "$os_UPDATE" ]]; then DISTRO="sle${os_RELEASE}" else DISTRO="sle${os_RELEASE}sp${os_UPDATE}" fi elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then # Drop the . release as we assume it's compatible DISTRO="rhel${os_RELEASE::1}" elif [[ "$os_VENDOR" =~ (XenServer) ]]; then DISTRO="xs$os_RELEASE" else # Catch-all for now is Vendor + Release + Update DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" fi export DISTRO } # Utility function for checking machine architecture # is_arch arch-type function is_arch { ARCH_TYPE=$1 [[ "$(uname -m)" == "$ARCH_TYPE" ]] } # Determine if current distribution is a Fedora-based distribution # (Fedora, RHEL, CentOS, etc). # is_fedora function is_fedora { if [[ -z "$os_VENDOR" ]]; then GetOSVersion fi [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ] } # Determine if current distribution is a SUSE-based distribution # (openSUSE, SLE). # is_suse function is_suse { if [[ -z "$os_VENDOR" ]]; then GetOSVersion fi [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ] } # Determine if current distribution is an Ubuntu-based distribution # It will also detect non-Ubuntu but Debian-based distros # is_ubuntu function is_ubuntu { if [[ -z "$os_PACKAGE" ]]; then GetOSVersion fi [ "$os_PACKAGE" = "deb" ] } # Git Functions # ============= # Returns openstack release name for a given branch name # ``get_release_name_from_branch branch-name`` function get_release_name_from_branch { local branch=$1 if [[ $branch =~ "stable/" ]]; then echo ${branch#*/} else echo "master" fi } # git clone only if directory doesn't exist already. Since ``DEST`` might not # be owned by the installation user, we create the directory and change the # ownership to the proper user. # Set global RECLONE=yes to simulate a clone when dest-dir exists # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo # does not exist (default is False, meaning the repo will be cloned). # Uses global ``OFFLINE`` # git_clone remote dest-dir branch function git_clone { GIT_REMOTE=$1 GIT_DEST=$2 GIT_REF=$3 RECLONE=$(trueorfalse False $RECLONE) local orig_dir=`pwd` if [[ "$OFFLINE" = "True" ]]; then echo "Running in offline mode, clones already exist" # print out the results so we know what change was used in the logs cd $GIT_DEST git show --oneline | head -1 cd $orig_dir return fi if echo $GIT_REF | egrep -q "^refs"; then # If our branch name is a gerrit style refs/changes/... if [[ ! -d $GIT_DEST ]]; then [[ "$ERROR_ON_CLONE" = "True" ]] && \ die $LINENO "Cloning not allowed in this configuration" git_timed clone $GIT_REMOTE $GIT_DEST fi cd $GIT_DEST git_timed fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD else # do a full clone only if the directory doesn't exist if [[ ! -d $GIT_DEST ]]; then [[ "$ERROR_ON_CLONE" = "True" ]] && \ die $LINENO "Cloning not allowed in this configuration" git_timed clone $GIT_REMOTE $GIT_DEST cd $GIT_DEST # This checkout syntax works for both branches and tags git checkout $GIT_REF elif [[ "$RECLONE" = "True" ]]; then # if it does exist then simulate what clone does if asked to RECLONE cd $GIT_DEST # set the url to pull from and fetch git remote set-url origin $GIT_REMOTE git_timed fetch origin # remove the existing ignored files (like pyc) as they cause breakage # (due to the py files having older timestamps than our pyc, so python # thinks the pyc files are correct using them) find $GIT_DEST -name '*.pyc' -delete # handle GIT_REF accordingly to type (tag, branch) if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then git_update_tag $GIT_REF elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then git_update_branch $GIT_REF elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then git_update_remote_branch $GIT_REF else die $LINENO "$GIT_REF is neither branch nor tag" fi fi fi # print out the results so we know what change was used in the logs cd $GIT_DEST git show --oneline | head -1 cd $orig_dir } # git can sometimes get itself infinitely stuck with transient network # errors or other issues with the remote end. This wraps git in a # timeout/retry loop and is intended to watch over non-local git # processes that might hang. GIT_TIMEOUT, if set, is passed directly # to timeout(1); otherwise the default value of 0 maintains the status # quo of waiting forever. # usage: git_timed <git-command> function git_timed { local count=0 local timeout=0 if [[ -n "${GIT_TIMEOUT}" ]]; then timeout=${GIT_TIMEOUT} fi until timeout -s SIGINT ${timeout} git "$@"; do # 124 is timeout(1)'s special return code when it reached the # timeout; otherwise assume fatal failure if [[ $? -ne 124 ]]; then die $LINENO "git call failed: [git $@]" fi count=$(($count + 1)) warn "timeout ${count} for git call: [git $@]" if [ $count -eq 3 ]; then die $LINENO "Maximum of 3 git retries reached" fi sleep 5 done } # git update using reference as a branch. # git_update_branch ref function git_update_branch { GIT_BRANCH=$1 git checkout -f origin/$GIT_BRANCH # a local branch might not exist git branch -D $GIT_BRANCH || true git checkout -b $GIT_BRANCH } # git update using reference as a branch. # git_update_remote_branch ref function git_update_remote_branch { GIT_BRANCH=$1 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH } # git update using reference as a tag. Be careful editing source at that repo # as working copy will be in a detached mode # git_update_tag ref function git_update_tag { GIT_TAG=$1 git tag -d $GIT_TAG # fetching given tag only git_timed fetch origin tag $GIT_TAG git checkout -f $GIT_TAG } # OpenStack Functions # =================== # Get the default value for HOST_IP # get_default_host_ip fixed_range floating_range host_ip_iface host_ip function get_default_host_ip { local fixed_range=$1 local floating_range=$2 local host_ip_iface=$3 local host_ip=$4 # Find the interface used for the default route host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)} # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then host_ip="" host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'` for IP in $host_ips; do # Attempt to filter out IP addresses that are part of the fixed and # floating range. Note that this method only works if the ``netaddr`` # python library is installed. If it is not installed, an error # will be printed and the first IP from the interface will be used. # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct # address. if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then host_ip=$IP break; fi done fi echo $host_ip } # Grab a numbered field from python prettytable output # Fields are numbered starting with 1 # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. # get_field field-number function get_field { while read data; do if [ "$1" -lt 0 ]; then field="(\$(NF$1))" else field="\$$(($1 + 1))" fi echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" done } # Add a policy to a policy.json file # Do nothing if the policy already exists # ``policy_add policy_file policy_name policy_permissions`` function policy_add { local policy_file=$1 local policy_name=$2 local policy_perm=$3 if grep -q ${policy_name} ${policy_file}; then echo "Policy ${policy_name} already exists in ${policy_file}" return fi # Add a terminating comma to policy lines without one # Remove the closing '}' and all lines following to the end-of-file local tmpfile=$(mktemp) uniq ${policy_file} | sed -e ' s/]$/],/ /^[}]/,$d ' > ${tmpfile} # Append policy and closing brace echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile} echo "}" >>${tmpfile} mv ${tmpfile} ${policy_file} } # Package Functions # ================= # _get_package_dir function _get_package_dir { local pkg_dir if is_ubuntu; then pkg_dir=$FILES/apts elif is_fedora; then pkg_dir=$FILES/rpms elif is_suse; then pkg_dir=$FILES/rpms-suse else exit_distro_not_supported "list of packages" fi echo "$pkg_dir" } # Wrapper for ``apt-get`` to set cache and proxy environment variables # Uses globals ``OFFLINE``, ``*_proxy`` # apt_get operation package [package ...] function apt_get { local xtrace=$(set +o | grep xtrace) set +o xtrace [[ "$OFFLINE" = "True" || -z "$@" ]] && return local sudo="sudo" [[ "$(id -u)" = "0" ]] && sudo="env" $xtrace $sudo DEBIAN_FRONTEND=noninteractive \ http_proxy=$http_proxy https_proxy=$https_proxy \ no_proxy=$no_proxy \ apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" } # get_packages() collects a list of package names of any type from the # prerequisite files in ``files/{apts|rpms}``. The list is intended # to be passed to a package installer such as apt or yum. # # Only packages required for the services in 1st argument will be # included. Two bits of metadata are recognized in the prerequisite files: # # - ``# NOPRIME`` defers installation to be performed later in `stack.sh` # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection # of the package to the distros listed. The distro names are case insensitive. function get_packages { local xtrace=$(set +o | grep xtrace) set +o xtrace local services=$@ local package_dir=$(_get_package_dir) local file_to_parse local service if [[ -z "$package_dir" ]]; then echo "No package directory supplied" return 1 fi if [[ -z "$DISTRO" ]]; then GetDistro echo "Found Distro $DISTRO" fi for service in ${services//,/ }; do # Allow individual services to specify dependencies if [[ -e ${package_dir}/${service} ]]; then file_to_parse="${file_to_parse} $service" fi # NOTE(sdague) n-api needs glance for now because that's where # glance client is if [[ $service == n-api ]]; then if [[ ! $file_to_parse =~ nova ]]; then file_to_parse="${file_to_parse} nova" fi if [[ ! $file_to_parse =~ glance ]]; then file_to_parse="${file_to_parse} glance" fi elif [[ $service == c-* ]]; then if [[ ! $file_to_parse =~ cinder ]]; then file_to_parse="${file_to_parse} cinder" fi elif [[ $service == ceilometer-* ]]; then if [[ ! $file_to_parse =~ ceilometer ]]; then file_to_parse="${file_to_parse} ceilometer" fi elif [[ $service == s-* ]]; then if [[ ! $file_to_parse =~ swift ]]; then file_to_parse="${file_to_parse} swift" fi elif [[ $service == n-* ]]; then if [[ ! $file_to_parse =~ nova ]]; then file_to_parse="${file_to_parse} nova" fi elif [[ $service == g-* ]]; then if [[ ! $file_to_parse =~ glance ]]; then file_to_parse="${file_to_parse} glance" fi elif [[ $service == key* ]]; then if [[ ! $file_to_parse =~ keystone ]]; then file_to_parse="${file_to_parse} keystone" fi elif [[ $service == q-* ]]; then if [[ ! $file_to_parse =~ neutron ]]; then file_to_parse="${file_to_parse} neutron" fi elif [[ $service == ir-* ]]; then if [[ ! $file_to_parse =~ ironic ]]; then file_to_parse="${file_to_parse} ironic" fi fi done for file in ${file_to_parse}; do local fname=${package_dir}/${file} local OIFS line package distros distro [[ -e $fname ]] || continue OIFS=$IFS IFS=$'\n' for line in $(<${fname}); do if [[ $line =~ "NOPRIME" ]]; then continue fi # Assume we want this package package=${line%#*} inst_pkg=1 # Look for # dist:xxx in comment if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then # We are using BASH regexp matching feature. package=${BASH_REMATCH[1]} distros=${BASH_REMATCH[2]} # In bash ${VAR,,} will lowecase VAR # Look for a match in the distro list if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then # If no match then skip this package inst_pkg=0 fi fi # Look for # testonly in comment if [[ $line =~ (.*)#.*testonly.* ]]; then package=${BASH_REMATCH[1]} # Are we installing test packages? (test for the default value) if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then # If not installing test packages the skip this package inst_pkg=0 fi fi if [[ $inst_pkg = 1 ]]; then echo $package fi done IFS=$OIFS done $xtrace } # Distro-agnostic package installer # install_package package [package ...] function update_package_repo { if [[ "NO_UPDATE_REPOS" = "True" ]]; then return 0 fi if is_ubuntu; then local xtrace=$(set +o | grep xtrace) set +o xtrace if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then # if there are transient errors pulling the updates, that's fine. # It may be secondary repositories that we don't really care about. apt_get update || /bin/true REPOS_UPDATED=True fi $xtrace fi } function real_install_package { if is_ubuntu; then apt_get install "$@" elif is_fedora; then yum_install "$@" elif is_suse; then zypper_install "$@" else exit_distro_not_supported "installing packages" fi } # Distro-agnostic package installer # install_package package [package ...] function install_package { update_package_repo real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@ } # Distro-agnostic function to tell if a package is installed # is_package_installed package [package ...] function is_package_installed { if [[ -z "$@" ]]; then return 1 fi if [[ -z "$os_PACKAGE" ]]; then GetOSVersion fi if [[ "$os_PACKAGE" = "deb" ]]; then dpkg -s "$@" > /dev/null 2> /dev/null elif [[ "$os_PACKAGE" = "rpm" ]]; then rpm --quiet -q "$@" else exit_distro_not_supported "finding if a package is installed" fi } # Distro-agnostic package uninstaller # uninstall_package package [package ...] function uninstall_package { if is_ubuntu; then apt_get purge "$@" elif is_fedora; then sudo yum remove -y "$@" elif is_suse; then sudo zypper rm "$@" else exit_distro_not_supported "uninstalling packages" fi } # Wrapper for ``yum`` to set proxy environment variables # Uses globals ``OFFLINE``, ``*_proxy`` # yum_install package [package ...] function yum_install { [[ "$OFFLINE" = "True" ]] && return local sudo="sudo" [[ "$(id -u)" = "0" ]] && sudo="env" # The manual check for missing packages is because yum -y assumes # missing packages are OK. See # https://bugzilla.redhat.com/show_bug.cgi?id=965567 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ no_proxy=$no_proxy \ yum install -y "$@" 2>&1 | \ awk ' BEGIN { fail=0 } /No package/ { fail=1 } { print } END { exit fail }' || \ die $LINENO "Missing packages detected" # also ensure we catch a yum failure if [[ ${PIPESTATUS[0]} != 0 ]]; then die $LINENO "Yum install failure" fi } # zypper wrapper to set arguments correctly # zypper_install package [package ...] function zypper_install { [[ "$OFFLINE" = "True" ]] && return local sudo="sudo" [[ "$(id -u)" = "0" ]] && sudo="env" $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ zypper --non-interactive install --auto-agree-with-licenses "$@" } # Process Functions # ================= # _run_process() is designed to be backgrounded by run_process() to simulate a # fork. It includes the dirty work of closing extra filehandles and preparing log # files to produce the same logs as screen_it(). The log filename is derived # from the service name and global-and-now-misnamed SCREEN_LOGDIR # _run_process service "command-line" |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |