diff --git a/README.md b/README.md index fa21333..aba57ff 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/fakefish.py b/app/fakefish.py index 18cffe7..5bd549b 100755 --- a/app/fakefish.py +++ b/app/fakefish.py @@ -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) @@ -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' @@ -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 @@ -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 @@ -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(): """ @@ -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() diff --git a/dell_scripts/bootfromcdonce.sh b/dell_scripts/bootfromcdonce.sh index ee0111f..096fce9 100755 --- a/dell_scripts/bootfromcdonce.sh +++ b/dell_scripts/bootfromcdonce.sh @@ -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 diff --git a/dell_scripts/mountcd.sh b/dell_scripts/mountcd.sh index e581c4b..c643b9e 100755 --- a/dell_scripts/mountcd.sh +++ b/dell_scripts/mountcd.sh @@ -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 diff --git a/dell_scripts/poweroff.sh b/dell_scripts/poweroff.sh index e4f60de..32d2ce4 100755 --- a/dell_scripts/poweroff.sh +++ b/dell_scripts/poweroff.sh @@ -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 diff --git a/dell_scripts/poweron.sh b/dell_scripts/poweron.sh index 96c5c5b..5260027 100755 --- a/dell_scripts/poweron.sh +++ b/dell_scripts/poweron.sh @@ -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 diff --git a/dell_scripts/unmountcd.sh b/dell_scripts/unmountcd.sh index 9619e65..01c6441 100755 --- a/dell_scripts/unmountcd.sh +++ b/dell_scripts/unmountcd.sh @@ -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