Skip to Content
Technical Articles

SAP Cloud Platform Backend service: Tutorial [23]: using command line for cloud

Hey,
Reading this blog increases your productivity!

Wow – but what does that mean?
With other words:
This blog helps you getting started using the command line tool for interacting with the cloud.
And more other words:
This blog is part of a series of tutorials explaining the usage of SAP Cloud Platform Backend service in detail.

Quicklinks:
Installation
Reference

In this particular case: use the command line to deploy applications to SAP Cloud Platform Cloud Foundry environment. Deploy and maintain, and other
I’m a bit scared…
Don’t worry:
The command line is not required for using the Backend service.
Only if you develop cloud-applications you can use the command line client.
And what if I don’t want?
No problem.
We can still be friends?
Sure.

The series of tutorials explains how to call Backend-service-APIs from applications in the cloud.
As such, I feel it is helpful to explain the Cloud Foundry command line client tool

How does this tool look like?
Well, it looks like a command line client tool:

Ahhh – it is a command shell…Why you didn’t tell?
Ehmm…
Why do I need to go to command shell?
First of all: it is not required. You can do everything via the Cloud Cockpit
Then why this blog?
The command line is faster.
You don’t need many clicks to navigate to the different UI screens
You don’t need to login again and again, because of browser session timeouts
You can copy and paste long commands, avoiding multiple wizard steps.
Cool

In this blog you’ll find the muse useful commands which will make your life more efficient
Not easier?
It will make your life more productive
Not easier?
Ehm, not really easier, but faster
I’m still not convinced
If you use the command line, you look more professional and this will impress your boss
Oh, cool, now I’m convinced! Let’s start…
Sigh…

Prerequisites

Nothing.
Really?
OK, the only prerequisite: you need an account in the cloud (e.g. Trial account)

Installation

Installation is simple:
Download Cloud Foundry CLI, the official command line client for Cloud Foundry
Unzip
Install

No cryptic configuration required?
No.

Download the zip from here: https://github.com/cloudfoundry/cli#downloads
There are 2 zips, which one is “the zip” ????
I would choose the installer. Direct link:
https://packages.cloudfoundry.org/stable?release=windows64&source=github

After download, unzip it.
Right click the installer.exe file and choose “Run as administrator”
That’s it.
The installer installs the binary file to C:\Program Files\Cloud Foundry
If you look into that directory, you’ll see a cf.exe file.
This is “THE TOOL”

Verify the installation:
Open your command shell and type cf , then press enter
As a result, the version of “Cloud Foundry command line tool” is printed, along with the usual usage instructions (obviously, the command is not complete)

Note:
In case of proxy issues:
See http://docs.cloudfoundry.org/cf-cli/http-proxy.html

Usage

 1 

First thing you need to do with the command line client tool is:
To point the client to your landscape.
This is done only once, then the client stores it in the user settings (see Appendix)

The command syntax:

cf api <endpoint>

Ya, ya, very helpful. How do I know the endpoint???
I’ve described it in the Appendix
Example:
cf api https://api.cf.eu10.hana.ondemand.com

After executing this command, the target endpoint is stored in the user settings. And it remains there until you change the endpoint by executing the command again, pointing to a different endpoint
After setting the endpoint, the client tool recognizes that we’re not logged in and it gives a hint.
So let’s log in

 2 

Second thing:
logging in
The command: cf login

After executing this command, the client tool prints the current endpoint, so you can be sure to provide the corresponding credentials.
Furthermore, the client tool asks for the next piece of information, required to login: The username.
In case of Trial, it is the Email.
After entering the Email and pressing Enter, the client asks for password
After entering the password, the client asks for Cloud Foundry org (only if there’s more than one)
Choose the org by entering the number of it
After choosing the org, the space has to be chosen (only if there’s more than one)

Finally, we’re in.
Below screenshot shows the login process, the user input is marked red

Note:
Even the user login is stored in the user settings, so you don’t need to re-enter the credentials every time you use the client.
That’s a big advantage comparing with the cockpit, where the browser requires very frequent login

OK, nice, I can do that
That’s it, from here you start the daily work

Note:
Instead of executing 2 commands plus interactive input, it is possible to enter everything in one command with params, and it can be stored in the history or in a note. See below for that extended login command

The daily work might start viewing the list of deployed apps (and their status)
The command: cf apps

The result would look similar like this:

 

Useful Examples

Login
You don’t need to execute the cf api <…> command, if you add the api as a parameter to the login command, like shown in the following example:
Login with api and user credentials
cf login -a https://api.cf.eu10.hana.ondemand.com -u youruser@gmail.com
-p Abc123

login with really everything, including org and space:
cf login -a <api> -u <user> -p <pwd> -o <org> -s <space>

Deploy an application
Probably the most frequent command:
cf push

The command requires the manifest file, in order to know what to deploy.
But it assumes that the manifest file is located in the current directory.
As such, you should first navigate to the root directory of your app, where the manifest.yml file is located. From there you can just execute cf push

Logs
Of course, one of the most important commands. View the log
There are 2 flavors:
1) Stream the logs.
Type the command in command line, then run the app and view the log entry while they’re
written
cf logs my App

2) Open the logs to view the logs after they occurred
cf logs my App –recent

Create service instance
Creating an instance of a Cloud Foundry service requires many clicks in the cockpit
The create-service command requires the following parameters:
<service name>
<service plan name>
<service instance name>

Example:
cf create-service xsuaa application myInstanceName

In case of xsuaa, we need to pass parameters.
This can be done by pointing to a file.
Or if the file is not tool long, provide it inline.
Like shown in the following example:
cf create-service xsuaa application myInstanceName
-c “{ \”xsappname\”:\”myName\”,\”tenant-mode\”:\”dedicated\” }”

Environment Variables
To view the environment variables of an app, use the env command followed by the app name

cf env <myAppName>

Example:
cf env myApp

Deletion
To delete an app, I would recommend 2 options:
Delete the route of the app and
Force the deletion without prompt

Example:
cf delete myApp -r -f

Using Alias

Read this chapter to become even more cool…!
Even more productive, more efficient, more impressing
Great, I will love it…!
My tip:
Use abbreviations
Every command has an abbreviation, an alias
Less typing

Example:
Instead of typing: cf create-service <…>, it is enough to type:  cf cs <…>

The following list contains the alias for each command

Reference

List of the most important commands.
Note: replace myApp with the name of your app

cf help cf h
cf help -a
cf help create-service
cf login cf l
cf login -a <> -u <> -p <> -o <> -s <>
cf logout cf lo
cf api 
cf api <https….>
cf target cf t
cf push cf p
cf apps cf a
cf app myApp
cf delete myApp cf d
cf start myApp cf st
cf stop myApp cf sp
cf restage myApp cf rg
cf env myApp cf e
cf logs myApp
cf logs myApp –recent
cf marketplace cf m
cf services cf s
cf service myService
cf create-service cf cs
cf create-service <serviceName> <servicePlan> <instanceName>
cf bind-service cf bs
cf delete-service cf ds
cf update-service
cf map-route myApp cfapps.eu10.hana.ondemand.com –hostname myNewRoute
cf delete-orphaned-routes

This was only my personal useful-command-list
You can find a better and complete list in the official CF CLI Reference Guide of Cloud Foundry
Alternatively: type cf help -a to get a full list of all available commands

Links

SAP Help Portal:
Getting started with a trial account: here
Working with the Cloud Foundry Command Line Interface: here

Official Cloud Foundry documentation:
The Cloud  Foundry command line client tool: https://github.com/cloudfoundry/cli
Installation: https://docs.cloudfoundry.org/cf-cli/install-go-cli.html
Push: https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html

Appendix 1

Some more useful information

API endpoint URLs

For Trial accounts, the following API endpoints are possible:

https://api.cf.eu10.hana.ondemand.com
https://api.cf.eu20.hana.ondemand.com
https://api.cf.us10.hana.ondemand.com
https://api.cf.us30.hana.ondemand.com

For the full list, refer to the SAP Help Portal:
Regions and API Endpoints Available for the Cloud Foundry Environment

Note:
Before using one of them, you need to sign up

To find the API endpoint for your account:
Go to the overview page of your subaccount.
View the API endpoint on the right side:

User settings

The location of user settings in Windows:

C:\users\youruser\.cf

The file: config.json contains the user settings
There’s also the folder with installed plugins

Appendix 2: MTA

The Cloud foundry command line client can be extended, it offers a hook for adding self-written plugins.
If you’re working with Multi-Target Applications (MTA), you need the MTA plugin in order to deploy an MTA archive
Please refer to the SAP Help Portal

More info about plugins: https://docs.cloudfoundry.org/cf-cli/use-cli-plugins.html

1 Comment
You must be Logged on to comment or reply to a post.