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

Improve services #828

Open
prasadtalasila opened this issue Jun 20, 2024 · 4 comments
Open

Improve services #828

prasadtalasila opened this issue Jun 20, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@prasadtalasila
Copy link
Contributor

The current services.js has two limitations.

  1. It is insecure
  2. It is not integrated into the deploy/docker/ installation procedures
@prasadtalasila prasadtalasila added the enhancement New feature or request label Jun 20, 2024
@prasadtalasila
Copy link
Contributor Author

The following procedure works for influxdb service.

  1. Have certificates issued by LetsEncrypt". Two files are generated: fullchain.pem and privkey.pem. Place them in /<dirpath>/certs/
  2. Change the certificate ownership to user executing docker command
  3. Change the influxdb start command in services.js to the following.
log(chalk.green("Start new InfluxDB server docker container"));
await $$`docker run -d -p ${influxdbConfig.port}:8086 \
  --name influxdb \
  --restart always \
  -v ${influxdbConfig.datapath}/data:/var/lib/influxdb2 \
  -v ${influxdbConfig.datapath}/config:/etc/influxdb2 \
  -v /<dirpath>/certs/fullchain.pem:/etc/ssl/fullchain.pem \
  -v /<dirpath>/certs/privkey.pem:/etc/ssl/privkey.pem \
  -e INFLUXD_TLS_CERT=/etc/ssl/fullchain.pem \
  -e INFLUXD_TLS_KEY=/etc/ssl/privkey.pem \
  -e DOCKER_INFLUXDB_INIT_MODE=setup \
  -e DOCKER_INFLUXDB_INIT_USERNAME=${influxdbConfig.username} \
  -e DOCKER_INFLUXDB_INIT_PASSWORD=${influxdbConfig.password} \
  -e DOCKER_INFLUXDB_INIT_ORG=dtaas \
  -e DOCKER_INFLUXDB_INIT_BUCKET=dtaas \
  influxdb:2.7`;
log(chalk.green("InfluxDB server docker container started successfully"));

Ref: stackoverflow

@prasadtalasila
Copy link
Contributor Author

prasadtalasila commented Sep 10, 2024

Add automation scripts.

rabbitmq

credentials format: credentials.csv

python script: rabbitmq.py

#!/bin/python3
import csv
from pprint import pprint
import subprocess

def execute_shell_command(command: str):
    # Execute the command
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    print("Output:", result.stdout)
    print("Error:", result.stderr)

def read_credentials(filename: str) -> dict:
    with open(filename, mode='r', newline='') as credsFile:
        credsDict = {}
        credentials = csv.DictReader(credsFile, delimiter=',')
        for credential in credentials:
            credsDict[credential['username']] = credential['password']
            vhost = credential['username']

            execute_shell_command(['rabbitmqctl add_user'+ ' '
                + credential['username'] + ' ' + credential['password']])
            execute_shell_command(['rabbitmqctl add_vhost ' + vhost])
            execute_shell_command(['rabbitmqctl set_permissions -p'
                + ' ' + vhost + ' ' + credential['username'] + ' '
                + '".*" ".*" ".*"'])
    return credsDict

if __name__ == "__main__":
    credentials = read_credentials("credentials.csv")
    #pprint(credentials)

CAVEAT: This code is in heavy need of refactoring.

docker commands

# on host machine
docker cp influxdb.py influxdb:/influxdb.py
docker cp credentials.csv influxdb:/credentials.csv
docker exec -it influxdb bash
# inside docker container
python3 influxdb.py

@prasadtalasila
Copy link
Contributor Author

prasadtalasila commented Sep 11, 2024

influxdb

#!/bin/python3
import csv
import json
from pprint import pprint
import subprocess

def execute_shell_command(command: str, verbose:bool = True) -> str:
    # Execute the command
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    if(verbose):
        print("Output:", result.stdout)
    if result.returncode != 0:
        print("Error:", result.stderr)
        return None
    else:
        return result.stdout

def read_credentials(filename: str) -> dict:
    with open(filename, mode='r', newline='') as credsFile:
        credsDict = {}
        credentials = csv.DictReader(credsFile, delimiter=',')
        for credential in credentials:
            username = credential['username']
            password = credential['password']
            credsDict[username] = password
            execute_shell_command(['influx user create --skip-verify -n' + ' ' + username + ' ' + '-p' + ' ' + password])

        users_json_list = json.loads(execute_shell_command(['influx user list --skip-verify --json'], verbose=False))
        users_json = { user_json['name']: user_json['id'] for user_json in users_json_list}
        print(users_json)

        for name,id in users_json.items():
            execute_shell_command(['influx org create --skip-verify --name' + ' ' + name + ' ' + '--description' + ' ' + name])
            print(name)
            print(id)
            execute_shell_command(['influx org members add --skip-verify --name' + ' ' + name + ' ' + '--owner --m' + ' ' + id])

    return credsDict

if __name__ == "__main__":
    credentials = read_credentials("credentials.csv")
    #pprint(credentials)

The format of credentials.csv is same as the one used by rabbitmq.py script.

CAVEAT: This code is in heavy need of refactoring.

docker commands

# on host machine
docker cp influxdb.py influxdb:/influxdb.py
docker cp credentials.csv influxdb:/credentials.csv
docker exec -it influxdb bash
# inside docker container
python3 influxdb.py

@prasadtalasila
Copy link
Contributor Author

Changes to services.js in order to host InfluxDB over HTTPS

log(chalk.green("Start new InfluxDB server docker container"));
await $$`docker run -d -p ${influxdbConfig.port}:8086 \
  --name influxdb \
  --restart always \
  -v ${influxdbConfig.datapath}/data:/var/lib/influxdb2 \
  -v ${influxdbConfig.datapath}/config:/etc/influxdb2 \
  -v ${influxdbConfig.datapath}/certs/fullchain.pem:/etc/ssl/fullchain.pem \
  -v ${influxdbConfig.datapath}/certs/privkey.pem:/etc/ssl/privkey.pem \
  -e DOCKER_INFLUXDB_INIT_MODE=setup \
  -e DOCKER_INFLUXDB_INIT_USERNAME=${influxdbConfig.username} \
  -e DOCKER_INFLUXDB_INIT_PASSWORD=${influxdbConfig.password} \
  -e DOCKER_INFLUXDB_INIT_ORG=dtaas \
  -e DOCKER_INFLUXDB_INIT_BUCKET=dtaas \
  -e INFLUXD_TLS_CERT=/etc/ssl/fullchain.pem \
  -e INFLUXD_TLS_KEY=/etc/ssl/privkey.pem \
  influxdb:2.7`;
log(chalk.green("InfluxDB server docker container started successfully"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

1 participant