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

Add application blade deployment script and common library. #12

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion vtds_application_vshasta/private/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
)
CONFIG_DIR = path_join(dirname(__file__), 'config')
APP_CONFIG_NAME = 'application_core_config.yaml'
DEPLOY_SCRIPT_NAME = 'deploy_application_to_node.py'
NODE_DEPLOY_SCRIPT_NAME = 'deploy_application_to_node.py'
BLADE_DEPLOY_SCRIPT_NAME = 'deploy_application_to_blade.py'
COMMON_DEPLOY_LIB_NAME = 'deploy_application_common.py'
SCRIPT_DIR_PATH = path_join(
dirname(__file__),
'scripts',
Expand Down
34 changes: 24 additions & 10 deletions vtds_application_vshasta/private/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
from vtds_base.layers.application import ApplicationAPI
from . import (
APP_CONFIG_NAME,
DEPLOY_SCRIPT_NAME,
NODE_DEPLOY_SCRIPT_NAME,
BLADE_DEPLOY_SCRIPT_NAME,
COMMON_DEPLOY_LIB_NAME,
script,
home
)
Expand Down Expand Up @@ -72,27 +74,37 @@ def __node_manifests(self):
'class_names': ['pit_node'],
'files': [
(
script(DEPLOY_SCRIPT_NAME),
home(DEPLOY_SCRIPT_NAME),
script(NODE_DEPLOY_SCRIPT_NAME),
home(NODE_DEPLOY_SCRIPT_NAME),
'node-deploy'
),
(
script(COMMON_DEPLOY_LIB_NAME),
home(COMMON_DEPLOY_LIB_NAME),
'common-deploy'
),
(self.app_config_path, home(APP_CONFIG_NAME), 'config'),
],
'script': path_join(os.sep, 'root', DEPLOY_SCRIPT_NAME),
'script': path_join(os.sep, 'root', NODE_DEPLOY_SCRIPT_NAME),
}
virtual_blades = self.stack.get_provider_api().get_virtual_blades()
blade_manifest = {
'type': 'blade',
'class_names': virtual_blades.blade_classes(),
'files': [
(
script(DEPLOY_SCRIPT_NAME),
home(DEPLOY_SCRIPT_NAME),
script(BLADE_DEPLOY_SCRIPT_NAME),
home(BLADE_DEPLOY_SCRIPT_NAME),
'node-deploy'
),
(
script(COMMON_DEPLOY_LIB_NAME),
home(COMMON_DEPLOY_LIB_NAME),
'common-deploy'
),
(self.app_config_path, home(APP_CONFIG_NAME), 'config'),
],
'script': path_join(os.sep, 'root', DEPLOY_SCRIPT_NAME),
'script': path_join(os.sep, 'root', BLADE_DEPLOY_SCRIPT_NAME),
}
return [
pit_node_manifest,
Expand Down Expand Up @@ -121,7 +133,7 @@ def __make_host_ip_map(self, node_to_network_map):
}

@staticmethod
def __deploy_manifest(connections, manifest):
def __deploy_manifest(connections, manifest, python_exe):
"""Copy files to the blades or nodes connected in
'connections' based on the manifest and run the appropriate
deployment script(s).
Expand Down Expand Up @@ -150,7 +162,7 @@ def __deploy_manifest(connections, manifest):
)
cmd = (
"chmod 755 %s;" % deploy_script +
"python3 " +
"%s " % python_exe +
"%s " % deploy_script +
class_name_template +
home(APP_CONFIG_NAME)
Expand Down Expand Up @@ -195,15 +207,17 @@ def deploy(self):
virtual_blades = self.stack.get_provider_api().get_virtual_blades()

# Deploy the manifests to the virtual nodes and virtual blades.
blade_py = self.stack.get_platform_api().get_blade_python_executable()
for manifest in self.__node_manifests():
class_names = manifest['class_names']
node_or_blade = manifest['type']
python_exe = "python3" if node_or_blade == 'node' else blade_py
with (
virtual_nodes.ssh_connect_nodes(class_names)
if node_or_blade == 'node'
else virtual_blades.ssh_connect_blades(class_names)
) as connections:
self.__deploy_manifest(connections, manifest)
self.__deploy_manifest(connections, manifest, python_exe)

def remove(self):
if not self.prepared:
Expand Down
210 changes: 210 additions & 0 deletions vtds_application_vshasta/private/scripts/deploy_application_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#
# MIT License
#
# (C) Copyright [2024] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""Deployment script for setting up Virtual Shasta on nodes

"""
from os import environ
import sys
from subprocess import (
Popen,
TimeoutExpired
)
import yaml


PYTHON = "python3"


class ContextualError(Exception):
"""Exception to report failures seen and contextualized within the
application.

"""


class UsageError(Exception): # pylint: disable=too-few-public-methods
"""Exception to report usage errors

"""


def write_out(string):
"""Write an arbitrary string on stdout and make sure it is
flushed.

"""
sys.stdout.write(string)
sys.stdout.flush()


def write_err(string):
"""Write an arbitrary string on stderr and make sure it is
flushed.

"""
sys.stderr.write(string)
sys.stderr.flush()


def usage(usage_msg, err=None):
"""Print a usage message and exit with an error status.

"""
if err:
write_err("ERROR: %s\n" % err)
write_err("%s\n" % usage_msg)
sys.exit(1)


def error_msg(msg):
"""Format an error message and print it to stderr.

"""
write_err("ERROR: %s\n" % msg)


def warning_msg(msg):
"""Format a warning and print it to stderr.

"""
write_err("WARNING: %s\n" % msg)


def info_msg(msg):
"""Format an informational message and print it to stderr.

"""
write_err("INFO: %s\n" % msg)


def run_cmd(cmd, args, stdin=sys.stdin, check=True, timeout=None, **kwargs):
"""Run a command with output on stdout and errors on stderr

"""
exitval = 0
try:
with Popen(
[cmd, *args],
stdin=stdin, stdout=sys.stdout, stderr=sys.stderr,
**kwargs
) as command:
time = 0
signaled = False
while True:
try:
exitval = command.wait(timeout=5)
except TimeoutExpired:
time += 5
if timeout and time > timeout:
if not signaled:
# First try to terminate the process
command.terminate()
continue
command.kill()
print()
# pylint: disable=raise-missing-from
raise ContextualError(
"'%s' timed out and did not terminate "
"as expected after %d seconds" % (
" ".join([cmd, *args]),
time
)
)
continue
# Didn't time out, so the wait is done.
break
print()
except OSError as err:
raise ContextualError(
"executing '%s' failed - %s" % (
" ".join([cmd, *args]),
str(err)
)
) from err
if exitval != 0 and check:
fmt = (
"command '%s' failed"
if not signaled
else "command '%s' timed out and was killed"
)
raise ContextualError(fmt % " ".join([cmd, *args]))
return exitval


def read_config(config_file):
"""Read in the specified YAML configuration file for this blade
and return the parsed data.

"""
try:
with open(config_file, 'r', encoding='UTF-8') as config:
return yaml.safe_load(config)
except OSError as err:
raise ContextualError(
"failed to load blade configuration file '%s' - %s" % (
config_file,
str(err)
)
) from err


def add_hosts(config):
"""Add the host entries provided by the configuration to
/etc/hosts

"""
host_map = config.get('host_ipv4_map', {})
with open("/etc/hosts", 'a', encoding='UTF-8') as hosts:
hosts.write("# Added by vTDS Application Layer Deployment\n")
for alias, ipaddr in host_map.items():
hosts.write("%-15.15s %s\n" % (ipaddr, alias))


def install_deb_packages(config):
"""Initialize 'apt' and install the required debian packages as
listed in the configuration.

"""
env = environ.copy()
env['NEEDRESTART_MODE'] = 'a'
env['DEBIAN_FRONTEND'] = 'noninteractive'
packages = config.get('debian_packages', [])
run_cmd('apt', ['update'], env=env)
run_cmd('apt', ['install', '-y', *packages], env=env)


def entrypoint(usage_msg, main_func):
"""Generic entrypoint function. This sets up command line
arguments for the invocation of a 'main' function and takes care
of handling any vTDS exceptions that are raised to report
errors. Other exceptions are allowed to pass to the caller for
handling.

"""
try:
main_func(sys.argv[1:])
except ContextualError as err:
error_msg(str(err))
sys.exit(1)
except UsageError as err:
usage(usage_msg, str(err))
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# MIT License
#
# (C) Copyright [2024] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""Deployment script for setting up Virtual Shasta on nodes

"""
from deploy_application_common import (
UsageError,
read_config,
add_hosts,
install_deb_packages,
entrypoint,
)


PYTHON = "python3"


def main(argv):
"""Main entry point.

"""
# Arguments are 'blade_class' the name of the blade class to which
# this blade belongs and 'config_path' the path to the
# configuration file used for this deployment.
if not argv:
raise UsageError("no arguments provided")
if len(argv) < 2:
raise UsageError("too few arguments")
if len(argv) > 2:
raise UsageError("too many arguments")

config = read_config(argv[1])
add_hosts(config)
install_deb_packages(config)


if __name__ == '__main__':
USAGE_MSG = """
usage: deploy_application_to_node node_class config_path

Where:

node_class is the name of the Virtual Node class to which this
Virtual Node belongs.
config_path is the path to a YAML file containing the application
configuration to apply.
"""[1:-1]
entrypoint(USAGE_MSG, main)
Loading