slapos-configure.sh 28.3 KB
Newer Older
1 2
#! /bin/bash
#
3 4
# This script need root rights. Before run it, make sure you have root
# right.
5
#
6 7
# It used to configure slapos node, it could be run at anytime to
# check the configure of slapos node. The main functions:
8
#
9
#     * Install msloop network adapter, named to re6stnet-lo
10 11 12 13 14 15 16 17 18
#
#     * ipv6: Ipv6 configure
#
#     * re6stnet: Install re6stnet and register to nexedi re6stnet if it hasn't
#
#     * node: Create node configure file by parameters ca/key
#
#     * client: Create client configure file by parameters ca/key
#
19
#     * cron: create cron configure file
20
#
21 22
#     * startup: add this script as startup item
#
23 24
# Usage:
#
25
#    ./slapos-configure
26 27 28
#
export PATH=/usr/local/bin:/usr/bin:$PATH

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
# ======================================================================
# Routine: get_system_and_admins_gids
# Get the ADMINs ids from /etc/group and /etc/passwd
# ======================================================================
get_system_and_admins_ids() {
    ret=0
    for fname in /etc/passwd /etc/group; do
	if ls -ld "${fname}" | grep -Eq  '^-r..r..r..'; then
	    true
	else
	    echo "The file $fname is not readable by all."
	    echo "Please run 'chmod +r $fname'."
	    echo
	    ret=1
	fi
    done

    [ ! -r /etc/passwd -o ! -r  /etc/group ] && return 1;

    ADMINSGID=$(sed -ne '/^[^:]*:S-1-5-32-544:.*:/{s/[^:]*:[^:]*:\([0-9]*\):.*$/\1/p;q}' /etc/group)
    SYSTEMGID=$(sed -ne '/^[^:]*:S-1-5-18:.*:/{s/[^:]*:[^:]*:\([0-9]*\):.*$/\1/p;q}' /etc/group)
    if [ -z "$ADMINSGID" -o -z "$SYSTEMGID" ]; then
		echo "It appears that you do not have correct entries for the"
		echo "ADMINISTRATORS and/or SYSTEM sids in /etc/group."
		echo
		echo "Use the 'mkgroup' utility to generate them"
		echo "   mkgroup -l > /etc/group"
		warning_for_etc_file group
		ret=1;
    fi

    ADMINSUID=$(sed -ne '/^[^:]*:[^:]*:[0-9]*:[0-9]*:[^:]*,S-1-5-32-544:.*:/{s/[^:]*:[^:]*:\([0-9]*\):.*$/\1/p;q}' /etc/passwd)
    SYSTEMUID=$(sed -ne '/^[^:]*:[^:]*:[0-9]*:[0-9]*:[^:]*,S-1-5-18:.*:/{s/[^:]*:[^:]*:\([0-9]*\):.*$/\1/p;q}' /etc/passwd)
    if [ -z "$ADMINSUID" -o -z "$SYSTEMUID" ]; then
		echo "It appears that you do not have correct entries for the"
		echo "ADMINISTRATORS and/or SYSTEM sids in /etc/passwd."
		echo
		echo "Use the 'mkpasswd' utility to generate it"
		echo "   mkpasswd -l > /etc/passwd."
		warning_for_etc_file passwd
		ret=1;
    fi
    return "${ret}"
}  # === get_system_and_admins_ids() === #
73 74 75 76 77

#-------------------------------------------------
# Common functions
#-------------------------------------------------

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
#
# Return connection name by line, and replace space with '%'
#
function get_all_connections()
{
    netsh interface ipv6 show interface | \
    grep "^[ 0-9]\+ " | \
    sed -e "s/^[ 0-9]\+[a-zA-Z]\+//" -e "s/^\s*//" -e "s/ /%/g"
}

#
# Check all the connection names, and compare the original connection
# list, return the new connection name
#
# If nothing found, return empty
# If more than one, return the first one
#
function get_new_connection()
{
    original_connections=" $* "
    current_connections=$(get_all_connections)

    for name in $current_connections ; do
        [[ ! "$original_connections" == *[\ ]$name[\ ]* ]] && \
        echo ${name//%/ } && return 0
    done
}

#
# Remove all ipv4/ipv6 addresses in the connection re6stnet-lo
#
function reset_connection()
{
    ifname=${1-re6stnet-lo}
    for addr in $(netsh interface ipv6 show address $ifname level=normal | \
                grep "^Manual" | \
                sed -e "s/^\(\w\+\s\+\)\{4\}//") ; do
        netsh interface ipv6 del address $ifname $addr
    done
    netsh interface ip set address $ifname source=dhcp
    # for addr in $(netsh interface ip show address $ifname | \
    #             grep "IP Address:" | \
    #             sed -e "s/IP Address://") ; do
    #     netsh interface del address $ifname $addr
    # done
}

#
# Transfer connection name to GUID
#
function connection2guid()
{
    ifname=${1-re6stnet-lo}
131 132 133 134
    #
    # This command doesn't work in the Windows 7, Window 8, maybe
    # Vista. Because no guid information in these platforms.
    #
135 136 137
    # netsh interface ipv6 show interface $ifname | \
    #     grep "^GUID\s*:" | \
    #     sed -e "s/^GUID\s*:\s*//"
138 139
    #
    # So we use getmac to repleace it:
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    getmac /fo list /v | grep -A3 "^Connection Name: *$ifname\$" \
        | grep "^Transport Name:" | sed -e "s/^.*Tcpip_//g"
}

#
# Show error message and waiting for user to press any key quit
#
function show_error_exit()
{
    msg=${1-Failed to configure Slapos Node in this computer.}
    echo $msg
    read -n 1 -p "Press any key to exit..."
    exit 1
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
function check_service_state()
{
    service_name=$1
    service_state=$(cygrunsrv --query $service_name | sed -n -e 's/^Current State[ :]*//p')
    echo Cygwin $service_name service state: $service_state
    if [[ ! x$service_state == "xRunning" ]] ; then
        echo Starting $service_name service ...
        net start $service_name || show_error_exit "Failed to start $service_name service"
        echo Start $service_name service OK.
    else
        echo Cygwin $service_name service is running.
    fi
}

#
# Check ipv6 connection by default ipv6 route
#
function check_ipv6_connection()
{
    netsh interface ipv6 show route | grep -q " ::/0 "
}

177 178 179
#
# Query the parameter, usage:
#
180
#   query_parameter ACTUAL EXCPETED MESSAGE
181 182 183 184 185 186
#
function query_parameter()
{
    if [[ X$1 == X || $1 == "*" || $1 == "all" ]] ; then
        return 1
    fi
187
    if [[ $1 == "?" || $1 == "query" ]] ; then
188 189 190 191 192 193 194 195 196 197 198 199 200
        read -n 1 -p $3 user_ack
        if [[ X$user_ack == X[Yy] ]] ; then
            return 1
        else
            return 0
        fi
    fi
    if [[ $1 == $2 ]] ; then
        return 1
    fi
    return 0
}

201 202 203 204 205 206 207 208 209 210 211
#-------------------------------------------------
# Check adminsitrator rights
#-------------------------------------------------
get_system_and_admins_ids ||  show_error_exit "Failed to get uids of system and amdinistrator account."
id | grep -q "$ADMINSUID(Administrators)" ||  show_error_exit "Error: Administrator right required to run this script."

for myprofile in ~/.bash_profile ~/.profile ; do
    grep -q "export CYGWIN=server" $myprofile || echo "export CYGWIN=server" >> $myprofile
    grep -q "export PATH=/opt/slapos/bin:" $myprofile || echo "export PATH=/opt/slapos/bin:$$PATH" >> $myprofile
done

212 213 214
#-------------------------------------------------
# Constants
#-------------------------------------------------
215 216 217 218 219
slapos_client_home=~/.slapos
client_configure_file=$slapos_client_home/slapos.cfg
client_certificate_file=$slapos_client_home/certificate
client_key_file=$slapos_client_home/key
client_template_file=/etc/slapos/slapos-client.cfg.example
220
url_client_template_file=http://git.erp5.org/gitweb/slapos.core.git/blob_plain/HEAD:/slapos-client.cfg.example
221 222 223 224 225

node_certificate_file=/etc/opt/slapos/ssl/computer.crt
node_key_file=/etc/opt/slapos/ssl/computer.key
node_config_file=/etc/opt/slapos/slapos.cfg
node_template_file=/etc/slapos/slapos.cfg.example
226 227
url_node_template_file=http://git.erp5.org/gitweb/slapos.core.git/blob_plain/HEAD:/slapos.cfg.example

228
slapos_ifname=re6stnet-lo
229 230 231 232
# Hope it will not confilct with original network in the local machine
ipv4_local_network=10.201.67.0/24

slapos_runner_file=/etc/slapos/scripts/slap-runner.html
233 234 235
slaprunner_cfg=http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin-0:/software/slaprunner/software.cfg
netdrive_reporter_cfg=http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin-0:/software/netdrive-reporter/software.cfg
wordpress_cfg=http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin:/software/wordpress/software.cfg
236 237 238 239

#-------------------------------------------------
# Create paths
#-------------------------------------------------
240

241 242 243
mkdir -p /etc/opt/slapos/ssl/partition_pki
mkdir -p $slapos_client_home

244 245 246 247 248
#-------------------------------------------------
# Configure cygwin server services
#-------------------------------------------------

echo Checking cygserver service ...
249
cygrunsrv --query cygserver > /dev/null 2>&1
250 251 252 253 254 255 256
if (( $? )) ; then
    echo Run cygserver-config ...
    /usr/bin/cygserver-config --yes || \
        show_error_exit "Failed to run cygserver-config"
else
    echo The cygserver service has been installed.
fi
257
check_service_state cygserver
258 259

echo Checking syslog-ng service ...
260
cygrunsrv --query syslog-ng > /dev/null 2>&1
261 262 263 264 265 266 267
if (( $? )) ; then
    echo Run syslog-ng-config ...
    /usr/bin/syslog-ng-config --yes || \
        show_error_exit "Failed to run syslog-ng-config"
else
    echo The syslog-ng service has been installed.
fi
268
check_service_state syslog-ng
269 270 271 272 273

#-------------------------------------------------
# Configure slapos network
#-------------------------------------------------

274 275 276 277 278 279 280 281 282 283 284 285 286 287
#
# Add msloop network adapter, ane name it as "re6stnet-lo"
#
echo Checking slapos network adapter: $slapos_ifname ...
original_connections=$(echo $(get_all_connections))
if [[ ! " $original_connections " == *[\ ]$slapos_ifname[\ ]* ]] ; then
    echo Installing slapos network adapter ...
    devcon install $WINDIR\\inf\\netloop.inf *MSLOOP
    connection_name=$(get_new_connection $original_connections)
    [[ "X$connection_name" == "X" ]] && \
        show_error_exit "Add msloop network adapter failed."
    echo
    netsh interface set interface name="$connection_name" newname="$slapos_ifname"
fi
288 289
#ip -4 addr add $ipv4_local_network dev $slapos_ifname
# reset_connection $slapos_ifname
290
echo SlapOS network adapter OK.
291
echo Slapos ipv4_local_network is $ipv4_local_network
292

293 294 295
#-------------------------------------------------
# Generate slapos node configure file
#-------------------------------------------------
296

297
echo Checking computer certificate file ...
298
if [[ ! -f $node_certificate_file ]] ; then
299 300
    read -p "Where is computer certificate file (/computer.crt): " certificate_file
    [[ X$certificate_file == X ]] && certificate_file=/computer.crt
301 302 303 304 305
    [[ ! -f "$certificate_file" ]] && \
        show_error_exit "Certificate file $certificate_file doesn't exists."
    echo "Copy certificate from $certificate_file to $node_certificate_file"
    certificate_file=$(cygpath -u $certificate_file)
    cp $certificate_file $node_certificate_file
306 307
else
    echo Found computer certificate file: $node_certificate_file
308
fi
309 310 311
openssl x509 -noout -in $node_certificate_file || \
    show_error_exit "Invalid computer certificate: $node_certificate_file."
echo Check computer certificate OK.
312

313
echo Checking computer guid ...
314 315 316 317 318
computer_id=$(grep  CN=COMP $node_certificate_file | sed -e "s/^.*, CN=//g" | sed -e "s%/emailAddress.*\$%%g")
[[ "$computer_id" == COMP-+([0-9]) ]] || \
    show_error_exit "Invalid computer id specified."
echo Computer GUID is: $computer_id

319
echo Checking computer key file ...
320
if [[ ! -f $node_key_file ]] ; then
321 322
    read -p "Where is computer key file (/computer.key): " key_file
    [[ X$key_file == X ]] && key_file=/computer.key
323 324 325 326 327
    [[ ! -f "$key_file" ]] && \
        show_error_exit "Key file $key_file doesn't exists."
    echo "Copy key from $key_file to $node_key_file"
    key_file=$(cygpath -u $key_file)
    cp $key_file $node_key_file
328 329
else
    echo Found computer key file: $node_key_file
330
fi
331 332 333
openssl rsa -noout -in $node_key_file -check || \
    show_error_exit "Invalid computer key: $node_key_file."
echo Check computer key OK.
334 335 336

# Create node configure file, replace interface_name with guid of
# re6stnet-lo
337
echo Checking computer configure file ...
338 339
if [[ ! -f $node_config_file ]] ; then
    [[ -f $node_template_file ]] || \
340
        (cd /etc/slapos; wget $url_node_template_file -O $node_template_file) || \
341
        show_error_exit "Download slapos.cfg.example failed."
342
    echo "Copy computer configure file from $node_template_file to $node_config_file"
343 344 345
    cp $node_template_file $node_config_file
fi

346 347 348
interface_guid=$(connection2guid $slapos_ifname) || \
    show_error_exit "Failed to get guid of interface: $slapos_ifname."

349 350 351 352 353
echo "Computer configuration information:"
echo "  interface name:     $slapos_ifname"
echo "  GUID:               $interface_guid"
echo "  ipv4_local_network: $ipv4_local_network"
echo "  computer_id:        $computer_id"
354 355 356 357 358 359
# generate /etc/slapos/slapos.cfg
sed -i  -e "s%^\\s*interface_name.*$%interface_name = $interface_guid%" \
        -e "s%^#\?\\s*ipv6_interface.*$%# ipv6_interface =%g" \
        -e "s%^ipv4_local_network.*$%ipv4_local_network = $ipv4_local_network%" \
        -e "s%^computer_id.*$%computer_id = $computer_id%" \
        $node_config_file
360
echo Check computer configure file OK.
361

362 363 364 365 366
#-------------------------------------------------
# Generate slapos client configure file
#-------------------------------------------------

echo Checking client certificate file ...
367
if [[ ! -f $client_certificate_file ]] ; then
368 369
    read -p "Where is client certificate file (/certificate): " certificate_file
    [[ X$certificate_file == X ]] && certificate_file=/certificate
370 371
    [[ ! -f "$certificate_file" ]] && \
        show_error_exit "Certificate file $certificate_file doesn't exists."
372
    echo "Copy client certificate from $certificate_file to $client_certificate_file"
373 374 375
    certificate_file=$(cygpath -u $certificate_file)
    cp $certificate_file $client_certificate_file
fi
376 377 378
openssl x509 -noout -in $client_certificate_file || \
    show_error_exit "Invalid client certificate: $client_certificate_file."
echo Check client certificate Ok.
379

380
echo Checking client key file ...
381
if [[ ! -f $client_key_file ]] ; then
382 383
    read -p "Where is client key file (/key): " key_file
    [[ X$key_file == X ]] && key_file=/key
384 385
    [[ ! -f "$key_file" ]] && \
        show_error_exit "Key file $key_file doesn't exists."
386
    echo "Copy client key from $key_file to $client_key_file"
387 388 389
    key_file=$(cygpath -u $key_file)
    cp $key_file $client_key_file
fi
390 391 392
openssl rsa -noout -in $client_key_file -check || \
    show_error_exit "Invalid client key: $client_key_file."
echo Checking computer key OK.
393

394
echo Checking client configure file ...
395
if [[ ! -f $client_configure_file ]] ; then
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
    cat <<EOF > $client_configure_file
[slapos]
master_url = https://slap.vifib.com/

[slapconsole]
# Put here retrieved certificate from SlapOS Master.
# Beware: put certificate from YOUR account, not the one from your node.
# You (as identified person from SlapOS Master) will request an instance, node your node.
# Conclusion: node certificate != person certificate.
cert_file = certificate file location coming from your slapos master account
key_file = key file location coming from your slapos master account
# Below are softwares maintained by slapos.org and contributors
alias =
  apache_frontend http://git.erp5.org/gitweb/slapos.git/blob_plain/HEAD:/software/apache-frontend/software.cfg
  dokuwiki http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.158:/software/dokuwiki/software.cfg
  drupal http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.151:/software/erp5/software.cfg
  erp5 http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.143:/software/erp5/software.cfg
  erp5_branch http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/erp5:/software/erp5/software.cfg
  fengoffice http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.158:/software/fengoffice/software.cfg
  kumofs http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.141:/software/kumofs/software.cfg
  kvm http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.156:/software/kvm/software.cfg
  maarch http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.159:/software/maarch/software.cfg
  mariadb http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.152:/software/mariadb/software.cfg
  memcached http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.82:/software/memcached/software.cfg
  mysql http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.65:/software/mysql-5.1/software.cfg
  opengoo http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.158:/software/opengoo/software.cfg
  postgresql http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.157:/software/postgres/software.cfg
  slaposwebrunner http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin-0:/software/slaprunner/software.cfg
  slaposwebrunner_lite http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin-0:/software/slaprunner-lite/software.cfg
  wordpress http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin:/software/wordpress/software.cfg
  xwiki http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.46:/software/xwiki/software.cfg
  zabbixagent http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.162:/software/zabbix-agent/software.cfg
  netdrive_reporter http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin-0:/software/netdrive-reporter/software.cfg
  demoapp http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/heads/cygwin-0:/software/demoapp/software.cfg
EOF
    echo "Client configure file $client_config_file created."
432 433
fi

434 435 436 437
echo Client configuration information:
echo     client certificate file: $client_certificate_file
echo     client key file:         $client_key_file
echo
438 439 440
sed -i -e "s%^cert_file.*$%cert_file = $client_certificate_file%" \
       -e "s%^key_file.*$%key_file = $client_key_file%" \
       $client_configure_file
441
echo Check client configure file OK.
442

443
#-------------------------------------------------
444
# Re6stnet
445
#-------------------------------------------------
446 447

# Check ipv6, install it if it isn't installed.
448 449 450 451
echo Checking ipv6 protocol ...
netsh interface ipv6 show interface > /dev/null || netsh interface ipv6 install || \
    show_error_exit "Failed to install ipv6 protocol."
echo IPv6 protocol has been installed.
452 453

# miniupnpc is required by re6stnet
454
echo Checking miniupnpc ...
455
if [[ ! -d /opt/miniupnpc ]] ; then
456 457 458 459 460 461 462 463 464 465 466
    [[ -f /miniupnpc.tar.gz ]] || show_error_exit "No package found: /miniupnpc.tar.gz"
    echo "Installing miniupnpc ..."
    cd /opt
    tar xzf /miniupnpc.tar.gz --no-same-owner
    mv $(ls -d miniupnpc-*) miniupnpc
    cd miniupnpc
    make
    python setup.py install || show_error_exit "Failed to install miniupnpc."
    echo "Install miniupnpc OK."
else
    echo Check miniupnpc OK.
467 468 469
fi

# pyOpenSSL is required by re6stnet
470
echo Checking pyOpenSSL ...
471
if [[ ! -d /opt/pyOpenSSL ]] ; then
472 473 474 475 476 477 478 479 480 481
    [[ -f /pyOpenSSL.tar.gz ]] || show_error_exit "No package found: /pyOpenSSL.tar.gz"
    echo "Installing pyOpenSSL ..."
    cd /opt
    tar xzf /pyOpenSSL.tar.gz --no-same-owner
    mv $(ls -d pyOpenSSL-*) pyOpenSSL
    cd pyOpenSSL
    python setup.py install ||  show_error_exit "Failed ot install pyOpenSSL."
    echo "Install pyOpenSSL OK."
else
    echo Check pyOpenSSL OK.
482 483 484
fi

# Install re6stnet
485
echo Checking re6stnet ...
486 487 488 489 490 491 492 493 494 495 496
if [[ ! -d /opt/re6stnet ]] ; then
    echo "Installing re6stnet ..."
    cd /opt
    if [[ -f /re6stnet.tar.gz ]] ; then
        tar xzf /re6stnet.tar.gz --no-same-owner
        mv $(ls -d re6stnet-*) re6stnet
    else
        echo "Clone re6stnet from http://git.erp5.org/repos/re6stnet.git"
		git clone -b cygwin http://git.erp5.org/repos/re6stnet.git
    fi
    cd re6stnet
497 498 499 500
    python setup.py install || show_error_exit "Failed to install re6stnet."
    echo "Install re6stnet OK."
else
    echo Check re6stnet OK.
501 502
fi

503
echo Checking re6stent configuration ...
504 505 506
mkdir -p /etc/re6stnet
cd /etc/re6stnet
if [[ ! -f re6stnet.conf ]] ; then
507 508 509 510 511 512 513 514 515 516 517 518
    echo Register to http://re6stnet.nexedi.com ...
    # Your subnet: 2001:67c:1254:e:19::/80 (CN=917529/32)
    mysubnet=$(re6st-conf --registry http://re6stnet.nexedi.com/ --anonymous | grep "^Your subnet:") \
        || show_error_exit "Register to nexedi re6stnet failed"
    echo Register OK.
    echo
    echo $mysubnet
    echo
    echo Write subnet information to re6stnet.conf
    echo "# $mysubnet" >> re6stnet.conf
    echo Write "table 0" to re6stnet.conf
    echo "table 0" >> re6stnet.conf
519 520 521
    echo "ovpnlog" >> re6stnet.conf
    echo "interface $slapos_ifname" >> re6stnet.conf
    echo "main-interface $slapos_ifname" >> re6stnet.conf
522

523
fi
524 525
[[ ! -f re6stnet.conf ]] && \
    show_error_exit "Failed to register to nexedi re6stnet: no /etc/re6stnet/re6stnet.conf found."
526
grep -q "^table 0" re6stnet.conf || \
527 528 529 530
    show_error_exit "Error: no parameter 'table 0' found in the /etc/re6stnet/re6stnet.conf"
grep -q "^# Your subnet: " re6stnet.conf || \
    show_error_exit "Error: no subnet found in the /etc/re6stnet/re6stnet.conf"
echo Check re6stnet configuration OK.
531 532 533 534 535 536 537 538 539 540
echo

#-------------------------------------------------
# Create openvpn tap-windows drivers used by re6stnet
#-------------------------------------------------

# Adding tap-windows driver will break others, so we add all drivers
# here. Get re6stnet client count, then remove extra drivers and add
# required drivers.
#
541
echo
542
echo Installing OpenVPN Tap-Windows Driver ...
543
echo
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
original_connections=$(echo $(get_all_connections))
client_count=$(sed -n -e "s/^client-count *//p" /etc/re6stnet/re6stnet.conf)
[[ -z $client_count ]] && client_count=10
echo Re6stnet client count = $client_count
re6stnet_name_list="re6stnet-tcp re6stnet-udp"
for (( i=1; i<=client_count; i=i+1 )) ; do
    re6stnet_name_list="$re6stnet_name_list re6stnet$i"
done
for re6stnet_ifname in $re6stnet_name_list ; do
    echo Checking interface $re6stnet_ifname ...
    if [[ ! " $original_connections " == *[\ ]$re6stnet_ifname[\ ]* ]] ; then
        echo Installing  interface $re6stnet_ifname ...
        ip vpntap add dev $re6stnet_ifname || show_error_exit "Failed to install openvpn tap-windows driver."
        echo Interface $re6stnet_ifname installed.
    else
        echo $re6stnet_ifname has been installed.
    fi
done
#
# Remove OpenVPN Tap-Windows Driver
564
#
565 566
# ip vpntap del dev re6stnet-x
#
567

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
#-------------------------------------------------
# IPv6 Connection
#-------------------------------------------------
echo "Checking native IPv6 ..."
check_ipv6_connection
# Run re6stnet if no native ipv6
if (( $? )) ; then
    echo "No native IPv6."
    echo Check re6stnet network ...
    which re6stnet > /dev/null 2>&1 || show_error_exit "Error: no re6stnet installed, please run Configure SlapOS first."
    service_name=slapos-re6stnet
    # re6st-conf --registry http://re6stnet.nexedi.com/ --is-needed
    cygrunsrv --query $service_name >/dev/null 2>&1
    if (( $? )) ; then
        [[ -d /var/log/re6stnet ]] || mkdir -p /var/log/re6stnet
        echo "Install slapos-re6stnet service ..."
584
        cygrunsrv -I $service_name -c /etc/re6stnet -p $(which re6stnet) -a "@re6stnet.conf" || \
585 586 587 588 589 590 591 592 593 594 595 596
            show_error_exit "Failed to install $service_name service."
        echo "Cygwin $service_name service installed."
        # echo "Waiting re6stent network work ..."
        # while true ; do
        #     check_ipv6_connection && break
        # done
    fi
    service_state=$(cygrunsrv --query $service_name | sed -n -e 's/^Current State[ :]*//p')
    if [[ ! x$service_state == "xRunning" ]] ; then
        echo "Starting $service_name service ..."
        cygrunsrv --start $service_name || show_error_exit "Failed to start $service_name service."
        service_state=$(cygrunsrv --query $service_name | sed -n -e 's/^Current State[ :]*//p')
597
    fi
598 599 600 601 602 603 604 605 606
    [[ x$service_state == "xRunning" ]] || show_error_exit "Failed to start $service_name service."
    echo Cygwin $service_name service is running.
    echo "You can check log files in the /var/log/re6stnet/*.log"
    echo
    echo "re6stnet network OK."
else
    echo "Native IPv6 Found."
fi

607 608 609
#-------------------------------------------------
# Create instance of Web Runner
#-------------------------------------------------
610
slaprunner_title="SlapOS-Node-Runner-In-$computer_id"
611
grep -q "window.location.href" $slapos_runner_file
612 613 614 615
if (( $? )) ; then
    echo
    echo Installing Web Runner ...
    echo
616

617
    re6stnet_ipv6=$(cat /etc/re6stnet/re6stnet.conf | grep "Your subnet" | \
618
        sed -e "s/^.*subnet: //g" -e "s/\/80 (CN.*\$/1/g")
619
    echo "Re6stnet address in this computer: $re6stnet_ipv6"
620 621
    netsh interface ipv6 show addr $slapos_ifname level=normal | grep -q $re6stnet_ipv6 || \
        netsh interface ipv6 add addr $slapos_ifname $re6stnet_ipv6
622 623 624 625
    echo Run slapformat ...
    /opt/slapos/bin/slapos node format -cv --now ||
        show_error_exit "Failed to run slapos format."
    echo
626

627
    echo "Supply $slaprunner_cfg in the computer $computer_id"
628
    /opt/slapos/bin/slapos supply  $slaprunner_cfg $computer_id
629
    echo "Request an instance $slaprunner_title ..."
630
    patch_file=/etc/slapos/patches/slapos-cookbook-inotifyx.patch
631
    while true ; do
632
        /opt/slapos/bin/slapos node software --verbose
633 634
        # Apply patches to slapos.cookbook for inotifix
        if [[ -f $patch_file ]] ; then
635 636 637 638 639
            for x in $(find /opt/slapgrid/ -name slapos.cookbook-*.egg) ; do
                echo Apply patch $patch_file at $x
                cd $x
                patch -f --dry-run -p1 < $patch_file > /dev/null && patch -p1 < $patch_file
            done
640
        fi
641 642
        /opt/slapos/bin/slapos node instance --verbose
        /opt/slapos/bin/slapos node report --verbose
643
        /opt/slapos/bin/slapos request $client_config_file $slaprunner_title $slaprunner_cfg --node computer_guid=$computer_id && break
644
        sleep 5
645 646 647 648 649 650 651
    done
    # Connection parameters of instance are:
    #  {'backend_url': 'http://[2001:67c:1254:45::c5d5]:50000',
    #  'cloud9-url': 'http://localhost:9999',
    #  'password_recovery_code': 'e2d01c14',
    #  'ssh_command': 'ssh 2001:67c:1254:45::c5d5 -p 2222',
    #  'url': 'http://softinst39090.host.vifib.net/'}
652
    slaprunner_url=$(/opt/slapos/bin/slapos request $client_config_file $slaprunner_title $slaprunner_cfg --node computer_guid=$computer_id | \
653
        grep backend_url | sed -e "s/^.*': '//g" -e "s/',.*$//g")
654
    echo Got node runner url: $slaprunner_url
655
    [[ -z $slaprunner_url ]] && show_error_exit "Failed to create instance of SlapOS Web Runner."
656

657
    cat <<EOF > $slapos_runner_file
658 659 660 661 662
<html>
<head><title>SlapOS Web Runner</title>
<script LANGUAGE="JavaScript">
<!--
function openwin() {
663
  window.location.href = "$slaprunner_url"
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
}
//-->
</script>
</head>
<body onload="openwin()"/>
</html>
EOF
    echo Generate file: $slapos_runner_file

    echo
    echo Install Web Runner OK.
    echo
fi

#-------------------------------------------------
# Configure crontab
#-------------------------------------------------
681 682 683
crontab_file=/var/cron/tabs/$(whoami)
if [[ ! -f $crontab_file ]] ; then
    cat <<EOF  > $crontab_file
684 685
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
MAILTO=""

# Run "Installation/Destruction of Software Releases" and "Deploy/Start/Stop Partitions" once per minute
* * * * * Administrator /opt/slapos/bin/slapos node software --verbose --logfile=/opt/slapos/log/slapos-node-software.log > /dev/null 2>&1
* * * * * Administrator /opt/slapos/bin/slapos node instance --verbose --logfile=/opt/slapos/log/slapos-node-instance.log > /dev/null 2>&1

# Run "Destroy Partitions to be destroyed" once per hour
0 * * * * Administrator /opt/slapos/bin/slapos node report --maximal_delay=3600 --verbose --logfile=/opt/slapos/log/slapos-node-report.log > /dev/null 2>&1

# Run "Check/add IPs and so on" once per hour
0 * * * * Administrator /opt/slapos/bin/slapos node format >> /opt/slapos/log/slapos-node-format.log 2>&1


# Make sure we have only good network routes if we use VPN
# * * * * * root if [ -f /etc/opt/slapos/openvpn-needed  ]; then ifconfig tapVPN | grep "Scope:Global" > /dev/null ;if [ $? = 0 ]; then ROUTES=$(ip -6 r l | grep default | awk '{print $5}'); for GW in $ROUTES ; do if [ ! $GW = tapVPN ]; then /sbin/ip -6 route del default dev $GW > /dev/null 2>&1;fi ;done ;fi ;fi
EOF
702
    echo Cron file $crontab_file created.
703
fi
704

705 706 707 708 709 710 711 712 713 714 715 716 717
echo Checking cron job ...
ps -ef | grep -q "/usr/sbin/cron"
if (( $? )) ; then
    echo Starting cron job ...
    /usr/sbin/cron &
    (( $? )) && show_error_exit "Failed to run cron-config"
    disown -h
    echo The cron job started.
else
    echo The cron job is running.
fi


718 719 720 721
#-------------------------------------------------
# Add slapos-configure to windows startup item
#-------------------------------------------------
slapos_run_key='\HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
722
slapos_run_entry=slapos-configure
723 724 725 726 727 728 729
slapos_run_script=/etc/slapos/scripts/slapos-configure.sh
echo Checking startup item ...
regtool -q get "$slapos_run_key\\$slapos_run_entry" || \
    regtool -q set "$slapos_run_key\\$slapos_run_entry" \
    "\"$(cygpath -w /usr/bin/bash)\" --login -i $slapos_run_script" || \
    show_error_exit "Failed to add slapos-configure.sh as windows startup item."
echo Startup item "$slapos_run_key\\$slapos_run_entry": $(regtool get "$slapos_run_key\\$slapos_run_entry")
730
echo
731

732
echo SlapOS Node configure successfully.
733
read -n 1 -t 60 -p "Press any key to exit..."
734
exit 0