{
  "AWSTemplateFormatVersion" : "2010-09-09",


  "Description" : "PG-Oracle-FDW-V1.9: Bastian Host, Oracle XE, RDS PostgreSQL to test Oracle_FDW",

  "Metadata"  : {
    "Author"      : "Mike Revitt - Database Migration Specialist",
    "Department"  : "Database Migration Acceleration (DMA)",
    "Company"     : "Amazon WEB Services",
    "Version"     : "1.9",
    "ModifiedOn"  : "17-Dec-2021",
    "Copyright"   : "Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: MIT-0"
  },

  "Parameters": {
    "EC2KeyName": {
      "Description"           : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
      "Type"                  : "AWS::EC2::KeyPair::KeyName",
      "Default"               : "",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
    },

    "KeyLocation": {
      "Description"           : "To enable ssh and scp from the Bastian Host to the Database Server you have to install the EC2 KeyPair onto the Bastian host. To do this you need to copy the EC2 KeyPair to S3 and specify the https address for the location of the EC2 KeyPair in S3.",
      "Type"                  : "String",
      "Default"               : "https://s3.eu-west-2.amazonaws.com/MikeKeyPair.pem"
    },

    "EC2Password": {
      "Description"           : "The Linux Host account password",
      "Type"                  : "String",
      "Default"               : "ChangeMe-No-Really!!",
      "NoEcho"                : "true",
      "MinLength"             : "8"
    },

    "DbIdentifier": {
      "Description"           : "The RDS database identifier, this forms the DNS name and must be unique across the account",
      "Type"                  : "String",
      "Default"               : "postgres-oracle-fdw",
      "AllowedPattern"        : "[a-zA-Z][a-zA-Z_\\-0-9]*",
      "ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters and a hyphen."
    },

    "DBPassword": {
      "Description"           : "The database admin password for the database we are creating",
      "Type"                  : "String",
      "Default"               : "ChangeMe-No-Really!!",
      "NoEcho"                : "true",
      "MinLength"             : "4"
    },

    "PublicSecurityGroupId": {
      "Description"           : "The ID of an existing security group allowing for SSH and RDP access to the bastian host from your LAN or VPN.",
      "Type"                  : "AWS::EC2::SecurityGroup::Id",
      "ConstraintDescription" : "must be the ID of an existing Security Group that allows port 22 and 3389 access."
    },

    "SshSecurityGroupId": {
      "Description"           : "The ID of an existing security group allowing for SSH access to the Oracle Database host from the PublicSecurityGroup.",
      "Type"                  : "AWS::EC2::SecurityGroup::Id",
      "ConstraintDescription" : "must be the ID of an existing Security Group that allows port 22 access."
    },

    "DataBaseSecurityGroupId": {
      "Description"           : "The ID of an existing Security Group that allows database access, ports 1521 and 5432, from the SshSecurityGroup.",
      "Type"                  : "AWS::EC2::SecurityGroup::Id",
      "ConstraintDescription" : "must be the ID of an existing Security Group that allows port 5432 and 1521 access."
    },

    "PublicSubnetId": {
      "Description"           : "The ID of the SubNet where the Bastian Host will reside and to which the PublicSecurityGroup will be attached",
      "Type"                  : "AWS::EC2::Subnet::Id",
      "ConstraintDescription" : "must be the ID of an existing Subnet, to which the PublicSecurityGroup provides access"
    },

    "DatabaseSubnetId": {
      "Description"           : "The ID of a SubNet where the Oracle Database Host will reside and to which the SshSecurityGroupId and DataBaseSecurityGroupId will be attached.",
      "Type"                  : "AWS::EC2::Subnet::Id",
      "ConstraintDescription" : "must be the ID of an existing Subnet, to which the DataBaseSecurityGroup and SshSecurityGroup provides access"
    },

    "RdsSubnetGroupName": {
      "Description"           : "The name of the database subnet group where the RDS PostgreSQL Database will reside and to which the SshSecurityGroupId and DataBaseSecurityGroupId will be attached.",
      "Type"                  : "String"
    },

    "DbfSchedule": {
      "Description"           : "Optional schedule to use for auto shutdown of the instances, creates a tag entry for the Job scheduler",
      "Type"                  : "String",
      "Default"               : "uk-office-hours"
    },

    "BastianInstanceType": {
      "Description"           :  "AMZN Bastian Host Type, Note that the default is the free tier instance size",
      "Type"                  :  "String",
      "Default"               :  "t3.micro",
      "AllowedValues"         : ["t3.micro",  "t3.small",  "t3.medium",  "t3.large",   "t3.xlarge",  "t3.2xlarge",
                                 "m5.large",  "m5.xlarge", "m5.2xlarge", "m5.4xlarge", "m5.8xlarge", "m5.12xlarge", "m5.16xlarge", "m5.24xlarge",
                                 "r5.large",  "r5.xlarge", "r5.2xlarge", "r5.4xlarge", "r5.8xlarge", "r5.12xlarge", "r5.16xlarge", "r5.24xlarge",
                                 "c5.large",  "c5.xlarge", "c5.2xlarge", "c5.4xlarge", "c5.9xlarge", "c5.12xlarge", "c5.18xlarge", "c5.24xlarge"],
      "ConstraintDescription" : "must be a valid Intel EC2 instance type."
    },
    
    "OracleXeInstanceType": {
      "Description"           :  "Oracle XE RHEL7 Host Type, Note that the minimum memory requirements of Oracle XE preclude the use of micro instances",
      "Type"                  :  "String",
      "Default"               :  "t3.small",
      "AllowedValues"         : ["t3.small",  "t3.medium", "t3.large",   "t3.xlarge",  "t3.2xlarge",
                                 "m5.large",  "m5.xlarge", "m5.2xlarge", "m5.4xlarge", "m5.8xlarge", "m5.12xlarge", "m5.16xlarge", "m5.24xlarge",
                                 "r5.large",  "r5.xlarge", "r5.2xlarge", "r5.4xlarge", "r5.8xlarge", "r5.12xlarge", "r5.16xlarge", "r5.24xlarge",
                                 "c5.large",  "c5.xlarge", "c5.2xlarge", "c5.4xlarge", "c5.9xlarge", "c5.12xlarge", "c5.18xlarge", "c5.24xlarge"],
      "ConstraintDescription" : "must be a valid Intel EC2 instance type with at least 1GB RAM to run Oracle XE."
    },

    "RdsInstanceClass": {
      "Description"           :  "RDS Database Instance Class, Note that the default is the free tier instance size",
      "Type"                  :  "String",
      "Default"               :  "db.t3.micro",
      "AllowedValues"         : ["db.t3.micro", "db.t3.small",  "db.t3.medium",  "db.t3.large",   "db.t3.xlarge",   "db.t3.2xlarge",
                                 "db.m5.large", "db.m5.xlarge", "db.m5.2xlarge", "db.m5.4xlarge", "db.m5.8xlarge",  "db.m5.12xlarge", "db.m5.16xlarge", "db.m5.24xlarge"],
      "ConstraintDescription" :  "must be a valid RDS instance Class."
    }
  },
  
  "Mappings": {
    "LinuxAMIMapName": {
      "us-east-1"    : {"amzn2" : "ami-0c94855ba95c71c99", "rhel7" : "ami-005b7876121b7244d"},
      "us-east-2"    : {"amzn2" : "ami-0603cbe34fd08cb81", "rhel7" : "ami-0d2bf41df19c4aac7"},
      "us-west-1"    : {"amzn2" : "ami-0e65ed16c9bf1abc7", "rhel7" : "ami-015474e24281c803d"},
      "us-west-2"    : {"amzn2" : "ami-01ce4793a2f45922e", "rhel7" : "ami-02d40d11bb3aaf3e5"},
      "eu-central-1" : {"amzn2" : "ami-08c148bb835696b45", "rhel7" : "ami-0f58468b80db2db66"},
      "eu-west-1"    : {"amzn2" : "ami-0bb3fad3c0286ebd5", "rhel7" : "ami-020e14de09d1866b4"},
      "eu-west-2"    : {"amzn2" : "ami-09b89ad3c5769cca2", "rhel7" : "ami-0e6c172f77df9f9c3"},
      "eu-west-3"    : {"amzn2" : "ami-0697b068b80d79421", "rhel7" : "ami-0f4643887b8afe9e2"}
    }
  },

  "Resources": {
    "RdsPostgreSQL": {
      "Type"            :  "AWS::RDS::DBInstance",
      "DeletionPolicy"  :  "Delete",
      "Properties"      : {
        "AllocatedStorage"                      :    "20",
        "AllowMajorVersionUpgrade"              :    "true",
        "AutoMinorVersionUpgrade"               :    "true",
        "BackupRetentionPeriod"                 :    "0",
        "DeletionProtection"                    :    "false",
        "DBInstanceClass"                       :   {"Ref" : "RdsInstanceClass"},
        "DBInstanceIdentifier"                  :   {"Ref" : "DbIdentifier"},
        "DBName"                                :    "postgres",
        "DBSubnetGroupName"                     :   {"Ref" : "RdsSubnetGroupName"},
        "EnableIAMDatabaseAuthentication"       :    "true",
        "EnablePerformanceInsights"             :    "true",
        "Engine"                                :    "postgres",
        "EngineVersion"                         :    "12.7",
        "LicenseModel"                          :    "postgresql-license",
        "MasterUsername"                        :    "mike_pg",
        "MasterUserPassword"                    :   {"Ref" : "DBPassword"},
        "PerformanceInsightsRetentionPeriod"    :    "7",
        "PreferredMaintenanceWindow"            :    "sun:03:05-sun:04:05",
        "StorageType"                           :    "gp2",
        "Tags"    : [
          {"Key"  : "Name",             "Value" :   "postgres-12-7"},
          {"Key"  : "Schedule",         "Value" :  {"Ref" : "DbfSchedule"}},
          {"Key"  : "ScheduleMessage",  "Value" :   "Initial Build"}
        ],
        "VPCSecurityGroups"                     : [{"Ref" : "DataBaseSecurityGroupId"}, {"Ref" : "SshSecurityGroupId"}]
      }
    },

    "OracleXeHost": {
      "Type"            :  "AWS::EC2::Instance",
      "DeletionPolicy"  :  "Delete",
      "Properties"      : {
        "BlockDeviceMappings" : [{
          "DeviceName": "/dev/sda1",
          "Ebs"       : {
            "DeleteOnTermination" :   "true",
            "VolumeSize"          :   "40",
            "VolumeType"          :   "gp3"
          }
        }],
        "ImageId"           :  {"Fn::FindInMap" : ["LinuxAMIMapName", {"Ref" : "AWS::Region"},  "rhel7"]},
        "InstanceType"      :  {"Ref" : "OracleXeInstanceType"},
        "KeyName"           :  {"Ref" : "EC2KeyName" },
        "NetworkInterfaces" : [{
          "DeviceIndex"               :   "0",
          "GroupSet"                  : [{"Ref" : "DataBaseSecurityGroupId"}, {"Ref" : "SshSecurityGroupId"}],
          "AssociatePublicIpAddress"  :   "false",
          "DeleteOnTermination"       :   "true",
          "SubnetId"                  :  {"Ref" : "DatabaseSubnetId"}
        }],
        "Tags"    : [
          {"Key"  : "Name",             "Value" :  "Database Server"},
          {"Key"  : "Schedule",         "Value" : {"Ref" : "DbfSchedule"}},
          {"Key"  : "ScheduleMessage",  "Value" :  "Initial Build"},
          {"Key"  : "EC2Distro",        "Value" :  "rhel7"}
        ],
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": ["", [
              "#!/bin/bash -xe\n",
              "# Record Start\n",
              "ERROR_LOG=/tmp/Install-Err.txt\n",
              "INSTALL_LOG=/tmp/Install-Log.txt\n",

              "date > /tmp/Finished-Install.txt\n",
              "date > $INSTALL_LOG\n",
              "date > $ERROR_LOG\n",

              "setupVariables()\n",
              "{\n",
              "  echo starting setupVariables at `date` >> $INSTALL_LOG\n",

              "  EC2Password=", {"Ref"  : "EC2Password"}, "\n",
              "  DBPassword=",  {"Ref"  : "DBPassword"},  "\n",
              "  echo EC2Password: $EC2Password >  /tmp/Parameter-Log.txt\n",
              "  echo DBPassword : $DBPassword  >> /tmp/Parameter-Log.txt\n",

              "  ORACLE_BASE='/opt/oracle'\n",
              "  ORACLE_HOME=$ORACLE_BASE/product/18c/dbhomeXE\n",
              "  TNS_ADMIN=$ORACLE_HOME/network/admin\n",
              "  TNS_NAMES=$TNS_ADMIN/tnsnames.ora\n",

              "}\n",

              "installRhelLinuxPackages()\n",
              "{\n",
              "  echo starting installRhelLinuxPackages at `date` >> $INSTALL_LOG\n",

              "  yum install -y gcc                   >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y gcc-c++               >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y elfutils-libelf-devel >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y python3               >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y java-11-openjdk       >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y unzip                 >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y vim                   >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y make                  >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y libaio                >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y sysstat               >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y redhat-rpm-config     >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  yum install -y tigervnc-server xorg-x11-fonts-Type1 >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  yum groupinstall -y 'Server with GUI' --setopt=group_package_types=mandatory,default,optional >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "installRhel7OraclePackages()\n",
              "{\n",
              "  echo starting installRhel7OraclePackages at `date` >> $INSTALL_LOG\n",

              "  curl -L https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/compat-libcap1-1.10-7.el7.x86_64.rpm                -o /tmp/compat-libcap1.el7.x86_64.rpm\n",
              "  curl -L https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/compat-libstdc++-33-3.2.3-72.el7.x86_64.rpm         -o /tmp/compat-libstdc++-33.el7.x86_64.rpm\n",
              "  curl -L https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/oracle-database-preinstall-18c-1.0-1.el7.x86_64.rpm -o /tmp/oracle-database-preinstall-XE.rpm\n",
              "  curl -L https://yum.oracle.com/repo/OracleLinux/OL7/UEKR6/x86_64/getPackage/kernel-uek-5.4.17-2102.200.13.el7uek.x86_64.rpm      -o /tmp/kernel-uek.el7uek.x86_64.rpm\n",

              "  echo 'Pretend to be OEL, so that you can install Oracle Database packages'\n",
              "  sleep 10\n",
              "  rpm -ivh /tmp/kernel-uek.el7uek.x86_64.rpm --justdb --nodeps >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum localinstall -y /tmp/compat-libcap1.el7.x86_64.rpm       >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum localinstall -y /tmp/compat-libstdc++-33.el7.x86_64.rpm  >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum localinstall -y /tmp/oracle-database-preinstall-XE.rpm   >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "installCfnInit()\n",
              "{\n",
              "  echo starting installCfnInit at `date` >> $INSTALL_LOG\n",

              "  rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n",

              "  pip3 install  https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz\n",
              "}\n",

              "makeSwapFile()\n",
              "{\n",
              "  echo starting makeSwapFile at `date` >> $INSTALL_LOG\n",

              "  dd  if=/dev/zero of=/swapfile bs=1024 count=163840\n",

              "  mkswap     /swapfile\n",
              "  chmod 0600 /swapfile\n",
              "  swapon     /swapfile\n",

              "  echo  '/swapfile                                 swap                    swap    defaults        0 0'  >> /etc/fstab\n",

              "  echo 'Set 4GB shared mamory area'\n",
              "  mount -t tmpfs shmfs -o size=4G /dev/shm\n",
              "  free -h >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "updateEtcHosts()\n",
              "{\n",
              "  echo starting at `date` >> $INSTALL_LOG\n",

              "  HostInstanceIP=`hostname -I`\n",

              "  echo $HostInstanceIP oracle-xe oracle-xe.db-freedom.aws.internal                        >  /etc/hosts\n",
              "  echo 127.0.0.1       localhost localhost.localdomain localhost4 localhost4.localdomain4 >> /etc/hosts\n",
              "}\n",

              "updateSysconfigNetwork()\n",
              "{\n",
              "  echo starting updateSysconfigNetwork at `date` >> $INSTALL_LOG\n",

              "  echo NETWORKING=yes                             >  /etc/sysconfig/network\n",
              "  echo HOSTNAME=oracle-xe.db-freedom.aws.internal >> /etc/sysconfig/network\n",
              "  echo NETWORKING_IPV6=no                         >> /etc/sysconfig/network\n",
              "  echo NOZEROCONF=yes                             >> /etc/sysconfig/network\n",

              "  hostnamectl set-hostname oracle-xe\n",
              "  hostname >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "createDatabaseAccounts()\n",
              "{\n",
              "  echo starting createDatabaseAccounts at `date` >> $INSTALL_LOG\n",

              "  groupadd -g 501 oinstall 2>> $ERROR_LOG\n",
              "  groupadd -g 502 dba      2>> $ERROR_LOG\n",
              "  groupadd -g 503 asmadmin 2>> $ERROR_LOG\n",
              "  groupadd -g 504 oper     2>> $ERROR_LOG\n",
              "  groupadd -g 505 asmoper  2>> $ERROR_LOG\n",
              "  useradd  -c 'Oracle User' -m -u 501 -g oinstall -G dba,asmadmin,oper,asmoper -s /bin/bash -d /home/oracle oracle\n",
              "}\n",

              "setPasswords()\n",
              "{\n",
              "  echo starting setPasswords at `date` >> $INSTALL_LOG\n",

              "  echo $EC2Password  | passwd oracle   --stdin\n",
              "  echo $EC2Password  | passwd ec2-user --stdin\n",
              "  echo $EC2Password  | passwd root     --stdin\n",
              "}\n",

              "setupBashProfiles()\n",
              "{\n",
              "  echo starting setupBashProfiles at `date` >> $INSTALL_LOG\n",

              "  echo ''                              >> /etc/profile\n",
              "  echo export ORACLE_BASE=$ORACLE_BASE >> /etc/profile\n",
              "  echo export ORACLE_HOME=$ORACLE_HOME >> /etc/profile\n",
              "  echo export ORACLE_SID=$ORACLE_SID   >> /etc/profile\n",
              "  echo export TNS_ADMIN=$TNS_ADMIN\n   >> /etc/profile\n",
              "  echo export LD_LIBRARY_PATH=/lib64/:$ORACLE_HOME/lib >> /etc/profile\n",
              "  echo export PATH=$ORACLE_HOME/bin:$PATH              >> /etc/profile\n",

              "  echo '#!/bin/bash +x'                          >  /home/oracle/.bash_profile\n",
              "  echo '# Get the aliases and functions'         >> /home/oracle/.bash_profile\n",
              "  echo 'if [ -f ~/.bashrc ]; then'               >> /home/oracle/.bash_profile\n",
              "  echo '      . ~/.bashrc'                       >> /home/oracle/.bash_profile\n",
              "  echo 'fi\n'                                    >> /home/oracle/.bash_profile\n",
              "  echo 'export ORAENV_ASK=NO\n'                  >> /home/oracle/.bash_profile\n",
              "  echo 'export CLASSPATH=/opt/JDBC/*:.\n'        >> /home/oracle/.bash_profile\n",
              "  echo 'PATH=$HOME/.local/bin:$PATH:$HOME/bin\n' >> /home/oracle/.bash_profile\n",
              "  echo 'export PATH'                             >> /home/oracle/.bash_profile\n",

              "  echo 'alias java11=/etc/alternatives/jre_11/bin/java' >> /home/oracle/.bashrc\n",

              "  chown oracle:oinstall /home/oracle/.bash_profile\n",
              "  chown oracle:oinstall /home/oracle/.bashrc\n",
              "}\n",

              "setupViProfile()\n",
              "{\n",
              "  echo starting setupViProfile at `date` >> $INSTALL_LOG\n",

              "  echo 'setupViProfile'\n",

              "  echo 'syntax on'         >> /home/oracle/.vimrc\n",
              "  echo 'colorscheme slate' >> /home/oracle/.vimrc\n",
              "  chown oracle:oinstall       /home/oracle/.vimrc\n",

              "  cp /home/oracle/.vimrc   /home/ec2-user/.vimrc\n",
              "  chown ec2-user:ec2-user  /home/ec2-user/.vimrc\n",
              "}\n",

              "setupSshAccess()\n",
              "{\n",
              "  echo starting setupSshAccess at `date` >> $INSTALL_LOG\n",

              "  echo 'setupSshAccess'\n",

              "  mkdir  /home/oracle/.ssh\n",
              "  cp /home/ec2-user/.ssh/authorized_keys /home/oracle/.ssh\n",
              "  chown -R oracle:oinstall /home/oracle/.ssh\n",
              "}\n",

              "setupOracleXE()\n",
              "{\n",
              "  echo starting setupOracleXE at `date` >> $INSTALL_LOG\n",

              "  ORACLE_INSTALLER='/etc/init.d/oracle-xe-18c'\n",
              "  ORACLE_SERVICE='oracle-xe-18c'\n",
              "  PATH=$ORACLE_HOME/bin:$PATH\n",
              "  ORACLE_SID='XE'\n",
              "  SERVICE_NAME='XEPDB1'\n",
              "}\n",

              "downloadOracleXE()\n",
              "{\n",
              "  echo starting downloadOracleXE at `date` >> $INSTALL_LOG\n",

              "  wget --http-user=dma@amazon.com https://download.oracle.com/otn-pub/otn_software/db-express/oracle-database-xe-18c-1.0-1.x86_64.rpm -O /tmp/oracle-database.rpm\n",
              "}\n",

              "installOracle()\n",
              "{\n",
              "  echo starting installOracle at `date` >> $INSTALL_LOG\n",

              "  yum localinstall -y /tmp/oracle-database.rpm                        >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  (echo $DBPassword; echo $DBPassword;) | $ORACLE_INSTALLER configure >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  echo  'EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);' >  /tmp/allowRemoteOracleAccess.sql\n",
              "  echo  'EXIT;'                                        >> /tmp/allowRemoteOracleAccess.sql\n",

              "  echo  'ALTER PLUGGABLE DATABASE ALL OPEN;'        > /tmp/start-pluggable-databases.sql\n",
              "  echo  'ALTER PLUGGABLE DATABASE ALL SAVE STATE;' >> /tmp/start-pluggable-databases.sql\n",
              "  echo  'ALTER SESSION SET CONTAINER=XEPDB1;'      >> /tmp/start-pluggable-databases.sql\n",
              "  echo  'ALTER SYSTEM REGISTER;'                   >> /tmp/start-pluggable-databases.sql\n",
              "  echo  'EXIT;'                                    >> /tmp/start-pluggable-databases.sql\n",

              "  su - oracle -c '$ORACLE_HOME/bin/sqlplus / as sysdba @/tmp/allowRemoteOracleAccess.sql'    >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  su - oracle -c '$ORACLE_HOME/bin/sqlplus / as sysdba @/tmp/start-pluggable-databases.sql'  >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  systemctl daemon-reload          >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  systemctl enable $ORACLE_SERVICE >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  . /etc/profile\n",
              "}\n",

              "createTnsNames()\n",
              "{\n",
              "  echo starting createTnsNames at `date` >> $INSTALL_LOG\n",

              "  mkdir -p $TNS_ADMIN  >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  echo 'XEPDB1 ='                         >> $TNS_NAMES\n",
              "  echo ' ( DESCRIPTION ='                 >> $TNS_NAMES\n",
              "  echo '   ( ADDRESS ='                   >> $TNS_NAMES\n",
              "  echo '     ( PROTOCOL = tcp )'          >> $TNS_NAMES\n",
              "  echo '     ( HOST     = oracle-xe )'    >> $TNS_NAMES\n",
              "  echo '     ( PORT     = 1521 )'         >> $TNS_NAMES\n",
              "  echo '   )'                             >> $TNS_NAMES\n",
              "  echo '   ( CONNECT_DATA ='              >> $TNS_NAMES\n",
              "  echo '     ( SERVER       = DEDICATED)' >> $TNS_NAMES\n",
              "  echo '     ( SERVICE_NAME = XEPDB1)'    >> $TNS_NAMES\n",
              "  echo '   )'                             >> $TNS_NAMES\n",
              "  echo ' )'                               >> $TNS_NAMES\n",

              "  chown oracle:oinstall  $TNS_NAMES >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "createMikeUser()\n",
              "{\n",
              "  echo starting createMikeUser at `date` >> $INSTALL_LOG\n",

              "  echo CREATE  USER MIKE_XE IDENTIFIED BY '\"'$DBPassword'\";' > /tmp/createMikeUser.sql\n",

              "  echo GRANT   CREATE  SESSION            TO  MIKE_XE';' >> /tmp/createMikeUser.sql\n",
              "  echo GRANT   SELECT_CATALOG_ROLE        TO  MIKE_XE';' >> /tmp/createMikeUser.sql\n",
              "  echo GRANT   SELECT  ANY DICTIONARY     TO  MIKE_XE';' >> /tmp/createMikeUser.sql\n",
              "  echo GRANT   SELECT  ON SYS.USER$       TO  MIKE_XE';' >> /tmp/createMikeUser.sql\n",
              "  echo GRANT   EXECUTE ON SYS.DBMS_CRYPTO TO  MIKE_XE';' >> /tmp/createMikeUser.sql\n",
              "  echo GRANT   DBA                        TO  MIKE_XE';' >> /tmp/createMikeUser.sql\n",
              "  echo EXIT';'                                           >> /tmp/createMikeUser.sql\n",

              "  echo '$ORACLE_HOME/bin/sqlplus sys/'$DBPassword'@XEPDB1 as sysdba @/tmp/createMikeUser.sql' > /tmp/createMikeUser.sh\n",

              "  chmod 755  /tmp/createMikeUser.sh\n",

              "  su - oracle -c '/tmp/createMikeUser.sh >> /tmp/CreateUser-Log.txt 2>> /tmp/CreateUser-Err.txt'\n",
              "}\n",

              "openFirewallPorts()\n",
              "{\n",
              "  echo starting openFirewallPorts at `date` >> $INSTALL_LOG\n",

              "  systemctl disable firewalld >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "createSqlScripts()\n",
              "{\n",
              " echo starting createSqlScripts at `date` >> $INSTALL_LOG\n",

              " echo 'CREATE TABLE oracle_fdw_local_parent'             >  /home/oracle/create_ora_tables.sql\n",
              " echo '( code        INTEGER      NOT NULL PRIMARY KEY,' >> /home/oracle/create_ora_tables.sql\n",
              " echo '  name        VARCHAR2(40) NOT NULL,'             >> /home/oracle/create_ora_tables.sql\n",
              " echo '  created     DATE DEFAULT SYSDATE   NOT NULL,'   >> /home/oracle/create_ora_tables.sql\n",
              " echo '  expiry_date DATE DEFAULT SYSDATE + 730 NULL,'   >> /home/oracle/create_ora_tables.sql\n",
              " echo '  order_date  DATE         NULL,'                 >> /home/oracle/create_ora_tables.sql\n",
              " echo '  order_value NUMBER(20,2) NULL,'                 >> /home/oracle/create_ora_tables.sql\n",
              " echo '  description VARCHAR2(80) NULL,'                 >> /home/oracle/create_ora_tables.sql\n",
              " echo '  updated     DATE         NULL);'                >> /home/oracle/create_ora_tables.sql\n",

              " echo 'CREATE TABLE oracle_fdw_local_child'              >> /home/oracle/create_ora_tables.sql\n",
              " echo '( code        INTEGER      NOT NULL PRIMARY KEY,' >> /home/oracle/create_ora_tables.sql\n",
              " echo '  parent      INTEGER      NOT NULL,'             >> /home/oracle/create_ora_tables.sql\n",
              " echo '  name        VARCHAR2(40) NOT NULL,'             >> /home/oracle/create_ora_tables.sql\n",
              " echo '  created     DATE DEFAULT SYSDATE   NOT NULL,'   >> /home/oracle/create_ora_tables.sql\n",
              " echo '  expiry_date DATE DEFAULT SYSDATE + 730 NULL,'   >> /home/oracle/create_ora_tables.sql\n",
              " echo '  order_date  DATE         NULL,'                 >> /home/oracle/create_ora_tables.sql\n",
              " echo '  order_value NUMBER(20,2) NULL,'                 >> /home/oracle/create_ora_tables.sql\n",
              " echo '  description VARCHAR2(80) NULL,'                 >> /home/oracle/create_ora_tables.sql\n",
              " echo '  updated     DATE         NULL);'                >> /home/oracle/create_ora_tables.sql\n",

              " echo \"exit;\"   >> /home/oracle/create_ora_tables.sql\n",

              " echo \"INSERT INTO oracle_fdw_local_parent(code, name, description) VALUES (1, 'One', 'First Value in Oracle');\"  >> /home/oracle/insert_ora_tables.sql\n",
              " echo \"INSERT INTO oracle_fdw_local_parent(code, name, description) VALUES (2, 'Two', 'Second Value in Oracle');\" >> /home/oracle/insert_ora_tables.sql\n",

              " echo \"INSERT INTO oracle_fdw_local_child(code, parent, name, description) VALUES(1, 1, 'One',  'First Child in Oracle');\"   >> /home/oracle/insert_ora_tables.sql\n",
              " echo \"INSERT INTO oracle_fdw_local_child(code, parent, name, description) VALUES(2, 1, 'Two',  'Second Child in Oracle');\"  >> /home/oracle/insert_ora_tables.sql\n",
              " echo \"INSERT INTO oracle_fdw_local_child(code, parent, name, description) VALUES(3, 2, 'Three','Third Child in Oracle');\"   >> /home/oracle/insert_ora_tables.sql\n",
              " echo \"INSERT INTO oracle_fdw_local_child(code, parent, name, description) VALUES(4, 2, 'Four', 'Fourth Child in Oracle');\"  >> /home/oracle/insert_ora_tables.sql\n",

              " echo \"commit;\" >> /home/oracle/insert_ora_tables.sql\n",
              " echo \"exit;\"   >> /home/oracle/insert_ora_tables.sql\n",
              "}\n",

              "# Main\n",
              "setupVariables\n",
              "setupOracleXE\n",
              "createDatabaseAccounts\n",

              "yum -y install curl >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "yum -y update       >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "makeSwapFile\n",
              "installRhelLinuxPackages\n",
              "installRhel7OraclePackages\n",
              "installCfnInit\n",
              "updateEtcHosts\n",
              "updateSysconfigNetwork\n",
              "setPasswords\n",
              "setupBashProfiles\n",
              "setupViProfile\n",
              "setupSshAccess\n",
              "downloadOracleXE\n",
              "installOracle\n",
              "createTnsNames\n",
              "createMikeUser\n",
              "openFirewallPorts\n",
              "createSqlScripts\n",

              "# Record Successful completion\n",
              "date >> /tmp/Finished-Install.txt\n",

              "/usr/local/bin/cfn-signal -s true \"", { "Ref" : "OracleXeWaitHandle" }, "\" >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "cp /tmp/*.txt              /home/oracle/\n",
              "cp /tmp/createMikeUser.sql /home/oracle/\n",
              "cp /tmp/createMikeUser.sh  /home/oracle/\n",

              "chown oracle /home/oracle/*.txt\n",
              "chown oracle /home/oracle/*.sql\n",
              "chown oracle /home/oracle/*.sh\n",

              "rm -fr /tmp/*\n",
              "reboot now\n"
            ]]
          }
        }
      }
    },

    "OracleXeWaitHandle"  : {
      "Type" : "AWS::CloudFormation::WaitConditionHandle"
    },

    "OracleXeWaitCondition" : {
      "Type"        :  "AWS::CloudFormation::WaitCondition",
      "DependsOn"   :  "OracleXeHost",
      "Properties"  : {
        "Handle"  : { "Ref" : "OracleXeWaitHandle" },
        "Timeout" :   "3600"
      }
    },

    "BastianHost": {
      "Type"            :  "AWS::EC2::Instance",
      "DependsOn"       :  "OracleXeWaitCondition",
      "DeletionPolicy"  :  "Delete",
      "Properties"      : {
        "BlockDeviceMappings" :[{
          "DeviceName": "/dev/xvda",
          "Ebs"       : {
            "DeleteOnTermination" :   "true",
            "VolumeSize"          :   "40",
            "VolumeType"          :   "gp3"
          }
        }],
        "ImageId"           :  {"Fn::FindInMap" : ["LinuxAMIMapName", {"Ref" : "AWS::Region"},  "amzn2"]},
        "InstanceType"      :  {"Ref" : "BastianInstanceType"},
        "KeyName"           :  {"Ref" : "EC2KeyName" },
        "NetworkInterfaces" : [{
          "DeviceIndex"               :   "0",
          "GroupSet"                  : [{"Ref" : "PublicSecurityGroupId"}],
          "AssociatePublicIpAddress"  :   "true",
          "DeleteOnTermination"       :   "true",
          "SubnetId"                  :  {"Ref" : "PublicSubnetId"}
        }],
        "Tags"    : [
          {"Key"  : "Name",             "Value" :  "Bastian Host"},
          {"Key"  : "Schedule",         "Value" : {"Ref" : "DbfSchedule"}},
          {"Key"  : "ScheduleMessage",  "Value" :  "Initial Build"},
          {"Key"  : "EC2Distro",        "Value" :  "amzn2"}
        ],
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": ["", [
              "#!/bin/bash -xe\n",
              "# Record Start\n",
              "ERROR_LOG=/tmp/Install-Err.txt\n",
              "INSTALL_LOG=/tmp/Install-Log.txt\n",

              "date > /tmp/Finished-Install.txt\n",
              "date > $INSTALL_LOG\n",
              "date > $ERROR_LOG\n",

              "setupVariables()\n",
              "{\n",
              "  echo starting setupVariables at `date` >> $INSTALL_LOG\n",

              "  KeyName=",     {"Ref"        :  "EC2KeyName"},   "\n",
              "  KeyLocation=", {"Ref"        :  "KeyLocation"},  "\n",
              "  EC2Password=", {"Ref"        :  "EC2Password"},  "\n",
              "  DBHostIP=",    {"Fn::GetAtt" : ["OracleXeHost",  "PrivateIp"]},  "\n",
              "  DBPassword=",  {"Ref"        :  "DBPassword"},   "\n",
              "  ORACLE_BASE=/usr/lib/oracle\n",
              "  ORACLE_HOME=$ORACLE_BASE/19.10/client64\n",
              "  TNS_ADMIN=$ORACLE_HOME/lib/network/admin\n",
              "  TNS_NAMES=$TNS_ADMIN/tnsnames.ora\n",

              "  echo KeyName    : $KeyName     >> /tmp/Parameter-Log.txt\n",
              "  echo KeyLocation: $KeyLocation >> /tmp/Parameter-Log.txt\n",
              "  echo EC2Password: $EC2Password >> /tmp/Parameter-Log.txt\n",
              "  echo DBHostIP   : $DBHostIP    >> /tmp/Parameter-Log.txt\n",
              "  echo DBPassword : $DBPassword  >> /tmp/Parameter-Log.txt\n",
              "  echo ORACLE_BASE: $ORACLE_BASE >> /tmp/Parameter-Log.txt\n",
              "  echo ORACLE_HOME: $ORACLE_HOME >> /tmp/Parameter-Log.txt\n",
              "  echo TNS_ADMIN  : $TNS_ADMIN   >> /tmp/Parameter-Log.txt\n",
              "}\n",

              "enableAmznLinuxExtras()\n",
              "{\n",
              "  echo Enable Java 11\n",
              "  amazon-linux-extras enable java-openjdk11\n",

              "  echo Enable Python\n",
              "  amazon-linux-extras enable python3.8\n",

              "  echo Enable Vim\n",
              "  amazon-linux-extras enable vim\n",

              "  echo Enable Mate Desktop\n",
              "  amazon-linux-extras enable mate-desktop1.x\n",

              "  echo Enable Libre Office\n",
              "  amazon-linux-extras enable libreoffice\n",

              "  echo Enable postgresql13\n",
              "  amazon-linux-extras enable postgresql13\n",
              "}\n",

              "configureAmznLibraries()\n",
              "{\n",
              "  echo -e 'Run Amzn Specific library commands'\n",
              "  echo -e '\tAdd RDP 7 Repository'\n",
              "  rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n",
              "  yum makecache\n",
              "}\n",

              "installAmznLinuxPackages()\n",
              "{\n",
              "  echo starting installRhelLinuxPackages at `date` >> $INSTALL_LOG\n",

              "  yum install  -y evince >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y gcc    >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y gvim   >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  yum install  -y elfutils-libelf-devel >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  yum install  -y gedit    >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y chromium >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y python3  >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  yum install  -y java-11-openjdk >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y postgresql      >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y libreoffice     >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y mate-desktop    >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y tigervnc        >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install  -y xrdp            >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  yum groupinstall -y Xfce >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "updateEtcHosts()\n",
              "{\n",
              "  echo starting updateEtcHosts at `date` >> $INSTALL_LOG\n",

              "  HostInstanceIP=`hostname -I`\n",

              "  echo $DBHostIP       oracle-xe     oracle-xe.db-freedom.aws.internal     >  /etc/hosts\n",
              "  echo $HostInstanceIP bastian-host  bastian-host.db-freedom.aws.internal  >> /etc/hosts\n",
              "  echo 127.0.0.1       localhost localhost.localdomain localhost4 localhost4.localdomain4 >> /etc/hosts\n",
              "}\n",

              "updateSysconfigNetwork()\n",
              "{\n",
              "  echo starting updateSysconfigNetwork at `date` >> $INSTALL_LOG\n",

              "  echo NETWORKING=yes                                >  /etc/sysconfig/network\n",
              "  echo HOSTNAME=bastian-host.db-freedom.aws.internal >> /etc/sysconfig/network\n",
              "  echo NETWORKING_IPV6=no                            >> /etc/sysconfig/network\n",
              "  echo NOZEROCONF=yes                                >> /etc/sysconfig/network\n",

              "  hostnamectl set-hostname bastian-host\n",
              "  hostname >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "createDatabaseAccounts()\n",
              "{\n",
              "  echo starting createDatabaseAccounts at `date` >> $INSTALL_LOG\n",

              "  groupadd -g 501 oinstall 2>> $ERROR_LOG\n",
              "  groupadd -g 502 dba      2>> $ERROR_LOG\n",
              "  groupadd -g 503 asmadmin 2>> $ERROR_LOG\n",
              "  groupadd -g 504 oper     2>> $ERROR_LOG\n",
              "  groupadd -g 505 asmoper  2>> $ERROR_LOG\n",
              "  useradd  -c 'Oracle User' -m -u 501 -g oinstall -G dba,asmadmin,oper,asmoper -s /bin/bash -d /home/oracle oracle\n",
              "}\n",

              "setPasswords()\n",
              "{\n",
              "  echo starting setPasswords at `date` >> $INSTALL_LOG\n",

              "  echo $EC2Password  | passwd oracle   --stdin\n",
              "  echo $EC2Password  | passwd ec2-user --stdin\n",
              "  echo $EC2Password  | passwd root     --stdin\n",
              "}\n",

              "setupBashProfiles()\n",
              "{\n",
              "  echo starting setupBashProfiles at `date` >> $INSTALL_LOG\n",

              "  echo ''                                      >> /etc/profile\n",
              "  echo export ORACLE_BASE=$ORACLE_BASE         >> /etc/profile\n",
              "  echo export ORACLE_HOME=$ORACLE_HOME         >> /etc/profile\n",
              "  echo export TNS_ADMIN=$TNS_ADMIN             >> /etc/profile\n",
              "  echo export LD_LIBRARY_PATH=/lib64/:$ORACLE_HOME/lib >> /etc/profile\n",
              "  echo export PATH=$ORACLE_HOME/bin:$PATH              >> /etc/profile\n",

              "  echo '#!/bin/bash +x'                   >  /home/oracle/.bash_profile\n",
              "  echo '# Get the aliases and functions'  >> /home/oracle/.bash_profile\n",
              "  echo 'if [ -f ~/.bashrc ]; then'        >> /home/oracle/.bash_profile\n",
              "  echo '      . ~/.bashrc'                >> /home/oracle/.bash_profile\n",
              "  echo 'fi\n'                             >> /home/oracle/.bash_profile\n",

              "  echo '# User specific environment and startup programs\n' >> /home/oracle/.bash_profile\n",
              "  echo 'export ORAENV_ASK=NO\n'                             >> /home/oracle/.bash_profile\n",
              "  echo 'export CLASSPATH=/opt/JDBC/*:.\n'                   >> /home/oracle/.bash_profile\n",
              "  echo 'PATH=$HOME/.local/bin:$PATH:$HOME/bin\n'            >> /home/oracle/.bash_profile\n",
              "  echo 'export PATH'                                        >> /home/oracle/.bash_profile\n",

              "  echo 'alias java11=/etc/alternatives/jre_11/bin/java'  >> /home/oracle/.bashrc\n",

              "  chown oracle:oinstall /home/oracle/.bash_profile\n",
              "  chown oracle:oinstall /home/oracle/.bashrc\n",
              "}\n",

              "setupViProfile()\n",
              "{\n",
              "  echo starting setupViProfile at `date` >> $INSTALL_LOG\n",

              "  echo 'setupViProfile'\n",

              "  echo 'syntax on'         >>  /home/oracle/.vimrc\n",
              "  echo 'colorscheme slate' >>  /home/oracle/.vimrc\n",
              "  chown oracle:oinstall        /home/oracle/.vimrc\n",

              "  cp /home/oracle/.vimrc   /home/ec2-user/.vimrc\n",
              "  chown ec2-user:ec2-user  /home/ec2-user/.vimrc\n",
              "}\n",

              "setupSshAccess()\n",
              "{\n",
              "  echo starting setupSshAccess at `date` >> $INSTALL_LOG\n",

              "  echo 'setupSshAccess'\n",

              "  mkdir                                  /home/oracle/.ssh\n",
              "  cp /home/ec2-user/.ssh/authorized_keys /home/oracle/.ssh\n",
              "  curl $KeyLocation                   -o /home/oracle/.ssh/$KeyName.pem\n",

              "  echo 'HOST mike-ec2'                                 >  /home/oracle/.ssh/config\n",
              "  echo '  'HostName oracle-xe.db-freedom.aws.internal  >> /home/oracle/.ssh/config\n",
              "  echo '  User ec2-user'                               >> /home/oracle/.ssh/config\n",
              "  echo '  'IdentityFile /home/oracle/.ssh/$KeyName.pem >> /home/oracle/.ssh/config\n",
              "  echo '  ServerAliveInterval 60'                      >> /home/oracle/.ssh/config\n",
              "  echo ''                                              >> /home/oracle/.ssh/config\n",
              "  echo 'HOST mike-oracle'                              >> /home/oracle/.ssh/config\n",
              "  echo '  'HostName oracle-xe.db-freedom.aws.internal  >> /home/oracle/.ssh/config\n",
              "  echo '  User oracle'                                 >> /home/oracle/.ssh/config\n",
              "  echo '  'IdentityFile /home/oracle/.ssh/$KeyName.pem >> /home/oracle/.ssh/config\n",
              "  echo '  ServerAliveInterval 60'                      >> /home/oracle/.ssh/config\n",
              "  echo ''                                              >> /home/oracle/.ssh/config\n",

              "  chown -R oracle:oinstall /home/oracle/.ssh\n",
              "  chmod    600             /home/oracle/.ssh/$KeyName.pem\n",
              "  chmod    600             /home/oracle/.ssh/config\n",
              "}\n",

              "installOracleClient()\n",
              "{\n",
              "  echo starting setupOracleClient at `date` >> $INSTALL_LOG\n",

              "  curl https://download.oracle.com/otn_software/linux/instantclient/191000/oracle-instantclient19.10-basic-19.10.0.0.0-1.x86_64.rpm   -o /tmp/oracle-instantclient-basic.rpm\n",
              "  curl https://download.oracle.com/otn_software/linux/instantclient/191000/oracle-instantclient19.10-sqlplus-19.10.0.0.0-1.x86_64.rpm -o /tmp/oracle-instantclient-sqlplus.rpm\n",
              "  curl https://download.oracle.com/otn_software/linux/instantclient/191000/oracle-instantclient19.10-tools-19.10.0.0.0-1.x86_64.rpm   -o /tmp/oracle-instantclient-tools.rpm\n",
              "  curl https://download.oracle.com/otn_software/linux/instantclient/191000/oracle-instantclient19.10-jdbc-19.10.0.0.0-1.x86_64.rpm    -o /tmp/oracle-instantclient-jdbc.rpm\n",
              "  curl https://download.oracle.com/otn_software/linux/instantclient/191000/oracle-instantclient19.10-odbc-19.10.0.0.0-1.x86_64.rpm    -o /tmp/oracle-instantclient-odbc.rpm\n",
              "  curl https://download.oracle.com/otn_software/linux/instantclient/191000/oracle-instantclient19.10-devel-19.10.0.0.0-1.x86_64.rpm   -o /tmp/oracle-instantclient-devel.rpm\n",
              "  curl https://download.oracle.com/otn_software/linux/instantclient/191000/oracle-instantclient19.10-precomp-19.10.0.0.0-1.x86_64.rpm -o /tmp/oracle-instantclient-precomp.rpm\n",

              "  yum install -y /tmp/oracle-instantclient-basic.rpm   >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y /tmp/oracle-instantclient-sqlplus.rpm >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y /tmp/oracle-instantclient-tools.rpm   >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y /tmp/oracle-instantclient-jdbc.rpm    >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y /tmp/oracle-instantclient-odbc.rpm    >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y /tmp/oracle-instantclient-devel.rpm   >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "  yum install -y /tmp/oracle-instantclient-precomp.rpm >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  echo $ORACLE_HOME/lib > /etc/ld.so.conf.d/oracle.conf\n",
              "  ldconfig >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "createTnsNames()\n",
              "{\n",
              "  echo starting createTnsNames at `date` >> $INSTALL_LOG\n",

              "  mkdir -p $TNS_ADMIN >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "  echo 'XEPDB1 ='                      >  $TNS_NAMES\n",
              "  echo ' ( DESCRIPTION ='              >> $TNS_NAMES\n",
              "  echo '   ( ADDRESS ='                >> $TNS_NAMES\n",
              "  echo '     ( PROTOCOL = tcp )'       >> $TNS_NAMES\n",
              "  echo '     ( HOST     = oracle-xe )' >> $TNS_NAMES\n",
              "  echo '     ( PORT     = 1521 )'      >> $TNS_NAMES\n",
              "  echo '   )'                          >> $TNS_NAMES\n",
              "  echo '   ( CONNECT_DATA ='           >> $TNS_NAMES\n",
              "  echo '      (SERVER = DEDICATED)'    >> $TNS_NAMES\n",
              "  echo '      (SERVICE_NAME = XEPDB1)' >> $TNS_NAMES\n",
              "  echo '   )'                          >> $TNS_NAMES\n",
              "  echo ' )'                            >> $TNS_NAMES\n",

              "  chown oracle:oinstall  $TNS_NAMES\n",
              "}\n",

              "setupRdpAccess()\n",
              "{\n",
              " echo starting setupRdpAccess at `date` >> $INSTALL_LOG\n",

              " echo 'xfce4-session' > /home/oracle/.Xclients\n",
              " chmod 755              /home/oracle/.Xclients\n",
              " chown oracle:oinstall  /home/oracle/.Xclients\n",

              " systemctl daemon-reload\n",
              " systemctl enable  xrdp\n",
              " systemctl start   xrdp\n",
              "}\n",

              "createSqlScripts()\n",
              "{\n",
              " echo starting createSqlScripts at `date` >> $INSTALL_LOG\n",

              " echo \"CREATE TABLE postgres_fdw_local_parent\"                             >  /home/oracle/create_pg_tables.sql\n",
              " echo \"( code        SERIAL        NOT NULL PRIMARY KEY,\"                  >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  name        TEXT          NOT NULL,\"                              >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  created     TIMESTAMP     DEFAULT clock_timestamp()  NOT NULL,\"   >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  expiry_date TIMESTAMP     DEFAULT clock_timestamp() + '2 years',\" >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  order_date  TIMESTAMP     NULL,\"                                  >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  order_value NUMERIC(20,2) NULL,\"                                  >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  description TEXT          NULL,\"                                  >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  updated     TIMESTAMP     NULL);\"                                 >> /home/oracle/create_pg_tables.sql\n",

              " echo \"CREATE TABLE postgres_fdw_local_child\"                                      >> /home/oracle/create_pg_tables.sql\n",
              " echo \"( code        SERIAL        NOT NULL PRIMARY KEY,\"                          >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  parent      INTEGER       NOT NULL REFERENCES postgres_fdw_local_parent,\" >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  name        TEXT          NOT NULL,\"                                      >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  created     TIMESTAMP     DEFAULT clock_timestamp()  NOT NULL,\"           >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  expiry_date TIMESTAMP     DEFAULT clock_timestamp() + '2 years',\"         >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  order_date  TIMESTAMP     NULL,\"                                          >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  order_value NUMERIC(20,2) NULL,\"                                          >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  description TEXT          NULL,\"                                          >> /home/oracle/create_pg_tables.sql\n",
              " echo \"  updated     TIMESTAMP     NULL);\"                                         >> /home/oracle/create_pg_tables.sql\n",

              " echo \"INSERT INTO postgres_fdw_local_parent(name, description) VALUES ('One', 'First Value in Postgres');\"  > /home/oracle/insert_pg_tables.sql\n",
              " echo \"INSERT INTO postgres_fdw_local_parent(name, description) VALUES ('Two', 'Second Value in Postgres');\" >> /home/oracle/insert_pg_tables.sql\n",

              " echo \"INSERT INTO postgres_fdw_local_child(parent, name, description) VALUES (1, 'One',  'First Child in PostgreSQL');\"  >> /home/oracle/insert_pg_tables.sql\n",
              " echo \"INSERT INTO postgres_fdw_local_child(parent, name, description) VALUES (1, 'Two',  'Second Child in PostgreSQL');\" >> /home/oracle/insert_pg_tables.sql\n",
              " echo \"INSERT INTO postgres_fdw_local_child(parent, name, description) VALUES (2, 'Three','Third Child in PostgreSQL');\"  >> /home/oracle/insert_pg_tables.sql\n",
              " echo \"INSERT INTO postgres_fdw_local_child(parent, name, description) VALUES (2, 'Four', 'Fourth Child in PostgreSQL');\" >> /home/oracle/insert_pg_tables.sql\n",
              "}\n",

              "getSqlScripts()\n",
              "{\n",
              " echo starting getSqlScripts at `date` >> $INSTALL_LOG\n",

              " echo export KeyName=$KeyName   >> /home/oracle/.bash_profile\n",
              " echo export DBHostIP=$DBHostIP >> /home/oracle/.bash_profile\n",

              " su - oracle -c 'scp -i .ssh/$KeyName.pem -o StrictHostKeyChecking=no oracle@$DBHostIP:/home/oracle/create_ora_tables.sql create_ora_tables.sql' >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              " su - oracle -c 'scp -i .ssh/$KeyName.pem -o StrictHostKeyChecking=no oracle@$DBHostIP:/home/oracle/insert_ora_tables.sql insert_ora_tables.sql' >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "}\n",

              "# Main\n",
              "setupVariables\n",
              "enableAmznLinuxExtras\n",
              "configureAmznLibraries\n",
              "createDatabaseAccounts\n",

              "yum -y install curl >> $INSTALL_LOG 2>> $ERROR_LOG\n",
              "yum -y update       >> $INSTALL_LOG 2>> $ERROR_LOG\n",

              "installAmznLinuxPackages\n",
              "updateEtcHosts\n",
              "updateSysconfigNetwork\n",
              "setPasswords\n",
              "setupBashProfiles\n",
              "setupViProfile\n",
              "setupSshAccess\n",
              "installOracleClient\n",
              "createTnsNames\n",
              "setupRdpAccess\n",
              "createSqlScripts\n",
              "getSqlScripts\n",

              "# Record Successful completion\n",
              "date >> /tmp/Finished-Install.txt\n",
              "cp /tmp/Finished-Install.txt /home/oracle/Finished-Install.txt\n",
              "cp /tmp/Parameter-Log.txt    /home/oracle/Parameter-Log.txt\n",
              "cp $INSTALL_LOG      /home/oracle/Install-Log.txt\n",
              "chown oracle /home/oracle/*.txt\n",

              "rm -fr /tmp/*\n",
              "reboot now\n"
            ]]
          }
        }
      }
    }
  },

  "Outputs": {
    "BastianHostName": {
      "Description" :  "The EC2 Bastian host DNS Name",
      "Value"       : {"Fn::GetAtt" : ["BastianHost", "PublicDnsName"]}
    },
    "BastianHostIPAddress": {
      "Description" :  "The EC2 Bastian host IP Address",
      "Value"       : {"Fn::GetAtt" : ["BastianHost", "PublicIp"]}
    },
    "OracleXeHostName": {
      "Description" :  "The Database Host DNS Name\n",
      "Value"       : {"Fn::GetAtt" : ["OracleXeHost", "PrivateDnsName"]}
    },
    "OracleXeHostIPAddress": {
      "Description" :  "The Database Host IP Address\n",
      "Value"       : {"Fn::GetAtt" : ["OracleXeHost", "PrivateIp"]}
    },
    "PostgreSQLInstanceID": {
      "Description" :  "The PostgreSQL Instance ID",
      "Value"       : {"Ref" : "DbIdentifier"}
    },
    "PostgreSQLEndpointAddress": {
      "Description" :  "The PostgreSQL database endpoint",
      "Value"       : {"Fn::GetAtt" : ["RdsPostgreSQL", "Endpoint.Address"]}
    }
  }
}
