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

Update postgresql to latest and enable it to run without root permissions #980

Merged
merged 1 commit into from
Feb 8, 2018

Conversation

irvingpop
Copy link
Contributor

@irvingpop irvingpop commented Nov 17, 2017

This PR updates postgresql and postgis to the latest (9.6.6 and 2.4.2 respectively) and also enables it to run without root permissions. In order to accomplish that I had to:

  • reorganize svc_data_path to include the pgdata and archive subfolders
  • remove all chown/chmod commands that require root permissions
  • remove all chpst statements which are no longer needed

@thesentinels
Copy link
Contributor

Thanks for the pull request! Here is what will happen next:

  1. Your PR will be reviewed by the maintainers
  2. If everything looks good, one of them will approve it, and your PR will be merged.

Thank you for contributing!

@irvingpop
Copy link
Contributor Author

cc: folks who have touched this plan recently: @themightychris @bodymindarts @eeyun for review

@@ -59,13 +58,12 @@ master_ready() {
bootstrap_replica_via_pg_basebackup() {
echo 'Bootstrapping replica via pg_basebackup from leader '

rm -rf {{pkg.svc_data_path}}/*
pg_basebackup --pgdata={{pkg.svc_data_path}} --xlog-method=stream --dbname='postgres://{{cfg.replication.name}}@{{svc.leader.sys.ip}}:{{cfg.port}}/postgres'
rm -rf {{cfg.data_directory}}/*
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: we should put some kind of guard around this, in case {{cfg.data_directory}} evaluates to "" and we blow away /

@@ -15,4 +16,4 @@ name = 'replication'
password = 'replication'
lag_health_threshold = 1048576
enable = false
archive_path = "{{pkg.svc_path}}/archive"
archive_path = "hab/svc/postgresql/data/archive"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

typo here, I got it

@irvingpop
Copy link
Contributor Author

update: reworked the data path stuff after feedback form @elliott-davis

@elliott-davis
Copy link
Contributor

My only other concern here is what happens to your data on upgrade?

@irvingpop
Copy link
Contributor Author

super good point - this would totally break anyone who's upgrading their postgresql plan. argh!

As an alternative, what if we keep the postgres data in svc_data_path and then put the archive dir into svc_var_path? I'm unclear how much it matters for the archive directory to survive a container stop/start

@elliott-davis
Copy link
Contributor

@irvingpop I feel like you were on to the right solution about creating a pgdata dir in data but maybe just check to see if that directory exists and if not just create and move it in the init/reconfigure hook?

@irvingpop
Copy link
Contributor Author

irvingpop commented Nov 20, 2017

pseudo-coding out a couple options here for the init hook, thoughts?

  1. Auto-detect pg data in the root of svc_data_path, attempt to move it for you
if [[ -f "{{pkg.svc_data_path}}/PG_VERSION" ]]; then
  echo "PGDATA detected in the root of the data path ( {{pkg.svc_data_path}} ), relocating...."
  mkdir -p {{pkg.svc_data_path}}/pgdata
  # bash extended globbing can cleanly move everything under a subfolder http://www.linuxjournal.com/content/bash-extended-globbing
  mv {{pkg.svc_data_path}}/!(pgdata) {{pkg.svc_data_path}}/pgdata/
else
  mkdir -p {{pkg.svc_data_path}}/pgdata
fi
  1. Auto-detect pg data in the root of svc_data_path, and exit if it exists:
if [[  -f "{{pkg.svc_data_path}}/PG_VERSION" ]]; then
  echo "PGDATA detected in the root of the data path ( {{pkg.svc_data_path}} ), please manually move it to {{pkg.svc_data_path}}/pgdata before attempting to start the service"
  exit 1
fi

@irvingpop
Copy link
Contributor Author

irvingpop commented Nov 20, 2017

lingering question: does the init hook have any assurances that a hab supervised service isn't running?

Update: yes, it does. we can safely relocate the data

@irvingpop
Copy link
Contributor Author

okay @elliott-davis @fnichol the latest commit should make life easier for existing installations that are upgrading. In testing:

postgresql_1  | postgresql.default(SR): Initializing
postgresql_1  | postgresql.default hook[init]:(HK): PGDATA detected in the root of the data path ( /hab/svc/postgresql/data ), relocating it to /hab/svc/postgresql/data/pgdata
postgresql_1  | postgresql.default hook[init]:(HK): Making sure hab user owns var and data paths
postgresql_1  | postgresql.default(SV): Starting service as user=hab, group=hab
postgresql_1  | postgresql.default(O): Executing run hook
postgresql_1  | postgresql.default(O): Writing postgresql.local.conf file based on memory settings
postgresql_1  | postgresql.default(O): Making sure hab user owns var and data paths
postgresql_1  | postgresql.default(O): Starting PostgreSQL
postgresql_1  | postgresql.default(O): 2017-11-20 21:37:40 GMT [63]: [1-1] user=,db=,client=  (0:00000)LOG:  redirecting log output to logging collector process
postgresql_1  | postgresql.default(O): 2017-11-20 21:37:40 GMT [63]: [2-1] user=,db=,client=  (0:00000)HINT:  Future log output will appear in directory "/hab/svc/postgresql/var/pg_log".

@@ -59,13 +63,14 @@ master_ready() {
bootstrap_replica_via_pg_basebackup() {
echo 'Bootstrapping replica via pg_basebackup from leader '

rm -rf {{pkg.svc_data_path}}/*
pg_basebackup --pgdata={{pkg.svc_data_path}} --xlog-method=stream --dbname='postgres://{{cfg.replication.name}}@{{svc.leader.sys.ip}}:{{cfg.port}}/postgres'
# TODO: guard this rm -rf
Copy link
Contributor

Choose a reason for hiding this comment

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

What kind of guard are you imagining? If there isn't a blocker, let's add it now. This rm -rf rightfully makes us nervous but it is a necessary evil with pg_basebackup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, that TODO comment is probably safe to delete now that it isn't simply rm -rf {{pkg.svc_data_path}}/*.

I was thinking of this issue 🤦‍♀️ and I think the correct way to guard that is suggested by ShellCheck: https://github.com/koalaman/shellcheck/wiki/SC2115

Regardless this seems safer to me as-is now that there's the /pgdata part in the argument. even in the highly unlikely situation that Habitat goes crazy and {{pkg.svc_data_path}} evaluates to an empty string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tl;dr - removed that comment

@fnichol
Copy link
Collaborator

fnichol commented Nov 30, 2017

Okay, I'll get on trying this out--looking really good!

@@ -12,7 +12,7 @@ hab start core/postgresql
```
or
```
docker run starkandwayne/postgresql
docker run chefdemo/postgresql
Copy link
Contributor

Choose a reason for hiding this comment

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

What would the origin be here after merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, I just realized that something (bldr?) is publishing official versions of this plan to https://hub.docker.com/r/habitat/postgresql/ so that would make more sense here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed!

@irvingpop irvingpop force-pushed the update_postgres_no_root branch 2 times, most recently from 24dce3c to 9a74a47 Compare December 1, 2017 00:12
rm -rf {{pkg.svc_data_path}}/*
pg_basebackup --pgdata={{pkg.svc_data_path}} --xlog-method=stream --dbname='postgres://{{cfg.replication.name}}@{{svc.leader.sys.ip}}:{{cfg.port}}/postgres'
rm -rf {{pkg.svc_data_path}}/pgdata/*
pg_basebackup --pgdata={{pkg.svc_data_path}}/pgdata --xlog-method=stream --dbname='postgres://{{cfg.replication.name}}@{{svc.leader.sys.ip}}:{{cfg.port}}/postgres'
Copy link
Contributor

Choose a reason for hiding this comment

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

Not blocking: a basebackup can take a long time for real-world databases. -v -P will give you some more output that can be reassuring when you are on minute 5-6 (or 20-21) of bootstrapping :D.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great idea! done

@rsertelon
Copy link
Contributor

Will this fix #887 ?

@irvingpop
Copy link
Contributor Author

@rsertelon not directly because this is for the postgresql plan and that issue was filed against the postgresql94 plan. but likely these fixes are applicable.

@irvingpop
Copy link
Contributor Author

please_sir_merge

Copy link
Collaborator

@fnichol fnichol left a comment

Choose a reason for hiding this comment

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

Left a couple of comments, but nothing show stopping. We're close to get this merged now, testing in a couple of hours. So sorry this took so long.

}

ensure_dir_ownership() {
echo 'Making sure hab user owns var, config and data paths'
echo 'Making sure hab user owns var and data paths'
chown -RL hab:hab {{pkg.svc_var_path}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should be able to get rid of this one now

chown -RL hab:hab {{pkg.svc_var_path}}
chown -RL hab:hab {{pkg.svc_config_path}}
chown -RL hab:hab {{pkg.svc_data_path}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

@@ -17,7 +17,7 @@ datestyle = 'iso, mdy'

default_text_search_config = 'pg_catalog.english'

data_directory = '{{pkg.svc_data_path}}'
data_directory = '{{pkg.svc_data_path}}/pgdata'
Copy link
Collaborator

Choose a reason for hiding this comment

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

While this will work, if anyone automatically updates their core/postgresql* package, the data dir will have moved. Something we'll want to think on, but despite that, I like this change.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nevermind, I missed the init hook fix

Copy link
Collaborator

@fnichol fnichol left a comment

Choose a reason for hiding this comment

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

As we'll be testing and can resolve any issues, let's merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants