slapos-node-config.sh 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#! /bin/bash
#
# Configure slapos node,
#
#     1. Install re6stnet if it hasn't
#
#     2. Register to nexedi re6stnet
#
#     3. Install msloop network adapter, named to re6stnet-lo
#
#        It'll used as main interface for slapos and re6stnet
#
#     4. Create node configure file by parameters ca/key and computer id
#
#     5. Add init-slapos-node.sh as system startup item
Jondy Zhao's avatar
Jondy Zhao committed
16
#
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# Usage:
#
#    ./slapos-node-config
#

#
# 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
Jondy Zhao's avatar
Jondy Zhao committed
38
#
39 40
function get_new_connection()
{
Jondy Zhao's avatar
Jondy Zhao committed
41
    original_connections=" $* "
42
    current_connections=$(get_all_connections)
Jondy Zhao's avatar
Jondy Zhao committed
43

44
    for name in $current_connections ; do
Jondy Zhao's avatar
Jondy Zhao committed
45 46
        [[ ! "$original_connections" == *[\ ]$name[\ ]* ]] && \
        echo ${name//%/ } && return 0
47 48 49 50 51 52 53 54 55 56
    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 | \
Jondy Zhao's avatar
Jondy Zhao committed
57
                grep "^Manual" | \
58 59 60
                sed -e "s/^\(\w\+\s\+\)\{4\}//") ; do
        netsh interface ipv6 del address $ifname $addr
    done
61 62 63 64 65 66
    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
67 68 69 70 71 72 73 74 75 76 77 78 79
}

#
# Transfer connection name to GUID
#
function connection2guid()
{
    ifname=${1-re6stnet-lo}
    netsh interface ipv6 show interface $ifname | \
        grep "^GUID\s*:" | \
        sed -e "s/^GUID\s*:\s*//"
}

Jondy Zhao's avatar
Jondy Zhao committed
80 81
node_certificate_file=/etc/opt/slapos/ssl/computer.crt
node_key_file=/etc/opt/slapos/ssl/computer.key
Jondy Zhao's avatar
Jondy Zhao committed
82 83
node_config_file=/etc/opt/slapos/slapos.cfg
node_template_file=/etc/slapos/slapos.cfg.example
84 85
run_key='\HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
slapos_run_entry=SlapOS-Node
Jondy Zhao's avatar
Jondy Zhao committed
86 87 88
slapos_ifname=re6stnet-lo

# Remove startup item first.
89 90 91 92 93
regtool -q unset "$run_key\\$slapos_run_entry"

#
# Add msloop network adapter, ane name it as "re6stnet-lo"
#
Jondy Zhao's avatar
Jondy Zhao committed
94 95 96 97
echo Checking slapos network adapter: $slapos_ifname ...
original_connections=$(echo $(get_all_connections))
if [[ ! " $original_connections " == *[\ ]$slapos_ifname[\ ]* ]] ; then
    echo Installing slapos network adapter ...
98 99 100 101 102
    devcon install $WINDIR\\inf\\netloop.inf *MSLOOP
    connection_name=$(get_new_connection $original_connections)
    [[ "X$connection_name" == "X" ]] && \
        echo "Add msloop network adapter failed." && \
        exit 1
Jondy Zhao's avatar
Jondy Zhao committed
103 104
    echo
    netsh interface set interface name="$connection_name" newname="$slapos_ifname"
105
fi
Jondy Zhao's avatar
Jondy Zhao committed
106 107
reset_connection $slapos_ifname
echo SlapOS network adapter OK.
108 109

#
Jondy Zhao's avatar
Jondy Zhao committed
110
# Generate Node Configure file
111
#
Jondy Zhao's avatar
Jondy Zhao committed
112
echo
113 114 115 116 117
echo Before continue to configure, make sure you have register your server to
echo slapos.org community Cloud, and have obtained X509 certificate and key
echo which are needed for the following configuration process.
echo
echo Refer to http://community.slapos.org/wiki/osoe-Lecture.SlapOS.Extended/developer-Installing.SlapOS.Slave.Node.Source
Jondy Zhao's avatar
Jondy Zhao committed
118 119 120
echo

mkdir -p /etc/opt/slapos/ssl/partition_pki
121

Jondy Zhao's avatar
Jondy Zhao committed
122
if [[ "$1" == COMP-+([0-9]) ]] ; then
123 124 125 126 127 128 129
    computer_id=$1
else
    [[ "X$1" == "X" ]] || echo "Invalid computer id: $1"
    echo
    echo Please input computer id you have registered, it looks like COMP-XXXX
    read -p "computer id: " computer_id
fi
Jondy Zhao's avatar
Jondy Zhao committed
130
[[ "$computer_id" == COMP-+([0-9]) ]] || \
131 132 133 134 135 136 137 138
    (echo "Invalid computer id specified."; exit 1)

if [[ -f "$2" ]] ; then
    echo "Copy certificate from $2 to $node_certificate_file"
    cp $2 $node_certificate_file
elif [[ ! -f $node_certificate_file ]] ; then
    read -p "Where is certificate file: " certificate_file
    [[ ! -f "$certificate_file" ]] && \
Jondy Zhao's avatar
Jondy Zhao committed
139
        echo "Certificate file $certificate_file doesn't exists." && exit 1
140
    echo "Copy certificate from $certificate_file to $node_certificate_file"
Jondy Zhao's avatar
Jondy Zhao committed
141
    cp $certificate_file $node_certificate_file
142 143 144 145 146 147 148 149
fi

if [[ -f "$3" ]] ; then
    echo "Copy key from $3 to $node_key_file"
    cp $3 $node_key_file
elif [[ ! -f $node_key_file ]] ; then
    read -p "Where is key file: " key_file
    [[ ! -f "$key_file" ]] && \
Jondy Zhao's avatar
Jondy Zhao committed
150
        echo "Key file $key_file doesn't exists." && exit 1
151
    echo "Copy key from $key_file to $node_key_file"
Jondy Zhao's avatar
Jondy Zhao committed
152
    cp $key_file $node_key_file
153 154 155
fi

# Hope it will not confilct with original network in the local machine
156
ipv4_local_network=10.201.67.0/24
157 158

# Add ipv4 address
Jondy Zhao's avatar
Jondy Zhao committed
159
ip -4 addr add $ipv4_local_network dev $slapos_ifname
160 161 162 163

# Create node configure file, replace interface_name with guid of
# re6stnet-lo

Jondy Zhao's avatar
Jondy Zhao committed
164 165 166
if [[ ! -f $node_config_file ]] ; then
    [[ -f $node_template_file ]] || \
        (cd /etc/slapos; wget http://git.erp5.org/gitweb/slapos.core.git/blob_plain/HEAD:/slapos.cfg.example) || \
167
        (echo "Download slapos.cfg.example failed."; exit 1)
Jondy Zhao's avatar
Jondy Zhao committed
168
    cp $node_template_file $node_config_file
169 170
fi

Jondy Zhao's avatar
Jondy Zhao committed
171
interface_guid=$(connection2guid $slapos_ifname)
172
# generate /etc/slapos/slapos.cfg
Jondy Zhao's avatar
Jondy Zhao committed
173 174 175 176 177
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
178 179 180 181 182 183 184 185 186

#
# Re6stnet
#

# Check ipv6, install it if it isn't installed.
netsh interface ipv6 show interface > /dev/null || netsh interface ipv6 install

# miniupnpc is required by re6stnet
Jondy Zhao's avatar
Jondy Zhao committed
187
if [[ ! -d /opt/miniupnpc ]] ; then
188
    if [[ -f /miniupnpc.tar.gz ]] ; then
Jondy Zhao's avatar
Jondy Zhao committed
189 190 191 192
        echo "Installing miniupnpc ..."
        cd /opt
        tar xzf /miniupnpc.tar.gz --no-same-owner
        mv $(ls -d miniupnpc-*) miniupnpc
193
        cd miniupnpc
Jondy Zhao's avatar
Jondy Zhao committed
194
        make
195
        python setup.py install || echo "Install miniupnpc failed."
Jondy Zhao's avatar
Jondy Zhao committed
196 197 198 199 200 201
    else
        echo "No miniupnpc source package found."
    fi
fi

# pyOpenSSL is required by re6stnet
Jondy Zhao's avatar
Jondy Zhao committed
202
if [[ ! -d /opt/pyOpenSSL ]] ; then
Jondy Zhao's avatar
Jondy Zhao committed
203 204 205 206 207 208 209
    if [[ -f /pyOpenSSL.tar.gz ]] ; then
        echo "Installing pyOpenSSL ..."
        cd /opt
        tar xzf /pyOpenSSL.tar.gz --no-same-owner
        mv $(ls -d pyOpenSSL-*) pyOpenSSL
        cd pyOpenSSL
        python setup.py install || echo "Install pyOpenSSL failed."
210 211 212 213
    fi
fi

# Install re6stnet
Jondy Zhao's avatar
Jondy Zhao committed
214
if [[ ! -d /opt/re6stnet ]] ; then
Jondy Zhao's avatar
Jondy Zhao committed
215
    echo "Installing re6stnet ..."
216 217
    cd /opt
    if [[ -f /re6stnet.tar.gz ]] ; then
Jondy Zhao's avatar
Jondy Zhao committed
218 219
        tar xzf /re6stnet.tar.gz --no-same-owner
        mv $(ls -d re6stnet-*) re6stnet
220
    else
Jondy Zhao's avatar
Jondy Zhao committed
221 222
        echo "Clone re6stnet from http://git.erp5.org/repos/re6stnet.git"
        git clone -b cygwin -n http://git.erp5.org/repos/re6stnet.git
223 224 225 226 227 228 229 230 231 232 233 234
    fi
    cd re6stnet
    python setup.py install || echo "Install re6stnet failed."
fi

mkdir -p /etc/re6stnet
cd /etc/re6stnet
if [[ ! -f re6stnet.conf ]] ; then
    re6st-conf --registry http://re6stnet.nexedi.com/
fi
[[ ! -f re6stnet.conf ]] && echo "Register to nexedi re6stnet failed" && exit 1

Jondy Zhao's avatar
Jondy Zhao committed
235
#
236 237
# Add run item when windows startup
#
Jondy Zhao's avatar
Jondy Zhao committed
238 239 240 241 242 243 244 245
init_script=/etc/slapos/scripts/init-slapos-node
echo "Add ${init_script}.sh as Windows startup item."
# if [[ ! -f ${init_script}.bat ]] ; then
#             cat <<EOF > ${init_script}.bat
# "$(cygpath -w /usr/bin/bash)" --login -i ${init_script}.sh
# EXIT 0
# EOF
# fi
246 247

regtool -q set "$run_key\\$slapos_run_entry" \
Jondy Zhao's avatar
Jondy Zhao committed
248
  "\"$(cygpath -w /usr/bin/bash)\" --login -i ${init_script}.sh" || \
249
echo "Add startup item failed."
Jondy Zhao's avatar
Jondy Zhao committed
250 251

exit 0