slapos-cygwin-bootstrap.sh 11.3 KB
Newer Older
1 2
#! /bin/bash
#
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# This script need root rights. Before run it, make sure you have root
# right. In Windows 7 and later,, you can start terminal by clicking
# Run as Administrator, then run this script in this terminal.
#
# Generally, run this script after install cygwin. It will patch
# cygwin and install slapos-cygwin so that cygwin can work with slapos
# node, the main actions include:
#
#     * Patch cygwin self packages, for example, libtool, cygport etc.
#
#     * Install slapos-cygwin package, for example, ip, ipwin, regpwd
#       etc. These commands are required by slapos node, but not
#       included in the official cygwin.
#
#     * Check and install IPv6 protocol.
#
#     * Set slapos node prefix. When there are many slapos nodes in
#       one same machine, we need a prefix to tell them. In this
#       script, it will set prefix by modify slapos-include.sh
#
23 24 25 26 27
export PATH=/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin:$PATH
if ! source /usr/share/csih/cygwin-service-installation-helper.sh ; then
    echo "Error: Missing csih package."
    exit 1
fi
Jondy Zhao's avatar
Jondy Zhao committed
28

29 30 31
# ======================================================================
# Functions
# ======================================================================
Jondy Zhao's avatar
Jondy Zhao committed
32 33
function show_usage()
{
34
    echo "This script is used to build a bootstrap slapos in cywin."
Jondy Zhao's avatar
Jondy Zhao committed
35 36 37
    echo ""
    echo "Usage:"
    echo ""
38 39 40 41 42 43 44
    echo "  ./slapos-cygwin-bootstrap.sh [--prefix=prefix] [-f | --force]"
    echo ""
    echo "      --prefix=prefix    The prefix is used by network connection name,"
    echo "                         cygwin service name."
    echo "      --force"
    echo "      -f                 Force mode, everything will be reinstalled "
    echo "                         even if it exits."
Jondy Zhao's avatar
Jondy Zhao committed
45 46 47 48
    echo ""
    echo "Before run this script, type the following command in the windows"
    echo "command console to install cygwin:"
    echo ""
49
    echo "  setup_cygwin.bat C:\slapos network"
Jondy Zhao's avatar
Jondy Zhao committed
50 51 52 53
    echo ""
}
readonly -f show_usage

54 55 56 57 58
function check_os_is_wow64()
{
  [[ $(uname) == CYGWIN_NT-*-WOW64 ]]
}
readonly -f check_os_is_wow64
59

60 61 62 63
function slapos_sanity_check()
{
    csih_check_program_or_error /usr/bin/cygport cygport
    csih_check_program_or_error /usr/bin/libtool libtool
64

65 66 67 68
    _filename=$(cygpath -a -w $(cygpath -w /)\\..\\setup.exe)
    [[ -f $(cygpath -u ${_filename}) ]] || csih_error "missing ${_filename}"
}
readonly -f slapos_sanity_check
69

70 71 72
function slapos_patch_cygwin()
{
    csih_inform "Patching cygwin packages for building slapos"
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
    csih_inform "libtool patched"
    sed -i -e "s/4\.3\.4/4.5.3/g" /usr/bin/libtool

    csih_inform "/etc/passwd generated"
    [[ -f /etc/passwd ]] || mkpasswd > /etc/passwd

    csih_inform "/etc/group generated"
    [[ -f /etc/group ]] || mkgroup > /etc/group

    _filename=/usr/bin/cygport
    if [[ -f ${_filename} ]] ; then
        csih_inform "Patching ${_filename} ..."
        sed -i -e 's/D="${workdir}\/inst"/D="${CYGCONF_PREFIX-${workdir}\/inst}"/g' ${_filename} &&
        csih_inform "OK"
    else
        csih_error "Missing cygport package, no ${_filename} found."
    fi
    _filename=/usr/share/cygport/cygclass/autotools.cygclass
    if [[ -f ${_filename} ]] ; then
        csih_inform "Patching ${_filename} ..."
        sed -i -e 's/prefix=$(__host_prefix)/prefix=${CYGCONF_PREFIX-$(__host_prefix)}/g' ${_filename} &&
        csih_inform "OK"
    else
        csih_error "Missing cygport package, no ${_filename} found."
    fi
    _filename=/usr/share/cygport/cygclass/cmake.cygclass
    if [[ -f ${_filename} ]] ; then
        csih_inform "Patching ${_filename} ..."
        sed -i -e 's/-DCMAKE_INSTALL_PREFIX=$(__host_prefix)/-DCMAKE_INSTALL_PREFIX=${CYGCONF_PREFIX-$(__host_prefix)}/g' ${_filename} &&
        csih_inform "OK"
    else
        csih_error "Missing cygport package, no ${_filename} found."
    fi

    # Change format of readme.txt
    _filename=$(cygpath -u $(cygpath -m /)/../readme.txt)
    if [[ -f ${_filename} ]] ; then
        echo "Changing $(cygpath -w ${_filename}) as dos format ..."
        unix2dos ${_filename} && echo OK.
    fi

115
    _filename=~/.minttyrc
116
    echo Checking  ${_filename} ...
117
    if [[ ! -f ${_filename} ]] ; then
118 119 120 121 122 123 124 125 126 127 128 129 130
        cat <<EOF > ${_filename}
BoldAsFont=no
Font=Courier New
FontHeight=16
Scrollbar=none
EOF
        echo "${_filename} has been generated."
    else
        echo OK.
    fi

    _filename="/cygtty.bat"
    echo Checking  ${_filename} ...
131
    if [[ ! -x ${_filename} ]] ; then
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        cat <<EOF > ${_filename}
@echo off

${slapos_cygroot:0:2}
chdir ${slapos_cygroot}\\bin

start mintty.exe -i /Cygwin-Terminal.ico -
EOF
        chmod +x ${_filename}
        echo "${_filename} has been generated."
    else
        echo OK.
    fi

    _filename="/autorebase.bat"
    echo Checking  ${_filename} ...
148
    if [[ ! -f ${_filename} ]] ; then
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
        cat <<EOF > ${_filename}
@echo off
${slapos_cygroot:0:2}
CHDIR ${slapos_cygroot}
${slapos_cygroot}\bin\find /opt/slapos -name "*.dll" > ${slapos_cygroot}\myfile.list
IF EXIST ${slapos_cygroot}\opt\slapgrid. ${slapos_cygroot}\bin\find /opt/slapgrid -name "*.dll" >> ${slapos_cygroot}\myfile.list
IF EXIST ${slapos_cygroot}\srv\slapgrid. ${slapos_cygroot}\bin\find /srv/slapgrid -name "*.dll" >> ${slapos_cygroot}\myfile.list
NET STOP ${slapos_prefix}cron
NET STOP ${slapos_prefix}re6stnet
NET STOP ${slapos_prefix}syslog-ng
NET STOP ${slapos_prefix}cygserver
bin\bash --login -c "/opt/slapos/bin/slapos node stop all"
bin\bash --login -c "for pid in \$(ps | grep '/usr/bin/python2.7' | gawk '{print \$4}') ; do TASKKILL /F /T /PID \$pid ; done"
PATH .\bin;%PATH%
dash /bin/rebaseall -T /myfile.list -v
PAUSE ...
EXIT 0
EOF
        chmod +x ${_filename}
        echo "${_filename} has been generated."
    else
        echo OK.
    fi

    csih_inform "Patch cygwin packages for building slapos OK"
    echo ""
}
readonly -f slapos_patch_cygwin

function install_slapos_cygwin_package()
{
    for _cmdname in ip useradd usermod groupadd brctl tunctl ; do
181
        [[ -x /usr/bin/${_cmdname} ]] && continue
182 183 184 185 186 187 188
        wget -c http://git.erp5.org/gitweb/slapos.package.git/blob_plain/heads/cygwin:/windows/scripts/${_cmdname} -O /usr/bin/${_cmdname} ||
        csih_error "download ${_cmdname} failed"
        csih_inform "download cygwin script ${_cmdname} OK"
        chmod +x /usr/bin/${_cmdname} || csih_error "chmod /usr/bin/${_cmdname} failed"
    done

    for _cmdname in regpwd ; do
189
        [[ -x /usr/bin/${_cmdname} ]] && continue
190 191 192 193
        wget -c http://dashingsoft.com/products/slapos/${_cmdname}.exe -O /usr/bin/${_cmdname}.exe ||
        csih_error "download ${_filename} failed"
        csih_inform "download ${_filename} OK"
        chmod +x /usr/bin/${_cmdname}.exe || csih_error "chmod /usr/bin/${_cmdname}.exe failed"
194
    done
195 196

    for _cmdname in ipwin ; do
197
        [[ -x /usr/bin/${_cmdname} ]] && continue
198 199 200 201 202 203 204 205 206
        if check_os_is_wow64 ; then
            _filename=${_cmdname}-x64.exe
        else
            _filename=${_cmdname}-x86.exe
        fi
        wget -c http://dashingsoft.com/products/slapos/${_filename} -O /usr/bin/${_cmdname}.exe ||
        csih_error "download ${_filename} failed"
        csih_inform "download ${_filename} OK"
        chmod +x /usr/bin/${_cmdname}.exe || csih_error "chmod /usr/bin/${_cmdname}.exe failed"
207
    done
208 209 210 211 212
    
    _path=/etc/slapos/scripts
    csih_inform "create path: ${_path}"
    mkdir -p ${_path}
    for _name in slapos-include.sh slapos-cygwin-bootstrap.sh slapos-configure.sh slapos-cleanup.sh ; do
213
        [[ -x ${_path}/${_name} ]] && continue
214 215 216
        wget -c http://git.erp5.org/gitweb/slapos.package.git/blob_plain/heads/cygwin:/windows/scripts/${_name} -O ${_path}/${_name} ||
        csih_error "download ${_name} failed"
        csih_inform "download script ${_path}/${_name} OK"
217
        chmod +x ${_path}/${_name} || csih_error "chmod ${_path}/${_name} failed"
218 219 220 221
    done
}
readonly -f install_slapos_cygwin_package

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
function remove_all_files()
{
    csih_inform "Remove /cygtty.bat"
    rm -rf /cygtty.bat

    csih_inform "Remove /autorebase.bat"
    rm -rf /autorebase.bat

    csih_inform "Remove ~/.minttyrc"
    rm -rf ~/.minttyrc

    for _cmdname in ip useradd usermod groupadd brctl tunctl regpwd.exe ipwin.exe ; do
        csih_inform "Remove /usr/bin/${_cmdname}"
        rm -rf /usr/bin/${_cmdname}
    done
    
    csih_inform "Remove /etc/slapos/scripts"
    rm -rf /etc/slapos/scripts
}
readonly -f remove_all_files

243 244 245 246 247 248 249 250 251 252 253 254
function install_ipv6_protocol()
{
    csih_inform "Starting configure IPv6 protocol ..."
    netsh interface ipv6 show interface > /dev/null || \
        netsh interface ipv6 install || \
        csih_error "install IPv6 protocol failed"

    csih_inform "Configure IPv6 protocol OK"
    echo ""
}
readonly -f install_ipv6_protocol

255
# -----------------------------------------------------------
256
# Start script
257
# -----------------------------------------------------------
258 259 260
csih_inform "Starting bootstrap slapos node ..."
echo ""

Jondy Zhao's avatar
Jondy Zhao committed
261 262 263
# -----------------------------------------------------------
# Command line options
# -----------------------------------------------------------
264 265
_prefix=
_install_mode=
266 267 268 269 270 271 272 273
while test $# -gt 0; do
    # Normalize the prefix.
    case "$1" in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) optarg= ;;
    esac

    case "$1" in
274 275
    --prefix=*)
    _prefix=$optarg
276
    ;;
277 278
    -f | --force)
    _install_mode=force
279
    ;;
280 281 282
    -h | --help)
    show_usage
    exit 0
283 284 285 286 287 288 289 290 291 292 293
    ;;
    *)
    show_usage
    exit 1
    ;;
    esac

    # Next please.
    shift
done

294
# ======================================================================
295
# Constants
296 297 298
# ======================================================================
declare -r slapos_prefix=${_prefix}
declare -r slapos_cygroot=$(cygpath -w /)
299 300

# -----------------------------------------------------------
301
# Sanity Check
302
# -----------------------------------------------------------
303
slapos_sanity_check
304

305 306 307 308 309 310 311 312
# ======================================================================
# Force mode: remove all the files before run script
# ======================================================================
[[ "${_install_mode}" == "force" ]] && 
csih_inform "Force mode, cleanup all the patched files ..." &&
remove_all_files &&
csih_inform "Cleanup OK."

313
# -----------------------------------------------------------
314
# Patch cygwin packages for building slapos
315
# -----------------------------------------------------------
316
slapos_patch_cygwin
317 318

# -----------------------------------------------------------
319
# Install slapos cygwin package
320
# -----------------------------------------------------------
321
install_slapos_cygwin_package
322 323 324 325 326 327 328 329 330
# -----------------------------------------------------------
# Set prefix for this slapos node
# -----------------------------------------------------------
if [[ -n "${slapos_prefix}" ]] ; then
    _filename=/etc/slapos/scripts/slapos-include.sh
    csih_inform "Set slapos prefix as ${slapos_prefix} by"
    csih_inform "changing file ${_filename}"
    sed -i -e "s%slapos_prefix=.*\$%slapos_prefix=${slapos_prefix}%" ${_filename}
fi
331 332

# -----------------------------------------------------------
333
# Check IPv6 protocol, install it if it isn't installed
334
# -----------------------------------------------------------
335
install_ipv6_protocol
336 337

# -----------------------------------------------------------
338
# End script
339 340
# -----------------------------------------------------------
echo ""
341
csih_inform "Configure slapos bootstrap node successfully."
342 343 344 345
echo ""

read -n 1 -t 60 -p "Press any key to exit..."
exit 0