3

I have a perfectly fine Swift-Docker-Kitura project on a Mac ...

enter image description here

You work on the code using Xcode, and then in terminal you docker build. It builds. You docker run and you can even see the web page locally on localhost. You can then docker push and it goes to the main AWS cloud and is hosted.

That's all great.

But what if I want to use Swift-Docker-Kitura "not on a Mac"?

Can you "build" and "push" such a swift project - on some sort of shell on AWS (or a similar service)?

Fattie
  • 27,874
  • 70
  • 431
  • 719

2 Answers2

6

There are multiple options to build and run Kitura web applications, though not all of the options are officially supported.

  1. Run it from an Xcode project on Mac, build in Xcode.
  2. Run it in the command line on Mac, build using swift build.
  3. Run it in the command line on Ubuntu, build using swift build.
  4. Run it on a cloud that supports Cloud Foundry buildpacks. See https://github.com/IBM-Swift/swift-buildpack. For example, on IBM Bluemix, see https://console.bluemix.net/catalog/starters/runtime-for-swift.
  5. Run it in an Ubuntu docker, see http://www.kitura.io/en/starter/leveragedocker.html, on any platform that supports docker.
  6. Create a docker image with the Kitura application code (see https://github.com/IBM-Swift/swift-ubuntu-docker#using-ibmcomswift-ubuntu-runtime) and push it to any cloud that supports executing docker images. For example, IBM Bluemix Kubernetes Cluster https://console.bluemix.net/containers-kubernetes/catalogCluster.
  7. You can even embed it in an iOS application, see https://developer.ibm.com/swift/2017/03/13/kitura-ios/.
  8. And you can run it on a mainframe, see https://developer.ibm.com/swift/2017/10/05/swift-zos-swift-4-0-beta-update/.

From iPhone to mainframe, on Mac, Ubuntu, Docker and various cloud providers, these are the options to run Kitura.

Disclaimer: I work in IBM. As of the day of this edit (December 28-th 2017), to my best knowledge, Kitura is supported by IBM only in the environments described here https://developer.ibm.com/swift/2017/10/30/commercial-support/.

Vadim Eisenberg
  • 3,337
  • 1
  • 18
  • 14
  • Hey Vadim, happy new year, thanks much for that answer. It's definitely more information than I had. i still really struggle though to understand "how the heck do I run on AWS? (or something??)" ... I just can't really "get started" :/ That link too while vastly expanding my knowledge seems to leave out the basics, I don't know...... :O thanks again though – Fattie Dec 27 '17 at 17:27
  • Hey @Fattie, Happy New Year! I have edited and extended my answer. Hope it is more clear now. – Vadim Eisenberg Dec 27 '17 at 20:20
2

Your options are limited. First of all Swift is only certified at this moment on Ubuntu. So the host you want to develop on either must run Ubuntu or alternatively must run Docker under which you can run Ubuntu.

In theory you can also compile swift on your target host. I did this for an older version of Swift once for Debain (Jessie) and although I managed to get it compiled it was certainly not stable. Things may have improved since then, I haven't checked that.

So the easiest way is to rely on Docker. This also allows you to still develop on your mac by editing in Xcode and then on the command line you have the options to compile it manually.

I use the following bash function to open my Linux environment:

SwiftDocker2 ()
{
    name=`perl -e 'open IN, "</usr/share/dict/words";rand($.) \
        < 1 && ($n=$_) while <IN>;print $n'`;
    docker run -i -t --name=${name} -h ${name} --log-driver=json-file \
        -w /project -v $(pwd):/project -p 9000:9000 ibmcom/swift-ubuntu /bin/bash;
    echo "Created image ${name}";
    echo "Stopping image";
    docker stop ${name};
    echo "Removing image";
    docker rm ${name}
}

In the above example I open port 9000 which may not be required by your application.

The last point I would like to make is the use of the build directory. I use a Makefile to do the build and in the Makefile I have the following section:

BUILD_LINUX = "./.build-linux"
BUILD_MACOS = "./.build-macos"
UNAME = $(shell uname)

ifeq (${UNAME}, Darwin)
BUILD_PATH = ${BUILD_MACOS}
else
BUILD_PATH = ${BUILD_LINUX}
endif

xcode:
    @swift package --build-path ${BUILD_PATH} -Xlinker -L/usr/local/lib generate-xcodeproj

build:
    rm -f Package.pins
    swift build --build-path ${BUILD_PATH} -Xlinker "-L/usr/local/lib"

By using this construction the MacOS environment will use the directory .build-macos for the build and Linux will use the file .build-Linux. The file Package.pins is removed during build to ensure that some memory about versions to use is not transferred between the two environments.

Fred Appelman
  • 716
  • 5
  • 13
  • Hey Fred, happy new year and thanks a million for this. I don't totally understand what you mean: say I have an AWS account and I create a (??) Ubuntu Thing. Is your first code fragment something I would run **on that shell** or do you mean locally on my Mac? Trying to figure this out, thanks! – Fattie Dec 27 '17 at 17:36
  • Just BTW I'll send over ANOTHER bounty when the time limit comes up :) thanks... – Fattie Dec 27 '17 at 17:37
  • I wasn't understanding that you were using AWS and I don't think it relevant; i.e. you have a Docker environment where you deploy. I myself am running on a Mac and use the function to switch temporarily to the Docker environment which is hosted on my Mac. As you indicated you already know how to push to Docker so ignore that part of my answer please. So if you login into the Ubuntu host where you want to run the development you could run the command: swift build That command is then enough to build your source code. – Fred Appelman Dec 28 '17 at 19:43
  • It will expect to find a Package.swift file to direct your compilation. See also The Makefile fragment above is something I used to build etc. E.g. instead of running `swift build` I will run `make build` which makes that the build is done in .build-linux instead of the standard .build directory. – Fred Appelman Dec 28 '17 at 19:43