lin
2025-08-21 57113df3a0e2be01232281fad9a5f2c060567981
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
#!/bin/bash
#
# Copyright 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
 
HELP="This is a script to bootstrap a localhost shard cluster for testing.\n\
The following defaults are preconfigured but modifyable:\n\
  SHARD_NAME: Name of the shard to register with master\n\
  NUM_HOSTS_MASTER/SHARD: Number of hosts to add to the master/shard.\n\
  MASTER/SHARD_BOARD: Boards to add to the master/shard\n\
  POOL: Pool to use for the hosts."
 
 
# Invalidate (delete) the hosts/labels/shard instead of adding them.
# Typically used to refresh a botched cluster.
INVALIDATE_ALL=0
AT_DIR=/usr/local/autotest
 
# See VagrantFile for details on how these afes are setup.
AFE=localhost:8001
SHARD_NAME=localhost:8004
 
# Number of hosts on master and shard. They will
# get autoassigned generic names like test_hostX.
NUM_HOSTS_MASTER=10
NUM_HOSTS_SHARD=5
NUM_FREON_HOSTS_SHARD=5
 
# A host can only have a single board. Jobs are sent
# to the shard based on the board.
MASTER_BOARD=board:link
SHARD_BOARD=board:stumpy
SHARD_FREON_BOARD=board:stumpy_freon
 
# All hosts need to be in a pool.
POOL=pool:bot
 
y_n_prompt() {
  read -r -p "Are you sure? [y/N] " response
  if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
    return 0
  else
    return 1
  fi
}
 
while getopts ":h" opt; do
  case $opt in
    h)
      echo -e "${HELP}" >&2
      exit 0
      ;;
  esac
done
 
atest_hosts() {
  hosts=("${!1}")
  labels="${2}"
  hostnames=''
  for H in ${hosts[*]}; do
    if [ "$hostnames" ]; then
      hostnames="$hostnames,$H"
    else
      hostnames=$H
    fi
  done
  if [ $INVALIDATE_ALL -eq 1 ]; then
    $AT_DIR/cli/atest host delete $hostnames --web $AFE
    $AT_DIR/cli/atest label delete $labels --web $AFE
  else
    $AT_DIR/cli/atest host create $hostnames --web $AFE
    $AT_DIR/cli/atest label add -m $hostnames $labels --web $AFE
  fi
}
 
MASTER_HOSTS=()
s=1
e=$NUM_HOSTS_MASTER
for i in $(seq $s $e); do
  MASTER_HOSTS[$i]=test_host$i;
done
 
SHARD_HOSTS=()
s=$(($e+1))
e=$(($NUM_HOSTS_SHARD+$e))
for i in $(seq $s $e); do
  SHARD_HOSTS[$i]=test_host$i;
done
 
SHARD_FREON_HOSTS=()
s=$(($e+1))
e=$(($NUM_FREON_HOSTS_SHARD+$e))
for i in $(seq $s $e); do
  SHARD_FREON_HOSTS[$i]=test_host$i;
done
 
operation='Adding: '
if [ $INVALIDATE_ALL -eq 1 ]; then
  operation='Removing '
fi
 
printf '%s following hosts to master \n' $operation
echo ${MASTER_HOSTS[*]}
if $(y_n_prompt); then
  atest_hosts MASTER_HOSTS[*] $POOL,$MASTER_BOARD
fi
 
printf '\n\n%s following hosts to shard \n' $operation
echo ${SHARD_HOSTS[*]}
if $(y_n_prompt); then
  atest_hosts SHARD_HOSTS[*] $POOL,$SHARD_BOARD
fi
 
printf '\n\n%s following hosts to shard \n' $operation
echo ${SHARD_FREON_HOSTS[*]}
if $(y_n_prompt); then
    atest_hosts SHARD_FREON_HOSTS[*] $POOL,$SHARD_FREON_BOARD
fi
 
printf '\n\n%s shard \n' $operation
echo $SHARD_NAME
if $(y_n_prompt); then
  if [ $INVALIDATE_ALL -eq 1 ]; then
    $AT_DIR/cli/atest shard delete $SHARD_NAME --web $AFE
  else
    $AT_DIR/cli/atest shard create $SHARD_NAME -l $SHARD_BOARD --web $AFE
  fi
fi