Created
October 12, 2017 20:41
-
-
Save fcoury/e32649b013e91938136d7596df538bed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ "$1" == "" ]; then | |
echo "usage: $0 [servername]" | |
echo "where: servername - the URL Raptor API is going to use" | |
echo " (ie, qa-raptor.teradata.com)" | |
exit 1 | |
fi | |
# get CentOS version | |
OSVER=$(rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release)) | |
# use "build" or "build:prod" | |
NPM_BUILD_TASK="build" | |
# assumes there's an SSH key added to GitHub for the repos | |
echo "" | |
echo "*** Installing dependencies..." | |
echo "" | |
# install dependencies | |
curl -sL https://rpm.nodesource.com/setup_6.x | sudo -E bash - | |
wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo | |
yum install -y epel-release | |
yum install -y gcc-c++ make nodejs git yarn nginx python-pip | |
# excludes PostgreSQL from main repos | |
pip install crudini | |
crudini --set /etc/yum.repos.d/CentOS-Base.repo base exclude 'postgresql*' | |
crudini --set /etc/yum.repos.d/CentOS-Base.repo updates exclude 'postgresql*' | |
# prepare PostgreSQL | |
cd /tmp | |
curl -O https://download.postgresql.org/pub/repos/yum/testing/10/redhat/rhel-6-x86_64/pgdg-centos10-10-2.noarch.rpm | |
rpm -ivh pgdg-centos10-10-2.noarch.rpm | |
cd - | |
echo "" | |
echo "*** Installing and configuring PostgreSQL..." | |
echo "" | |
# installs PostgreSQL | |
yum install -y postgresql10-server | |
# configure PostgreSQL | |
service postgresql-10 initdb | |
chkconfig postgresql-10 on | |
sed -e '82ihost all all 127.0.0.1/32 md5' -i /var/lib/pgsql/10/data/pg_hba.conf | |
service postgresql-10 start | |
PG_ROLE_USER="raptor_role" | |
#PG_ROLE_PASS="$(date +%s | sha256sum | base64 | head -c 32 ; echo)" | |
PG_ROLE_PASS="$(openssl rand -base64 32)" | |
PG_USER="raptor_app" | |
#PG_PASS="$(date +%s | sha256sum | base64 | head -c 32 ; echo)" | |
PG_PASS="$(openssl rand -base64 32)" | |
cat > /tmp/create.sql <<-EOS | |
CREATE USER ${PG_ROLE_USER} WITH | |
LOGIN | |
SUPERUSER | |
CREATEDB | |
CREATEROLE | |
INHERIT | |
REPLICATION | |
CONNECTION LIMIT -1 | |
PASSWORD '${PG_ROLE_PASS}'; | |
CREATE USER ${PG_USER} WITH | |
LOGIN | |
SUPERUSER | |
CREATEDB | |
CREATEROLE | |
INHERIT | |
REPLICATION | |
CONNECTION LIMIT -1 | |
PASSWORD '${PG_PASS}'; | |
CREATE TABLESPACE raptor_ts OWNER ${PG_ROLE_USER} LOCATION '/usr/local/pgsql/data'; | |
ALTER TABLESPACE raptor_ts OWNER TO ${PG_ROLE_USER}; | |
CREATE DATABASE raptor_db | |
WITH | |
OWNER = ${PG_ROLE_USER} | |
TEMPLATE = template0 | |
ENCODING = 'UTF8' | |
LC_COLLATE = 'C' | |
TABLESPACE = raptor_ts | |
CONNECTION LIMIT = -1; | |
EOS | |
cat > /tmp/database.sql <<-EOS | |
CREATE SCHEMA raptor_schema | |
AUTHORIZATION ${PG_ROLE_USER}; | |
ALTER DEFAULT PRIVILEGES IN SCHEMA raptor_schema | |
GRANT ALL ON TABLES TO ${PG_USER}; | |
ALTER DEFAULT PRIVILEGES IN SCHEMA raptor_schema | |
GRANT SELECT, USAGE ON SEQUENCES TO ${PG_USER}; | |
CREATE SEQUENCE raptor_schema.user_id_sequence | |
INCREMENT 1 | |
START 1 | |
MINVALUE 1 | |
; | |
ALTER SEQUENCE raptor_schema.user_id_sequence | |
OWNER TO ${PG_ROLE_USER}; | |
CREATE SEQUENCE raptor_schema.pricing_table_id_sequence | |
INCREMENT 1 | |
START 1 | |
MINVALUE 1 | |
; | |
ALTER SEQUENCE raptor_schema.pricing_table_id_sequence | |
OWNER TO ${PG_ROLE_USER}; | |
CREATE SEQUENCE raptor_schema.configuration_id_sequence | |
INCREMENT 1 | |
START 1 | |
MINVALUE 1 | |
; | |
ALTER SEQUENCE raptor_schema.configuration_id_sequence | |
OWNER TO ${PG_ROLE_USER}; | |
CREATE SEQUENCE raptor_schema.configuration_spec_id_sequence | |
INCREMENT 1 | |
START 1 | |
MINVALUE 1 | |
; | |
ALTER SEQUENCE raptor_schema.configuration_spec_id_sequence | |
OWNER TO ${PG_ROLE_USER}; | |
-- Table: raptor_schema.users | |
-- DROP TABLE raptor_schema.users; | |
CREATE TABLE raptor_schema.users | |
( | |
id bigint NOT NULL DEFAULT nextval('raptor_schema.user_id_sequence'::regclass), | |
email character varying(255) COLLATE pg_catalog."default" NOT NULL, | |
username character varying(255) COLLATE pg_catalog."default" NOT NULL, | |
admin boolean NOT NULL, | |
job_title character varying(255) COLLATE pg_catalog."default", | |
work_phone character varying(30) COLLATE pg_catalog."default", | |
mobile_phone character varying(30) COLLATE pg_catalog."default", | |
preferred_paper_size character varying(20) COLLATE pg_catalog."default", | |
preferred_paper_orientation character varying(20) COLLATE pg_catalog."default", | |
display_name character varying(255) COLLATE pg_catalog."default" NOT NULL, | |
CONSTRAINT user_pkey PRIMARY KEY (id) | |
USING INDEX TABLESPACE raptor_ts, | |
CONSTRAINT user_username_key UNIQUE (username) | |
USING INDEX TABLESPACE raptor_ts | |
) | |
WITH ( | |
OIDS = FALSE | |
) | |
TABLESPACE raptor_ts; | |
ALTER TABLE raptor_schema.users | |
OWNER to ${PG_ROLE_USER}; | |
GRANT ALL ON TABLE raptor_schema.users TO ${PG_USER}; | |
GRANT ALL ON TABLE raptor_schema.users TO ${PG_ROLE_USER}; | |
-- Table: raptor_schema.pricing_tables | |
-- DROP TABLE raptor_schema.pricing_tables; | |
CREATE TABLE raptor_schema.pricing_tables | |
( | |
id bigint NOT NULL DEFAULT nextval('raptor_schema.pricing_table_id_sequence'::regclass), | |
user_id bigint NOT NULL, | |
filename character varying(255) COLLATE pg_catalog."default" NOT NULL, | |
imported_at timestamp with time zone NOT NULL, | |
status character varying(20) COLLATE pg_catalog."default" NOT NULL, | |
CONSTRAINT pricing_tables_pkey PRIMARY KEY (id) | |
USING INDEX TABLESPACE raptor_ts, | |
CONSTRAINT pricing_tables_user_id_fkey FOREIGN KEY (user_id) | |
REFERENCES raptor_schema.users (id) MATCH SIMPLE | |
ON UPDATE NO ACTION | |
ON DELETE NO ACTION | |
) | |
WITH ( | |
OIDS = FALSE | |
) | |
TABLESPACE raptor_ts; | |
ALTER TABLE raptor_schema.pricing_tables | |
OWNER to ${PG_ROLE_USER}; | |
GRANT ALL ON TABLE raptor_schema.pricing_tables TO ${PG_USER}; | |
GRANT ALL ON TABLE raptor_schema.pricing_tables TO ${PG_ROLE_USER}; | |
-- Table: raptor_schema.configurations | |
-- DROP TABLE raptor_schema.configurations; | |
CREATE TABLE raptor_schema.configurations | |
( | |
id bigint NOT NULL DEFAULT nextval('raptor_schema.configuration_id_sequence'::regclass), | |
pricing_table_id bigint NOT NULL, | |
sequence integer NOT NULL, | |
min_sequence integer NOT NULL, | |
max_sequence integer NOT NULL, | |
CONSTRAINT configurations_pkey PRIMARY KEY (id) | |
USING INDEX TABLESPACE raptor_ts, | |
CONSTRAINT configurations_pricing_table_id_fkey FOREIGN KEY (pricing_table_id) | |
REFERENCES raptor_schema.pricing_tables (id) MATCH SIMPLE | |
ON UPDATE NO ACTION | |
ON DELETE NO ACTION | |
) | |
WITH ( | |
OIDS = FALSE | |
) | |
TABLESPACE raptor_ts; | |
ALTER TABLE raptor_schema.configurations | |
OWNER to ${PG_ROLE_USER}; | |
GRANT ALL ON TABLE raptor_schema.configurations TO ${PG_USER}; | |
GRANT ALL ON TABLE raptor_schema.configurations TO ${PG_ROLE_USER}; | |
-- Table: raptor_schema.configuration_specs | |
-- DROP TABLE raptor_schema.configuration_specs; | |
CREATE TABLE raptor_schema.configuration_specs | |
( | |
id bigint NOT NULL DEFAULT nextval('raptor_schema.configuration_spec_id_sequence'::regclass), | |
configuration_id bigint NOT NULL, | |
spec_id integer NOT NULL, | |
spec_value character varying(255) COLLATE pg_catalog."default" NOT NULL, | |
CONSTRAINT configuration_specs_pkey PRIMARY KEY (id) | |
USING INDEX TABLESPACE raptor_ts, | |
CONSTRAINT configuration_specs_configuration_id_fkey FOREIGN KEY (configuration_id) | |
REFERENCES raptor_schema.configurations (id) MATCH SIMPLE | |
ON UPDATE NO ACTION | |
ON DELETE NO ACTION | |
) | |
WITH ( | |
OIDS = FALSE | |
) | |
TABLESPACE raptor_ts; | |
ALTER TABLE raptor_schema.configuration_specs | |
OWNER to ${PG_ROLE_USER}; | |
GRANT ALL ON TABLE raptor_schema.configuration_specs TO ${PG_USER}; | |
GRANT ALL ON TABLE raptor_schema.configuration_specs TO ${PG_ROLE_USER}; | |
EOS | |
mkdir -p /usr/local/pgsql/data | |
chown postgres:postgres /usr/local/pgsql/data | |
chmod 666 /tmp/create.sql | |
chmod 666 /tmp/database.sql | |
echo "" | |
echo "*** Creating PostgreSQL users..." | |
echo "" | |
su - postgres -c 'psql -a -f /tmp/create.sql' | |
echo "" | |
echo "*** Creating PostgreSQL database..." | |
echo "" | |
su - postgres -c 'psql -d raptor_db -a -f /tmp/database.sql' | |
shopt -s checkwinsize | |
echo "" | |
echo "*** Downloading RAPTOR..." | |
echo "" | |
# configure the server | |
if [ ! -d ${HOME}/raptor ]; then | |
touch ${HOME}/.ssh/known_hosts | |
chmod 0644 ${HOME}/.ssh/known_hosts | |
ssh-keyscan github.com >> ${HOME}/.ssh/known_hosts | |
git clone [email protected]:gistia/teradata-raptor.git raptor | |
fi | |
echo "" | |
echo "*** Building RAPTOR..." | |
echo "" | |
cd ${HOME}/raptor | |
yarn | |
cd ${HOME}/raptor/server | |
yarn | |
cd - | |
if [ "${NPM_BUILD_TASK}" == "build" ]; then | |
sed -i "s|http\://localhost\:3001||" src/environments/environment.ts | |
fi | |
yarn run ${NPM_BUILD_TASK} | |
cat <<-EOS > ${HOME}/update.sh | |
#!/bin/bash | |
cd ${HOME}/raptor | |
git pull | |
cd ${HOME}/raptor | |
yarn | |
cd ${HOME}/raptor/server | |
yarn | |
cd - | |
yarn run ${NPM_BUILD_TASK} | |
rm -fR /usr/share/nginx/raptor/ | |
mkdir -p /usr/share/nginx/raptor | |
cp -r dist/* /usr/share/nginx/raptor | |
EOS | |
if [ "${OSVER}" == "7" ]; then | |
cat <<-EOS >> ${HOME}/update.sh | |
service raptor restart | |
EOS | |
else | |
cat <<-EOS >> ${HOME}/update.sh | |
/etc/init.d/raptor restart | |
EOS | |
fi | |
chmod +x ${HOME}/update.sh | |
# configure upstart | |
if [ "${OSVER}" == "7" ]; then | |
cat <<-EOS > /lib/systemd/system/raptor.service | |
[Unit] | |
Description=Raptor Backend Server | |
[Service] | |
Type=simple | |
WorkingDirectory=${HOME}/raptor | |
ExecStart=/usr/bin/npm run server | |
Environment=PGUSER=${PG_USER} | |
Environment=PGHOST=localhost | |
Environment=PGPASSWORD=${PG_PASS} | |
Environment=PGDATABASE=raptor_db | |
Environment=PGPORT=5432 | |
[Install] | |
WantedBy=multi-user.target | |
EOS | |
else | |
cd ${HOME}/raptor | |
sed -e "s|%pg_user%|${PG_USER}|" -e "s|%pg_password%|${PG_PASS}|" scripts/deploy/raptor > /etc/init.d/raptor | |
chmod +x /etc/init.d/raptor | |
chkconfig --add raptor | |
cd - | |
fi | |
# configure nginx | |
cd ${HOME}/raptor | |
mkdir -p /usr/share/nginx/raptor | |
cp -r dist/* /usr/share/nginx/raptor | |
setenforce 0 || : | |
if [ "${OSVER}" == "7" ]; then | |
sudo openssl dhparam -out /progenity/ssl/dhparam.pem 4096 | |
firewall-cmd --zone=public --add-port=80/tcp --permanent | |
firewall-cmd --zone=public --add-port=443/tcp --permanent | |
firewall-cmd --reload | |
fi | |
cat <<-EOS > /etc/nginx/conf.d/raptor.conf | |
upstream raptor-api { | |
server 0.0.0.0:3001; | |
} | |
server { | |
listen 80; | |
server_name $1; | |
location / { | |
root /usr/share/nginx/raptor; | |
try_files \$uri /index.html; | |
} | |
location /api/v1 { | |
proxy_pass http://raptor-api; | |
proxy_set_header Host \$http_host; # required for docker client's sake | |
proxy_set_header X-Real-IP \$remote_addr; # pass on real client's IP | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
proxy_set_header X-Forwarded-Proto \$scheme; | |
proxy_set_header Connection keep-alive; | |
proxy_read_timeout 900; | |
} | |
} | |
EOS | |
echo "" | |
echo "*** Starting RAPTOR..." | |
echo "" | |
if [ "${OSVER}" == "7" ]; then | |
service raptor start | |
service nginx start | |
else | |
/etc/init.d/raptor start | |
/etc/init.d/nginx start | |
fi | |
echo "" | |
echo "All done! Visit http://$1 to access RAPTOR." | |
echo "" | |
echo "RAPTOR Database Users:" | |
echo " ${PG_USER} ${PG_PASS}" | |
echo " ${PG_ROLE_USER} ${PG_ROLE_PASS}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment