From 21c13026c1b1ee6cabde62ef5f44cd5f06d881c4 Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Mon, 20 May 2019 15:46:27 -0400 Subject: [PATCH] Add postgres setup to mac and linux set up scripts Summary: This patch installs postgresql server as part of the overall khan setup process. To allow our access to the database in our tests and dev environment, we need to make a couple small tweaks to the default settings. In os x land, we need to create a postgres user since the only user created by homebrew matches the logged in user. In linux world, we need to relax the authentication requirements to match the settings that homebrew provides - namely to allow local users to login with any username. Note that we create the postgres databases as Makefile step within webapp (to keep all schema changes to that repo). That forthcoming change will need to land before this one. Test Plan: To test my changes on os x, I uninstalled posgres using homebrew manually, then ran the steps in `./mac-setup.sh` and verified that they worked without error. To test my changes on linux, I booted a blank ubuntu (18.04) container using docker, with the devtools directory mounted as a volume: `` docker run -v `/home/dhruv/khan/devtools`:/root/khan/devtools --rm -it ubuntu `` I then commented out all steps in `linux_setup.sh` except `install_postgresql` and ran the following `` install_postgresql runs apt-get update apt-get install -y sudo curl gnupg software-properties-common cd /root/khan/devtools/khan-dotfiles ./linux-setup.sh ``` Reviewers: benkraft, Kai, csilvers Reviewed By: Kai, csilvers Subscribers: kphilip, aric, #classroom-be, csilvers Differential Revision: https://phabricator.khanacademy.org/D54534 --- linux-setup.sh | 19 +++++++++++++++++++ mac-setup.sh | 22 +++++++++++++++++++++- postgresql/pg_hba.conf | 13 +++++++++++++ setup.sh | 8 ++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 postgresql/pg_hba.conf diff --git a/linux-setup.sh b/linux-setup.sh index 2b7b198..d892a48 100755 --- a/linux-setup.sh +++ b/linux-setup.sh @@ -247,6 +247,24 @@ install_watchman() { fi } +install_postgresql() { + # Instructions taken from + # https://pgdash.io/blog/postgres-11-getting-started.html + # Postgres 11 is not available in 18.04, so we need to add the pg apt repository. + curl -s https://www.postgresql.org/media/keys/ACCC4CF8.asc \ + | sudo apt-key add - + + sudo add-apt-repository -y "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -c -s`-pgdg main" + sudo apt-get update + sudo apt-get install -y postgresql-11 postgresql-contrib libpq-dev + + # Set up authentication to allow connections from the postgres user with no + # password. This matches the authentication setup that homebrew installs on + # a mac. Unlike a mac, we do not need to create a postgres user manually. + sudo cp -av postgresql/pg_hba.conf "/etc/postgresql/11/main/pg_hba.conf" + sudo service postgresql restart +} + setup_clock() { # This shouldn't be necessary, but it seems it is. if ! grep -q 3.ubuntu.pool.ntp.org /etc/ntp.conf; then @@ -288,5 +306,6 @@ install_protoc install_watchman setup_clock config_inotify +install_postgresql trap - EXIT diff --git a/mac-setup.sh b/mac-setup.sh index f644dbc..9d079c7 100755 --- a/mac-setup.sh +++ b/mac-setup.sh @@ -281,6 +281,25 @@ install_node() { fi } +install_postgresql() { + if ! brew ls postgresql >/dev/null 2>&1; then + info "Installing postgresql\n" + brew install postgresql@11 + else + success "postgresql already installed" + fi + + # We create a postgres user locally that we use in test and dev. + if ! psql \ + -tc "SELECT rolname from pg_catalog.pg_roles" postgres \ + | grep -c 'postgres' > /dev/null 2>&1 ; then + info "Creating postgres user for dev\n" + psql --quiet -c "CREATE ROLE postgres LOGIN SUPERUSER;" postgres; + else + success "postgres user already created" + fi +} + install_nginx() { info "Checking for nginx\n" if ! type nginx >/dev/null 2>&1; then @@ -343,7 +362,7 @@ install_protoc() { } install_watchman() { - if ! which watchman ; then + if ! which watchman >/dev/null 2>&1; then update "Installing watchman..." brew install watchman fi @@ -431,6 +450,7 @@ install_homebrew install_slack update_git install_node +install_postgresql install_nginx install_image_utils install_helpful_tools diff --git a/postgresql/pg_hba.conf b/postgresql/pg_hba.conf new file mode 100644 index 0000000..87f8388 --- /dev/null +++ b/postgresql/pg_hba.conf @@ -0,0 +1,13 @@ +# We allow any connection by the postgres user to local addresses, without a +# password. + +# TYPE DATABASE USER ADDRESS METHOD +# "local" is for Unix domain socket connections only +local all postgres trust + +# IPv4 local connections: +host all postgres 127.0.0.1/32 trust +# IPv6 local connections: +host all postgres ::1/128 trust + + diff --git a/setup.sh b/setup.sh index d99d5b5..e123586 100755 --- a/setup.sh +++ b/setup.sh @@ -318,6 +318,13 @@ download_db_dump() { fi } +create_pg_databases() { + if [ "$WEBAPP" = true ]; then + echo "Creating postgres databases" + ( cd "$REPOS_DIR/webapp" ; make pg_create ) + fi +} + # Make sure we store userinfo so we can pass appropriately when ka-cloning. update_userinfo() { echo "Updating your git user info" @@ -393,6 +400,7 @@ install_deps # pre-reqs: clone_repos, install_and_setup_gcloud install_hooks # pre-req: clone_repos setup_arc # pre-req: clone_repos download_db_dump # pre-req: install_deps +create_pg_databases # pre-req: install_deps echo