def _create_app_by_template(self): """ It creates an application in OpenShift environment by OpenShift template Steps: * oc cluster up * oc create -f <template> -n openshift * oc new-app memcached -p APPLICATION_NAME=memcached :return: """ self._register_docker_to_openshift_registry() self.runHost('oc get is') oc_template_app = self.runHost('oc process -f "%s"' % self.template, verbose=common.is_not_silent()) self._change_openshift_account() oc_template_create = None try: oc_template_create = self.runHost( 'oc create -f %s -n %s' % (self.template, self.project_name), verbose=common.is_not_silent()) except CmdError as cme: common.print_info('oc create -f failed with traceback %s' % cme.message) self.runHost('oc status') self._oc_get_output('all') return False self._change_openshift_account(account=common.get_openshift_user(), password=common.get_openshift_passwd()) template_name = self._get_openshift_template() time.sleep(1) self._create_app(template=template_name) self.runHost('oc status') return True
def _change_openshift_account(self, account="system:admin", password=None): """ Function switches to specific account inside OpenShift environment :param account: Either user specified account or 'system:admin' :param password: Either user specified account """ if password is None: s = self.runHost("oc login -u %s" % account, verbose=common.is_not_silent()) else: s = self.runHost("oc login -u %s -p %s" % (account, password), verbose=common.is_not_silent())
def start(self, command="/bin/bash"): """ starts the OpenShift application :param command: Do not use it directly (It is defined in config.yaml) :return: None """ # Clean environment before running tests try: self._app_remove() except Exception as e: common.print_info(e, "OpenShift applications were removed") pass project = self.runHost('oc new-project %s' % self.project_name, ignore_status=True, verbose=common.is_not_silent()) if self.template is None: if not self._app_exists(): # This part is used for running an application without template or s2i self._create_app() else: common.print_debug(self.template) self._change_openshift_account( account=common.get_openshift_user(), password=common.get_openshift_passwd()) self._remove_apps_from_openshift_resources(common.TEMPLATE) if not self._create_app_by_template(): return False # Verify application is really deploy and prepared for testing. if not self._verify_pod(): return False self._get_ip_instance()
def __oc_status(self): oc_status = self.runHost("oc status", ignore_status=True, verbose=common.is_not_silent()) common.print_debug(oc_status.stdout) common.print_debug(oc_status.stderr) return oc_status.exit_status
def _oc_delete(self, resource, name): """ Function removes a resource with given name from OpenShift environment :param resource: a name of resource :param name: a name in given resource :return: return value from oc delete command """ oc_delete = self.runHost("oc delete %s %s" % (resource, name), ignore_status=True, verbose=common.is_not_silent()) return oc_delete.exit_status
def __stop_openshift_cluster(self): """ Internal method, do not use it anyhow. It stops OpenShift cluster :return: None """ if common.get_openshift_local(): if int(self.__oc_status()) == 0: common.print_info("Stopping OpenShift") self.runHost("oc cluster down", verbose=common.is_not_silent()) else: common.print_info("OpenShift is already stopped.")
def _register_docker_to_openshift_registry(self): """ Function pushes an OpenShift docker-registry into docker Commands which are use are in this order * oc whoami -t * switch to OpenShift system:admin account * gets an IP of OpenShift registry * switch to user defined OpenShift user and password. * runs docker login with token and IP:5000 * tag container name * push container name into docker """ whoami = self.runHost("oc whoami -t", ignore_status=True, verbose=common.is_not_silent()) self._change_openshift_account() if self.get_docker_pull(): self.runHost('docker pull %s' % self.container_name) openshift_ip_register = self._get_openshift_ip_registry() self._change_openshift_account(account=common.get_openshift_user(), password=common.get_openshift_passwd()) docker_login = self.runHost( 'docker login -u {user} -p {token} {ip}:5000'.format( user=common.get_openshift_user(), token=whoami.stdout.strip(), ip=openshift_ip_register), ignore_status=True, verbose=common.is_not_silent()) oc_path = "{ip}:5000/{project}/{name}".format( ip=openshift_ip_register, name=self.app_name, project=self.project_name) self.runHost('docker tag %s %s' % (self.container_name, oc_path), ignore_status=True) self.runHost('docker push %s' % oc_path, ignore_status=True)
def __prepare_selinux(self): # disable selinux by default if not turned off if not os.environ.get('MTF_SKIP_DISABLING_SELINUX'): # https://github.com/fedora-modularity/meta-test-family/issues/53 # workaround because systemd nspawn is now working well in F-26 if not os.path.exists(selinux_state_file): print_info("Disabling selinux") actual_state = self.runHost("getenforce", ignore_status=True).stdout.strip() with open(selinux_state_file, 'w') as openfile: openfile.write(actual_state) if setseto not in actual_state: self.runHost("setenforce %s" % setseto, verbose=is_not_silent(), sudo=True)
def __cleanup(self): if not os.environ.get('MTF_SKIP_DISABLING_SELINUX'): print_info("Turning back selinux to previous state") actual_state = self.runHost("getenforce", ignore_status=True).stdout.strip() if os.path.exists(selinux_state_file): print_info("Turning back selinux to previous state") with open(selinux_state_file, 'r') as openfile: stored_state = openfile.readline() if stored_state != actual_state: self.runHost("setenforce %s" % stored_state, ignore_status=True, verbose=is_not_silent(), sudo=True) os.remove(selinux_state_file) else: print_info("Selinux state is not stored, skipping.")
def _get_pod_status(self): """ This method checks if the POD is running within OpenShift environment. :return: True if POD is running with status "Running" False all other statuses """ pod_initiated = False pod_state = self.runHost("oc get pods -o json", ignore_status=True, verbose=common.is_not_silent()) pod_state = self._convert_string_to_json(pod_state.stdout) for pod in pod_state: if self._check_app_in_json(pod, self.app_name): self._pod_status = pod.get('status').get('phase') if self._pod_status == "Running": pod_initiated = True break return pod_initiated
def _remove_apps_from_openshift_namespaces(self, oc_service="svc"): """ It removes an application from specific "namespace" like svc, dc, is. :param oc_service: Service from which we would like to remove application """ # Check status of svc/dc/is oc_get = self.runHost("oc get %s -o json" % oc_service, ignore_status=True).stdout oc_get = self._convert_string_to_json(oc_get) # The output is like # dovecot 172.30.1.1:5000/myproject/dovecot latest 15 minutes ago # memcached 172.30.1.1:5000/myproject/memcached latest 13 minutes ago for item in oc_get: if self._check_app_in_json(item, self.app_name): # If application exists in svc / dc / is namespace, then remove it oc_delete = self.runHost("oc delete %s %s" % (oc_service, self.app_name), ignore_status=True, verbose=common.is_not_silent())
def _openshift_login(self, oc_ip="127.0.0.1", oc_user='******', oc_passwd='developer', env=False): """ It logins to an OpenShift environment on specific IP and under user and his password. :param oc_ip: an IP where is an OpenShift environment running :param oc_user: an username under which we can login to OpenShift environment :param oc_passwd: a password for specific username :param env: is used for specification OpenShift IP, user and password, otherwise defaults are used :return: """ if env: oc_ip = common.get_openshift_ip() oc_user = common.get_openshift_user() oc_passwd = common.get_openshift_passwd() oc_output = self.runHost( "oc login %s:8443 --username=%s --password=%s" % (oc_ip, oc_user, oc_passwd), verbose=common.is_not_silent()) return oc_output.exit_status