|
| 1 | +# |
| 2 | +# Copyright (c) 2023 Oracle and/or its affiliates. |
| 3 | +# Licensed under the Universal Permissive License v 1.0 as shown at |
| 4 | +# https://oss.oracle.com/licenses/upl. |
| 5 | +# |
| 6 | +# Since: July, 2018 |
| 7 | + |
| 8 | +# Description: Creates an Oracle database Vagrant virtual machine. |
| 9 | +# Optional plugins: |
| 10 | +# vagrant-env (use .env files for configuration) |
| 11 | +# vagrant-proxyconf (if you don't have direct access to the Internet) |
| 12 | +# see https://github.com/tmatilai/vagrant-proxyconf for configuration |
| 13 | +# |
| 14 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 15 | +# |
| 16 | + |
| 17 | +# -*- mode: ruby -*- |
| 18 | +# vi: set ft=ruby : |
| 19 | + |
| 20 | +# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! |
| 21 | +VAGRANTFILE_API_VERSION = "2" |
| 22 | + |
| 23 | +# Box metadata location and box name |
| 24 | +BOX_URL = "https://oracle.github.io/vagrant-projects/boxes" |
| 25 | +BOX_NAME = "oraclelinux/8" |
| 26 | + |
| 27 | +# UI object for printing information |
| 28 | +ui = Vagrant::UI::Prefixed.new(Vagrant::UI::Colored.new, "vagrant") |
| 29 | + |
| 30 | +# Define constants |
| 31 | +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| |
| 32 | + # Use vagrant-env plugin if available |
| 33 | + if Vagrant.has_plugin?("vagrant-env") |
| 34 | + config.env.load(".env.local", ".env") # enable the plugin |
| 35 | + end |
| 36 | + |
| 37 | + # VM name |
| 38 | + VM_NAME = default_s('VM_NAME', 'oracle23c-free-vagrant') |
| 39 | + |
| 40 | + # Memory for the VM (in MB, 2300 MB is ~2.25 GB) |
| 41 | + VM_MEMORY = default_i('VM_MEMORY', 2300) |
| 42 | + |
| 43 | + # VM time zone |
| 44 | + # If not specified, will be set to match host time zone (if possible) |
| 45 | + VM_SYSTEM_TIMEZONE = default_s('VM_SYSTEM_TIMEZONE', host_tz) |
| 46 | + |
| 47 | + # Save database installer RPM file for reuse when VM is rebuilt |
| 48 | + VM_KEEP_DB_INSTALLER = default_b('VM_KEEP_DB_INSTALLER', false) |
| 49 | + |
| 50 | + # Database character set |
| 51 | + VM_ORACLE_CHARACTERSET = default_s('VM_ORACLE_CHARACTERSET', 'AL32UTF8') |
| 52 | + |
| 53 | + # Listener port |
| 54 | + VM_LISTENER_PORT = default_i('VM_LISTENER_PORT', 1521) |
| 55 | + |
| 56 | + # Oracle Database password for SYS, SYSTEM and PDBADMIN accounts |
| 57 | + # If left blank, the password will be generated automatically |
| 58 | + VM_ORACLE_PWD = default_s('VM_ORACLE_PWD', '') |
| 59 | +end |
| 60 | + |
| 61 | +# Convenience methods |
| 62 | +def default_s(key, default) |
| 63 | + ENV[key] && ! ENV[key].empty? ? ENV[key] : default |
| 64 | +end |
| 65 | + |
| 66 | +def default_i(key, default) |
| 67 | + default_s(key, default).to_i |
| 68 | +end |
| 69 | + |
| 70 | +def default_b(key, default) |
| 71 | + default_s(key, default).to_s.downcase == "true" |
| 72 | +end |
| 73 | + |
| 74 | +def host_tz |
| 75 | + # get host time zone for setting VM time zone |
| 76 | + # if host time zone isn't an integer hour offset from GMT, fall back to UTC |
| 77 | + offset_sec = Time.now.gmt_offset |
| 78 | + if (offset_sec % (60 * 60)) == 0 |
| 79 | + offset_hr = ((offset_sec / 60) / 60) |
| 80 | + timezone_suffix = offset_hr >= 0 ? "-#{offset_hr.to_s}" : "+#{(-offset_hr).to_s}" |
| 81 | + 'Etc/GMT' + timezone_suffix |
| 82 | + else |
| 83 | + 'UTC' |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| |
| 88 | + config.vm.box = BOX_NAME |
| 89 | + config.vm.box_url = "#{BOX_URL}/#{BOX_NAME}.json" |
| 90 | + config.vm.define VM_NAME |
| 91 | + |
| 92 | + # Provider-specific configuration |
| 93 | + config.vm.provider "virtualbox" do |v| |
| 94 | + v.memory = VM_MEMORY |
| 95 | + v.name = VM_NAME |
| 96 | + end |
| 97 | + config.vm.provider :libvirt do |v| |
| 98 | + v.memory = VM_MEMORY |
| 99 | + end |
| 100 | + |
| 101 | + # add proxy configuration from host env - optional |
| 102 | + if Vagrant.has_plugin?("vagrant-proxyconf") |
| 103 | + ui.info "Getting Proxy Configuration from Host..." |
| 104 | + has_proxy = false |
| 105 | + ["http_proxy", "HTTP_PROXY"].each do |proxy_var| |
| 106 | + if proxy = ENV[proxy_var] |
| 107 | + ui.info "HTTP proxy: " + proxy |
| 108 | + config.proxy.http = proxy |
| 109 | + has_proxy = true |
| 110 | + break |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + ["https_proxy", "HTTPS_PROXY"].each do |proxy_var| |
| 115 | + if proxy = ENV[proxy_var] |
| 116 | + ui.info "HTTPS proxy: " + proxy |
| 117 | + config.proxy.https = proxy |
| 118 | + has_proxy = true |
| 119 | + break |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + if has_proxy |
| 124 | + # Only consider no_proxy if we have proxies defined. |
| 125 | + no_proxy = "" |
| 126 | + ["no_proxy", "NO_PROXY"].each do |proxy_var| |
| 127 | + if ENV[proxy_var] |
| 128 | + no_proxy = ENV[proxy_var] |
| 129 | + ui.info "No proxy: " + no_proxy |
| 130 | + no_proxy += "," |
| 131 | + break |
| 132 | + end |
| 133 | + end |
| 134 | + config.proxy.no_proxy = no_proxy + "localhost,127.0.0.1" |
| 135 | + end |
| 136 | + else |
| 137 | + ["http_proxy", "HTTP_PROXY", "https_proxy", "HTTPS_PROXY"].each do |proxy_var| |
| 138 | + if ENV[proxy_var] |
| 139 | + ui.warn 'To enable proxies in your VM, install the vagrant-proxyconf plugin' |
| 140 | + break |
| 141 | + end |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + # VM hostname |
| 146 | + # must be "localhost", or listener configuration will fail |
| 147 | + config.vm.hostname = "localhost" |
| 148 | + |
| 149 | + # Oracle port forwarding |
| 150 | + config.vm.network "forwarded_port", guest: VM_LISTENER_PORT, host: VM_LISTENER_PORT |
| 151 | + |
| 152 | + # Provision everything on the first run |
| 153 | + config.vm.provision "shell", path: "scripts/install.sh", env: |
| 154 | + { |
| 155 | + "SYSTEM_TIMEZONE" => VM_SYSTEM_TIMEZONE, |
| 156 | + "KEEP_DB_INSTALLER" => VM_KEEP_DB_INSTALLER, |
| 157 | + "ORACLE_CHARACTERSET" => VM_ORACLE_CHARACTERSET, |
| 158 | + "LISTENER_PORT" => VM_LISTENER_PORT, |
| 159 | + "ORACLE_PWD" => VM_ORACLE_PWD |
| 160 | + } |
| 161 | + |
| 162 | +end |
0 commit comments