Skip to content

Commit

Permalink
Make local hostname (edge2ai-x.dim.local) unique for multi-cluster de…
Browse files Browse the repository at this point in the history
…ployments.

Before this change all clusters in a multi-cluster deployment would have the same
local hostname (edge2ai-1.dim.local). Now those names are unique per host and have
the format edge2ai-X.dim.local, where X is the cluster ID.
  • Loading branch information
asdaraujo committed Nov 24, 2021
1 parent 730a58c commit 68766c4
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 103 deletions.
4 changes: 2 additions & 2 deletions setup/terraform/resources/bootstrap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ nifi.minifi.config=./conf/config.yml

# MiNiFi Command & Control Configuration
nifi.c2.enable=true
nifi.c2.rest.url=http://edge2ai-1.dim.local:10088/efm/api/c2-protocol/heartbeat
nifi.c2.rest.url.ack=http://edge2ai-1.dim.local:10088/efm/api/c2-protocol/acknowledge
nifi.c2.rest.url=http://LOCAL_HOSTNAME:10088/efm/api/c2-protocol/heartbeat
nifi.c2.rest.url.ack=http://LOCAL_HOSTNAME:10088/efm/api/c2-protocol/acknowledge
nifi.c2.agent.heartbeat.period=10000
nifi.c2.agent.class=iot-1
nifi.c2.agent.identifier=agent-iot-1
Expand Down
4 changes: 2 additions & 2 deletions setup/terraform/resources/bootstrap.conf.tls
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ nifi.minifi.config=./conf/config.yml

# MiNiFi Command & Control Configuration
nifi.c2.enable=true
nifi.c2.rest.url=http://edge2ai-1.dim.local:10088/efm/api/c2-protocol/heartbeat
nifi.c2.rest.url.ack=http://edge2ai-1.dim.local:10088/efm/api/c2-protocol/acknowledge
nifi.c2.rest.url=http://LOCAL_HOSTNAME:10088/efm/api/c2-protocol/heartbeat
nifi.c2.rest.url.ack=http://LOCAL_HOSTNAME:10088/efm/api/c2-protocol/acknowledge
nifi.c2.agent.heartbeat.period=10000
nifi.c2.agent.class=iot-1
nifi.c2.agent.identifier=agent-iot-1
Expand Down
22 changes: 18 additions & 4 deletions setup/terraform/resources/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export TRUSTSTORE_PEM=${SEC_BASE}/x509/truststore.pem
export KEYSTORE_JKS=${SEC_BASE}/jks/keystore.jks
export TRUSTSTORE_JKS=${SEC_BASE}/jks/truststore.jks

# Load cluster metadata
PUBLIC_DNS=${PUBLIC_DNS:-dummy}
if [[ -f $BASE_DIR/clusters_metadata.sh ]]; then
source $BASE_DIR/clusters_metadata.sh
PEER_CLUSTER_ID=$(( (CLUSTER_ID/2)*2 + (CLUSTER_ID+1)%2 ))
PEER_PUBLIC_DNS=$(echo "$CLUSTERS_PUBLIC_DNS" | awk -F, -v pos=$(( PEER_CLUSTER_ID + 1 )) '{print $pos}')
PEER_PUBLIC_DNS=${PEER_PUBLIC_DNS:-$PUBLIC_DNS}
else
CLUSTER_ID=0
PEER_CLUSTER_ID=0
PEER_PUBLIC_DNS=$PUBLIC_DNS
fi
LOCAL_HOSTNAME=edge2ai-${CLUSTER_ID}.dim.local
export CLUSTER_ID PEER_CLUSTER_ID PEER_PUBLIC_DNS LOCAL_HOSTNAME

function is_kerberos_enabled() {
echo $ENABLE_KERBEROS
Expand Down Expand Up @@ -623,7 +637,7 @@ function create_certs() {

# Create CSR
local public_ip=$(curl -s http://ifconfig.me || curl -s http://api.ipify.org/)
export ALT_NAMES="DNS:edge2ai-1.dim.local,DNS:$(hostname -f),DNS:*.${public_ip}.nip.io,DNS:*.cdsw.${public_ip}.nip.io"
export ALT_NAMES="DNS:${LOCAL_HOSTNAME},DNS:$(hostname -f),DNS:*.${public_ip}.nip.io,DNS:*.cdsw.${public_ip}.nip.io"
openssl req\
-new\
-key ${KEY_PEM} \
Expand Down Expand Up @@ -662,7 +676,7 @@ EOF
# Sign cert
if [[ $ipa_host != "" ]]; then
kinit -kt $KEYTABS_DIR/admin.keytab admin
ipa host-add-principal $(hostname -f) "host/edge2ai-1.dim.local"
ipa host-add-principal $(hostname -f) "host/${LOCAL_HOSTNAME}"
ipa host-add-principal $(hostname -f) "host/*.${public_ip}.nip.io"
ipa host-add-principal $(hostname -f) "host/*.cdsw.${public_ip}.nip.io"
ipa cert-request ${CSR_PEM} --principal=host/$(hostname -f)
Expand Down Expand Up @@ -778,9 +792,9 @@ EOF
cat $CERT_PEM $ROOT_PEM >> $sib_cert
chown shellinabox:shellinabox $sib_cert
chmod 400 $sib_cert
rm -f ${sib_dir}/certificate-{localhost,edge2ai-1.dim.local,${CLUSTER_HOST}}.pem
rm -f ${sib_dir}/certificate-{localhost,${LOCAL_HOSTNAME},${CLUSTER_HOST}}.pem
ln -s $sib_cert ${sib_dir}/certificate-localhost.pem
ln -s $sib_cert ${sib_dir}/certificate-edge2ai-1.dim.local.pem
ln -s $sib_cert ${sib_dir}/certificate-${LOCAL_HOSTNAME}.pem
ln -s $sib_cert ${sib_dir}/certificate-${CLUSTER_HOST}.pem

}
Expand Down
14 changes: 8 additions & 6 deletions setup/terraform/resources/create_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from datetime import datetime
from optparse import OptionParser
import cm_client
import json
import os
import re
import requests
import sys
import socket
import time
import urllib3

Expand Down Expand Up @@ -90,7 +89,7 @@ def cm_version():

def the_pwd():
return os.environ['THE_PWD']

def cluster_id():
try:
if 'CLUSTER_ID' in os.environ:
Expand All @@ -100,6 +99,9 @@ def cluster_id():

return 0

def local_hostname():
return os.environ.get('LOCAL_HOSTNAME', 'edge2ai-1.local.dim')

class ClusterCreator:
def __init__(self, host, krb_princ='scm/[email protected]', tls_ca_cert=None):
self.host = host
Expand Down Expand Up @@ -273,7 +275,7 @@ def setup_cm(self, key_file, cm_repo_url, use_kerberos, use_tls, kerberos_type,

# Update cluster banner
c_id = cluster_id()
banner = 'CLUSTER_{}'.format(c_id)
banner = 'Cluster ID: {}, Host: {}'.format(c_id, socket.gethostname())
header_color = HEADER_COLORS[c_id % len(HEADER_COLORS)]
self.cm_api.update_config(
message='Customizing CM header and banner',
Expand Down Expand Up @@ -339,8 +341,8 @@ def _enable_kerberos(self, kerberos_type, ipa_host):
]
if kerberos_type == 'MIT':
config += [
cm_client.ApiConfig(name='KDC_ADMIN_HOST', value='edge2ai-1.dim.local'),
cm_client.ApiConfig(name='KDC_HOST', value='edge2ai-1.dim.local'),
cm_client.ApiConfig(name='KDC_ADMIN_HOST', value=local_hostname()),
cm_client.ApiConfig(name='KDC_HOST', value=local_hostname()),
cm_client.ApiConfig(name='KDC_TYPE', value='MIT KDC'),
]
else:
Expand Down
4 changes: 2 additions & 2 deletions setup/terraform/resources/labs/utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


def execute_sql(cmd, db_name, username, password):
cmd_line = 'PGPASSWORD={pwd} psql --host edge2ai-1.dim.local --port 5432 --username {usr} {db}'.format(
usr=username, pwd=password, db=db_name)
cmd_line = 'PGPASSWORD={pwd} psql --host {hostname} --port 5432 --username {usr} {db}'.format(
usr=username, pwd=password, db=db_name, hostname=get_hostname())
proc = Popen(cmd_line, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate(cmd.encode('utf-8'))
return proc.returncode, stdout, stderr
4 changes: 3 additions & 1 deletion setup/terraform/resources/labs/utils/ssb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
_API_EXTERNAL = 'external'
_API_UI = 'ui'


def _get_api_url():
return get_url_scheme() + '://cdp.{}.nip.io:8000/api/v1'.format(get_public_ip())

Expand Down Expand Up @@ -41,6 +42,8 @@ def _api_call(func, path, data=None, files=None, headers=None, api_type=_API_INT
if api_type != _API_UI:
headers['Content-Type'] = 'application/json'
data = json.dumps(data)
if api_type == _API_EXTERNAL:
headers['Username'] = 'admin'
if token:
headers['X-CSRF-TOKEN'] = _SSB_CSRF_TOKEN
url = _get_url(api_type) + path
Expand Down Expand Up @@ -171,7 +174,6 @@ def execute_sql(stmt, job_name=None, parallelism=None, sample_interval_millis=No
}
headers = {
'Accept': 'application/json',
'Username': 'admin',
'Content-Type': 'application/json',
}
return _api_post('/ssb/sql/execute', data, headers=headers, api_type=_API_EXTERNAL)
Expand Down
12 changes: 6 additions & 6 deletions setup/terraform/resources/labs/workshop_cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
name STRING
) WITH (
'connector' = 'postgres-cdc',
'hostname' = 'edge2ai-1.dim.local',
'hostname' = '{hostname}',
'username' = 'cdc_user',
'password' = 'supersecret1',
'database-name' = 'cdc_test',
Expand All @@ -49,7 +49,7 @@
'debezium.slot.name' = 'flink',
'debezium.snapshot.mode' = 'initial'
);
'''
'''.format(hostname=get_hostname())

LAB3_TRANSACTIONS = '''
INSERT INTO transactions
Expand All @@ -75,13 +75,13 @@
PRIMARY KEY (id) NOT ENFORCED
) WITH (
'connector' = 'jdbc',
'url' = 'jdbc:postgresql://edge2ai-1.dim.local:5432/cdc_test',
'url' = 'jdbc:postgresql://{hostname}:5432/cdc_test',
'table-name' = 'trans_replica',
'password' = '{pwd}',
'username' = 'cdc_user',
'driver' = 'org.postgresql.Driver'
);
'''.format(pwd=get_the_pwd())
'''.format(pwd=get_the_pwd(), hostname=get_hostname())

LAB4_INSERT_INTO_REPLICA = '''
INSERT INTO trans_replica
Expand All @@ -95,13 +95,13 @@
`name` VARCHAR(2147483647)
) WITH (
'connector' = 'kafka',
'properties.bootstrap.servers' = 'edge2ai-1.dim.local:9092',
'properties.bootstrap.servers' = '{hostname}:9092',
'topic' = 'trans_changelog',
'key.format' = 'json',
'key.fields' = 'id',
'value.format' = 'debezium-json'
);
'''
'''.format(hostname=get_hostname())

LAB5_INSERT_CHANGELOG = '''
INSERT INTO trans_changelog
Expand Down
4 changes: 2 additions & 2 deletions setup/terraform/resources/labs/workshop_dataviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from .utils import dataviz

CONNECTION_TYPE = 'impyla'
CONNECTION_NAME = 'Local Impala1'
CONNECTION_NAME = 'Local Impala'
CONNECTION_PARAMS = {
"HOST": "cdp.52.26.198.174.nip.io",
"HOST": get_hostname(),
"PORT": "21050",
"MODE": "binary",
"AUTH": "nosasl",
Expand Down
2 changes: 1 addition & 1 deletion setup/terraform/resources/labs/workshop_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def lab2_edge_flow(self):
'org.apache.nifi.processors.mqtt.ConsumeMQTT',
(100, 100),
{
'Broker URI': 'tcp://edge2ai-1.dim.local:1883',
'Broker URI': 'tcp://{hostname}:1883'.format(hostname=get_hostname()),
'Client ID': 'minifi-iot',
'Topic Filter': 'iot/#',
'Max Queue Size': '60',
Expand Down
2 changes: 1 addition & 1 deletion setup/terraform/resources/labs/workshop_ssb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .utils import ssb, schreg

KAFKA_PROVIDER_NAME = 'edge2ai-kafka'
KAFKA_PROVIDER_BROKERS = 'edge2ai-1.dim.local:9092'
KAFKA_PROVIDER_BROKERS = '{}:9092'.format(get_hostname())
KAFKA_PROVIDER_PROTOCOL = 'plaintext'

SR_PROVIDER_NAME = 'sr'
Expand Down
30 changes: 9 additions & 21 deletions setup/terraform/resources/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ EOF
chown -R root:root /opt/cloudera/cem/${EFM_BASE_NAME}
sed -i.bak 's#APP_EXT_LIB_DIR=.*#APP_EXT_LIB_DIR=/usr/share/java#' /opt/cloudera/cem/efm/conf/efm.conf
sed -i.bak \
's#^efm.server.address=.*#efm.server.address=edge2ai-1.dim.local#;'\
's#^efm.server.address=.*#efm.server.address='"${LOCAL_HOSTNAME}"'#;'\
's#^efm.server.port=.*#efm.server.port=10088#;'\
's#^efm.security.user.certificate.enabled=.*#efm.security.user.certificate.enabled=false#;'\
's#^efm.nifi.registry.enabled=.*#efm.nifi.registry.enabled=true#;'\
's#^efm.nifi.registry.url=.*#efm.nifi.registry.url=http://edge2ai-1.dim.local:18080#;'\
's#^efm.nifi.registry.url=.*#efm.nifi.registry.url=http://'"${LOCAL_HOSTNAME}"':18080#;'\
's#^efm.nifi.registry.bucketName=.*#efm.nifi.registry.bucketName=IoT#;'\
's#^efm.heartbeat.maxAgeToKeep=.*#efm.heartbeat.maxAgeToKeep=1h#;'\
's#^efm.event.maxAgeToKeep.debug=.*#efm.event.maxAgeToKeep.debug=5m#;'\
's#^efm.db.url=.*#efm.db.url=jdbc:postgresql://edge2ai-1.dim.local:5432/efm#;'\
's#^efm.db.url=.*#efm.db.url=jdbc:postgresql://'"${LOCAL_HOSTNAME}"':5432/efm#;'\
's#^efm.db.driverClass=.*#efm.db.driverClass=org.postgresql.Driver#;'\
's#^efm.db.password=.*#efm.db.password='"${THE_PWD}"'#' /opt/cloudera/cem/efm/conf/efm.properties
if [[ $ENABLE_TLS == yes ]]; then
Expand All @@ -251,7 +251,7 @@ EOF
's#^efm.server.ssl.trustStoreType=.*#efm.server.ssl.trustStoreType=jks#;'\
's#^efm.server.ssl.trustStorePassword=.*#efm.server.ssl.trustStorePassword='"$THE_PWD"'#;'\
's#^efm.security.user.certificate.enabled=.*#efm.security.user.certificate.enabled=true#;'\
's#^efm.nifi.registry.url=.*#efm.nifi.registry.url=https://edge2ai-1.dim.local:18433#' /opt/cloudera/cem/efm/conf/efm.properties
's#^efm.nifi.registry.url=.*#efm.nifi.registry.url=https://'"${LOCAL_HOSTNAME}"':18433#' /opt/cloudera/cem/efm/conf/efm.properties
fi
echo -e "\nefm.encryption.password=${THE_PWD}${THE_PWD}" >> /opt/cloudera/cem/efm/conf/efm.properties

Expand All @@ -268,10 +268,11 @@ EOF
chown -R root:root /opt/cloudera/cem/${MINIFITK_BASE_NAME}
rm -f /opt/cloudera/cem/minifi/conf/bootstrap.conf
if [[ $ENABLE_TLS == yes ]]; then
sed "s/THE_PWD/$THE_PWD/" $BASE_DIR/bootstrap.conf.tls > /opt/cloudera/cem/minifi/conf/bootstrap.conf
SOURCE_BOOTSTRAP_CONF=$BASE_DIR/bootstrap.conf.tls
else
cp $BASE_DIR/bootstrap.conf /opt/cloudera/cem/minifi/conf/bootstrap.conf
SOURCE_BOOTSTRAP_CONF=$BASE_DIR/bootstrap.conf
fi
sed "s/THE_PWD/$THE_PWD/;s/LOCAL_HOSTNAME/$LOCAL_HOSTNAME/" $SOURCE_BOOTSTRAP_CONF > /opt/cloudera/cem/minifi/conf/bootstrap.conf
/opt/cloudera/cem/minifi/bin/minifi.sh install

echo "-- Disable services here for packer images - will reenable later"
Expand Down Expand Up @@ -475,23 +476,10 @@ fi
export CLUSTER_HOST=$PUBLIC_DNS
export CDSW_DOMAIN=cdsw.${PUBLIC_IP}.nip.io

echo "-- Load cluster metadata"
if [[ -f $BASE_DIR/clusters_metadata.sh ]]; then
source $BASE_DIR/clusters_metadata.sh
PEER_CLUSTER_ID=$(( (CLUSTER_ID/2)*2 + (CLUSTER_ID+1)%2 ))
PEER_PUBLIC_DNS=$(echo "$CLUSTERS_PUBLIC_DNS" | awk -F, -v pos=$(( PEER_CLUSTER_ID + 1 )) '{print $pos}')
PEER_PUBLIC_DNS=${PEER_PUBLIC_DNS:-$PUBLIC_DNS}
else
CLUSTER_ID=0
PEER_CLUSTER_ID=0
PEER_PUBLIC_DNS=$PUBLIC_DNS
fi
export CLUSTER_ID PEER_CLUSTER_ID PEER_PUBLIC_DNS

echo "-- Set /etc/hosts - Public DNS must come first"
sed -i.bak '/edge2ai-1.dim.local/ d' /etc/hosts
sed -i.bak "/${LOCAL_HOSTNAME}/ d" /etc/hosts
sed -i '/^::1/d' /etc/hosts
echo "$PRIVATE_IP $PUBLIC_DNS $PRIVATE_DNS edge2ai-1.dim.local" >> /etc/hosts
echo "$PRIVATE_IP $PUBLIC_DNS $PRIVATE_DNS $LOCAL_HOSTNAME" >> /etc/hosts

echo "-- Configure networking"
hostnamectl set-hostname ${CLUSTER_HOST}
Expand Down
1 change: 1 addition & 0 deletions setup/terraform/tf/deploy_cdp.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ resource "null_resource" "deploy_cdp" {
"set -o pipefail",
"set -o xtrace",
"trap 'echo Return code: $?' 0",
"sudo bash -c 'cat /tmp/resources/clusters_metadata.sh >> /etc/profile'",
"# Prepare resources",
"chmod +x /tmp/resources/*sh",
"# Deploy workshop",
Expand Down
14 changes: 7 additions & 7 deletions workshop_cdc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Let's start by connecting to PostgreSQL and creating the table.
+
[source,shell]
----
psql --host edge2ai-1.dim.local --port 5432 --username cdc_user cdc_test
psql --host localhost --port 5432 --username cdc_user cdc_test
----
+
When connected you should see the `psql` prompt, with the database name, as shown below:
Expand Down Expand Up @@ -231,7 +231,7 @@ Set the following required properties in your statement:
[source,yaml]
----
connector: postgres-cdc
hostname: edge2ai-1.dim.local
hostname: <CLUSTER_HOSTNAME>
username: cdc_user
password: supersecret1
database-name: cdc_test
Expand Down Expand Up @@ -259,7 +259,7 @@ CREATE TABLE transactions_cdc (
name STRING
) WITH (
'connector' = 'postgres-cdc',
'hostname' = 'edge2ai-1.dim.local',
'hostname' = '<CLUSTER_HOSTNAME>',
'username' = 'cdc_user',
'password' = 'supersecret1',
'database-name' = 'cdc_test',
Expand Down Expand Up @@ -339,7 +339,7 @@ image:images/cdc/job-status-running.png[width=400]
+
[source,shell]
----
psql --host edge2ai-1.dim.local --port 5432 --username cdc_user cdc_test
psql --host localhost --port 5432 --username cdc_user cdc_test
----
+
In the `psql` prompt, execute the following commands to insert one more record in the `transactions` table and update the record with id `101`:
Expand Down Expand Up @@ -373,7 +373,7 @@ Nevertheless, you can replicate the data to any other database accessible via JD
+
[source,shell]
----
psql --host edge2ai-1.dim.local --port 5432 --username cdc_user cdc_test
psql --host localhost --port 5432 --username cdc_user cdc_test
----
+
[source,sql]
Expand Down Expand Up @@ -411,7 +411,7 @@ image::images/cdc/primary-key-clause.png[width=400]
[source,yaml]
----
connector: jdbc
url: jdbc:postgresql://edge2ai-1.dim.local:5432/cdc_test
url: jdbc:postgresql://<CLUSTER_HOSTNAME>:5432/cdc_test
table-name: trans_replica
username: cdc_user
password: supersecret1
Expand Down Expand Up @@ -472,7 +472,7 @@ image:images/cdc/kafka-json-template.png[width=300]
[source,yaml]
----
connector: kafka
properties.bootstrap.servers: edge2ai-1.dim.local:9092
properties.bootstrap.servers: <CLUSTER_HOSTNAME>:9092
topic: trans_changelog
key.format: json
key.fields: id
Expand Down
2 changes: 1 addition & 1 deletion workshop_dataviz.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Connection name: Local Impala
+
[source,yaml]
----
Hostname: <YOUR_CLUSTER_HOSTNAME> (something like: cdp.x.x.x.x.nip.io)
Hostname: <CLUSTER_HOSTNAME> (something like: cdp.x.x.x.x.nip.io)
Port #: 21050
Username: [leave blank]
Password: [leave blank]
Expand Down
Loading

0 comments on commit 68766c4

Please sign in to comment.