Skip to content
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

Add exposed DSN option for postgres:info command #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ dokku postgres:info lolipop
# you can also retrieve a specific piece of service info via flags
dokku postgres:info lolipop --data-dir
dokku postgres:info lolipop --dsn
dokku postgres:info lolipop --exposed-dsn
dokku postgres:info lolipop --exposed-ports
dokku postgres:info lolipop --id
dokku postgres:info lolipop --internal-ip
Expand Down Expand Up @@ -265,6 +266,27 @@ The `import` command should be used with any non-plain-text files exported by `p
dokku postgres:connect db < ./dump.sql
```

## Exposed DSN

```shell
# expose the database (you must open the exposed port on your firewall if you have one)
dokku postgres:expose lolipop

# exposed dsn available on service info
dokku postgres:info lolipop
=====> Container Information
...
Exposed dsn: postgres://postgres:[email protected]:28804/lolipop
Exposed ports: 5432->28804
...
```

So now you connect to your database with [`pgcli`](https://www.pgcli.com/) or with your favorite app.

```shell
pgcli $(ssh [email protected] postgres:info lolipop --exposed-dsn)
```

## Security

The connection to the database is done over SSL. A self-signed certificate is
Expand Down
1 change: 1 addition & 0 deletions common-functions
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ service_info() {
"--config-dir: ${SERVICE_ROOT}/config"
"--data-dir: ${SERVICE_ROOT}/data"
"--dsn: ${SERVICE_URL}"
"--exposed-dsn: $(service_exposed_url "$SERVICE")"
"--exposed-ports: $(service_exposed_ports "$SERVICE")"
"--id: ${SERVICE_CONTAINER_ID}"
"--internal-ip: $(get_container_ip "${SERVICE_CONTAINER_ID}")"
Expand Down
15 changes: 15 additions & 0 deletions functions
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,18 @@ service_url() {
local PASSWORD="$(service_password "$SERVICE")"
echo "$PLUGIN_SCHEME://postgres:$PASSWORD@$SERVICE_DNS_HOSTNAME:${PLUGIN_DATASTORE_PORTS[0]}/$DATABASE_NAME"
}

service_exposed_url() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
[[ ! -f $PORT_FILE ]] && echo '-' && return 0
local GLOBAL_VHOST_FILE="$DOKKU_ROOT/VHOST"
[[ ! -f $GLOBAL_VHOST_FILE ]] && echo '-' && return 0

local PORTS=($(cat "$PORT_FILE"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This port may not be exposed to the public, so this external dsn would fail to work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "This port may not be exposed to the public"?

You mean because of a firewall for example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The port is by default only on the internal ip address. You'd need to call SERVICE:expose on the service (with the port you want to map it to specified) and only then would this code work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe I missed some explanation. I currently run ssh [email protected] postgres:expose postgres-database before pgcli $(ssh [email protected] postgres:info postgres-database --external-dsn). If not External dsn returns - as Exposed ports do.

$ ssh [email protected] postgres:info postgres-database          
=====> Container Information
       Config dir:          /var/lib/dokku/services/postgres/postgres-database/config
       Data dir:            /var/lib/dokku/services/postgres/postgres-database/data
       Dsn:                 postgres://postgres:xxxxxxxxxxxx@dokku-postgres-postgres-database:5432/postgres_database
       External dsn:        -                        
       Exposed ports:       -                        
       Id:                  9dc918204b5fb0a4b8848a71
       Internal ip:         172.17.0.3               
       Links:               postgres-database       
       Service root:        /var/lib/dokku/services/postgres/postgres-database
       Status:              running                  
       Version:             postgres:10.4  
$ ssh [email protected] postgres:expose postgres-database
$ ssh [email protected] postgres:info postgres-database          
=====> Container Information
       Config dir:          /var/lib/dokku/services/postgres/postgres-database/config
       Data dir:            /var/lib/dokku/services/postgres/postgres-database/data
       Dsn:                 postgres://postgres:xxxxxxxxxxxx@dokku-postgres-postgres-database:5432/postgres_database
       External dsn:        postgres://postgres:[email protected]:28804/postgres_database                        
       Exposed ports:       5432->28804                        
       Id:                  9dc918204b5fb0a4b8848a71
       Internal ip:         172.17.0.3               
       Links:               postgres-database       
       Service root:        /var/lib/dokku/services/postgres/postgres-database
       Status:              running                  
       Version:             postgres:10.4 

Maybe I have to update the README for more explanation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right I'm just concerned that if the db is not exposed, then people will get confused and start filing issues about how this doesn't work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll work on updating README and maybe add precision on command help info about the option.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do some changes on README and flag description.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be empty if the service is not exposed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

External dsn: already returns - if the service is not exposed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fine for you?

local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
local DATABASE_NAME="$(get_database_name "$SERVICE")"
local GLOBAL_VHOSTS=($(cat "$GLOBAL_VHOST_FILE"))
echo "$PLUGIN_SCHEME://postgres:$PASSWORD@${GLOBAL_VHOSTS[0]}:${PORTS[0]}/$DATABASE_NAME"
}
2 changes: 2 additions & 0 deletions subcommands/info
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ service-info-cmd() {
#E dokku $PLUGIN_COMMAND_PREFIX:info lolipop --config-dir
#E dokku $PLUGIN_COMMAND_PREFIX:info lolipop --data-dir
#E dokku $PLUGIN_COMMAND_PREFIX:info lolipop --dsn
#E dokku $PLUGIN_COMMAND_PREFIX:info lolipop --exposed-dsn
#E dokku $PLUGIN_COMMAND_PREFIX:info lolipop --exposed-ports
#E dokku $PLUGIN_COMMAND_PREFIX:info lolipop --id
#E dokku $PLUGIN_COMMAND_PREFIX:info lolipop --internal-ip
Expand All @@ -23,6 +24,7 @@ service-info-cmd() {
#F --config-dir, show the service configuration directory
#F --data-dir, show the service data directory
#F --dsn, show the service DSN
#F --exposed-dsn, show the exposed service DSN (you must expose the service first)
#F --exposed-ports, show service exposed ports
#F --id, show the service container id
#F --internal-ip, show the service internal ip
Expand Down
3 changes: 3 additions & 0 deletions tests/service_info.bats
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ teardown() {
run dokku "$PLUGIN_COMMAND_PREFIX:info" l --dsn
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:info" l --exposed-dsn
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:info" l --exposed-ports
assert_success

Expand Down