Skip to content

Commit

Permalink
Use env vars rather than position args
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Vazquez <[email protected]>
  • Loading branch information
mvazquezc committed May 18, 2022
1 parent 0033805 commit ae9fd17
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ The way it works is by running a set of scripts that interact with the hardware

The [app/](./app/) directory contains the FakeFish application. Inside the [custom_scripts](./app/custom_scripts/) folder we need to create the following scripts:

|Script|What should it do?|Script parameters|
|------|------------------|-----------------|
|`poweron.sh`|Power on the server|`$1` BMC IP, `$2` USERNAME, `$3` PASSWORD|
|`poweroff.sh`|Power off the server|`$1` BMC IP, `$2` USERNAME, `$3` PASSWORD|
|`bootfromcdonce.sh`|Set server to boot from virtual CD once|`$1` BMC IP, `$2` USERNAME, `$3` PASSWORD|
|`mountcd.sh`|Mount the iso received in the server's virtual CD|`$1` BMC IP, `$2` USERNAME, `$3` PASSWORD, `$4` ISO FILE URL|
|`unmountcd.sh`|Unmount the iso from the server's virtual CD|`$1` BMC IP, `$2` USERNAME, `$3` PASSWORD|
|Script|What should it do?|Env Vars|Script parameters|
|------|------------------|--------|-----------------|
|`poweron.sh`|Power on the server|BMC_ENDPOINT BMC_USERNAME BMC_PASSWORD|None|
|`poweroff.sh`|Power off the server|BMC_ENDPOINT BMC_USERNAME BMC_PASSWORD|None|
|`bootfromcdonce.sh`|Set server to boot from virtual CD once|BMC_ENDPOINT BMC_USERNAME BMC_PASSWORD|None|
|`mountcd.sh`|Mount the iso received in the server's virtual CD|BMC_ENDPOINT BMC_USERNAME BMC_PASSWORD| `$1` ISO FILE URL|
|`unmountcd.sh`|Unmount the iso from the server's virtual CD|BMC_ENDPOINT BMC_USERNAME BMC_PASSWORD|None|

The script names must match above naming, you can check the [dell_scripts](./dell_scripts/) folder to find example scripts with the correct naming.

Expand Down
35 changes: 23 additions & 12 deletions app/fakefish.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def system_resource():
else:
app.logger.info('Running script that sets boot from VirtualCD once')
try:
subprocess.check_call(['custom_scripts/bootfromcdonce.sh', bmc_ip, username, password])
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/bootfromcdonce.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to set boot from virtualcd once', 400)

Expand All @@ -84,14 +85,16 @@ def system_reset_action():
if reset_type == 'On':
app.logger.info('Running script that powers on the server')
try:
subprocess.check_call(['custom_scripts/poweron.sh', bmc_ip, username, password])
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/poweron.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to poweron the server', 400)
power_state = 'On'
else:
app.logger.info('Running script that powers off the server')
try:
subprocess.check_call(['custom_scripts/poweroff.sh', bmc_ip, username, password])
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/poweroff.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to poweroff the server', 400)
power_state = 'Off'
Expand Down Expand Up @@ -126,7 +129,8 @@ def virtualmedia_insert():
image_url = image
app.logger.info('Running script that mounts cd with iso %s', image)
try:
subprocess.check_call(['custom_scripts/mountcd.sh', bmc_ip, username, password, image_url])
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/mountcd.sh', image_url], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to mount virtualcd', 400)
return '', 204
Expand All @@ -135,14 +139,15 @@ def virtualmedia_insert():
methods=['POST'])
def virtualmedia_eject():
global bmc_ip
username, password = get_credentials(flask.request)
global inserted
global image_url
username, password = get_credentials(flask.request)
inserted = False
image_url = ''
app.logger.info('Running script that unmounts cd')
try:
subprocess.check_call(['custom_scripts/unmountcd.sh', bmc_ip, username, password])
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/unmountcd.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to unmount virtualcd', 400)
return '', 204
Expand All @@ -160,6 +165,13 @@ def get_credentials(flask_request):
app.logger.debug('Username: ' + username + ', password: ' + password)
return username, password

def set_env_vars(bmc_endpoint, username, password):
my_env = os.environ.copy()
my_env["BMC_ENDPOINT"] = bmc_endpoint
my_env["BMC_USERNAME"] = username
my_env["BMC_PASSWORD"] = password
return my_env

def run():
"""
Expand All @@ -169,13 +181,12 @@ def run():

if __name__ == '__main__':

inserted = False
image_url = ''
power_state = 'On'
bmc_ip = os.environ.get('BMC_IP', None)
if bmc_ip is not None:
app.logger.info(bmc_ip)
else:
if bmc_ip is None:
app.logger.error('Configure the BMC IP using the environment variable BMC_IP')
exit()

inserted = False
image_url = ''
power_state = 'On'
run()
11 changes: 6 additions & 5 deletions dell_scripts/bootfromcdonce.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

#### IMPORTANT: This script is only meant to show how to implement required scripts to make custom hardware compatible with FakeFish. Dell hardware is supported by the `idrac-virtualmedia` provider in Metal3.
#### This script has to set the server's boot to once from cd and return 0 if operation succeeded, 1 otherwise
BMC_ENDPOINT=${1}
USERNAME=${2}
PASSWORD=${3}
#### You will get the following vars as environment vars
#### BMC_ENDPOINT - Has the BMC IP
#### BMC_USERNAME - Has the username configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT
#### BMC_PASSWORD - Has the password configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT

/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} set iDRAC.VirtualMedia.BootOnce 1
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} set iDRAC.VirtualMedia.BootOnce 1
if [ $? -eq 0 ]; then
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} set iDRAC.ServerBoot.FirstBootDevice VCD-DVD
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} set iDRAC.ServerBoot.FirstBootDevice VCD-DVD
if [ $? -eq 0 ]; then
exit 0
else
Expand Down
15 changes: 8 additions & 7 deletions dell_scripts/mountcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
#### IMPORTANT: This script is only meant to show how to implement required scripts to make custom hardware compatible with FakeFish. Dell hardware is supported by the `idrac-virtualmedia` provider in Metal3.
#### This script has to mount the iso in the server's virtualmedia and return 0 if operation succeeded, 1 otherwise
#### Note: Iso image to mount will be received as the first argument ($1)
#### You will get the following vars as environment vars
#### BMC_ENDPOINT - Has the BMC IP
#### BMC_USERNAME - Has the username configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT
#### BMC_PASSWORD - Has the password configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT

BMC_ENDPOINT=${1}
USERNAME=${2}
PASSWORD=${3}
ISO=${4}
ISO=${1}

# Disconnect image just in case
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} remoteimage -d
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} remoteimage -d

# Connect image
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} remoteimage -c -l ${ISO}
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} remoteimage -c -l ${ISO}

if ! /opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} remoteimage -s | grep ${ISO}; then
if ! /opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} remoteimage -s | grep ${ISO}; then
exit 1
fi

Expand Down
9 changes: 5 additions & 4 deletions dell_scripts/poweroff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#### IMPORTANT: This script is only meant to show how to implement required scripts to make custom hardware compatible with FakeFish. Dell hardware is supported by the `idrac-virtualmedia` provider in Metal3.
#### This script has to poweroff the server and return 0 if operation succeeded, 1 otherwise
BMC_ENDPOINT=${1}
USERNAME=${2}
PASSWORD=${3}
#### You will get the following vars as environment vars
#### BMC_ENDPOINT - Has the BMC IP
#### BMC_USERNAME - Has the username configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT
#### BMC_PASSWORD - Has the password configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT

/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} serveraction powerdown
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} serveraction powerdown
if [ $? -eq 0 ]; then
exit 0
else
Expand Down
9 changes: 5 additions & 4 deletions dell_scripts/poweron.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#### IMPORTANT: This script is only meant to show how to implement required scripts to make custom hardware compatible with FakeFish. Dell hardware is supported by the `idrac-virtualmedia` provider in Metal3.
#### This script has to poweron the server and return 0 if operation succeeded, 1 otherwise
BMC_ENDPOINT=${1}
USERNAME=${2}
PASSWORD=${3}
#### You will get the following vars as environment vars
#### BMC_ENDPOINT - Has the BMC IP
#### BMC_USERNAME - Has the username configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT
#### BMC_PASSWORD - Has the password configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT

/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} serveraction powerup
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} serveraction powerup
if [ $? -eq 0 ]; then
exit 0
else
Expand Down
9 changes: 5 additions & 4 deletions dell_scripts/unmountcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#### IMPORTANT: This script is only meant to show how to implement required scripts to make custom hardware compatible with FakeFish. Dell hardware is supported by the `idrac-virtualmedia` provider in Metal3.
#### This script has to unmount the iso from the server's virtualmedia and return 0 if operation succeeded, 1 otherwise
BMC_ENDPOINT=${1}
USERNAME=${2}
PASSWORD=${3}
#### You will get the following vars as environment vars
#### BMC_ENDPOINT - Has the BMC IP
#### BMC_USERNAME - Has the username configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT
#### BMC_PASSWORD - Has the password configured in the BMH/InstallConfig and that is used to access BMC_ENDPOINT

# Disconnect image
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${USERNAME} -p ${PASSWORD} remoteimage -d
/opt/dell/srvadmin/bin/idracadm7 -r ${BMC_ENDPOINT} -u ${BMC_USERNAME} -p ${BMC_PASSWORD} remoteimage -d
if [ $? -eq 0 ]; then
exit 0
else
Expand Down

0 comments on commit ae9fd17

Please sign in to comment.