使用 Shell 脚本的 Vagrant 配置

问题描述 投票:0回答:1

我一直在尝试使用 Vagrant 配置和 shell 脚本自动执行一些配置更改,但不幸的是,当我运行 vagrant up 时,我的 shell 脚本将无法按预期运行。我已经测试过单独运行 shell 脚本并且它可以工作,但是使用 vagrant 配置我无法运行它。这是我的 shell 脚本:

#!/bin/bash

check_vagrant_installed() {
  if ! command -v vagrant &>/dev/null; then
    echo "Vagrant is not installed. Exiting."
    exit 1
  fi
  exit 0
}

check_vagrant_running() {
  if ! vagrant status | grep 'running' &>/dev/null; then
    echo "Vagrant box is not running. Starting the Box."
    vagrant up
    if [ $? -ne 0 ]; then
      echo "Failed to start the Vagrant box. Exiting."
      exit 1
    fi
  fi
  exit 0
}

provision_vm() {
  sudo yum install -y mod_ssl httpd || exit 1
}

create_hello_html() {
  sudo mkdir -p /apps/hello-http/html
  sudo sh -c 'cat <<EOF > /apps/hello-http/html/hello.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello, this is a sample HTML page!</h1>
</body>
</html>
EOF' || exit 1
}

update_apache_config() {
  sudo sed -i 's|/var/www/html|/apps/hello-http/html|g' /etc/httpd/conf/httpd.conf || exit 1
  sudo sed -i 's|/var/www|/apps/hello-http|g' /etc/httpd/conf/httpd.conf || exit 1
  sudo sed -i 's|index.html|hello.html|g' /etc/httpd/conf/httpd.conf || exit 1
  echo "Document root updated in Apache configuration."
}

validate_apache_config() {
  echo "Checking Apache configuration syntax..."
  sudo apachectl configtest || exit 1
}

change_selinux_context() {
  sudo chcon -R httpd_sys_content_t $documentRoot || exit 1
  sudo chcon -t httpd_sys_content_t $documentRoot/hello.html || exit 1
  echo "SELinux context changed successfully."
}

restart_apache_service() {
  local httpStatus
  httpStatus=$(sudo systemctl restart httpd && echo $?) || exit 1
  if [ "$httpStatus" -eq 0 ]; then
    echo "Apache service restarted successfully."
    echo "HTTP status code: 200"
  else
    echo "Failed to restart Apache service. HTTP status code: $httpStatus"
  fi
}

update_ssl_protocol() {
  sudo sed -i 's/SSLProtocol all -SSLv2 -SSLv3/SSLProtocol all -SSLv2 -SSLv3 -TLSv1/' "$sslconfigPath" || exit 1
}

update_error_log() {
  sudo mkdir -p /var/log/weblogs/http || exit 1
  sudo sed -i 's|logs/error_log|/var/log/weblogs/http/error_log|g' $apacheconfigPath || exit 1
  sudo sed -i 's|logs/access_log|/var/log/weblogs/http/access_log|g' $apacheconfigPath || exit 1
}

main() {
  check_vagrant_installed
  check_vagrant_running
  provision_vm
  create_hello_html
  update_apache_config
  validate_apache_config
  change_selinux_context
  restart_apache_service
  update_ssl_protocol
  update_error_log
  restart_apache_service
}

# Path to Apache's document root directory
documentRoot='/apps/hello-http/html'

# Path to Apache's SSL Configuration
sslconfigPath='/etc/httpd/conf.d/ssl.conf'

# Path to Apache Configuration
apacheconfigPath='/etc/httpd/conf/httpd.conf'

main
exit 0

这是我的流浪文件:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.
  #config.vm.provision "shell", path: "centos_VM/deployVM.sh"
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"
  config.vm.box_version = "2004.01"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = 2
  config.vm.provision :shell, path: ".vagrant/deployVM.sh"
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 443, host: 8443
  end

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Disable the default share of the current code directory. Doing this
  # provides improved isolation between the vagrant box and your host
  # by making sure your Vagrantfile isn't accessible to the vagrant box.
  # If you use this you may want to enable additional shared subfolders as
  # shown above.
  # config.vm.synced_folder ".", "/vagrant", disabled: true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

这是输出:

enter image description here

有人可以指导我如何使用 vagrant 配置来运行我的 shell 脚本吗?正在努力学习。

shell vagrant vagrant-provision
1个回答
0
投票

您试图检查 Vagrant 是否安装在 Vagrant 本身启动的虚拟机上,这是没有意义的。脚本总是会在那一点停止。脚本正在运行,但无需检查虚拟机内是否安装了 Vagrant。 Vagrant 应该安装在主机上。

删除 check_vagrant_installed() 和 check_vagrant_running() 函数,您的脚本将继续执行您定义的逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.