You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/07/03 15:19:56 UTC

[trafficcontrol] 04/06: add init for trafficops

This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 1e1dcce602fb7714a306b6a1641a4920742c4046
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Tue Jun 26 13:00:37 2018 -0600

    add init for trafficops
---
 infrastructure/cdn-in-a-box/docker-compose.yml     | 14 +++++
 .../{variables.env => traffic_ops/Dockerfile-init} | 33 ++++--------
 .../traffic_ops/init/trafficops-init.sh            | 60 ++++++++++++++++++++++
 .../cdn-in-a-box/traffic_ops/init/users.json       | 15 ++++++
 infrastructure/cdn-in-a-box/traffic_ops/run.sh     |  1 +
 infrastructure/cdn-in-a-box/variables.env          | 10 ++--
 6 files changed, 105 insertions(+), 28 deletions(-)

diff --git a/infrastructure/cdn-in-a-box/docker-compose.yml b/infrastructure/cdn-in-a-box/docker-compose.yml
index edcfdba..1038539 100644
--- a/infrastructure/cdn-in-a-box/docker-compose.yml
+++ b/infrastructure/cdn-in-a-box/docker-compose.yml
@@ -98,6 +98,20 @@ services:
       - db
       - trafficops-perl
 
+  # trafficops-init is a small container that uses admin credentials to add needed data to trafficops
+  # after it has successfully started
+  trafficops-init:
+    build:
+      context: traffic_ops
+      dockerfile: Dockerfile-init
+    depends_on:
+      - trafficops
+    domainname: cdn.local
+    env_file:
+      - variables.env
+    networks:
+      - tcnet
+
   trafficportal:
     build:
       context: traffic_portal
diff --git a/infrastructure/cdn-in-a-box/variables.env b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile-init
similarity index 64%
copy from infrastructure/cdn-in-a-box/variables.env
copy to infrastructure/cdn-in-a-box/traffic_ops/Dockerfile-init
index 2d6a463..a24d78d 100644
--- a/infrastructure/cdn-in-a-box/variables.env
+++ b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile-init
@@ -14,26 +14,13 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-DOMAIN=cdn.local
-CERT_CITY=Denver
-CERT_COMPANY=NotComcast
-CERT_COUNTRY=US
-CERT_STATE=CO
-DB_NAME=traffic_ops
-DB_ROOT_PASS=twelve
-DB_USER_PASS=twelve
-DB_PORT=5432
-DB_SERVER=db
-DB_USER=traffic_ops
-POSTGRES_PASSWORD=twelve
-PGPASSWORD=twelve
-TM_USER=tmonitor
-TM_PASSWORD=twelve
-TO_ADMIN_PASSWORD=twelve
-TO_ADMIN_USER=admin
-TO_EMAIL=cdnadmin@example.com
-TO_HOST=trafficops
-TO_PORT=6443
-TO_SECRET=blahblah
-TP_EMAIL=none
-TP_HOST=trafficportal
+
+############################################################
+# Dockerfile to initialize Traffic Ops
+############################################################
+
+FROM alpine
+
+RUN apk add --no-cache curl bash
+ADD init /
+CMD /trafficops-init.sh
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/init/trafficops-init.sh b/infrastructure/cdn-in-a-box/traffic_ops/init/trafficops-init.sh
new file mode 100755
index 0000000..b8825d2
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/traffic_ops/init/trafficops-init.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Required env vars
+# Check that env vars are set
+set -x
+for v in TO_HOST TO_PORT TO_ADMIN_USER TO_ADMIN_PASSWORD; do
+    [[ -z $(eval echo \$$v) ]] || continue
+    echo "$v is unset"
+    exit 1
+done
+
+TO_URL="https://$TO_HOST:$TO_PORT"
+# wait until the ping endpoint succeeds
+while ! curl -k $TO_URL/api/1.3/ping; do
+   echo waiting for trafficops
+   sleep 3
+done
+
+export COOKIEJAR=/tmp/cookiejar.$(echo $TO_URL $TO_ADMIN_USER | md5sum | awk '{print $1}')
+
+login() {
+    local datadir=$(mktemp -d)
+    local login="$datadir/login.json"
+    local url=$TO_URL/api/1.3/user/login
+    local datatype='Accept: application/json'
+    cat > "$login"  <<-CREDS
+    { "u" : "$TO_ADMIN_USER", "p" : "$TO_ADMIN_PASSWORD" }
+CREDS
+
+    res=$(curl -k -H "$datatype" --cookie "$COOKIEJAR" --cookie-jar "$COOKIEJAR" -X POST --data @"$login" "$url")
+    rm -rf "$datadir"
+    if [[ $res != *"Successfully logged in."* ]]; then
+        echo $res
+        return -1
+    fi
+}
+
+login
+
+for f in *.json; do
+    ep=$(basename $f .json)
+    curl -k -s --cookie "$COOKIEJAR" -X POST --data @"$f" "$TO_URL/api/1.3/$ep"
+done
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/init/users.json b/infrastructure/cdn-in-a-box/traffic_ops/init/users.json
new file mode 100644
index 0000000..c3c38e4
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/traffic_ops/init/users.json
@@ -0,0 +1,15 @@
+[
+    {
+        "email": "tmonitor@cdn.example.com",
+        "fullName": "Traffic Monitor",
+        "gid": 0,
+        "localPasswd": "jhdslvhdfsuklvfhsuvlhs",
+        "confirmLocalPasswd": "jhdslvhdfsuklvfhsuvlhs",
+        "role": 4,
+        "rolename": "admin",
+        "tenant": "root",
+        "tenantId": 1,
+        "uid": 0,
+        "username": "tmon"
+    }
+]
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/run.sh b/infrastructure/cdn-in-a-box/traffic_ops/run.sh
index f5fd76d..aa7a832 100755
--- a/infrastructure/cdn-in-a-box/traffic_ops/run.sh
+++ b/infrastructure/cdn-in-a-box/traffic_ops/run.sh
@@ -68,6 +68,7 @@ cd $TO_DIR && \
 	./db/admin.pl --env=production reset && \
 	./db/admin.pl --env=production seed || echo "db setup failed!"
 
+# Add admin user -- all other users should be created using API
 /adduser.pl $TO_ADMIN_USER $TO_ADMIN_PASSWORD admin | psql -U$DB_USER -h$DB_SERVER $DB_NAME || echo "adding traffic_ops admin user failed!"
 
 cd $TO_DIR && $TO_DIR/local/bin/hypnotoad script/cdn
diff --git a/infrastructure/cdn-in-a-box/variables.env b/infrastructure/cdn-in-a-box/variables.env
index 2d6a463..7a8d24f 100644
--- a/infrastructure/cdn-in-a-box/variables.env
+++ b/infrastructure/cdn-in-a-box/variables.env
@@ -14,21 +14,21 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-DOMAIN=cdn.local
 CERT_CITY=Denver
 CERT_COMPANY=NotComcast
 CERT_COUNTRY=US
 CERT_STATE=CO
 DB_NAME=traffic_ops
-DB_ROOT_PASS=twelve
-DB_USER_PASS=twelve
 DB_PORT=5432
+DB_ROOT_PASS=twelve
 DB_SERVER=db
 DB_USER=traffic_ops
-POSTGRES_PASSWORD=twelve
+DB_USER_PASS=twelve
+DOMAIN=cdn.local
 PGPASSWORD=twelve
-TM_USER=tmonitor
+POSTGRES_PASSWORD=twelve
 TM_PASSWORD=twelve
+TM_USER=tmonitor
 TO_ADMIN_PASSWORD=twelve
 TO_ADMIN_USER=admin
 TO_EMAIL=cdnadmin@example.com