Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 1 | # -*- mode: ruby -*- |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | host_ip = "192.168.121.1" |
| 5 | operator_key = "${HOME}/.ssh/onap-key" |
| 6 | vagrant_user = "vagrant" |
| 7 | vagrant_password = "vagrant" |
| 8 | synced_folder_main = "/vagrant" |
| 9 | synced_folder_config = "#{synced_folder_main}/config" |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 10 | synced_folder_tools_config = "#{synced_folder_main}/tools/config" |
Pawel Wieczorek | df0edea | 2019-11-29 16:18:36 +0100 | [diff] [blame] | 11 | os_config = "#{synced_folder_config}/local.conf" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 12 | cluster_yml = "cluster.yml" |
| 13 | apt_prefs_dir = "/etc/apt/apt.conf.d" |
| 14 | apt_prefs = "95silent-approval" |
| 15 | |
Pawel Wieczorek | 3cb7821 | 2019-12-09 16:00:47 +0100 | [diff] [blame] | 16 | vm_memory = 1 * 1024 |
| 17 | vm_memory_os = 6 * 1024 |
| 18 | vm_memory_onap = 12 * 1024 |
| 19 | vm_cpu = 1 |
| 20 | vm_cpus = 2 |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 21 | vm_box = "generic/ubuntu1804" |
| 22 | |
Pawel Wieczorek | 3cb7821 | 2019-12-09 16:00:47 +0100 | [diff] [blame] | 23 | operation = { name: 'operator', hostname: 'operator', ip: '172.17.4.254', cpus: vm_cpu, memory: vm_memory } |
| 24 | devstack = { name: 'devstack', hostname: 'devstack', ip: '172.17.4.200', cpus: vm_cpus, memory: vm_memory_os } |
| 25 | control = { name: 'control', hostname: 'control', ip: '172.17.4.100', cpus: vm_cpu, memory: vm_memory } |
| 26 | worker = { name: 'worker', hostname: 'worker', ip: '172.17.4.101', cpus: vm_cpus, memory: vm_memory_onap } |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 27 | |
Pawel Wieczorek | 3cb7821 | 2019-12-09 16:00:47 +0100 | [diff] [blame] | 28 | cluster = [] << control << worker |
Pawel Wieczorek | df0edea | 2019-11-29 16:18:36 +0100 | [diff] [blame] | 29 | all = cluster.dup << operation << devstack |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 30 | |
| 31 | operation_post_msg = "Run: \"vagrant provision #{operation[:name]} --provision-with=rke_up,setup_kubectl\" to complete cluster creation" |
| 32 | |
| 33 | $replace_dns = <<-SCRIPT |
| 34 | HOST_IP="$1" |
| 35 | rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved |
| 36 | echo nameserver "$HOST_IP" | tee /etc/resolv.conf |
| 37 | SCRIPT |
| 38 | |
Pawel Wieczorek | df0edea | 2019-11-29 16:18:36 +0100 | [diff] [blame] | 39 | $enable_ipv6 = <<-SCRIPT |
| 40 | sed -i'' 's/net.ipv6.conf.all.disable_ipv6.*$/net.ipv6.conf.all.disable_ipv6 = 0/' /etc/sysctl.conf |
| 41 | sysctl -p |
| 42 | SCRIPT |
| 43 | |
| 44 | $setup_devstack = <<-SCRIPT |
| 45 | CONFIG="$1" |
| 46 | git clone https://opendev.org/openstack/devstack |
| 47 | cd devstack |
| 48 | cp "$CONFIG" . |
| 49 | ./stack.sh |
| 50 | SCRIPT |
| 51 | |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 52 | $add_to_docker_group = <<-SCRIPT |
| 53 | USER="$1" |
| 54 | echo "Adding ${USER} to 'docker' group" |
| 55 | usermod -aG docker "$USER" |
| 56 | SCRIPT |
| 57 | |
| 58 | $setup_debconf = <<-SCRIPT |
| 59 | echo "Setting debconf frontend to noninteractive" |
| 60 | sed -i'.orig' '/^Config:/a Frontend: noninteractive' /etc/debconf.conf |
| 61 | SCRIPT |
| 62 | |
| 63 | $install_sshpass = <<-SCRIPT |
| 64 | apt-get update |
| 65 | echo "Installing 'sshpass'" |
| 66 | apt-get install sshpass |
| 67 | SCRIPT |
| 68 | |
Pawel Wieczorek | 0664ac4 | 2019-12-09 16:16:48 +0100 | [diff] [blame^] | 69 | $install_make = <<-SCRIPT |
| 70 | apt-get update |
| 71 | echo "Installing 'make'" |
| 72 | apt-get install make |
| 73 | SCRIPT |
| 74 | |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 75 | $generate_key = <<-SCRIPT |
| 76 | KEY_FILE="$1" |
| 77 | echo "Generating SSH key (${KEY_FILE})" |
| 78 | ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N "" |
| 79 | SCRIPT |
| 80 | |
| 81 | $deploy_key = <<-SCRIPT |
| 82 | KEY="$1" |
| 83 | USER="$2" |
| 84 | PASS="$PASSWORD" |
| 85 | IPS="$3" |
| 86 | echo "Deploying ${KEY} for ${USER}" |
| 87 | for ip in $IPS; do |
| 88 | echo "on ${ip}" |
| 89 | sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}" |
| 90 | done |
| 91 | SCRIPT |
| 92 | |
| 93 | $link_dotfiles = <<-SCRIPT |
| 94 | SYNC_DIR="$1" |
| 95 | for rc in ${SYNC_DIR}/dot_*; do |
| 96 | src="$rc" |
| 97 | dst="${HOME}/.${rc##*dot_}" |
| 98 | echo "Symlinking ${src} to ${dst}" |
| 99 | ln -sf "$src" "$dst" |
| 100 | done |
| 101 | SCRIPT |
| 102 | |
| 103 | $link_file = <<-SCRIPT |
| 104 | SYNC_DIR="$1" |
| 105 | FILE="$2" |
| 106 | src="${SYNC_DIR}/${FILE}" |
| 107 | dst="$3" |
| 108 | echo "Symlinking ${src} to ${dst}" |
| 109 | ln -sf "$src" "$dst" |
| 110 | SCRIPT |
| 111 | |
| 112 | $rke_up = "rke up" |
| 113 | $rke_down = "rke remove --force" |
| 114 | |
Pawel Wieczorek | f1176da | 2019-12-05 13:45:45 +0100 | [diff] [blame] | 115 | $get_oom = <<-SCRIPT |
| 116 | BRANCH="${1:-5.0.1-ONAP}" |
| 117 | git clone -b "$BRANCH" https://git.onap.org/oom --recurse-submodules |
| 118 | SCRIPT |
| 119 | |
| 120 | $get_helm_plugins = "cp -R ${HOME}/oom/kubernetes/helm/plugins/ ${HOME}/.helm" |
| 121 | |
Pawel Wieczorek | 0664ac4 | 2019-12-09 16:16:48 +0100 | [diff] [blame^] | 122 | $setup_helm_cluster = <<-SCRIPT |
| 123 | export KUBECONFIG="${HOME}/.kube/config.onap" |
| 124 | kubectl config use-context onap |
| 125 | kubectl -n kube-system create serviceaccount tiller |
| 126 | kubectl create clusterrolebinding tiller --clusterrole=cluster-admin --serviceaccount=kube-system:tiller |
| 127 | helm init --service-account tiller |
| 128 | kubectl -n kube-system rollout status deploy/tiller-deploy |
| 129 | SCRIPT |
| 130 | |
| 131 | # FIXME: replace sleep command with helm repo readiness probe |
| 132 | $setup_helm_repo = <<-SCRIPT |
| 133 | helm serve & |
| 134 | sleep 3 |
| 135 | helm repo add local http://127.0.0.1:8879 |
| 136 | make -C ${HOME}/oom/kubernetes all |
| 137 | make -C ${HOME}/oom/kubernetes onap |
| 138 | SCRIPT |
| 139 | |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 140 | Vagrant.configure('2') do |config| |
| 141 | all.each do |machine| |
| 142 | config.vm.define machine[:name] do |config| |
| 143 | config.vm.box = vm_box |
| 144 | config.vm.hostname = machine[:hostname] |
| 145 | |
| 146 | config.vm.provider :virtualbox do |v| |
| 147 | v.name = machine[:name] |
Pawel Wieczorek | 3cb7821 | 2019-12-09 16:00:47 +0100 | [diff] [blame] | 148 | v.memory = machine[:memory] |
| 149 | v.cpus = machine[:cpus] |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 150 | end |
| 151 | |
| 152 | config.vm.provider :libvirt do |v| |
Pawel Wieczorek | 3cb7821 | 2019-12-09 16:00:47 +0100 | [diff] [blame] | 153 | v.memory = machine[:memory] |
| 154 | v.cpus = machine[:cpus] |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 155 | end |
| 156 | |
| 157 | config.vm.network :private_network, ip: machine[:ip] |
| 158 | config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip |
| 159 | |
Pawel Wieczorek | df0edea | 2019-11-29 16:18:36 +0100 | [diff] [blame] | 160 | if machine[:name] == 'devstack' |
| 161 | config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile" |
| 162 | |
| 163 | config.vm.provision "enable_ipv6", type: :shell, run: "always", inline: $enable_ipv6 |
| 164 | config.vm.provision "setup_devstack", type: :shell, privileged: false, inline: $setup_devstack, args: os_config |
| 165 | end |
| 166 | |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 167 | if machine[:name] == 'control' |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 168 | config.vm.provision "customize_control", type: :shell, path: "tools/imported/openstack-k8s-controlnode.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 169 | config.vm.provision "fix_groups_control", type: :shell, inline: $add_to_docker_group, args: vagrant_user |
| 170 | end |
| 171 | |
| 172 | if machine[:name] == 'worker' |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 173 | config.vm.provision "customize_worker", type: :shell, path: "tools/imported/openstack-k8s-workernode.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 174 | config.vm.provision "fix_group_worker", type: :shell, inline: $add_to_docker_group, args: vagrant_user |
| 175 | end |
| 176 | |
| 177 | if machine[:name] == 'operator' |
| 178 | config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 179 | |
| 180 | config.vm.provision "setup_debconf", type: :shell, inline: $setup_debconf |
| 181 | config.vm.provision "link_apt_prefs", type: :shell, run: "always" do |s| |
| 182 | s.inline = $link_file |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 183 | s.args = [synced_folder_tools_config, apt_prefs, apt_prefs_dir] |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 184 | end |
| 185 | config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s| |
| 186 | s.inline = $link_dotfiles |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 187 | s.args = synced_folder_tools_config |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 188 | end |
| 189 | config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s| |
| 190 | s.privileged = false |
| 191 | s.inline = $link_dotfiles |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 192 | s.args = synced_folder_tools_config |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 193 | end |
| 194 | |
| 195 | config.vm.provision "install_sshpass", type: :shell, inline: $install_sshpass |
| 196 | config.vm.provision "generate_key", type: :shell, privileged: false, inline: $generate_key, args: operator_key |
| 197 | |
| 198 | ips = "" |
| 199 | cluster.each { |node| ips << node[:ip] << " " } |
| 200 | config.vm.provision "deploy_key", type: :shell do |s| |
| 201 | s.privileged = false |
| 202 | s.inline = $deploy_key |
| 203 | s.args = [operator_key, vagrant_user, ips] |
| 204 | s.env = {'PASSWORD': vagrant_password} |
| 205 | end |
| 206 | |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 207 | config.vm.provision "get_rke", type: :shell, path: "tools/get_rke.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 208 | config.vm.provision "link_cluster_yml", type: :shell, run: "always" do |s| |
| 209 | s.privileged = false |
| 210 | s.inline = $link_file |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 211 | s.args = [synced_folder_config, cluster_yml, "$HOME"] |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 212 | end |
| 213 | |
| 214 | config.vm.post_up_message = operation_post_msg |
| 215 | config.vm.provision "rke_up", type: :shell, run: "never", privileged: false, inline: $rke_up |
| 216 | config.trigger.before :destroy do |trigger| |
| 217 | trigger.warn = "Removing cluster" |
| 218 | trigger.run_remote = {privileged: false, inline: $rke_down} |
| 219 | end |
| 220 | |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 221 | config.vm.provision "get_kubectl", type: :shell, path: "tools/get_kubectl.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 222 | config.vm.provision "setup_kubectl", type: :shell, run: "never" do |s| |
| 223 | s.privileged = false |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame] | 224 | s.path = "tools/setup_kubectl.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 225 | end |
Pawel Wieczorek | f1176da | 2019-12-05 13:45:45 +0100 | [diff] [blame] | 226 | config.vm.provision "get_helm", type: :shell, path: "tools/get_helm.sh" |
| 227 | config.vm.provision "get_oom", type: :shell, privileged: false, inline: $get_oom |
| 228 | config.vm.provision "get_helm_plugins", type: :shell, privileged: false, inline: $get_helm_plugins |
Pawel Wieczorek | 0664ac4 | 2019-12-09 16:16:48 +0100 | [diff] [blame^] | 229 | config.vm.provision "install_make", type: :shell, inline: $install_make |
| 230 | config.vm.provision "setup_helm_cluster", type: :shell, run: "never", privileged: false, inline: $setup_helm_cluster |
| 231 | config.vm.provision "setup_helm_repo", type: :shell, run: "never", privileged: false, inline: $setup_helm_repo |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 232 | end |
| 233 | end |
| 234 | end |
| 235 | end |