0

I've been trying to automate some configuration changes using Vagrant provisioning and shell scripts but unfortunately when I run vagrant up, my shell script will not run as expected. I've tested running the shell script by itself and it works but with vagrant provisioning I'm unable to run it. Here is my shell script:

#!/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

Here is my vagrant file:

# -*- 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

Here is the output:

enter image description here

Can someone please guide me on how can I use vagrant provisioning to run my shell script. Trying to learn about it.

1 Answer 1

0

You're trying to check if Vagrant is installed on the VM that Vagrant itself is starting, which doesn't make sense. The script will always stop at that point. The script is running, but there's no need to check if Vagrant is installed inside the VM. Vagrant should be installed on the host machine.

Remove the check_vagrant_installed() and check_vagrant_running() functions and your script will continue with the logic you've defined.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.