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=core.is_debug()) 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=core.is_debug()) 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 _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=core.is_debug()) 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=core.is_debug()) except CmdError as cme: core.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=core.is_debug()) else: s = self.runHost("oc login -u %s -p %s" % (account, password), verbose=core.is_debug())
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: core.print_info(e, "OpenShift applications were removed") pass project = self.runHost('oc new-project %s' % self.project_name, ignore_status=True, verbose=core.is_debug()) 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: core.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.conf["openshift"]["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 copyFrom(self, src, dest): """ Copy file from one location (module) to another one to (host) :param src: str :param dest: str :return: None """ self.runHost("cp -r %s %s" % (src, dest), verbose=core.is_debug())
def run(self, *args, **kwargs): """ Run command inside module, parametr command and others are passed to proper module Helper :param args: command :param kwargs: shell, ignore_status, verbose :return: object avocado.process.run """ if core.is_debug(): self.__print_breaks("COMMAND IN MODULE <->") return self.backend.run(*args, **kwargs)
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=core.is_debug()) return oc_delete.exit_status
def runHost(self, *args, **kwargs): """ Run command on host (local machine). all parameters are passed inside. the most important is command what contains command to run :param args: pass thru :param kwargs: pass thru :return: object of avocado.process.run """ if core.is_debug(): self.__print_breaks("COMMAND ON HOST <!>") return self.backend.runHost(*args, **kwargs)
def start(self, command="/bin/true"): """ Start 'service' inside NSPAWN container Keep it running with sleep infinity, systemd-run needs to have it running :param command: Do not use it directly (It is defined in config.yaml) :return: None """ command = self.info.get('start') or command self.run(command, internal_background=False, ignore_bg_processes=True, verbose=core.is_debug()) self.status() common.trans_dict["GUESTPACKAGER"] = self.get_packager()
def status(self, command=None): """ get status if container is running :return: bool """ if not self.docker_id and common.get_if_reuse(): result = self.runHost("docker ps -q --filter %s" % self.docker_static_name[2:], ignore_status=True, verbose=core.is_debug()) # lenght of docker id number is 12 if result.exit_status == 0 and len(result.stdout) > 10: self.docker_id = result.stdout.strip() return True if self.docker_id and self.docker_id[:12] in self.runHost( "docker ps", shell=True, verbose=core.is_not_silent()).stdout: return True else: return False
def _openshift_login(self, oc_ip=common.conf["openshift"]["local_ip"], oc_user=common.conf["openshift"]["local_user"], oc_passwd=common.conf["openshift"]["local_password"], 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=core.is_debug()) return oc_output.exit_status