Skip to content

Commit

Permalink
Issue 17 (#22)
Browse files Browse the repository at this point in the history
* issue_17: remove redfish emulator cloning in setup.sh file

Signed-off-by: Bruno Mesnet <bruno.mesnet@fr.ibm.com>

* issue_17: remove redfish emulator cloning in setup.sh file

Signed-off-by: Bruno Mesnet <bruno.mesnet@fr.ibm.com>

* adapt test to new architecture

Signed-off-by: Bruno Mesnet <bruno.mesnet@fr.ibm.com>

* call virtualenv creation option to test.sh

Signed-off-by: Bruno Mesnet <bruno.mesnet@fr.ibm.com>

* add DMTF Redfish emulator header

Signed-off-by: Bruno Mesnet <bruno.mesnet@fr.ibm.com>

---------

Signed-off-by: Bruno Mesnet <bruno.mesnet@fr.ibm.com>
  • Loading branch information
bmesnet committed Sep 18, 2023
1 parent c947a08 commit ab42a18
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 99 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

The OFMF Reference implementation provides a basic implementation of the OpenFabrics Management Framework. This project is based (fork) on the [Swordfish API Emulator](https://github.com/SNIA/Swordfish-API-Emulator), that provides the support for interacting with a RedFish/SwordFish system via RESTful operations (create, read, update, and delete). Before contributing to this project, read our [license](https://github.com/OFMFWG/OFMF-Reference/blob/master/LICENSE.md) and make sure you agree with the terms.

The OFMF Reference implementation is maintained by the OpenFabrics Alliance, while the Swordfish API Emulator code is maintained on GitHub by the SNIA, and the Redfish Interface Emulator code is maintained on GitHub by the DMTF.
The OFMF Reference implementation is maintained by the OpenFabrics Alliance, while the Swordfish API Emulator code is maintained on GitHub by the SNIA.

----

## Prerequisites for the emulator

The OFMF reference implementation requires Python 3.5 or higher, make sure to properly configure your system before attempting using this project. If not available on your system, please refer to the best practices for installing bython onto your operating system.
The OFMF reference implementation requires Python 3.5 or higher, make sure to properly configure your system before attempting using this project. If not available on your system, please refer to the best practices for installing python onto your operating system.

This project, by default uses virtualenv for separating the emulator environment from the main system python environment. Make sure virtualenv is available as part of your system python environment. Please note, using virtualenv is not a requirement for the OFMF Reference to properly function.
It is recommended for this project to use virtualenv for separating the emulator environment from the main system python environment. Make sure virtualenv is available as part of your system python environment. Please note, using virtualenv is not a requirement for the OFMF Reference to properly function.

To set up the emulator Python virtualenv and requirements, run:"
```
virtualenv --python=python3 venv
venv/bin/pip install -q -r requirements.txt
```

----

## Installing the OFMF Reference implementation

### <u>Basic OFMF installation process</u>
We provide a (bash) script that automatically installs and runs the emulator on a Linux system. By default the emulator is installed in the `../OFMF` folder. However the destination folder can be changed when first isntalling the project.
The script will take care of creating a virtual environment called `venv` inside the installation folder annd will take care of installing all the requirements of the project.
We provide a (bash) script that automatically installs and runs the emulator on a Linux system.
The script will take care of installing all the requirements of the project.

To start the installation process, run:
```
Expand All @@ -31,10 +37,10 @@ after completion this script will also execute the OFMF that will be listening o

The `setup.sh` script provides parameters for configuring the installation of the OFMF:
```
./setup.sh -w DEST_FOLDER -p PORT -n
./setup.sh -p PORT -n -v
```

the `-w` argument can be used for installing the OFMF in a custom destination folder. The `-p` argument allows fo remulator to be started listening on an alternative port. Finally, the `-n` argument is used only for copy new files into the destination folder withour re-installing the entire environment. An example usage for the `-n` argument is when a developer makes changes to one of the files in the project and wants to test the changes. She would modify the source code of any of the files and then run the below command only to update the code base.
The `-p` argument allows the emulator to be started listening on an alternative port. Finally, the `-n` argument is used only for copy new files into the destination folder withour re-installing the entire environment. An example usage for the `-n` argument is when a developer makes changes to one of the files in the project and wants to test the changes. He would modify the source code of any of the files and then run the below command only to update the code base. The `-p` argument will create the virtual environment to separate the emulator environment from the main system environment.

```
./setup.sh -n
Expand All @@ -43,17 +49,18 @@ the `-w` argument can be used for installing the OFMF in a custom destination fo
After a successful installation, the OFMF can be started by running the below commands:

```
source INST_DIR/venv/bin/activate
python INST_DIR/emulator.py
python3 ofmf-main.py
```
or if a virtual environement has been set up:
```
venv/bin/python ofmf-main.py
```

where `INST_DIR` is the directory where the OFMF was initially installed.

----

## Interacting with the OFMF

Users can interact with the OFMF with any tool, command line or GUI based, that can send HTTP methods (GET, POST, DELETE, PATHC, PUT) to the below URI:
Users can interact with the OFMF with any tool, command line or GUI based, that can send HTTP methods (GET, POST, DELETE, PATCH, PUT) to the below URI:

`http://localhost:5000/redfish/v1/`

Expand Down
36 changes: 36 additions & 0 deletions api_emulator/account_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright Notice:
# Copyright 2016-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Interface-Emulator/blob/main/LICENSE.md

# Redfish Emulator Role Service.
# Temporary version, to be removed when AccountService goes dynamic

class AccountService(object):

def __init__(self):
self._accounts = { 'Administrator': 'Password',
'User': 'Password' }
self._roles = { 'Administrator': 'Admin',
'User': 'ReadOnlyUser' }

def checkPriviledgeLevel(self, user, level):
if self._roles[user] == level:
return True
else:
return False

def getPassword(self, username):
if username in self._accounts:
return self._accounts[username]
else:
return None

def checkPrivilege(self, privilege, username, errorResponse):
def wrap(func):
def inner(*args, **kwargs):
if self.checkPriviledgeLevel(username(), privilege):
return func(*args, **kwargs)
else:
return errorResponse()
return inner
return wrap
24 changes: 24 additions & 0 deletions api_emulator/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright Notice:
# Copyright 2016-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Interface-Emulator/blob/main/LICENSE.md

# Exceptions thrown in the Emulator

class ConfigurationError(Exception):
pass

class StaticLoadError(Exception):
pass

class CreatePooledNodeError(Exception):
pass

class RemovePooledNodeError(Exception):
pass

class OVFParseError(Exception):
pass

class EventSubscriptionError(Exception):
pass

38 changes: 38 additions & 0 deletions api_emulator/resource_dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright Notice:
# Copyright 2016-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Interface-Emulator/blob/main/LICENSE.md

# Resource Dictionary

# Variable to store resource dictionary
import logging

resdict = {}

class ResourceDictionary(object):

def __init__(self):
logging.info('Init ResourceDictionary.')


def get_resource(self, path):
obj = resdict[path].configuration
return obj

def get_object(self, path):
return resdict[path]

def add_resource(self, path, obj):
resdict[path] = obj
return obj

def delete_resource(self, path):
del resdict[path]

def print_dictionary(self):
for x in resdict:
print('Key: ')
print(x)
print('Value: ')
print(resdict[x])

5 changes: 5 additions & 0 deletions api_emulator/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright Notice:
# Copyright 2016-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Interface-Emulator/blob/main/LICENSE.md

__version__ = '1.1.9'
126 changes: 42 additions & 84 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
# THE POSSIBILITY OF SUCH DAMAGE.

BASE_DIR=$(pwd)
WORK_DIR=../OFMF

API_PORT=5001
SETUP_ONLY=
RESET_RESOURCES=
VIRTUAL_ENV=

COMMON_NAME="$1"
EXTFILE=certificate_config.cnf
Expand All @@ -45,19 +44,15 @@ necessary source files ready and start a local instance of the emulator.
USAGE:
$(basename $0) [--port PORT] [--workspace DIR] [--no-start]
$(basename $0) [--port PORT] [--no-start] [--virtual-env]
Options:
-p | --port PORT -- Port to run the emulator on. Default is $API_PORT.
-w | --workspace DIR -- Directory to set up the emulator. Defaults to
'$WORK_DIR'.
-n | --no-start -- Prepare the emulator but do not start it.
-u | --update -- Update working dir with modified files
-r | --reset -- Reset the Resources folder (RedFish tree)
-v | --virtual-env -- Create a virtual environment with all needed in './venv'
EOF
}
Expand All @@ -69,18 +64,11 @@ while [ "$1" != "" ]; do
shift
API_PORT=$1
;;
-w | --workspace )
shift
WORK_DIR=$1
;;
-n | --no-start)
SETUP_ONLY="true"
;;
-u | --update)
UPDATE_ONLY="true"
;;
-r | --reset)
RESET_RESOURCES="true"
-v | --virtual-env)
VIRTUAL_ENV="true"
;;
*)
print_help
Expand All @@ -99,6 +87,13 @@ if ! [ -x "$(command -v python3)" ]; then
exit 1
fi

VERSION=$(python3 -V 2>&1 | cut -d\ -f 2) # python 2 prints version to stderr
VERSION=(${VERSION//./ }) # make an version parts array
if [[ ${VERSION[0]} -lt 3 ]] || [[ ${VERSION[0]} -eq 3 && ${VERSION[1]} -lt 5 ]] ; then
echo "Python version is:" ${VERSION[0]}.${VERSION[1]} "> Python 3.5+ needed!" 1>&2
exit 1
fi

if ! [ -x "$(command -v virtualenv)" ]; then
echo "Error: virtualenv is required."
echo ""
Expand All @@ -117,87 +112,39 @@ if ! [ -x "$(command -v git)" ]; then
exit 1
fi

if [ "$RESET_RESOURCES" == "true" ]; then
echo ""
echo "Wiping resources folder with original files"
echo ""

rm -r $WORK_DIR/Resources
cp -r Resources $WORK_DIR/
fi

if [ "$UPDATE_ONLY" == "true" ]; then
echo ""
echo "Updating installed emulator with modified files"
echo ""

for f in `git ls-files -m`; do
echo "Updating: $f"
cp -f $f $WORK_DIR/$f
done

exit 0
fi

echo "Creating workspace: '$WORK_DIR'..."
rm -fr $WORK_DIR
mkdir $WORK_DIR

# Get the Redfish base
echo "Getting Redfish emulator base files..."
git clone --depth 1 https://github.com/DMTF/Redfish-Interface-Emulator \
$WORK_DIR

# Set up our virtual environment
echo "Setting up emulator Python virtualenv and requirements..."
# cd $WORK_DIR
virtualenv --python=python3 "$WORK_DIR"/venv
"$WORK_DIR"/venv/bin/pip install -q -r "$BASE_DIR"/requirements.txt

# Remove Redfish static / starting mockups
rm -r "$WORK_DIR"/api_emulator/redfish/static

# Remove Redfish templates, and .py files.
rm -rf "$WORK_DIR"/api_emulator/redfish/templates
rm -rf "$WORK_DIR"/api_emulator/redfish/*.py

# Copy over the Swordfish bits
echo "Applying Swordfish additions..."
cp -r -f "$BASE_DIR"/api_emulator "$WORK_DIR"/
cp -r -f "$BASE_DIR"/Resources "$WORK_DIR"/
cp -r -f "$BASE_DIR"/emulator-config.json "$WORK_DIR"/
cp -r -f "$BASE_DIR"/g.py "$WORK_DIR"/
cp -r -f "$BASE_DIR"/emulator.py "$WORK_DIR"/
cp -r -f "$BASE_DIR"/ofmf-main.py "$WORK_DIR"/
cp -r -f "$BASE_DIR"/certificate_config.cnf "$WORK_DIR"/
cp -r -f "$BASE_DIR"/v3.ext "$WORK_DIR"/
cp -r -f "$BASE_DIR"/tests "$WORK_DIR"/
if [ "$VIRTUAL_ENV" == "true" ]; then
echo "Setting up emulator Python virtualenv and requirements..."
virtualenv --python=python3 venv
venv/bin/pip install -q -r requirements.txt
fi

# generating server key
echo "Generating private key"
openssl genrsa -out "$WORK_DIR"/server.key 2048
openssl genrsa -out "$BASE_DIR"/server.key 2048

# generating public key
echo "Generating public key"
openssl rsa -in "$WORK_DIR"/server.key -pubout -out "$WORK_DIR"/server_public.key
openssl rsa -in "$BASE_DIR"/server.key -pubout -out "$BASE_DIR"/server_public.key

# ## Update Common Name in External File
# /bin/echo "commonName = $COMMON_NAME" >> $EXTFILE

# Generating Certificate Signing Request using config file
echo "Generating Certificate Signing Request"
openssl req -new -key "$WORK_DIR"/server.key -out "$WORK_DIR"/server.csr -config "$WORK_DIR"/$EXTFILE
openssl req -new -key "$BASE_DIR"/server.key -out "$BASE_DIR"/server.csr -config "$BASE_DIR"/$EXTFILE

echo "Generating self signed certificate"
openssl x509 -in "$WORK_DIR"/server.csr -out "$WORK_DIR"/server.crt -req -signkey "$WORK_DIR"/server.key -days 365 -extfile "$WORK_DIR"/v3.ext
openssl x509 -in "$BASE_DIR"/server.csr -out "$BASE_DIR"/server.crt -req -signkey "$BASE_DIR"/server.key -days 365 -extfile "$BASE_DIR"/v3.ext

if [ "$SETUP_ONLY" == "true" ]; then
echo ""
echo "Emulator files have been prepared. Run the following to start the" \
"emulator:"
echo ""
echo " cd $WORK_DIR"
echo " ./venv/bin/python ofmf-main.py"
echo " python3 ofmf-main.py"
echo " or if you have set up a virtual environment: (see -v option)"
echo " venv/bin/python ofmf-main.py"
echo ""
exit 0
fi
Expand All @@ -214,12 +161,23 @@ $(tput bold)Press Ctrl-C when done.$(tput sgr0)
---------------------------------------------------------------------
EOF

cd "$WORK_DIR"
#"$WORK_DIR"/venv/bin/python emulator.py -port $API_PORT
"$WORK_DIR"/venv/bin/python ofmf-main.py -port $API_PORT

echo ""
echo "Emulator can be rerun from '$WORK_DIR' by running the command:"
echo ""
echo "venv/bin/python emulator.py"
if [ -d "venv" ]; then
venv/bin/python ofmf-main.py -port $API_PORT
echo ""
echo ""
echo "Emulator can be rerun from here by running the command:"
echo " venv/bin/python ofmf-main.py"
else
#python3 ofmf-main.py -port $API_PORT
res=$(python3 ofmf-main.py -port $API_PORT)
echo $res
echo ""
echo ""
echo "Emulator can be rerun from here by running the command:"
echo " python3 ofmf-main.py"
echo ""
echo "If running './setup.sh' complains about a missing module,"
echo " try considering './setup.sh -v' to create a virtual environment"
fi
echo ""

0 comments on commit ab42a18

Please sign in to comment.