[CORD-1360] Using new config
Change-Id: I2689f6f5781c0ad040a5e953fcacb224d22ef655
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..485dee6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+.idea
diff --git a/xos/openstack-onboard.yaml b/xos/openstack-onboard.yaml
index d92a6c6..c096bee 100644
--- a/xos/openstack-onboard.yaml
+++ b/xos/openstack-onboard.yaml
@@ -12,5 +12,5 @@
properties:
base_url: file:///opt/xos_services/openstack/xos/
synchronizer: synchronizer/manifest
- synchronizer_run: xos-synchronizer.py
+ synchronizer_run: openstack-synchronizer.py
diff --git a/xos/synchronizer/__init__.py b/xos/synchronizer/__init__.py
index e56cd39..9b61dfb 100644
--- a/xos/synchronizer/__init__.py
+++ b/xos/synchronizer/__init__.py
@@ -1,9 +1,5 @@
-from xos.config import Config
-
-try:
- observer_disabled = Config().observer_disabled
-except:
- observer_disabled = False
+# FIXME this file needs a refactoring
+observer_disabled = False
def EnableObserver(x):
""" used for manage.py --noobserver """
diff --git a/xos/synchronizer/client.py b/xos/synchronizer/client.py
index d1383f4..872ab89 100644
--- a/xos/synchronizer/client.py
+++ b/xos/synchronizer/client.py
@@ -1,17 +1,20 @@
import urlparse
+
try:
from keystoneauth1.identity import v2 as keystoneauth_v2
from keystoneauth1 import session as keystone_session
from keystoneclient.v2_0 import client as keystone_client
- #from glance import client as glance_client
+ # from glance import client as glance_client
import glanceclient
from novaclient.v2 import client as nova_client
- from neutronclient.v2_0 import client as neutron_client
+ from neutronclient.v2_0 import client as neutron_client
+
has_openstack = True
except:
has_openstack = False
-from xos.config import Config
+from xosconfig import Config
+
def require_enabled(callable):
def wrapper(*args, **kwds):
@@ -19,8 +22,10 @@
return callable(*args, **kwds)
else:
return None
+
return wrapper
+
def parse_novarc(filename):
opts = {}
f = open(filename, 'r')
@@ -37,9 +42,11 @@
f.close()
return opts
+
class Client:
- def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, cacert=None, admin=True, *args, **kwds):
-
+ def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None,
+ cacert=None, admin=True, *args, **kwds):
+
self.has_openstack = has_openstack
self.url = controller.auth_url
if admin:
@@ -60,17 +67,15 @@
if url:
self.url = url
if token:
- self.token = token
+ self.token = token
if endpoint:
self.endpoint = endpoint
if cacert:
self.cacert = cacert
else:
- self.cacert = getattr(Config(), "nova_ca_ssl_cert", "None")
+ self.cacert = Config.get("nova.ca_ssl_cert")
- #if '@' in self.username:
- # self.username = self.username[:self.username.index('@')]
class KeystoneClient(Client):
def __init__(self, *args, **kwds):
@@ -98,46 +103,49 @@
Client.__init__(self, *args, **kwds)
if has_openstack:
self.client = glanceclient.get_client(host='0.0.0.0',
- username=self.username,
- password=self.password,
- tenant=self.tenant,
- auth_url=self.url,
- cacert=self.cacert
- )
+ username=self.username,
+ password=self.password,
+ tenant=self.tenant,
+ auth_url=self.url,
+ cacert=self.cacert
+ )
+
@require_enabled
def __getattr__(self, name):
return getattr(self.client, name)
+
class GlanceClient(Client):
def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
Client.__init__(self, *args, **kwds)
if has_openstack:
- self.client = glanceclient.Client(version,
- endpoint=endpoint,
- token=token,
- cacert=cacert
- )
+ self.client = glanceclient.Client(version,
+ endpoint=endpoint,
+ token=token,
+ cacert=cacert
+ )
@require_enabled
def __getattr__(self, name):
- return getattr(self.client, name)
+ return getattr(self.client, name)
+
class NovaClient(Client):
def __init__(self, *args, **kwds):
Client.__init__(self, *args, **kwds)
if has_openstack:
self.client = nova_client.client.Client(
- "2",
- username=self.username,
- api_key=self.password,
- project_id=self.tenant,
- auth_url=self.url,
- region_name='',
- extensions=[],
- service_type='compute',
- service_name='',
- cacert=self.cacert
- )
+ "2",
+ username=self.username,
+ api_key=self.password,
+ project_id=self.tenant,
+ auth_url=self.url,
+ region_name='',
+ extensions=[],
+ service_type='compute',
+ service_name='',
+ cacert=self.cacert
+ )
@require_enabled
def connect(self, *args, **kwds):
@@ -147,6 +155,7 @@
def __getattr__(self, name):
return getattr(self.client, name)
+
class NovaDB(Client):
def __init__(self, *args, **kwds):
Client.__init__(self, *args, **kwds)
@@ -155,7 +164,6 @@
nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
self.client = nova_db_api
-
@require_enabled
def connect(self, *args, **kwds):
self.__init__(*args, **kwds)
@@ -164,6 +172,7 @@
def __getattr__(self, name):
return getattr(self.client, name)
+
class NeutronClient(Client):
def __init__(self, *args, **kwds):
Client.__init__(self, *args, **kwds)
@@ -174,6 +183,7 @@
auth_url=self.url,
ca_cert=self.cacert
)
+
@require_enabled
def connect(self, *args, **kwds):
self.__init__(*args, **kwds)
@@ -182,24 +192,26 @@
def __getattr__(self, name):
return getattr(self.client, name)
+
class OpenStackClient:
"""
A simple native shell to the openstack backend services.
This class can receive all nova calls to the underlying testbed
"""
- def __init__ ( self, *args, **kwds) :
+ def __init__(self, *args, **kwds):
# instantiate managers
self.keystone = KeystoneClient(*args, **kwds)
url_parsed = urlparse.urlparse(self.keystone.url)
hostname = url_parsed.netloc.split(':')[0]
- token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
-# glance_endpoint = self.keystone.client.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
-# self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds)
+ token = self.keystone.client.tokens.authenticate(username=self.keystone.username,
+ password=self.keystone.password,
+ tenant_name=self.keystone.tenant)
+ # glance_endpoint = self.keystone.client.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
+ # self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds)
self.nova = NovaClient(*args, **kwds)
# self.nova_db = NovaDB(*args, **kwds)
self.neutron = NeutronClient(*args, **kwds)
-
@require_enabled
def connect(self, *args, **kwds):
diff --git a/xos/synchronizer/driver.py b/xos/synchronizer/driver.py
index 1e795e4..8135fa6 100644
--- a/xos/synchronizer/driver.py
+++ b/xos/synchronizer/driver.py
@@ -1,6 +1,6 @@
import commands
import hashlib
-from xos.config import Config
+from xosconfig import Config
from synchronizers.new_base.modelaccessor import *
try:
@@ -9,15 +9,11 @@
except:
has_openstack = False
-manager_enabled = Config().api_nova_enabled
+manager_enabled = Config.get("nova.enabled")
class OpenStackDriver:
def __init__(self, config = None, client=None):
- if config:
- self.config = Config(config)
- else:
- self.config = Config()
if client:
self.shell = client
@@ -32,7 +28,7 @@
auth = {'username': caller.email,
'password': hashlib.md5(caller.password).hexdigest()[:6],
'tenant': tenant}
- client = OpenStackClient(controller=controller, cacert=self.config.nova_ca_ssl_cert, **auth)
+ client = OpenStackClient(controller=controller, cacert=Config.get("nova.ca_ssl_cert"), **auth)
else:
admin_driver = self.admin_driver(tenant=tenant, controller=controller)
client = OpenStackClient(tenant=tenant, controller=admin_driver.controller)
@@ -47,7 +43,7 @@
controller = Controller.objects.get(id=controller.id)
if not tenant:
tenant = controller.admin_tenant
- client = OpenStackClient(tenant=tenant, controller=controller, cacert=self.config.nova_ca_ssl_cert)
+ client = OpenStackClient(tenant=tenant, controller=controller, cacert=Config.get("nova.ca_ssl_cert"))
driver = OpenStackDriver(client=client)
driver.admin_user = client.keystone.users.find(name=controller.admin_user)
driver.controller = controller
@@ -425,12 +421,12 @@
def spawn_instance(self, name, key_name=None, availability_zone=None, hostname=None, image_id=None, security_group=None, pubkeys=[], nics=None, metadata=None, userdata=None, flavor_name=None):
if not flavor_name:
- flavor_name = self.config.nova_default_flavor
+ flavor_name = Config.get("nova.default_flavor")
flavor = self.shell.nova.flavors.find(name=flavor_name)
if not security_group:
- security_group = self.config.nova_default_security_group
+ security_group = Config.get("nova.default_security_group")
files = {}
#if pubkeys:
diff --git a/xos/synchronizer/error_mapper.py b/xos/synchronizer/error_mapper.py
index 9eb878d..8bb02d3 100644
--- a/xos/synchronizer/error_mapper.py
+++ b/xos/synchronizer/error_mapper.py
@@ -1,25 +1,18 @@
-from xos.config import Config
from xos.logger import Logger, logging, logger
+
class ErrorMapper:
- def __init__(self, error_map_file):
- self.error_map = {}
- try:
- error_map_lines = open(error_map_file).read().splitlines()
- for l in error_map_lines:
- if (not l.startswith('#')):
- splits = l.split('->')
- k,v = map(lambda i:i.rstrip(),splits)
- self.error_map[k]=v
- except:
- logging.info('Could not read error map')
+ def __init__(self, error_map_file):
+ self.error_map = {}
+ try:
+ error_map_lines = open(error_map_file).read().splitlines()
+ for l in error_map_lines:
+ if (not l.startswith('#')):
+ splits = l.split('->')
+ k, v = map(lambda i: i.rstrip(), splits)
+ self.error_map[k] = v
+ except:
+ logging.info('Could not read error map')
-
- def map(self, error):
- return self.error_map[error]
-
-
-
-
-
-
+ def map(self, error):
+ return self.error_map[error]
diff --git a/xos/synchronizer/openstack-synchronizer.py b/xos/synchronizer/openstack-synchronizer.py
new file mode 100644
index 0000000..fe6bab2
--- /dev/null
+++ b/xos/synchronizer/openstack-synchronizer.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+import os
+import argparse
+import sys
+
+sys.path.append('/opt/xos')
+from xosconfig import Config
+
+config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/openstack_config.yaml')
+Config.init(config_file, 'synchronizer-config-schema.yaml')
+
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
+from xos.logger import Logger, logging, logger
+import time
+
+from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.backend import Backend
+from synchronizers.new_base.event_loop import set_driver
+
+logger = Logger(level=logging.INFO)
+
+# TODO: These two lines are the only difference between this file and
+# new_base/openstack-synchronizer.py. Reconcile these.
+# set the driver.
+from synchronizers.openstack.driver import OpenStackDriver
+set_driver(OpenStackDriver())
+
+
+def main():
+ models_active = False
+ wait = False
+ while not models_active:
+ try:
+ _ = Instance.objects.first()
+ _ = NetworkTemplate.objects.first()
+ models_active = True
+ except Exception,e:
+ logger.info(str(e))
+ logger.info('Waiting for data model to come up before starting...')
+ time.sleep(10)
+ wait = True
+
+ if (wait):
+ time.sleep(60) # Safety factor, seeing that we stumbled waiting for the data model to come up.
+ backend = Backend()
+ backend.run()
+
+if __name__ == '__main__':
+
+ main()
diff --git a/xos/synchronizer/openstack_config.yaml b/xos/synchronizer/openstack_config.yaml
new file mode 100644
index 0000000..b904e1c
--- /dev/null
+++ b/xos/synchronizer/openstack_config.yaml
@@ -0,0 +1,24 @@
+name: openstack-synchronizer
+accessor:
+ username: xosadmin@opencord.org
+ password: "@/opt/xos/services/openstack/credentials/xosadmin@opencord.org"
+dependency_graph: "/opt/xos/synchronizers/openstack/model-deps"
+steps_dir: "/opt/xos/synchronizers/openstack/steps"
+sys_dir: "/opt/xos/synchronizers/openstack/sys"
+model_policies_dir: "/opt/xos/synchronizers/openstack/model_policies"
+images_directory: "/opt/xos/images"
+required_models:
+ - ControllerImages
+ - ControllerNetwork
+ - ControllerSitePrivilege
+ - ControllerSite
+ - ControllerSlicePrivilege
+ - ControllerSlice
+ - ControllerUser
+ - Image
+ - Instance
+ - Port
+ - Role
+nova:
+ ca_ssl_cert: "/etc/ssl/certs/ca-certificates.crt"
+ enabled: True
\ No newline at end of file
diff --git a/xos/synchronizer/openstack_from_api_config b/xos/synchronizer/openstack_from_api_config
deleted file mode 100644
index 486e848..0000000
--- a/xos/synchronizer/openstack_from_api_config
+++ /dev/null
@@ -1,30 +0,0 @@
-[api]
-nova_enabled=True
-
-[nova]
-ca_ssl_cert=/etc/ssl/certs/ca-certificates.crt
-
-[log]
-file=console
-level=debug
-
-# Sets options for the synchronizer
-[observer]
-name=openstack
-dependency_graph=/opt/xos/synchronizers/openstack/model-deps
-steps_dir=/opt/xos/synchronizers/openstack/steps
-sys_dir=/opt/xos/synchronizers/openstack/sys
-model_policies_dir=/opt/xos/synchronizers/openstack/model_policies
-#logfile=/var/log/xos_backend.log
-log_file=console
-log_level=debug
-pretend=False
-backoff_disabled=True
-save_ansible_output=True
-accessor_kind=api
-accessor_password=@/opt/xos/services/openstack/credentials/xosadmin@opencord.org
-images_directory=/opt/xos/images
-required_models=ControllerImages,ControllerNetwork,ControllerSitePrivilege,ControllerSite,ControllerSlicePrivilege,ControllerSlice,ControllerUser,Image,Instance,Port,Role
-
-[networking]
-use_vtn=True
diff --git a/xos/synchronizer/openstack_synchronizer_config b/xos/synchronizer/openstack_synchronizer_config
deleted file mode 100644
index 3b7f8e9..0000000
--- a/xos/synchronizer/openstack_synchronizer_config
+++ /dev/null
@@ -1,31 +0,0 @@
-[db]
-name=xos
-user=postgres
-password=password
-host=xos_db
-port=5432
-
-[api]
-nova_enabled=True
-
-[nova]
-ca_ssl_cert=/etc/ssl/certs/ca-certificates.crt
-
-[observer]
-name=openstack
-dependency_graph=/opt/xos/model-deps
-steps_dir=/opt/xos/synchronizers/openstack/steps
-sys_dir=/opt/xos/synchronizers/openstack/sys
-model_policies_dir=/opt/xos/synchronizers/openstack/model_policies
-logfile=/var/log/xos_backend.log
-save_ansible_output=True
-backoff_disabled=True
-pretend=False
-images_directory=/opt/xos/images
-
-[feefie]
-client_id='vicci_dev_central'
-user_id='pl'
-
-[networking]
-use_vtn=True
diff --git a/xos/synchronizer/run-from-api.sh b/xos/synchronizer/run-from-api.sh
index 031528e..b91460f 100755
--- a/xos/synchronizer/run-from-api.sh
+++ b/xos/synchronizer/run-from-api.sh
@@ -1,2 +1 @@
-export XOS_DIR=/opt/xos
-python xos-synchronizer.py -C $XOS_DIR/synchronizers/openstack/openstack_from_api_config
+python openstack-synchronizer.py
diff --git a/xos/synchronizer/steps/purge_disabled_users.py b/xos/synchronizer/steps/purge_disabled_users.py
deleted file mode 100644
index 425d4e4..0000000
--- a/xos/synchronizer/steps/purge_disabled_users.py
+++ /dev/null
@@ -1,24 +0,0 @@
-import os
-import base64
-import datetime
-from xos.config import Config
-from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
-from xos.logger import observer_logger as logger
-from synchronizers.new_base.modelaccessor import *
-
-#class SyncRoles(OpenStackSyncStep):
-# provides=[User]
-# requested_interval=0
-# observes=User
-#
-# def fetch_pending(self, deleted):
-# if (deleted):
-# # users marked as deleted
-# return User.deleted_objects.all()
-# else:
-# # disabled users that haven't been updated in over a week
-# one_week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
-# return User.objects.filter(is_active=False, updated__gt=one_week_ago)
-#
-# def sync_record(self, user):
-# user.delete()
diff --git a/xos/synchronizer/steps/sync_container.py b/xos/synchronizer/steps/sync_container.py
index 7b69fa5..bc7fd22 100644
--- a/xos/synchronizer/steps/sync_container.py
+++ b/xos/synchronizer/steps/sync_container.py
@@ -4,7 +4,6 @@
import sys
import base64
import time
-from xos.config import Config
from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
from synchronizers.new_base.syncstep import DeferredException
from synchronizers.new_base.ansible_helper import run_template_ssh
diff --git a/xos/synchronizer/steps/sync_controller_images.py b/xos/synchronizer/steps/sync_controller_images.py
index 2241fec..721209f 100644
--- a/xos/synchronizer/steps/sync_controller_images.py
+++ b/xos/synchronizer/steps/sync_controller_images.py
@@ -1,9 +1,7 @@
import os
import base64
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.syncstep import *
-from xos.logger import observer_logger as logger
from synchronizers.new_base.ansible_helper import *
from synchronizers.new_base.modelaccessor import *
diff --git a/xos/synchronizer/steps/sync_controller_networks.py b/xos/synchronizer/steps/sync_controller_networks.py
index 329bb57..124574f 100644
--- a/xos/synchronizer/steps/sync_controller_networks.py
+++ b/xos/synchronizer/steps/sync_controller_networks.py
@@ -3,12 +3,10 @@
import struct
import socket
from netaddr import IPAddress, IPNetwork
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.syncstep import *
from xos.logger import observer_logger as logger
from synchronizers.new_base.ansible_helper import *
-from xos.config import Config
from synchronizers.new_base.modelaccessor import *
class SyncControllerNetworks(OpenStackSyncStep):
@@ -89,7 +87,7 @@
'gateway': controller_network.gateway,
'start_ip':start_ip,
'end_ip':end_ip,
- 'use_vtn':getattr(Config(), "networking_use_vtn", False),
+ 'use_vtn':True,
'delete':False
}
return network_fields
diff --git a/xos/synchronizer/steps/sync_controller_site_privileges.py b/xos/synchronizer/steps/sync_controller_site_privileges.py
index 2768090..cc41341 100644
--- a/xos/synchronizer/steps/sync_controller_site_privileges.py
+++ b/xos/synchronizer/steps/sync_controller_site_privileges.py
@@ -1,7 +1,6 @@
import os
import base64
import json
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.syncstep import *
from xos.logger import observer_logger as logger
diff --git a/xos/synchronizer/steps/sync_controller_sites.py b/xos/synchronizer/steps/sync_controller_sites.py
index ba489aa..fbdc454 100644
--- a/xos/synchronizer/steps/sync_controller_sites.py
+++ b/xos/synchronizer/steps/sync_controller_sites.py
@@ -1,10 +1,8 @@
import os
import base64
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.syncstep import *
from synchronizers.new_base.ansible_helper import *
-from xos.logger import observer_logger as logger
import json
from synchronizers.new_base.modelaccessor import *
diff --git a/xos/synchronizer/steps/sync_controller_slice_privileges.py b/xos/synchronizer/steps/sync_controller_slice_privileges.py
index 3a2d167..a2aa3a4 100644
--- a/xos/synchronizer/steps/sync_controller_slice_privileges.py
+++ b/xos/synchronizer/steps/sync_controller_slice_privileges.py
@@ -1,7 +1,6 @@
import os
import base64
import json
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.syncstep import *
from synchronizers.new_base.ansible_helper import *
diff --git a/xos/synchronizer/steps/sync_controller_slices.py b/xos/synchronizer/steps/sync_controller_slices.py
index a01c2b2..3cac6a9 100644
--- a/xos/synchronizer/steps/sync_controller_slices.py
+++ b/xos/synchronizer/steps/sync_controller_slices.py
@@ -1,6 +1,5 @@
import os
import base64
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.syncstep import *
from synchronizers.new_base.ansible_helper import *
diff --git a/xos/synchronizer/steps/sync_controller_users.py b/xos/synchronizer/steps/sync_controller_users.py
index 449dc11..7a67fd0 100644
--- a/xos/synchronizer/steps/sync_controller_users.py
+++ b/xos/synchronizer/steps/sync_controller_users.py
@@ -1,6 +1,5 @@
import os
import base64
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.syncstep import *
from synchronizers.new_base.ansible_helper import *
diff --git a/xos/synchronizer/steps/sync_images.py b/xos/synchronizer/steps/sync_images.py
index 61e8f18..b3994dd 100644
--- a/xos/synchronizer/steps/sync_images.py
+++ b/xos/synchronizer/steps/sync_images.py
@@ -1,6 +1,5 @@
import os
import base64
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from xos.logger import observer_logger as logger
from synchronizers.new_base.modelaccessor import *
diff --git a/xos/synchronizer/steps/sync_instances.py b/xos/synchronizer/steps/sync_instances.py
index 640c712..13e023c 100644
--- a/xos/synchronizer/steps/sync_instances.py
+++ b/xos/synchronizer/steps/sync_instances.py
@@ -1,33 +1,35 @@
import os
import base64
import socket
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from synchronizers.new_base.ansible_helper import *
from synchronizers.new_base.syncstep import *
from xos.logger import observer_logger as logger
from synchronizers.new_base.modelaccessor import *
-RESTAPI_HOSTNAME = getattr(Config(), "server_restapi_hostname", getattr(Config(), "server_hostname", socket.gethostname()))
-RESTAPI_PORT = int(getattr(Config(), "server_restapi_port", getattr(Config(), "server_port", "8000")))
+RESTAPI_HOSTNAME = socket.gethostname()
+RESTAPI_PORT = "8000"
+
def escape(s):
- s = s.replace('\n',r'\n').replace('"',r'\"')
+ s = s.replace('\n', r'\n').replace('"', r'\"')
return s
+
class SyncInstances(OpenStackSyncStep):
- provides=[Instance]
- requested_interval=0
- observes=Instance
- playbook='sync_instances.yaml'
+ provides = [Instance]
+ requested_interval = 0
+ observes = Instance
+ playbook = 'sync_instances.yaml'
def fetch_pending(self, deletion=False):
objs = super(SyncInstances, self).fetch_pending(deletion)
- objs = [x for x in objs if x.isolation=="vm"]
+ objs = [x for x in objs if x.isolation == "vm"]
return objs
def get_userdata(self, instance, pubkeys):
- userdata = '#cloud-config\n\nopencloud:\n slicename: "%s"\n hostname: "%s"\n restapi_hostname: "%s"\n restapi_port: "%s"\n' % (instance.slice.name, instance.node.name, RESTAPI_HOSTNAME, str(RESTAPI_PORT))
+ userdata = '#cloud-config\n\nopencloud:\n slicename: "%s"\n hostname: "%s"\n restapi_hostname: "%s"\n restapi_port: "%s"\n' % (
+ instance.slice.name, instance.node.name, RESTAPI_HOSTNAME, str(RESTAPI_PORT))
userdata += 'ssh_authorized_keys:\n'
for key in pubkeys:
userdata += ' - %s\n' % key
@@ -36,7 +38,7 @@
def get_nic_for_first_slot(self, nics):
# Try to find a NIC with "public" visibility
for nic in nics[:]:
- network=nic.get("network", None)
+ network = nic.get("network", None)
if network:
tem = network.template
if (tem.visibility == "public"):
@@ -44,11 +46,11 @@
# Otherwise try to find a private network
for nic in nics[:]:
- network=nic.get("network", None)
+ network = nic.get("network", None)
if network:
tem = network.template
- if (tem.visibility == "private") and (tem.translation=="none") and ("management" not in tem.name):
- return nic
+ if (tem.visibility == "private") and (tem.translation == "none") and ("management" not in tem.name):
+ return nic
raise Exception("Could not find a NIC for first slot")
@@ -69,13 +71,13 @@
# move the management network to the second spot
for nic in nics[:]:
- network=nic.get("network", None)
+ network = nic.get("network", None)
if network:
tem = network.template
- if (tem.visibility == "private") and (tem.translation=="none") and ("management" in tem.name):
-#MCORD
-# if len(result)!=1:
-# raise Exception("Management network needs to be inserted in slot 1, but there are %d private nics" % len(result))
+ if (tem.visibility == "private") and (tem.translation == "none") and ("management" in tem.name):
+ # MCORD
+ # if len(result)!=1:
+ # raise Exception("Management network needs to be inserted in slot 1, but there are %d private nics" % len(result))
result.append(nic)
nics.remove(nic)
@@ -88,24 +90,26 @@
# sanity check - make sure model_policy for slice has run
if ((not instance.slice.policed) or (instance.slice.policed < instance.slice.updated)):
- raise DeferredException("Instance %s waiting on Slice %s to execute model policies" % (instance, instance.slice.name))
+ raise DeferredException(
+ "Instance %s waiting on Slice %s to execute model policies" % (instance, instance.slice.name))
# sanity check - make sure model_policy for all slice networks have run
for network in instance.slice.ownedNetworks.all():
if ((not network.policed) or (network.policed < network.updated)):
- raise DeferredException("Instance %s waiting on Network %s to execute model policies" % (instance, network.name))
+ raise DeferredException(
+ "Instance %s waiting on Network %s to execute model policies" % (instance, network.name))
inputs = {}
- metadata_update = {}
+ metadata_update = {}
if (instance.numberCores):
metadata_update["cpu_cores"] = str(instance.numberCores)
-# not supported by API... assuming it's not used ... look into enabling later
-# for tag in instance.slice.tags.all():
-# if tag.name.startswith("sysctl-"):
-# metadata_update[tag.name] = tag.value
+ # not supported by API... assuming it's not used ... look into enabling later
+ # for tag in instance.slice.tags.all():
+ # if tag.name.startswith("sysctl-"):
+ # metadata_update[tag.name] = tag.value
- slice_memberships = SlicePrivilege.objects.filter(slice_id=instance.slice.id)
+ slice_memberships = SlicePrivilege.objects.filter(slice_id=instance.slice.id)
pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
if instance.creator.public_key:
pubkeys.add(instance.creator.public_key)
@@ -116,10 +120,10 @@
if instance.slice.service and instance.slice.service.public_key:
pubkeys.add(instance.slice.service.public_key)
- nics=[]
+ nics = []
# handle ports the were created by the user
- port_ids=[]
+ port_ids = []
for port in Port.objects.filter(instance_id=instance.id):
if not port.port_id:
raise DeferredException("Instance %s waiting on port %s" % (instance, port))
@@ -129,22 +133,26 @@
existing_port_networks = [port.network for port in Port.objects.filter(instance_id=instance.id)]
existing_port_network_ids = [x.id for x in existing_port_networks]
- networks = [ns.network for ns in NetworkSlice.objects.filter(slice_id=instance.slice.id) if ns.network.id not in existing_port_network_ids]
+ networks = [ns.network for ns in NetworkSlice.objects.filter(slice_id=instance.slice.id) if
+ ns.network.id not in existing_port_network_ids]
networks_ids = [x.id for x in networks]
- controller_networks = ControllerNetwork.objects.filter(controller_id=instance.node.site_deployment.controller.id)
+ controller_networks = ControllerNetwork.objects.filter(
+ controller_id=instance.node.site_deployment.controller.id)
controller_networks = [x for x in controller_networks if x.id in networks_ids]
-
for network in networks:
- if not ControllerNetwork.objects.filter(network_id=network.id, controller_id=instance.node.site_deployment.controller.id).exists():
- raise DeferredException("Instance %s Private Network %s lacks ControllerNetwork object" % (instance, network.name))
+ if not ControllerNetwork.objects.filter(network_id=network.id,
+ controller_id=instance.node.site_deployment.controller.id).exists():
+ raise DeferredException(
+ "Instance %s Private Network %s lacks ControllerNetwork object" % (instance, network.name))
for controller_network in controller_networks:
# Lenient exception - causes slow backoff
if controller_network.network.template.translation == 'none':
- if not controller_network.net_id:
- raise DeferredException("Instance %s Private Network %s has no id; Try again later" % (instance, controller_network.network.name))
- nics.append({"kind": "net", "value": controller_network.net_id, "network": controller_network.network})
+ if not controller_network.net_id:
+ raise DeferredException("Instance %s Private Network %s has no id; Try again later" % (
+ instance, controller_network.network.name))
+ nics.append({"kind": "net", "value": controller_network.net_id, "network": controller_network.network})
# now include network template
network_templates = [network.template.shared_network_name for network in networks \
@@ -158,14 +166,15 @@
if (not nics):
for net in nets:
- if net['name']=='public':
+ if net['name'] == 'public':
nics.append({"kind": "net", "value": net['id'], "network": None})
nics = self.sort_nics(nics)
image_name = None
controller_images = instance.image.controllerimages.all()
- controller_images = [x for x in controller_images if x.controller_id==instance.node.site_deployment.controller.id]
+ controller_images = [x for x in controller_images if
+ x.controller_id == instance.node.site_deployment.controller.id]
if controller_images:
image_name = controller_images[0].image.name
logger.info("using image from ControllerImage object: " + str(image_name))
@@ -178,18 +187,10 @@
image_name = image.name
logger.info("using image from glance: " + str(image_name))
- try:
- legacy = Config().observer_legacy
- except:
- legacy = False
+ host_filter = instance.node.name.strip()
- if (legacy):
- host_filter = instance.node.name.split('.',1)[0]
- else:
- host_filter = instance.node.name.strip()
-
- availability_zone_filter = 'nova:%s'%host_filter
- instance_name = '%s-%d'%(instance.slice.name,instance.id)
+ availability_zone_filter = 'nova:%s' % host_filter
+ instance_name = '%s-%d' % (instance.slice.name, instance.id)
self.instance_name = instance_name
userData = self.get_userdata(instance, pubkeys)
@@ -200,30 +201,29 @@
sanitized_nics = [{"kind": nic["kind"], "value": nic["value"]} for nic in nics]
controller = instance.node.site_deployment.controller
- fields = {'endpoint':controller.auth_url,
- 'endpoint_v3': controller.auth_url_v3,
- 'domain': controller.domain,
- 'admin_user': instance.creator.email,
- 'admin_password': instance.creator.remote_password,
- 'project_name': instance.slice.name,
- 'tenant': instance.slice.name,
- 'tenant_description': instance.slice.description,
- 'name':instance_name,
- 'ansible_tag':instance_name,
- 'availability_zone': availability_zone_filter,
- 'image_name':image_name,
- 'flavor_name':instance.flavor.name,
- 'nics':sanitized_nics,
- 'meta':metadata_update,
- 'user_data':r'%s'%escape(userData)}
+ fields = {'endpoint': controller.auth_url,
+ 'endpoint_v3': controller.auth_url_v3,
+ 'domain': controller.domain,
+ 'admin_user': instance.creator.email,
+ 'admin_password': instance.creator.remote_password,
+ 'project_name': instance.slice.name,
+ 'tenant': instance.slice.name,
+ 'tenant_description': instance.slice.description,
+ 'name': instance_name,
+ 'ansible_tag': instance_name,
+ 'availability_zone': availability_zone_filter,
+ 'image_name': image_name,
+ 'flavor_name': instance.flavor.name,
+ 'nics': sanitized_nics,
+ 'meta': metadata_update,
+ 'user_data': r'%s' % escape(userData)}
return fields
-
def map_sync_outputs(self, instance, res):
- instance_id = res[0]['openstack']['OS-EXT-SRV-ATTR:instance_name']
+ instance_id = res[0]['openstack']['OS-EXT-SRV-ATTR:instance_name']
instance_uuid = res[0]['id']
- try:
+ try:
hostname = res[0]['openstack']['OS-EXT-SRV-ATTR:hypervisor_hostname']
ip = socket.gethostbyname(hostname)
instance.ip = ip
@@ -234,23 +234,22 @@
instance.instance_uuid = instance_uuid
instance.instance_name = self.instance_name
instance.save()
-
-
+
def map_delete_inputs(self, instance):
controller_register = json.loads(instance.node.site_deployment.controller.backend_register)
- if (controller_register.get('disabled',False)):
- raise InnocuousException('Controller %s is disabled'%instance.node.site_deployment.controller.name)
+ if (controller_register.get('disabled', False)):
+ raise InnocuousException('Controller %s is disabled' % instance.node.site_deployment.controller.name)
- instance_name = '%s-%d'%(instance.slice.name,instance.id)
+ instance_name = '%s-%d' % (instance.slice.name, instance.id)
controller = instance.node.site_deployment.controller
- input = {'endpoint':controller.auth_url,
- 'admin_user': instance.creator.email,
- 'admin_password': instance.creator.remote_password,
- 'project_name': instance.slice.name,
- 'tenant': instance.slice.name,
- 'tenant_description': instance.slice.description,
- 'name':instance_name,
- 'ansible_tag':instance_name,
- 'delete': True}
+ input = {'endpoint': controller.auth_url,
+ 'admin_user': instance.creator.email,
+ 'admin_password': instance.creator.remote_password,
+ 'project_name': instance.slice.name,
+ 'tenant': instance.slice.name,
+ 'tenant_description': instance.slice.description,
+ 'name': instance_name,
+ 'ansible_tag': instance_name,
+ 'delete': True}
return input
diff --git a/xos/synchronizer/steps/sync_ports.py b/xos/synchronizer/steps/sync_ports.py
index cfd697f..f8a9d0b 100644
--- a/xos/synchronizer/steps/sync_ports.py
+++ b/xos/synchronizer/steps/sync_ports.py
@@ -1,6 +1,5 @@
import os
import base64
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from xos.logger import observer_logger as logger
from synchronizers.new_base.modelaccessor import *
diff --git a/xos/synchronizer/steps/sync_roles.py b/xos/synchronizer/steps/sync_roles.py
index c202f74..8267f4b 100644
--- a/xos/synchronizer/steps/sync_roles.py
+++ b/xos/synchronizer/steps/sync_roles.py
@@ -1,6 +1,5 @@
import os
import base64
-from xos.config import Config
from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
from xos.logger import observer_logger as logger
from synchronizers.new_base.modelaccessor import *
diff --git a/xos/synchronizer/xos-synchronizer.py b/xos/synchronizer/xos-synchronizer.py
deleted file mode 100644
index 6cd8e7a..0000000
--- a/xos/synchronizer/xos-synchronizer.py
+++ /dev/null
@@ -1,86 +0,0 @@
-#!/usr/bin/env python
-import os
-import argparse
-import sys
-
-sys.path.append('/opt/xos')
-
-os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
-from xos.config import Config, DEFAULT_CONFIG_FN
-from xos.logger import Logger, logging, logger
-import time
-
-from synchronizers.new_base.modelaccessor import *
-from synchronizers.new_base.backend import Backend
-from synchronizers.new_base.event_loop import set_driver
-
-config = Config()
-
-logger = Logger(level=logging.INFO)
-
-# TODO: These two lines are the only difference between this file and
-# new_base/xos-synchronizer.py. Reconcile these.
-# set the driver.
-from synchronizers.openstack.driver import OpenStackDriver
-set_driver(OpenStackDriver())
-
-# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
-def daemon():
- """Daemonize the current process."""
- if os.fork() != 0: os._exit(0)
- os.setsid()
- if os.fork() != 0: os._exit(0)
- os.umask(0)
- devnull = os.open(os.devnull, os.O_RDWR)
- os.dup2(devnull, 0)
- # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
- logdir=os.path.dirname(config.observer_logfile)
- # when installed in standalone we might not have httpd installed
- if not os.path.isdir(logdir): os.mkdir(logdir)
- crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
- os.dup2(crashlog, 1)
- os.dup2(crashlog, 2)
-
- if hasattr(config, "observer_pidfile"):
- pidfile = config.get("observer_pidfile")
- else:
- pidfile = "/var/run/xosobserver.pid"
- try:
- file(pidfile,"w").write(str(os.getpid()))
- except:
- print "failed to create pidfile %s" % pidfile
-
-def main():
- # Generate command line parser
- parser = argparse.ArgumentParser(usage='%(prog)s [options]')
- parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
- help='Run as daemon.')
- # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
- # throwing unrecognized argument exceptions
- parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
- help='Name of config file.')
- args = parser.parse_args()
-
- if args.daemon: daemon()
-
- models_active = False
- wait = False
- while not models_active:
- try:
- _ = Instance.objects.first()
- _ = NetworkTemplate.objects.first()
- models_active = True
- except Exception,e:
- logger.info(str(e))
- logger.info('Waiting for data model to come up before starting...')
- time.sleep(10)
- wait = True
-
- if (wait):
- time.sleep(60) # Safety factor, seeing that we stumbled waiting for the data model to come up.
- backend = Backend()
- backend.run()
-
-if __name__ == '__main__':
-
- main()