summaryrefslogtreecommitdiffstats
path: root/tools/deploy.sh
blob: 9bf18998f1497ae972798ac89c537c34103204d7 (plain)
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
#!/bin/bash

set -ex

export OS_USERNAME=${OS_USERNAME:-shipyard}
export OS_PASSWORD=${OS_PASSWORD:-password123}

export IPMI_USER=${IPMI_USER:-root}
export IPMI_PASS=${IPMI_PASS:-root}

export GERRIT_REFSPEC=${GERRIT_REFSPEC:-master}

export TERM_OPTS=${TERM_OPTS:-" "}

## Source Environment Variables.

help() {
  echo "Usage: deploy.sh <site_name> <deploy_site|update_site|update_software>"
}

if [[ $# -lt 2 ]]
  then
    help
    exit 1
fi

source $(dirname "$(realpath $0)")/../site/$1/$1.env

if [ -z "$AS_HOME" ]; then
  WORK_DIR=$(mktemp -d)
  trap "{ sudo rm -rf $WORK_DIR; }" EXIT
else
  WORK_DIR=$AS_HOME
fi

cd ${WORK_DIR}

AIRSHIP_CMD=treasuremap/tools/airship

## Deps

pkg_check() {
  for pkg in $@; do
    sudo dpkg -s $pkg &> /dev/null || sudo apt -y install $pkg
  done
}

pkg_check docker.io git ipmitool python3-yaml


## Cleanup

genesis_cleanup() {

  # reset bare-metal servers
  ALL_NODES="${GEN_IPMI} ${NODES_IPMI}"
  for node in $ALL_NODES; do
    ipmitool -I lanplus -H $node -U $IPMI_USER -P $IPMI_PASS chassis power off
  done

  sleep 11 # wait for genesis node to power-off
  ipmitool -I lanplus -H $GEN_IPMI -U $IPMI_USER -P $IPMI_PASS chassis power on

  while ! ssh $GEN_SSH hostname; do :; done

  scp $WORK_DIR/airship/tools/clean-genesis.sh $GEN_SSH:
  ssh $GEN_SSH chmod a+x clean-genesis.sh
  ssh $GEN_SSH sudo ./clean-genesis.sh -fk 
}


## Repos

read_yaml() {
  python3 -c "import yaml;print(yaml.load(open('$1'))$2)"
}

git_checkout() {
  git clone $1
  cd ${1##*/}

  # check refs for patchsets
  if [[ $2 == *"refs"* ]]; then
    git fetch origin $2
    git checkout FETCH_HEAD
  else
    git checkout $2
  fi

  git log -1
  cd ..
}

clone_repos() {
  if [ -d "airship" ]; then
    echo "Found existing airship folder. Skip repo clone."
  else
    # clone/checkout site manifests
    git_checkout 'https://gerrit.opnfv.org/gerrit/airship' $GERRIT_REFSPEC
  fi

  if [ -d "treasuremap" ]; then
    echo "Found existing treasuremap folder in the working directory. Skip repo clone."
  else
    # clone treasuremap (only required for tools/airship)
    # match treasuremap to global from site-definition
    SITE_DEF_KEY="['data']['repositories']['global']['revision']"
    TREASUREMAP_REF=$(read_yaml $SITE_DEF "$SITE_DEF_KEY")
    echo "TREASUREMAP_REF $TREASUREMAP_REF"
    git_checkout 'https://review.opendev.org/airship/treasuremap' $TREASUREMAP_REF
  fi
}

## Deployment

pegleg_collect() {
  if [ -d "collect/${SITE_NAME}" ]; then
    sudo rm -rf collect/${SITE_NAME}
  fi
  sudo mkdir -p collect/${SITE_NAME}
  sudo -E ${AIRSHIP_CMD} pegleg site -e global=treasuremap -r /target/airship collect -s collect/${SITE_NAME} $SITE_NAME

  sudo mkdir -p render/${SITE_NAME}
  sudo -E ${AIRSHIP_CMD} pegleg site -e global=treasuremap -r /target/airship render $SITE_NAME \
    -s /target/render/${SITE_NAME}/manifest.yaml
}

pre_genesis() {
  
  scp $WORK_DIR/airship/tools/files/seccomp_default $GEN_SSH:
  ssh $GEN_SSH 'sudo mkdir -p /var/lib/kubelet/seccomp'
  ssh $GEN_SSH 'sudo chown root:root /var/lib/kubelet/seccomp'
  ssh $GEN_SSH 'sudo chown root:root ~/seccomp_default'
  ssh $GEN_SSH 'sudo mv ~/seccomp_default /var/lib/kubelet/seccomp'

  scp $WORK_DIR/airship/tools/files/sources.list $GEN_SSH:

  ssh $GEN_SSH 'sudo cp -n /etc/apt/sources.list /etc/apt/sources.list.orig'
  ssh $GEN_SSH 'sudo chown root:root ~/sources.list'
  ssh $GEN_SSH 'sudo mv ~/sources.list /etc/apt/sources.list'

  ssh $GEN_SSH 'wget -qO - http://mirror.mirantis.com/testing/kubernetes-extra/bionic/archive-kubernetes-extra.key | sudo apt-key add -'
  # thsi fails but appaerntly not required.
  # ssh $GEN_SSH 'wget -qO - http://linux.dell.com/repo/community/openmanage/930/bionic/dists/bionic/Release.gpg | sudo apt-key add -'
  ssh $GEN_SSH 'sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32'
  ssh $GEN_SSH 'sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1285491434D8786F'

  if [ -d "render/${SITE_NAME}" ]; then
    sudo rm -rf render/${SITE_NAME}
  fi

  ssh $GEN_SSH 'sudo cp /etc/default/grub /etc/default/grub.orig'
  ssh $GEN_SSH 'sudo sed -i "/GRUB_CMDLINE_LINUX=\"/c GRUB_CMDLINE_LINUX=\"hugepagesz=1G hugepages=12 transparent_hugepage=never default_hugepagesz=1G dpdk-socket-mem=4096,4096 iommu=pt intel_iommu=on amd_iommu=on cgroup_disable=hugetlb console=ttyS1,115200n8\"" /etc/default/grub'
  ssh $GEN_SSH 'sudo update-grub'

  # upstream pre-geneis is not ready to be used directly yet
  # sudo mkdir -p render/${SITE_NAME}
  # sudo -E ${AIRSHIP_CMD} pegleg site -r /target/treasuremap render $SITE_NAME \
  #    -s /target/render/${SITE_NAME}/manifest.yaml
  # sudo -E treasuremap/tools/genesis-setup/pre-genesis.sh render/${SITE_NAME}/manifest.yaml
}

generate_certs() {
  # create certificates based on PKI catalogs

  if [ -d "certs/${SITE_NAME}" ]; then
    sudo rm -rf certs/${SITE_NAME}
  fi

  sudo mkdir -p certs/${SITE_NAME}

  # remove old certificates before collect
  sudo rm -f airship/site/${SITE_NAME}/secrets/certificates/certificates.yaml

  pegleg_collect

  sudo -E ${AIRSHIP_CMD} promenade generate-certs -o /target/certs/${SITE_NAME} collect/${SITE_NAME}/*.yaml

  # copy certs
  mkdir -p airship/site/${SITE_NAME}/secrets/certificates
  sudo cp certs/${SITE_NAME}/certificates.yaml \
    airship/site/${SITE_NAME}/secrets/certificates/certificates.yaml
}

promenade_bundle() {

  if [ -d "bundle/${SITE_NAME}" ]; then
    sudo rm -rf bundle/${SITE_NAME}
  fi
  sudo mkdir -p bundle/${SITE_NAME}

  PROMENADE_KEY=$(sudo -E ${AIRSHIP_CMD} promenade build-all \
    --validators -o /target/bundle/${SITE_NAME} /target/collect/${SITE_NAME}/*.yaml | \
    sed -n '/Copy this decryption key for use during script execution:/{n;p;d;}; x')
}

genesis_deploy() {
  scp bundle/${SITE_NAME}/genesis.sh $GEN_SSH:
  ssh $GEN_SSH PROMENADE_ENCRYPTION_KEY=$PROMENADE_KEY sudo -E ./genesis.sh
}

site_action() {

  # Site deployment with Shipyard, see more details here
  # https://airship-treasuremap.readthedocs.io/en/latest/authoring_and_deployment.html#deploy-site-with-shipyard

  sudo -E ${AIRSHIP_CMD} shipyard create configdocs \
    $SITE_NAME --directory=/target/collect/$SITE_NAME --replace
  sudo -E ${AIRSHIP_CMD} shipyard commit configdocs

  sudo -E ${AIRSHIP_CMD} shipyard create action \
    --allow-intermediate-commits $1

  sudo -E treasuremap/tools/gate/wait-for-shipyard.sh
}

shipyard_action() {

  # Site deployment with Shipyard, see more details here
  # https://airship-treasuremap.readthedocs.io/en/latest/authoring_and_deployment.html#deploy-site-with-shipyard

  sudo -E ${AIRSHIP_CMD} shipyard $1 $2 $3
}


create_public_network() {
  export OS_AUTH_URL=${OS_AUTH_URL_IDENTITY}
  sudo -E treasuremap/tools/openstack stack create --wait \
    -t /target/airship/tools/files/heat-public-net-deployment-$SITE_NAME.yaml \
    public-network
}


create_dpdk_flavor() {
  export OS_AUTH_URL=${OS_AUTH_URL_IDENTITY}
  sudo -E treasuremap/tools/openstack flavor create --ram 1024 --disk 1 --vcpus 1 --public --property hw:mem_page_size=large dpdk.tiny
}

create_cirros_image() {
  export OS_AUTH_URL=${OS_AUTH_URL_IDENTITY}
  wget http://download.cirros-cloud.net/0.5.1/cirros-0.5.1-x86_64-disk.img
  sudo -E treasuremap/tools/openstack image create --disk-format qcow2 --container-format bare --public --file /target/cirros-0.5.1-x86_64-disk.img cirros
  rm -rf cirros-0.5.1-x86_64-disk.img cirros
}

case "$2" in
'pre_genesis')
  pre_genesis
  ;;
'deploy_site')
  read -n 1 -p "This script will clean up the genesis node. Continue (Y/N) ?" input
  case $input in
    [Yy] ) break;;
    [Nn] ) exit 1;;
      * ) echo "Please answer yes or no."; exit 1;
  esac

  clone_repos
  pegleg_collect
  promenade_bundle
  genesis_cleanup
  pre_genesis
  genesis_deploy
  site_action $2
#  create_public_network
  ;;
'update_site')
  clone_repos
  pegleg_collect
  site_action $2
  ;;
'update_software')
  clone_repos
  pegleg_collect
  site_action $2
  ;;
'init_cloud')
#  wget http://download.cirros-cloud.net/0.5.1/cirros-0.5.1-x86_64-disk.img
#  openstack image create --disk-format qcow2 --container-format bare --public --file /target/cirros-0.5.1-x86_64-disk.img cirros
#  rm -rf cirros-0.5.1-x86_64-disk.img cirros
#  openstack flavor create --ram 1024 --disk 1 --vcpus 1 --public --hw:mem_page_size=large dpdk.tiny
  create_cirros_image
  create_dpdk_flavor
  create_public_network
  ;;
'generate_certs')
  clone_repos
  generate_certs
  ;;
'shipyard')
  shipyard_action $3 $4 $5
  ;;
*) help
   echo "*** $2"
   exit 1
  ;;
esac