-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable binding of postgres CF db service to CF hosted console #1231
Changes from 10 commits
165e665
4c382c6
6bb1abc
44bd5c9
e48217c
6b9a1d9
7ee0973
3298acf
f940d82
6bd0aca
90170e2
214b7b8
938d8d3
1b5ca6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,7 @@ node_modules/ | |
bower_components/ | ||
dist/ | ||
components/*/backend/vendor | ||
dev-certs/ | ||
out/ | ||
outputs/ | ||
tmp/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package datastore | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/SUSE/stratos-ui/components/app-core/backend/config" | ||
log "github.com/Sirupsen/logrus" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
SERVICES_ENV = "VCAP_SERVICES" | ||
) | ||
|
||
type VCAPService struct { | ||
Credentials VCAPCredential `json:"credentials"` | ||
Tags []string `json:"tags"` | ||
} | ||
|
||
type VCAPCredential struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
Dbname string `json:"dbname"` | ||
Hostname string `json:"hostname"` | ||
Port string `json:"port"` | ||
Uri string `json:"uri"` | ||
} | ||
|
||
// Discover cf db services via their 'uri' env var and apply settings to the DatabaseConfig objects | ||
func ParseCFEnvs(db *DatabaseConfig) bool { | ||
if config.IsSet(SERVICES_ENV) == false { | ||
return false | ||
} | ||
|
||
// Extract struts from VCAP_SERVICES env | ||
vcapServicesStr := config.GetString(SERVICES_ENV) | ||
var vcapServices map[string][]VCAPService | ||
err := json.Unmarshal([]byte(vcapServicesStr), &vcapServices) | ||
if err != nil { | ||
log.Warnf("Unable to convert %s env var into JSON", SERVICES_ENV) | ||
return false | ||
} | ||
|
||
for _, services := range vcapServices { | ||
if len(services) == 0 { | ||
continue | ||
} | ||
service := services[0] | ||
|
||
for _, tag := range service.Tags { | ||
if strings.HasPrefix(tag, "stratos_postgresql") { | ||
dbCredentials := service.Credentials | ||
// At the moment we only handle Postgres | ||
db.DatabaseProvider = "pgsql" | ||
db.Username = dbCredentials.Username | ||
db.Password = dbCredentials.Password | ||
db.Database = dbCredentials.Dbname | ||
db.Host = dbCredentials.Hostname | ||
db.Port, err = strconv.Atoi(dbCredentials.Port) | ||
db.SSLMode = "disable" | ||
log.Info("Discovered Cloud Foundry postgres service and applied config") | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -312,7 +312,10 @@ func loadPortalConfig(pc interfaces.PortalConfig) (interfaces.PortalConfig, erro | |
|
||
func loadDatabaseConfig(dc datastore.DatabaseConfig) (datastore.DatabaseConfig, error) { | ||
log.Debug("loadDatabaseConfig") | ||
if err := config.Load(&dc); err != nil { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a plugin extension point so that the CF-specific hosting code can go in the cloud foundry hosting component. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had a look and also talked to Irfan, it's not something we can do at the moment. This is called right at the beginning of the PP start up process before the portal proxy object and plugins are initialised. Fixing this would require a bit of a rework. |
||
if datastore.ParseCFEnvs(&dc) == true { | ||
log.Info("Using Cloud Foundry DB service") | ||
} else if err := config.Load(&dc); err != nil { | ||
return dc, fmt.Errorf("Unable to load database configuration. %v", err) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Associated a Cloud Foundry database service | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be "Associate" |
||
|
||
As mentioned in the standard cf push instructions [here]("../README.md") the console as deployed via cf push | ||
does not contain any way to persist date over application restarts and db entries such as registered endpoints | ||
and user tokens are lost. To resolve this a Cloud Foundry db service can be binded to the console. Run through | ||
the steps below to implement. | ||
|
||
> **NOTE** The console supports postgresql. Your Cloud Foundry deployment should contain a service for | ||
the desired db tagged with 'postgresql'. | ||
|
||
1. Enable the endpoint dashboard | ||
* This is not strictly required, but at the moment is the only motivator to follow the next steps | ||
* Add the following to the manifest | ||
``` | ||
env: | ||
FORCE_ENDPOINT_DASHBOARD: true | ||
``` | ||
1. Create the console app and associated a postgres service instance | ||
* Use the instructions [here]("../README.md") | ||
* Log into the console and then navigate to the console application | ||
* In the `Services` tab associate a new postgres service instance to the application | ||
* Validate in the `Variables` tab that `VCAP_SERVICES` is populated with new postgres credentials | ||
* Remember to update the manifest with the services section | ||
1. Set up the associated db instance. Run the following from the root of the console | ||
``` | ||
cf push -c "deploy/cloud-foundry/db-migration/db-migrate.sh" -u "process" | ||
``` | ||
> **NOTE** All subsequent pushes, restarts, restaging will use this migration command. | ||
It's therefore very important to execute the next step in order for the console to start | ||
1. Restart the app via cf push | ||
``` | ||
cf push -c "null" | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
echo "Attempting to migrating database" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bad grammar - "Attempting to migrate database" |
||
|
||
DB_MIGRATE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
DEPLOY_DIR=${DB_MIGRATE_DIR}/../../ | ||
|
||
export STRATOS_TEMP=$(mktemp -d) | ||
|
||
export GOPATH=${DB_MIGRATE_DIR}/goose | ||
export GOBIN=$GOPATH/bin | ||
go get bitbucket.org/liamstask/goose/cmd/goose | ||
|
||
export STRATOS_DB_ENV="$STRATOS_TEMP/db.env" | ||
node ${DB_MIGRATE_DIR}/parse_db_environment.js $STRATOS_DB_ENV | ||
source $STRATOS_DB_ENV | ||
|
||
cd $DEPLOY_DIR | ||
case $DB_TYPE in | ||
"postgresql") | ||
echo "Migrating postgresql instance on $DB_HOST" | ||
$GOBIN/goose -env cf_postgres up | ||
if [ $? -eq 0 ]; then | ||
while | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its more common to put: while true; do |
||
echo "Database successfully migrated. Please restart the application via 'cf push -c \"null\"'"; | ||
sleep 60 | ||
do | ||
: | ||
done | ||
else | ||
echo Database migration failed | ||
fi | ||
;; | ||
*) | ||
echo Unknown DB type '$DB_TYPE' | ||
;; | ||
esac |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in the cloud-foundry-hosting component