Create and Push an App programmatically from another App in Cloud Foundry - cloud-foundry

Is it possible to create and push an application into Cloud Foundry from a different application that is already running in Cloud Foundry? Essentially I need to do a 'cf push' from within the application.
I saw there's a Cloud Foundry API that I can use (https://apidocs.cloudfoundry.org/237/apps/creating_an_app.html). Can anyone give me an example?

The Cloud Foundry Java Client is designed for use cases like this. It takes care of negotiating the OAuth2 security used by the CF API and provides nice abstractions for common operations that require multiple CF API calls (e.g. pushing an app, which involves creating app metadata, pushing the bits, and starting the app). I would prefer this option over using Java to shell out to the cf CLI, as you get greater control over the CF API and aren't subject the CLI UI to detect success/failure.
There's nothing stopping an app from running on CF from pushing other apps, but the platform doesn't do a lot to help with this either. Specifically, you'll need the address of the CF API endpoint (e.g. http://api.mycf.example.com) to be able to target, authenticate, and deploy apps. But there is no way for an app running on CF to know the API endpoint of the CF it is running on. So you will need to configure the CF API endpoint into the application that will be pushing other apps.

TL;DR Yes it is possible. Shell out to the CF cli.
The easiest way to do this would be to just include the CF cli in your application and shell out to it to push your app. You'll have to make sure that the binary you use matches the OS where your app is running (most likely Ubuntu 14.04). But that way, you can just provide it with your user's CF credentials and use the cf push logic in the CLI.
There is a CloudFoundry API you can use, but I'd advise against it. Trying to authenticate and push a cf app will be tricky. cf push does a lot under the hood -- it not only creates the app via the API endpoint you found, but it also uploads application files in an intelligent way, it attaches routes and services to the app, starts the app, polls the CF API for the updates on the status of the staging task, etc. You'd have to implement all of this on your own (not to mention the OAuth2 workflow to get an auth token).
There are some third-party CF libraries you might find useful (depending on the language you are using):
https://github.com/cloudfoundry/cf-java-client
https://www.npmjs.com/package/cf-nodejs-client
Lastly the CloudFoundry cli is open-sourced (https://github.com/cloudfoundry/cli). Bear in mind that as discussed above the push logic consists of multiple operations, and as such the codebase might be difficult to grok.

Related

How can I invoke a desktop app from Google Cloud Platform services?

I'm building a desktop client app (win/linux/mac) with a backend hosted in GCP (I'm considering other cloud platforms too). The desktop app should be minimalistic and provide access to local machine resources to the backend. And I'm looking for a way to invoke my app from the server (when some event occurs) and then the app would do some work on a local machine. Here's what I've tried so far.
Google Cloud Pub/Sub. Seems like it does what I need, but to make it work I have to create a service account, generate JSON key and store it locally, which is not good. I can restrict the service account access permissions, of course, but still it doesn't look good to me. Maybe there are other ways to auth my app running at the end user machine? I want to keep my desktop app minimal (ideally without UI, just an "agent" console process / Windows service). Maybe I could consider a login screen to connect the app with the backend, if that solves the problem, but I don't want to overcomplicate.
Google Cloud Run + SignalR / WebSockets. This solution also looks good, but it has one significant disadvantage. As long as there's at least one open WebSocket the Cloud Run instance is considered active and therefore billed. There are other difficulties related to scalability and container instances synchronization too.
What do you think about the options above, and what are the other possibilities? Am I left with REST API and polling for updates? I'm quite new to the cloud stuff so any help is appreciated. Thanks!
If you want to be able to invoke your local app from Google Cloud, you need 2 things
The first one, to register your app on Google Cloud, with, preferably, a auth mecanism (can be an API key for example). Like this, the GCP backend know where to call you app (which IP/port) and how (the auth mechanism)
The second is to have your app up and running and listen external communication. HTTP is the easiest way. Wait a HTTP call on the IP/port defined during the registration, check the auth and perform the process.
You can store the data (location and the auth) in firestore for example, and use Cloud Run to perform the HTTP call.
You can also inverse the solution and to poll (long polling or regular poll) the backend from the local app when it is running.
The 2 approach are possible, the second one slightly easier but can be challenging to manage the security.

Why does pivotal cloud foundry does not remove a route when a corresponding app is deleted?

I have been exploring Pivotal Cloud Foundry - Public Cloud Version by setting up an Pivotal Web Services account.
While exploring the CF CLI I followed the below tutorial.
https://pivotal.io/platform/pcf-tutorials/getting-started-with-pivotal-cloud-foundry/introduction
I was able to successfully push the sample spring boot app to PCF Cloud and follow the tutorial.
When I started to play around with it, I tried deleting the app using the below command. cf-spring is my app name.
cf delete cf-spring
When navigated to my Pivotal Web Service account, I could see the app has been deleted. The route still displays the name of the route which was created when an app was created using the cf push command.
So I tried to delete the route using the below command.
It does not allow me to delete using CLI ? Does the app delete command should have taken care to remove the related routes associated with the application ?
Try cf delete-route -h to see the syntax of the delete-route command.
You probably want to run cf delete-route cfapps.io --hostname cf-spring-rested-platypus.
In recent versions of cf-cli you can also run cf delete-orphaned-routes.
I think the reason CF doesn't completely delete the route when you delete an app is so that it kind of keeps the hostname reserved for your use, ie: someone else can't reuse it in another org or space if you've just taken down your app for a refresh. It's kind of annoying when you actually want to reuse the hostname in another space, but considering the multi-tenancy capabilities it kind of makes sense to keep them intact/reserved.
cf delete APP_NAME will not delete the route bound to the Apps and this should not happen first-of-all. Because the router-concept in Cloud foundry is that
One App can have Multiple Routes and Multiple Routes can be configured to one App.
Its Many-to-One mapping. So there is a possibility that the routes you created would still be mapped to another App and it would be as Orphaned-route, only when there are no apps for that routes..
So its a cautious decision and a wise decision too !!

Can we have Ui for cloudfoundry deployed on AWS

Iam new to Cloudfoundry on AWS. I have deployed cloudfoundry on AWS, but when i want to push an application into cf which i have deployed i was using command line tool (cf). Is there any way we have UI like Cloudfoundry where we can check our app running on UI , and even on Bluemix we can have ace.ng.bluemix.net to login and check our apps.
I want to know if i can check my appp deployed in my custom cloudfoundry deployed on AWS in UI where i can see other applications as well running.
It would be very helpful if i get this.
Thanks & regards,
Srilatha K.
There is the admin-ui, which is a bit more detailed than the Pivotal WS/CF interface but will provide you a BUI as an alternative to cf cli:
https://github.com/cloudfoundry-incubator/admin-ui
You can make use of BOSH.
It takes care of deployment & life cycles managements...
Reference - http://www.slideshare.net/ManuelGarcia122/cloud-foundry-handson-deployment-workshop
Slide 29 onwards
Useful link - http://blog.altoros.com/tag/cloud-foundry

Grails App with Multi Tenant Single DB plugin URL implementation in Cloudfoundry

I am currently creating a web app using Grails implementing Multi Tenant Single DB plugin. The plugin allows me to have multiple tenants on a single db using a tenantID to differentiate between tenants. The plugin detects witch tenant will deal the current request that is made on my app by resolving using different domains/subdomains for each tenant.
For example:
Tenant 1 = companyA.myapp.com
Tenant 2 = companyB.myapp.com
On my local machine running Grails development mode I was able to implement the different hosts by changing my /etc/hosts and each tenant would have their own subdomain.
I am currently interested in using cloud foundry as my cloud platform but when I deploy my app to cloud foundry it is already using my app name as the subdomain for cloud foundry.
For example:
- myapp.cloudfoundry.com
Is it possible to change or control the domain name resolver in
cloud foundry?
Does anybody know how to handle multi-tenant subdomains as explained above in cloud foundry? Probably provide the steps in implementing this using cloud foundry?
What is the best approach to implement this using cloud foundry?
My App is using Grails 2.0.4 and Multi Tenant Single DB plugin 0.8.2.
Thanks
Unfortunately the current beta version CloudFoundry does not allow modification of the cloudfoundry subdomain. The plan is to have the GA towards the end of this year with a private preview of the version of the site available sooner in the fall. At that time you could be able to customize the subdomain.
Therefore you might need to change a little in your TenantResolver to only check the subdomain that varies.
To implement your requirement, did you try installing the grails cf plugin? If not you can start from here.
If you are using cli, installing the plugin just needs command in your project workspace:
grails install-plugin cloud-foundry
When your app is ready for deployment, push it to cloudfoundry:
grails cf-push
Note that you will have to have your cf credentials configured in the grails config file.
After that you can map multiple URLs you want using:
grails cf-map user1.yourapp.cloudfoundry.com
If you have already known about vmc which is the client command line interface for cf, you can see the urls mapped with your app by
vmc apps
If not you can refer to the installation guide to start if you would like to do that.
If you are using STS/eclipse, things will be even easier. First you need to have grails-support extension as well as the cloud foundry integration installed. For detailed docs of the cf integration please refer here.
After your app is deployed, right click the project and choose "Grails Tools" -> "Open Grails Command Prompt". This will enable you to have same grails cf plugin commands as the CLI does.
Hope this can help your move forward in the cloudfoundry world. Let me know if you have more questions.
Thanks,
William

Write a Plug-in for Cloud foundry authentication

I need to integrate the Cloudfoundry with third party authentication tool, say, Keystone. I need to write a plug-in where it can be attached / installed to the cloud foundry with out any code changes in Cloudfoundry. If I want to use the authentication from keystone I will install the plug-in for Cloudfoundry. The code changes should only go to the plug-in and not to the Cloudfoundry code.
Please let me know how can I achieve this.
Thanks in advance,
Sateesh B.
The following link https://github.com/cloudfoundry/vcap-java-client is a java client library that does restful calls to vcap (aka cloud foundry). Now in my opinion, if you want to use a custom authentication method (be whatever you choose) in high level terms, you would have to make it into a adapter design pattern.
Once your users pass your authentication, your application can then communicate with Cloud Foundry via rest calls or libraries such as https://github.com/cloudfoundry/vcap-java-client and have their account created or etc. You just have to add that layer of your own authentication which then you can use the data from the authenticated user in creating or fetching cloud foundry info/apps.
This way you haven't touched cloud foundry and you can easily implement more than one way of authentication.