6

I'm trying to figure out how to deploy in IBM Bluemix a Cloudfoundry app that uses Vapor framework.

IBM is providing facilities and guidance for using his platform for developing server side Swift apps with his framework, Kitura. I think as a Cloudfoundry provider, with the proper Swift buildpack, we must be able to deploy generic server side Swift code.

Finally, while learning bits of CF, I reached the point that with the CloudFoundry CLI:

  1. I'm connecting to the Bluemix API endpoint (api .eu-gb .bluemix .net)
  2. Login ok (after expanding the timeout setting env. var CF_DIAL_TIMEOUT to 20)
  3. Making a "cf push". It creates the app and uploads and compiles everything.
  4. The app state is "running".

But when I load the page (https://sommobilitatcore.eu-gb.mybluemix.net/) I get:

404 Not Found: Requested route ('sommobilitatcore.eu-gb.mybluemix.net') does not exist.

Can anyone help me on that? Thanks!

Some context:

The manifest.yml:

applications:
- path: .
  memory: 256M
  instances: 1
  name: SomMobilitatCore
  disk_quota: 1024M
  buildpack: https://github.com/IBM-Swift/swift-buildpack.git

The Procfile

web: App

(main.swift is in Source/App/)

No port is configured in Vapor Config files, then Vapor is trying to listen to port 80:

import Vapor
import HTTP

let drop = Droplet()

let _ = drop.config["app", "key"]?.string ?? ""

drop.get("/") { request in
    return try drop.view.make("welcome.html")
}

(...)

let port = drop.config["app", "port"]?.int ?? 80

// Print what link to visit for default port
drop.serve()

UPDATE:

Finally get it working without Procfile, the manifest.yml

- path: .
  instances: 1
  memory: 256M
  disk_quota: 1024M
  name: SomMobilitat4
  command: App --env=production --workdir="./"
  buildpack: swift_buildpack

And the /Config/production/servers.json :

{
    "production": {
        "port": "$PORT"
    }
}

I neither specify the port variable in the main.swift file. With the updated Vapor version:

import Vapor
import HTTP

let drop = Droplet()

drop.get("/") { request in
    return "hello vapor in bluemix cloudfoundry"
}


drop.run()

If you are new to Cloudfoundry or IBM Bluemix, this is a way to work:

  1. You signup in a Cloudfoundry provider (ex: bluemix)

  2. You have you Vapor project locally.

  3. Add to it a .cfignore file with this short line: Packages/ to avoid uploading Packages to the server.

  4. Add to it the mentioned manifest.yml file.

  5. Download and install the Cloudfoundry CLI: https://docs.cloudfoundry.org/cf-cli/

    With the CLI:

  6. cf api https://api.eu-gb.bluemix.net

  7. cf login

  8. cf push

Community
  • 1
  • 1

2 Answers2

1

To run a Vapor app on Bluemix:

  1. Add Config directory with servers.json (use exactly these names). servers.json should contain the following:
        {
            "myserver": {
                "port": "$PORT"
            }
        }
      

It will instruct Vapor to start a server named myserver on the port taken from $PORT environment variable used by Bluemix.

  1. In your Procfile, add --workDir=. parameter, so it would contain:

    web: App --workDir=.
    

    It will instruct Vapor to look for Config directory in the current directory during run time.

Vadim Eisenberg
  • 3,337
  • 1
  • 18
  • 14
0

I am not very familiar with Swift and Vapor, but for any cloud foundry application you have to use the port from the VCAP_APPLICATION environment variable.

Bluemix created a useful package for Swift that you can get this port number.

In your Package.swift file add the following line:

.Package(url: "https://github.com/IBM-Swift/Swift-cfenv.git", majorVersion: 1, minor: 7)

Then you can use something like this to get the correct port number to start your application:

import CloudFoundryEnv

...

let appEnv: AppEnv
appEnv = try CloudFoundryEnv.getAppEnv()
let port = appEnv.port 

You can check Bluemix Swift start code here for more details:

https://github.com/IBM-Bluemix/Kitura-Starter

Alex da Silva
  • 4,552
  • 2
  • 17
  • 25
  • Hi Alex, I think you are right and seems a port configuration problem. I tried this way but then I get: – Daniel Valls Estella Oct 18 '16 at 09:35
  • DEA/130Instance (index 0) failed to start accepting connections – Daniel Valls Estella Oct 18 '16 at 09:36
  • I have also tried to set VCAP_APPLICATION through Vapor configuration files: but fails the same way. – Daniel Valls Estella Oct 18 '16 at 09:47
  • Hi Daniel, as Alex suggested, you may want to look at https://github.com/IBM-Bluemix/Kitura-Starter. The main thing you'll need is a manifest.yml file in your repo. If you include the command attribute in your manifest.yml file, then you won't need a Procfile. You may also want to go over the README file for the buildpack: https://github.com/IBM-Swift/swift-buildpack. You may also want to check out this repo: https://github.com/IBM-Swift/Swift-cfenv. Is your Vapor-based application on GitHub? If so, we could take a look at it. – rolivieri Oct 18 '16 at 18:08
  • For some, I think, unrelated problem I didn't get it work in that way. Finally it worked to me without the need of this package. But it's nice to know about it. Vapor can get the enviorent $PORT variable in its configuration files and it worked. Thanks. – Daniel Valls Estella Oct 24 '16 at 21:46