Skip to content

Commit 49d3ef9

Browse files
committed
Merge pull request #116 from QualiSystems/feature/borismod_40_save_vnic
Feature/borismod 40 save vnic
2 parents 00ce2e4 + d5720cf commit 49d3ef9

31 files changed

Lines changed: 260 additions & 87 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ tests/config.ini
6565
*.cache
6666
*.whl
6767
*.tar.gz
68-
vCenterShell/vCenterShell/vCenterShell.zip
6968
*.orig
7069
vCenterShell.zip
7170
vCenterShellPackage/Resource Scripts/*
71+
vCenterShellPackage/Topology Scripts/*.*
7272
debug.txt

common/logger/service.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
from common.logger import getLogger, configure_loglevel
66

77
_logger = getLogger("vCenterCommon")
8-
DEFAULT_LOG_FILE = "./logs/vCenterShell.log"
98

109

1110
class LoggingService(object):
12-
def __init__(self, log_level_console, log_level_file=None, filename=DEFAULT_LOG_FILE):
11+
def __init__(self, log_level_console, log_level_file, filename):
1312
from common.utilites.io import extract_folder_name, compose_folder_if_not_existed
1413

1514
log_level_file = log_level_file or log_level_console

common/wrappers/command_wrapper.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
COMMA = ','
77
EXECUTING_COMMAND = 'executing command: {0}'
88
CONNECTED_TO_CENTER = 'connected to vcenter: {0}'
9-
DEBUG_CONNECTION_INFO = 'connection params: host: {0} username: {1} password: {2} port: {3}'
9+
DEBUG_CONNECTION_INFO = 'connection params: host: {0} username: {1} port: {2}'
1010
LOGGER_CANNOT_BE_NONE = 'logger cannot be None'
1111
COMMAND_CANNOT_BE_NONE = 'command cannot be None'
1212
INFO_CONNECTING_TO_VCENTER = 'connecting to vcenter: {0}'
@@ -43,7 +43,6 @@ def execute_command_with_connection(self, connection_details, command, *args):
4343
logger.debug(
4444
DEBUG_CONNECTION_INFO.format(connection_details.host,
4545
connection_details.username,
46-
connection_details.password,
4746
connection_details.port))
4847

4948
si = self.pv_service.connect(connection_details.host,

deployed_app_service/__init__.py

Whitespace-only changes.

deployed_app_service/__main__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import qualipy.scripts.cloudshell_scripts_helpers as helpers
2+
3+
from common.model_factory import ResourceModelParser
4+
from deployed_app_service.connect import DeployedAppService
5+
6+
7+
def main():
8+
resource_model_parser = ResourceModelParser()
9+
deployed_app_service = DeployedAppService(resource_model_parser)
10+
11+
vlan_spec_type = helpers.get_user_param('VLAN_SPEC_TYPE')
12+
13+
if not vlan_spec_type:
14+
raise ValueError('VLAN_SPEC_TYPE is missing')
15+
16+
vlan_id = helpers.get_user_param('VLAN_ID')
17+
if not vlan_id:
18+
raise ValueError('VLAN_ID is missoing')
19+
20+
deployed_app_service.connect(vlan_spec_type, vlan_id)
21+
22+
23+
if __name__ == "__main__":
24+
main()

deployed_app_service/connect.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import qualipy.scripts.cloudshell_scripts_helpers as helpers
2+
from qualipy.api.cloudshell_api import InputNameValue
3+
4+
5+
class DeployedAppService(object):
6+
def __init__(self, resource_model_parser):
7+
self.resource_model_parser = resource_model_parser
8+
9+
def connect(self, access_mode, virtual_network):
10+
"""
11+
Connect method should be executed on Deployed App
12+
It executes connect command on vCenter resource for each connected VLAN
13+
:param virtual_network: virtual network to connect to
14+
:param access_mode: Access mode: Access or Trunk
15+
"""
16+
resource_context = helpers.get_resource_context_details()
17+
generic_deployed_app_resource_model = self.resource_model_parser.convert_to_resource_model(resource_context)
18+
19+
if not generic_deployed_app_resource_model.vm_uuid:
20+
raise ValueError('VM _UUID is not set on Generic Deployed App')
21+
22+
if not generic_deployed_app_resource_model.cloud_provider:
23+
raise ValueError('Cloud Provider is not set on Generic Deployed App')
24+
25+
self.execute_command_on_vcenter_resource(access_mode, generic_deployed_app_resource_model, virtual_network)
26+
27+
@staticmethod
28+
def execute_command_on_vcenter_resource(access_mode, generic_deployed_app_resource_model, virtual_network):
29+
session = helpers.get_api_session()
30+
reservation_id = helpers.get_reservation_context_details().id
31+
session.ExecuteCommand(reservation_id, generic_deployed_app_resource_model.cloud_provider,
32+
'Resource',
33+
'Connect VM',
34+
[InputNameValue('COMMAND', "connect"),
35+
InputNameValue('VLAN_ID', virtual_network),
36+
InputNameValue('VLAN_SPEC_TYPE', access_mode),
37+
InputNameValue('VM_UUID', generic_deployed_app_resource_model.vm_uuid)],
38+
True)

driver_packager.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import os
22
import sys
33
import zipfile
4+
import ConfigParser
45

5-
DRIVER_FILE_NAME_FORMAT = 'vCenterShellPackage/Resource Scripts/{0}.zip'
6+
DRIVER_FILE_BASE_DIR = 'vCenterShellPackage'
67
STRIPING_CHARS = ' \t\n\r'
78
DRIVER_FOLDER = 'driver_folder'
89
INCLUDE_DIRS = 'include_dirs'
910
TARGET_NAME = 'target_name'
1011
VERSION_FILENAME = 'version.txt'
12+
TARGET_DIR = 'target_dir'
1113

1214

1315
def zip_dir(path, zip_handler, include_dir=True):
@@ -42,21 +44,17 @@ def add_version_file_to_zip(ziph, driver_path=None):
4244
def main(args):
4345
config_file_name = args[1]
4446

45-
with open(config_file_name) as f_config:
46-
if f_config is None:
47-
raise Exception('no packager config file found')
48-
config = dict()
49-
config_raw = f_config.read().splitlines()
50-
for att in config_raw:
51-
cnf_att = att.split(':')
52-
config[cnf_att[0].strip(' \t\n\r')] = cnf_att[1].strip(STRIPING_CHARS).split(',')
47+
config = ConfigParser.ConfigParser()
48+
config.readfp(open(config_file_name))
5349

54-
target_name = config[TARGET_NAME][0]
55-
driver = config[DRIVER_FOLDER][0]
50+
driver = config.get('Packaging', DRIVER_FOLDER)
51+
include_dirs = config.get('Packaging', INCLUDE_DIRS).split(',')
52+
target_name = config.get('Packaging', TARGET_NAME)
53+
target_dir = config.get('Packaging', TARGET_DIR)
5654

57-
print 'packing driver: {0}'.format(target_name)
55+
zip_name = os.path.join(DRIVER_FILE_BASE_DIR, target_dir, target_name + '.zip')
5856

59-
zip_name = DRIVER_FILE_NAME_FORMAT.format(target_name)
57+
print 'Packing driver {0} into {1}'.format(target_name, zip_name)
6058

6159
ensure_dir(zip_name)
6260

@@ -80,13 +78,10 @@ def main(args):
8078

8179
add_version_file_to_zip(zip_file)
8280

83-
for dir_to_include in config[INCLUDE_DIRS]:
81+
for dir_to_include in include_dirs:
8482
zip_dir(dir_to_include, zip_file)
8583

8684
zip_file.close()
8785

88-
print 'done!'
89-
90-
9186
if __name__ == "__main__":
9287
main(sys.argv)

environment_scripts/__main__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from environment_scripts.connect_all import EnvironmentConnector
2+
3+
4+
def main():
5+
environment_connector = EnvironmentConnector()
6+
environment_connector.connect_all()
7+
8+
9+
if __name__ == "__main__":
10+
main()

environment_scripts/connect_all.py

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,77 @@
11
import qualipy.scripts.cloudshell_scripts_helpers as helpers
22
from qualipy.api.cloudshell_api import InputNameValue
3+
from common.utilites.common_utils import first_or_default
34

4-
session = helpers.get_api_session()
5-
reservation_id = helpers.get_reservation_context_details().id
6-
connectors = session.GetReservationDetails(reservation_id).ReservationDescription.Connectors
7-
attributes = helpers.get_resource_context_details().attributes
8-
vlan = attributes['VLAN Id']
9-
access_mode = attributes['Access Mode']
10-
resource_name = helpers.get_resource_context_details().name
11-
12-
connectedResources = [connector.Target for connector in connectors if connector.Source == resource_name] + \
13-
[connector.Source for connector in connectors if connector.Target == resource_name]
14-
15-
if not connectedResources:
16-
raise Exception('There is no visual connectors connected to this VLAN')
17-
18-
for connectedResource in connectedResources:
19-
session.ExecuteCommand(reservation_id, connectedResource, 'Resource', 'Connect',
20-
[InputNameValue('COMMAND', "connect"),
21-
InputNameValue('VLAN_ID', vlan),
22-
InputNameValue('VLAN_SPEC_TYPE', access_mode)],
23-
True)
5+
from common.model_factory import ResourceModelParser
6+
7+
8+
class EnvironmentConnector(object):
9+
10+
def connect_all(self):
11+
"""
12+
Connects all the VLAN Auto services to all the Deployed Apps in the same Environment
13+
:return:
14+
"""
15+
session = helpers.get_api_session()
16+
reservation_id = helpers.get_reservation_context_details().id
17+
18+
# GetReservationDetails is performance heavy operation
19+
reservation_details = session.GetReservationDetails(reservation_id)
20+
vlan_services = self._get_vlan_auto_services(reservation_details)
21+
22+
connectors = self._get_connectors(reservation_details)
23+
24+
if not vlan_services or not connectors:
25+
return
26+
27+
for vlan_service in vlan_services:
28+
29+
if not len(vlan_service.Attributes):
30+
raise ValueError('No attributes on service {0}'.format(vlan_service.ServiceName))
31+
32+
access_mode = self._get_attribute(vlan_service.Attributes, 'Access Mode')
33+
virtual_network = self._get_attribute(vlan_service.Attributes, 'Virtual Network')
34+
35+
# Get Deployed App connected to VLAN Auto service
36+
connected_resources = self._get_connected_resources(connectors, vlan_service)
37+
38+
if not connected_resources:
39+
continue
40+
41+
for connected_resource in connected_resources:
42+
self._execute_connect_command_on_connected_resource(access_mode, connected_resource, reservation_id,
43+
session, virtual_network)
44+
45+
@staticmethod
46+
def _get_attribute(attributes, attribute_name):
47+
attribute = next(item for item in attributes if item.Name == attribute_name)
48+
if not attribute or not attribute.Value:
49+
raise ValueError('Attribute {0} is missing'.format(attribute_name))
50+
return attribute.Value
51+
52+
@staticmethod
53+
def _get_connectors(reservation_details):
54+
connectors = [connector for connector in reservation_details.ReservationDescription.Connectors]
55+
return connectors
56+
57+
@staticmethod
58+
def _get_vlan_auto_services(reservation_details):
59+
vlan_services = [service for service in reservation_details.ReservationDescription.Services
60+
if service.ServiceName == 'VLAN Auto']
61+
return vlan_services
62+
63+
@staticmethod
64+
def _execute_connect_command_on_connected_resource(access_mode, connected_resource, reservation_id, session,
65+
virtual_network):
66+
session.ExecuteCommand(reservation_id, connected_resource, 'Resource', 'Connect',
67+
[InputNameValue('VLAN_ID', virtual_network),
68+
InputNameValue('VLAN_SPEC_TYPE', access_mode)],
69+
True)
70+
71+
@staticmethod
72+
def _get_connected_resources(connectors, vlan_service):
73+
connected_resources = [connector.Target for connector in connectors if
74+
connector.Source == vlan_service.ServiceName] + \
75+
[connector.Source for connector in connectors if
76+
connector.Target == vlan_service.ServiceName]
77+
return connected_resources

orchestration_service/orchestrator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def execute_app_orchestration():
2626
api.SetAttributesValues(
2727
[ResourceAttributesUpdateRequest(
2828
deployment_result.LogicalResourceName,
29-
[AttributeNameValue("VM UUID", deployment_result.VmUuid),
29+
[AttributeNameValue("VM_UUID", deployment_result.VmUuid),
3030
AttributeNameValue("Cloud Provider", deployment_result.CloudProviderResourceName)])])
3131

3232
# logical resource execute "Power On"
@@ -63,3 +63,6 @@ def deploy_app(api, app_name, deployment_service, reservation_id):
6363
except Exception as exc:
6464
logger.error("Error deploying app {0}. Error: {1}".format(app_name, str(exc)))
6565
exit(1)
66+
67+
68+

0 commit comments

Comments
 (0)