def fetch(self, directory): if not os.path.exists(directory): os.makedirs(directory) self.path = os.path.join(directory, self.filename) url = self.artifact if 'CCT_ARTIFACT_CACHE' in os.environ: cache = os.environ['CCT_ARTIFACT_CACHE'] logger.info('Using CCT_ARTIFACT_CACHE=%s to fetch artifact' % cache) for var in [ v for v in dir(self) if not callable(getattr(self, v)) ]: if var.startswith('_'): continue token = '#%s#' % var cache = cache.replace(token, getattr(self, var)) url = cache if self.check_sum(): logger.info("Using cached artifact for %s" % self.filename) return logger.info("Fetching %s from %s." % (self.filename, url)) try: if os.path.basename(url) == url: raise CCTError( "Artifact is referenced by filename - can't download it.") urlrequest.urlretrieve(url, self.path) except Exception as ex: if self.hint: raise CCTError('artifact: "%s" was not found. %s' % (self.path, self.hint)) else: raise CCTError( "cannot download artifact from url %s, error: %s" % (url, ex)) if not self.check_sum(): if self.hint: raise CCTError('hash is not correct for artifact: "%s". %s' % (self.path, self.hint)) else: raise CCTError( "artifact from %s doesn't match required chksum %s" % (url, self.chksum))
def _run_function(self, name, *args): cmd = '/bin/bash -c " source %s ; %s %s"' % (self.script, name, " ".join(args)) try: env = dict(os.environ) mod_dir = os.path.dirname(self.script) env['CCT_MODULE_PATH'] = mod_dir logger.info( 'Created CCT_MODULE_PATH environment variable for module %s' % mod_dir) for name, mod_dir in self.modules_dirs.items(): var_name = 'CCT_MODULE_PATH_%s' % name.upper().replace( '-', '_') env[var_name] = mod_dir logger.info('Created %s environment variable for module %s.' % (var_name, mod_dir)) for name, artifact in self.artifacts.items(): var_name = 'CCT_ARTIFACT_PATH_' + name.upper().replace( '-', '_') logger.info('Created %s environment variable pointing to %s.' % (var_name, artifact.path)) env[var_name] = artifact.path out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, env=env, shell=True) self.logger.debug("Step ended with output: %s" % out) except subprocess.CalledProcessError as e: raise CCTError(e.output)
def shell_as_user(self, user=None, *command): user_uid = os.getuid() if user: user_uid = pwd.getpwnam(user).pw_uid self.logger.debug("Executing shell command: '%s'" % " ".join(command)) process = subprocess.Popen(" ".join(command), preexec_fn=self._demote(user_uid), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self.env, shell=True) stdout, stderr = process.communicate() retcode = process.wait() if stdout: self.logger.debug("Captured stdout: %s" % stdout) if stderr: self.logger.error("Captured stderr: %s" % stderr) if retcode == 0: self.logger.debug("Command '%s' executed successfully" % " ".join(command)) else: raise CCTError("Command '%s' failed" % " ".join(command))
def install_module(self, url, version, override=False): if override and not version: raise CCTError('Cannot override vesrion without specifying it!') self.version = version self.override = override repo_dir = "%s/%s" % (self.directory, os.path.basename(url)) if repo_dir.endswith('git'): repo_dir = repo_dir[:-4] logger.info("Cloning module into %s" % repo_dir) clone_repo(url, repo_dir, self.version, self.override) self.discover_modules(repo_dir)
def _wait_for_as(self): start = time() while time() < start + self.jboss_timeout: try: self._run_jboss_cli("connect") logger.debug("Application server is ready.") return except Exception as e: logger.debug("waiting for Application server to start. %s", e) sleep(5) logger.error("Cannot connect cli to application server.") raise CCTError("Cannot connect cli to application server.")
def teardown(self): if self.jboss_process: logger.debug("Stopping application server.") self._run_jboss_cli("shutdown") start = time() while self.jboss_process.poll() is None or time( ) > start + self.jboss_timeout: sleep(5) logger.debug("Waiting for application server to stop.") if self.jboss_process.poll(): raise CCTError("Cannot stop application server.") else: if self.jboss_home: self._clear()
def clone_repo(url, path, version=None, force=False): try: if not os.path.exists(path): logger.info("Cloning %s into %s" % (url, path)) subprocess.check_call(["git", "clone", url, path]) if version: logger.info('Checking out %s revision' % version) subprocess.check_call(['git', 'checkout', version], cwd=path) elif os.path.exists(path) and force: logger.info('Forcing %s revision for %s' % (version, path)) subprocess.check_call(['git', 'checkout', version], cwd=path) except Exception as ex: logger.error("Cannot clone repo %s into %s: %s", url, path, ex) raise CCTError('Cannot clone repo %s, %s' % (url, ex))
def setup(self, jboss_home=None, jboss_timeout=120): self.jboss_timeout = jboss_timeout logger.debug("Got jboss home '%s'." % jboss_home) if jboss_home: self.jboss_home = jboss_home else: try: self.jboss_home = os.environ['JBOSS_HOME'] except: logger.error("Cannot determine JBOSS_HOME location.") raise CCTError('Cannot determine JBOSS_HOME location.') logger.debug('launching standalone jboss: "%s"' % (self.jboss_home + "/bin/standalone.sh")) self.jboss_process = Popen(self.jboss_home + "/bin/standalone.sh", stdout=PIPE, stderr=PIPE) self._wait_for_as()
def _run_jboss_cli(self, command): with tempfile.NamedTemporaryFile(delete=False) as tf: tf.write("%s \nexit" % command) tf.flush() logger.debug('launching cli: "%s %s"' % ((self.jboss_home + "/bin/jboss-cli.sh"), tf.name)) cli = Popen( [ self.jboss_home + "/bin/jboss-cli.sh", "--connect", "--file=%s" % tf.name ], stdout=PIPE, stderr=PIPE, ) out, err = cli.communicate() if cli.returncode == 0: #success logger.debug('Command completed succesfully.') return else: logger.error('Command failed, msg: %s.' % (out + err)) raise CCTError( "Cannot run jboss_cli command return code: '%s'.", cli.returncode)