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

cli updates #1588

Merged
merged 12 commits into from
Oct 1, 2024
Merged
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ PYPI_URL=https://pypi.org
NPM_URL=https://registry.npmjs.org
SECRET_KEY_BASE=bdb4300d46c9d4f116ce3dbbd54cac6b20802d8be1c2333cf5f6f90b1627799ac5d043e8460744077bc0bd6aacdd5c4bf53f499a68303c6752e7f327b874b96a
OPENC3_CLOUD=local
OPENC3_LANGUAGE=ruby
# Change to arn:aws-us-gov for deploying to AWS Gov Cloud
OPENC3_AWS_ARN_PREFIX=arn:aws

# This can be used to set the default language for generators
# OPENC3_LANGUAGE=ruby

# Don't automatically make the tools bucket public read
# OPENC3_NO_BUCKET_POLICY=1

Expand Down
44 changes: 22 additions & 22 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:

env:
OPENC3_API_PASSWORD: password
OPENC3_LANGUAGE: ruby

jobs:
openc3-cli:
Expand Down Expand Up @@ -73,50 +74,49 @@ jobs:
run: |
set -e
### PLUGIN ###
./openc3.sh cliroot generate | grep "Unknown generator" || true
./openc3.sh cliroot generate plugin | grep "Usage" || true
./openc3.sh cliroot generate plugin cli-test
./openc3.sh cliroot generate plugin cli-test | grep "already exists" || true
./openc3.sh cli generate plugin | grep "Usage" || true
./openc3.sh cli generate plugin cli-test
./openc3.sh cli generate plugin cli-test | grep "already exists" || true
cd openc3-cosmos-cli-test
../openc3.sh cliroot rake build VERSION=1.0.0
../openc3.sh cliroot validate openc3-cosmos-cli-test-1.0.0.gem
../openc3.sh cli rake build VERSION=1.0.0
../openc3.sh cli validate openc3-cosmos-cli-test-1.0.0.gem
### TARGET ###
../openc3.sh cliroot generate target MY-cli
../openc3.sh cli generate target MY-cli
# Verify the target name 'MY_CLI' and lib filename 'my_cli.rb'
ls targets/MY_CLI/lib | grep my_cli.rb
../openc3.sh cliroot rake build VERSION=1.0.1
../openc3.sh cliroot validate openc3-cosmos-cli-test-1.0.1.gem
../openc3.sh cli rake build VERSION=1.0.1
../openc3.sh cli validate openc3-cosmos-cli-test-1.0.1.gem
### MICROSERVICE ###
../openc3.sh cliroot generate microservice background
../openc3.sh cli generate microservice background
# Verify the microservice name 'BACKGROUND' and filename 'background.rb'
ls microservices/BACKGROUND | grep background.rb
../openc3.sh cliroot rake build VERSION=1.0.2
../openc3.sh cliroot validate openc3-cosmos-cli-test-1.0.2.gem
../openc3.sh cli rake build VERSION=1.0.2
../openc3.sh cli validate openc3-cosmos-cli-test-1.0.2.gem
### CONVERSION ###
../openc3.sh cliroot generate conversion MY_CLI upcase
../openc3.sh cli generate conversion MY_CLI upcase
# Verify the conversion filename 'upcase_conversion.rb'
ls targets/MY_CLI/lib | grep upcase_conversion.rb
../openc3.sh cliroot rake build VERSION=1.0.3
../openc3.sh cliroot validate openc3-cosmos-cli-test-1.0.3.gem
../openc3.sh cli rake build VERSION=1.0.3
../openc3.sh cli validate openc3-cosmos-cli-test-1.0.3.gem
### LIMITS_RESPONSE ###
../openc3.sh cliroot generate limits_response MY_CLI example
../openc3.sh cli generate limits_response MY_CLI example
# Verify the conversion filename 'example_limits_response.rb'
ls targets/MY_CLI/lib | grep example_limits_response.rb
../openc3.sh cliroot rake build VERSION=1.0.3
../openc3.sh cliroot validate openc3-cosmos-cli-test-1.0.3.gem
../openc3.sh cli rake build VERSION=1.0.3
../openc3.sh cli validate openc3-cosmos-cli-test-1.0.3.gem
- name: openc3.sh cli script list, run, spawn
shell: 'script -q -e -c "bash {0}"'
run: |
set -e
docker exec -it cosmos-openc3-redis-1 sh -c "echo -e 'AUTH openc3 openc3password\nset OPENC3__TOKEN 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8' | redis-cli"
# list shows all the available file names
./openc3.sh cliroot script list | tee /dev/tty | grep "INST/procedures/stash.rb"
./openc3.sh cli script list | tee /dev/tty | grep "INST/procedures/stash.rb"
# spawning a script prints only a PID
./openc3.sh cliroot script spawn INST/procedures/checks.rb | grep -v "^\s*\d+\s*$"
./openc3.sh cli script spawn INST/procedures/checks.rb | grep -v "^\s*\d+\s*$"
# run a script that will fail and look for the failure message
./openc3.sh cliroot script run --wait 10 INST/procedures/checks.rb | tee /dev/tty | grep -q "script failed"
./openc3.sh cli script run --wait 10 INST/procedures/checks.rb | tee /dev/tty | grep -q "script failed"
# run a script that will complete successfully
./openc3.sh cliroot script run INST/procedures/stash.rb | tee /dev/tty | grep "script complete"
./openc3.sh cli script run INST/procedures/stash.rb | tee /dev/tty | grep "script complete"
- name: openc3.sh util save,load
shell: 'script -q -e -c "bash {0}"'
run: |
Expand Down
4 changes: 0 additions & 4 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

networks:
default:
name: openc3-cosmos-network

services:
openc3-minio:
user: "${OPENC3_USER_ID:-1001}:${OPENC3_GROUP_ID:-1001}"
Expand Down
4 changes: 2 additions & 2 deletions openc3.bat
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ if "%1" == "cli" (
FOR /F "tokens=*" %%i in ('findstr /V /B /L /C:# %~dp0.env') do SET %%i
set params=%*
call set params=%%params:*%1=%%
REM Start (and remove when done --rm) the openc3-operator container with the current working directory
REM Start (and remove when done --rm) the openc3-cosmos-cmd-tlm-api container with the current working directory
REM mapped as volume (-v) /openc3/local and container working directory (-w) also set to /openc3/local.
REM This allows tools running in the container to have a consistent path to the current working directory.
REM Run the command "ruby /openc3/bin/openc3" with all parameters ignoring the first.
REM Run the command "ruby /openc3/bin/openc3cli" with all parameters ignoring the first.
docker compose -f %~dp0compose.yaml run -it --rm -v %cd%:/openc3/local -w /openc3/local -e OPENC3_API_PASSWORD=!OPENC3_API_PASSWORD! --no-deps openc3-cosmos-cmd-tlm-api ruby /openc3/bin/openc3cli !params!
GOTO :EOF
)
Expand Down
12 changes: 2 additions & 10 deletions openc3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ fi
set -e

usage() {
echo "Usage: $1 [cli, cliroot, start, stop, cleanup, build, run, dev, test, util]" >&2
echo "Usage: $1 [cli, start, stop, cleanup, build, run, dev, test, util]" >&2
echo "* cli: run a cli command as the default user ('cli help' for more info)" 1>&2
echo "* cliroot: run a cli command as the root user ('cli help' for more info)" 1>&2
echo "* start: build and run" >&2
echo "* stop: stop the containers (compose stop)" >&2
echo "* cleanup [local] [force]: REMOVE volumes / data (compose down -v)" >&2
Expand All @@ -58,21 +57,14 @@ case $1 in
# Source the .env file to setup environment variables
set -a
. "$(dirname -- "$0")/.env"
# Start (and remove when done --rm) the openc3-operator container with the current working directory
# Start (and remove when done --rm) the openc3-cosmos-cmd-tlm-api container with the current working directory
# mapped as volume (-v) /openc3/local and container working directory (-w) also set to /openc3/local.
# This allows tools running in the container to have a consistent path to the current working directory.
# Run the command "ruby /openc3/bin/openc3cli" with all parameters starting at 2 since the first is 'openc3'
args=`echo $@ | { read _ args; echo $args; }`
${DOCKER_COMPOSE_COMMAND} -f "$(dirname -- "$0")/compose.yaml" run -it --rm -v `pwd`:/openc3/local:z -w /openc3/local -e OPENC3_API_PASSWORD=$OPENC3_API_PASSWORD --no-deps openc3-cosmos-cmd-tlm-api ruby /openc3/bin/openc3cli $args
set +a
;;
cliroot )
set -a
. "$(dirname -- "$0")/.env"
args=`echo $@ | { read _ args; echo $args; }`
${DOCKER_COMPOSE_COMMAND} -f "$(dirname -- "$0")/compose.yaml" run -it --rm --user=root -v `pwd`:/openc3/local:z -w /openc3/local -e OPENC3_API_PASSWORD=$OPENC3_API_PASSWORD --no-deps openc3-cosmos-cmd-tlm-api ruby /openc3/bin/openc3cli $args
set +a
;;
start )
./openc3.sh build
./openc3.sh run
Expand Down
61 changes: 37 additions & 24 deletions openc3/python/openc3/utilities/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def instance(cls, level=INFO):
# below the method name log level.
# @param block [Proc] Block to call which should return a string to append
# to the log message
def debug(self, message=None, scope=None, user=None, type=LOG, url=None):
def debug(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.DEBUG:
self.log_message(
Expand All @@ -138,10 +138,11 @@ def debug(self, message=None, scope=None, user=None, type=LOG, url=None):
user=user,
type=type,
url=url,
other=other,
)

# (see #debug)
def info(self, message=None, scope=None, user=None, type=LOG, url=None):
def info(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.INFO:
self.log_message(
Expand All @@ -151,10 +152,11 @@ def info(self, message=None, scope=None, user=None, type=LOG, url=None):
user=user,
type=type,
url=url,
other=other,
)

# (see #debug)
def warn(self, message=None, scope=None, user=None, type=LOG, url=None):
def warn(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.WARN:
self.log_message(
Expand All @@ -164,10 +166,11 @@ def warn(self, message=None, scope=None, user=None, type=LOG, url=None):
user=user,
type=type,
url=url,
other=other,
)

# (see #debug)
def error(self, message=None, scope=None, user=None, type=LOG, url=None):
def error(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.ERROR:
self.log_message(
Expand All @@ -177,10 +180,11 @@ def error(self, message=None, scope=None, user=None, type=LOG, url=None):
user=user,
type=type,
url=url,
other=other,
)

# (see #debug)
def fatal(self, message=None, scope=None, user=None, type=LOG, url=None):
def fatal(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.FATAL:
self.log_message(
Expand All @@ -190,29 +194,38 @@ def fatal(self, message=None, scope=None, user=None, type=LOG, url=None):
user=user,
type=type,
url=url,
other=other,
)

def log_message(self, log_level, message, scope, user, type, url):
with self.instance_mutex:
now_time = datetime.now(timezone.utc)
data = {
"time": int(now_time.timestamp() * 1000000000),
# Can't use isoformat because it appends "+00:00" instead of "Z"
"@timestamp": now_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"level": log_level,
}
if self.microservice_name:
data["microservice_name"] = self.microservice_name
if self.detail_string:
data["detail"] = self.detail_string
# EE: If a user is passed, put its name. Don't include user data if no user was passed.
if user:
data["user"] = user
data["container_name"] = self.container_name
def build_log_data(self, log_level, message, user=None, type=None, url=None, other=None):
now_time = datetime.now(timezone.utc)
data = {
"time": int(now_time.timestamp() * 1000000000),
# Can't use isoformat because it appends "+00:00" instead of "Z"
"@timestamp": now_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"level": log_level,
}
if self.microservice_name is not None:
data["microservice_name"] = self.microservice_name
if self.detail_string is not None:
data["detail"] = self.detail_string
# EE: If a user is passed, put its name. Don't include user data if no user was passed.
if user is not None:
data["user"] = user
data["container_name"] = self.container_name
if message is not None:
data["message"] = message
if type is not None:
data["type"] = type
if url:
data["url"] = url
if url is not None:
data["url"] = url
if other is not None:
data = data | other
return data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good checks throughout for not None


def log_message(self, log_level, message, scope, user, type, url, other=None):
with self.instance_mutex:
data = self.build_log_data(log_level, message, user, type, url, other)
if self.stdout:
match log_level:
case "WARN" | "ERROR" | "FATAL":
Expand Down
Loading