Pascal Dynamic Libraries: More Than You Want To Know

Part 4: Testing on Amazon Web Services


Contents

Introduction
Ubuntu server
Windows server
Misc. notes


Introduction

These notes discuss testing several of Part 3's libraries and examples on Amazon Web Services (AWS), focusing on the Free Tier that currently provides 12 months of free use of both Windows and Linux server instances.

More specifically, these notes should be of interest to the following groups:

You can sign up for a free AWS account here: aws.amazon.com


Ubuntu server

Once you've logged into your AWS account, go to the EC2 Dashboard and create a Linux instance. You do this on the Instances panel by clicking Launch Instance and choosing an Amazon Machine Image (AMI) that you want to use. The notes that follow assume that you've selected an Ubuntu Server 14.04 LTS AMI. Amazon Linux, Red Hat Linux and SUSE Linux AMIs are also available, but these are not covered here. Note that all AMIs are 64-bit.

Once you launch the instance, you'll be prompted to create a public key and download its private key file (.pem extension). You'll need this file to SSH into the instance from your computer.

Once the instance is running, you can SSH into it. For example, on a Mac, if your key file file is named myubuntu-1.pem, enter this in a Terminal window:

  ssh -i myubuntu-1.pem ubuntu@xx.xx.xx.xx
where xx.xx.xx.xx is your instance's public IP address, which you can get from the EC2 Dashboard's Instances panel. This will log you into your instance's home folder ("ubuntu"). Enter "exit" to log out.

Important! If you get an error message about permissions for your key file, be sure to do this:

  chmod 400 myubuntu-1.pem
Once logged in, you can use sudo on your Ubuntu instance to install just about anything you need. We'll start with installing Free Pascal.

Tip: If you need a text editor on your Ubuntu instance, try the pre-installed nano.

Installing Free Pascal

Since you won't be able to run a Web browser on your Ubuntu instance via SSH, you'll need to use apt-get, curl, Subversion, etc. to install software. Unfortunately, apt-get will install an out-of-date version of the Free Pascal compiler for 64-bit Linux. And there doesn't appear to be a directly reference-able FPC .deb file that can be retrieved by curl. However, you can download the FPC .deb from the Lazarus SourceForge site in your computer's browser and then copy it to your Ubuntu instance using a script. For example, you could create a file on your computer named copy2ubu.sh and paste these lines into it:

if [ "$1" = "" ]; then
  echo "No file specified"
elif ! [ -e "$1" ]; then
  echo "File does not exist"
else
  scp -i myubuntu-1.pem "$1" ubuntu@ec2-xx-xx-xx-xx-xxxxx.compute.amazonaws.com:~
fi
(The string between the "@" and the ":" is your Ubuntu instance's public DNS, which can be obtained from the Instances panel.)

Now use the script to copy the .deb file to your Ubuntu instance like this:

  ./copy2ubu.sh fpc_2.6.4-140420_amd64.deb
Install the package on your Ubuntu instance like this:

  sudo dpkg -i fpc_2.6.4-140420_amd64.deb
Tip: To copy a file from your Ubuntu instance to your computer, you can use a script like this on your computer:

  scp -i myubuntu-1.pem ubuntu@ec2-xx-xx-xx-xx-xxxxx.compute.amazonaws.com:"$1" .

Testing Free Pascal

On your Ubuntu instance, retrieve Part 3's example source code like this:

  curl --remote-name https://macpgmr.github.io/MacXPlatform/p4g.zip
Unzip this file in a subfolder under your home folder.

You'll also need the Web Service Toolkit (WST) to compile Part 3's ndfd library, so first install Subversion:

  sudo apt-get install subversion
Now change to a subfolder where you want to download the WST source and enter this:

  svn co https://svn.code.sf.net/p/lazarus-ccr/svn/wst/trunk .
Next change to the p4g example code's ndfd subfolder and compile the ndfd library with FPC (adjust the -Fu path to point to your WST source):

  ppcx64 -CiroR -O2 -XX -Cg -k-fPIC -Fu~/Tools/WST -olibndfd64.so ndfd.pas
To compile the examples below, you'll need to put the library somewhere that the linker can find it. You can just copy it to /usr/lib like this:

  sudo cp -p libndfd64.so /usr/lib
Now compile the example testndfd.pas console app:

  ppcx64 -Fu~/Tools/WST testndfd
And test the console app:

  ./testndfd

Testing the GDAL unit

First install GDAL. Since Ubuntu's default GDAL repository may be out-of-date, add this repository:

  sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable && sudo apt-get update
Now install the GDAL/OGR programs and libraries:

  sudo apt-get install gdal-bin libgdal-dev
Check to see what version of GDAL is installed by running one of the command-line utilities:

  ogr2ogr --version
Now change to the p4g example code's gdal subfolder and compile the example testgdal.pas console app:

  ppcx64 testgdal
And run it:

  ./testgdal
You should see the same version that ogr2ogr displayed.

Testing Mono

Some version of Mono will already be installed on Ubuntu, but follow the steps on www.mono-project.com for setting up the current versions of Mono and mono_mod. Then install the current Mono:

  sudo apt-get install mono-runtime && sudo apt-get update
Now change to the p4g example code's ndfd subfolder and compile the C# wrapper class and testndfd.cs console app:

  ./comp-cs-interop.sh
  ./comp-cs.sh
By default, these scripts use the /platform:x86 switch, which sets the Ndfd.Interop.dll assembly's Corflags 32BIT flag, but Mono ignores this .NET flag and still expects a 64-bit ndfd library. However, it will still look for libndfd.so, instead of libndfd64.so. You can change the comp-cs-interop.sh script and add the /define:USE_64_BIT switch to change this behavior, but it's probably easier just to redirect via a symlink:

  sudo ln -s /usr/lib/libndfd64.so /usr/lib/libndfd.so
Now you can run the test app:

  mono testndfd.exe

Compiling AspCast

Retrieve the DotSpatial assemblies like this:

  curl --remote-name https://macpgmr.github.io/MacXPlatform/DotSpatial_p4g.zip
Unzip these files in the AspCast example's refs subfolder. Also copy the Ndfd.Interop.dll assembly to the refs subfolder.

Now compile the AspCast ASP.NET Web app with Mono:

  xbuild AspCast.sln
To prepare for deployment, zip up these files:

  zip aspcast Default.aspx
  zip aspcast ImageHandler.ashx
  zip aspcast Global.asax
  zip aspcast web.config
  zip aspcast Images/*.*
  zip aspcast Layers/*.*
  zip aspcast bin/*.*

Deploying AspCast

First install Apache and mod_mono:

  sudo apt-get install apache2 libapache2-mod-mono
Now deploy the AspCast files (substitute your folder for Tools):

  cd /var/www/html
  sudo mkdir aspcast
  cd aspcast
  sudo cp -p ~/Tools/p4g/AspCast/AspCast/aspcast.zip .
  sudo unzip aspcast.zip
  sudo rm aspcast.zip
Make sure you've created the /usr/lib/libndfd.so symlink as described above.

Also make sure Apache is running:

  sudo service apache2 restart
Finally, on the EC2 Dashboard's Security Groups panel, edit the Inbound rules for your Ubuntu instance's security group. By default it only allows SSH, so you'll need to add HTTP (don't delete SSH).

Now, in a Web browser back on your computer, paste your Ubuntu instance's public DNS (from Instances panel) and add "/aspcast" to it (without quotes). Enter this URL and the AspCast app should appear in your browser.

Testing Python

Python 2.7 and 3.4 interpreters will already be installed, but you'll need to install the Python libraries:

  sudo apt-get install libpython2.7-dev libpython3.4-dev
Change to the p4g example code's ndfd subfolder and compile the Python extension modules:

  ppcx64 -B -Cg -ondfdmod64.so ndfdmod.pas
  ppcx64 -B -Cg -dUSE_PYTHON3 -ondfdmod64_3.so ndfdmod.pas
Test with both Python 2.7 and 3.4:

  python testndfd.py
  python3 testndfd.py

Windows server

Log into your AWS account, go to the EC2 Dashboard and create a Windows instance. You do this on the Instances panel by clicking Launch Instance and choosing an Amazon Machine Image (AMI) that you want to use. The notes that follow assume you've selected a Microsoft Windows Server 2012 R2 Base AMI. Note that all AMIs are 64-bit.

Once you launch the instance, you'll be prompted to create a public key and download its private key file (.pem extension). You'll need this file to connect to the instance from your computer.

Next install a remote desktop client app on your computer. For example, on a Mac, download the Microsoft Remote Desktop app from the Apple App Store. Then follow Amazon's instructions for connecting to your Windows instance.

Tip: On a Mac, once you've downloaded the .rdp file, you can double-click it to start Microsoft Remote Desktop and connect to your Windows instance. But to customize your setup, you'll need to use File | Import and select the .rdp file. Now you can edit the setup. For example, you can turn off the default full screen mode and scale the Windows desktop to the size of the window on your Mac. You can also set up a shared folder on your Mac for copying files to and from your Windows instance.

Installing and testing software

Because Windows Server 2012 provides a normal Windows desktop, you can install and test software the same as you would on non-server Windows. The desktop has the Windows 8 look, where the Start menu has been replaced by a kind of start screen.

To test Part 3's examples, you can download and install whatever software you need, including Free Pascal, Subversion (for retrieving Web Service Toolkit source), Xamarin Studio, Python, etc. Note that .NET 4.0 is already installed.

If you need a text editor, Windows includes both Notepad and WordPad. Notepad does not know how to handle text files with Unix-style line endings, but WordPad does.

Windows Server includes something called PowerShell, but the familiar Command Prompt is also available.

Tip: On a Mac laptop, use a two-finger click on the trackpad to simulate a right-click. The normal control+click combination won't work in Windows.

Compiling AspCast

You can compile and test AspCast with Xamarin Studio, as described in Part 3. Be sure to copy the ndfd.dll library to the AspCast bin subfolder and the DotSpatial and Ndfd.Interop.dll assemblies to the AspCast refs subfolder.

To prepare for deployment, zip up these files:

  zip aspcast Default.aspx
  zip aspcast ImageHandler.ashx
  zip aspcast Global.asax
  zip aspcast web.config
  zip aspcast Images/*.*
  zip aspcast Layers/*.*
  zip aspcast bin/*.*

Deploying AspCast

Follow Amazon's instructions for installing Microsoft's IIS Web server.

Next create a subfolder named "aspcast" under C:\inetpub\wwwroot and unzip the AspCast files in this subfolder.

Finally, make sure you've done the following:

Now, in a Web browser back on your computer, paste your Windows instance's public DNS (from Instances panel) and add "/aspcast" to it (without quotes). Enter this URL and the AspCast app should appear in your browser.

Deploying QxCast

Now, in a Web browser back on your computer, paste your Windows instance's public DNS (from Instances panel) and add "/qxcast" to it (without quotes). Enter this URL and the QxCast app should appear in your browser.


Misc. notes

  1. If you stop your instance, its public IP address may change the next time you start it. Presumably when an instance is not running, its IP address is returned to a pool of addresses that Amazon uses. You'll need to edit any scripts that use the IP address. However, just rebooting the instance does not change the instance's IP address.

  2. In an instance's security group, you can specify whether inbound traffic is allowed from any IP address (0.0.0.0), a range of IP addresses, or even a specific IP address (eg, your local computer's). Keep in mind that if you limit SSH or RDP inbound traffic to a specific IP address, you may need to update the IP address if your ISP routinely changes the IP address that you use. When editing the security group, you can choose "My IP" from the Source drop-down list to update the IP address to whatever you're currently logged into AWS with.

  3. Instance time zones are set to UTC by default.


Copyright 2015 by Phil Hess.

macpgmr (at) icloud (dot) com

First posted Sept. 13, 2015; last edited June 10, 2017.