def __init__(self, _exit, verbose): # setup_client try: # setup client depending if running on linux or using boot2docker (osx/win) if sys.platform == 'linux': self.client = docker_py.Client(version='auto') else: # get b2d ip self.ip = str(sh.boot2docker.ip()).strip() try: # try secure connection first kw = kwargs_from_env(assert_hostname=False) self.client = docker_py.Client(version='auto', **kw) except docker_py.errors.DockerException as e: # shit - some weird boot2docker, python, docker-py, requests, and ssl error # https://github.com/docker/docker-py/issues/465 if verbose: log.debug(e) log.warn("Cannot connect securely to Docker, trying insecurely") kw = kwargs_from_env(assert_hostname=False) if 'tls' in kw: kw['tls'].verify = False self.client = docker_py.Client(version='auto', **kw) except Exception as e: if verbose: log.error("Could not connect to Docker - try running 'docker info' first") if sys.platform != 'linux': log.error("Please ensure you've run 'boot2docker up' and 'boot2docker shellinit' first and have added the ENV VARs it suggests") if _exit: raise e
def stackhut_api_call(endpoint, msg, secure=True): url = urllib.parse.urljoin(utils.SERVER_URL, endpoint) log.debug("Calling Stackhut Server at {} with \n\t{}".format(url, json.dumps(msg))) r = requests.post(url, data=json.dumps(msg), headers=json_header) if r.status_code == requests.codes.ok: return r.json() else: log.error("Error {} talking to Stackhut Server".format(r.status_code)) log.error(r.text) r.raise_for_status()
def push_image(self, tag): if self.push: log.info("Uploading image {} - this may take a while...".format(tag)) with t_utils.Spinner(): r = self.docker.client.push(tag, stream=True) r_summary = [json.loads(x.decode('utf-8')) for x in r][-1] if 'error' in r_summary: log.error(r_summary['error']) log.error(r_summary['errorDetail']) raise RuntimeError("Error pushing to Docker Hub, check your connection and auth details") else: log.debug(r_summary['status'])
def build_dockerfile(self, tag, dockerfile='Dockerfile'): log.debug("Running docker build for {}".format(tag)) cache_flag = '--no-cache=True' if self.no_cache else '--no-cache=False' cmds = ['-f', dockerfile, '-t', tag, '--rm', cache_flag, '.'] log.info("Starting build, this may take some time, please wait...") try: if utils.VERBOSE: self.docker.run_docker_sh('build', cmds, _out=lambda x: log.debug(x.strip())) else: self.docker.run_docker_sh('build', cmds) except sh.ErrorReturnCode as e: log.error("Couldn't complete build") log.error("Build error - {}".format(e.stderr.decode('utf-8').strip())) if not utils.VERBOSE: log.error("Build Traceback - \n{}".format(e.stdout.decode('utf-8').strip())) raise RuntimeError("Docker Build failed") from None
def run(self): super().run() # validation checks self.usercfg.assert_logged_in() service = Service(self.hutcfg, self.usercfg.username) # run the contract regardless rpc.generate_contract() if self.local: # call build+push first using Docker builder if not self.no_build: service.build_push(force=self.force, push=True, dev=self.dev) else: import tempfile import requests import os.path from stackhut_common import utils from stackhut_client import client log.info("Starting Remote build, this may take a while (especially the first time), please wait...") # compress and upload the service with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False) as f: f.close() # get the upload url r_file = stackhut_api_user_call("file", dict(filename=os.path.basename(f.name)), self.usercfg) sh.tar( "-czvf", f.name, "--exclude", ".git", "--exclude", "__pycache__", "--exclude", "run_result", "--exclude", ".stackhut", ".", ) log.debug("Uploading package {} ({:.2f} Kb)...".format(f.name, os.path.getsize(f.name) / 1024)) with open(f.name, "rb") as f1: with Spinner(): r = requests.put(r_file["url"], data=f1) r.raise_for_status() # remove temp file os.unlink(f.name) # call the remote build service auth = client.SHAuth(self.usercfg.username, hash=self.usercfg["hash"]) sh_client = client.SHService("stackhut", "stackhut", auth=auth, host=utils.SERVER_URL) log.debug("Uploaded package, calling remote build...") try: with Spinner(): r = sh_client.Default.remoteBuild(r_file["key"], self.dev) except client.SHRPCError as e: log.error("Build error, remote build output below...") log.error(e.data["output"]) return 1 else: log.debug("Remote build output...\n" + r["cmdOutput"]) if not r["success"]: raise RuntimeError("Build failed") log.info("...completed Remote build") # Inform the SH server re the new/updated service # build up the deploy message body test_request = json.loads(self._read_file("test_request.json")) readme = self._read_file("README.md") data = { "service": service.short_name, # StackHut Service, "github_url": self.hutcfg.github_url, "example_request": test_request, "description": self.hutcfg.description, "private_service": self.hutcfg.private, "readme": readme, "schema": self.create_methods(), } log.info("Deploying image '{}' to StackHut".format(service.short_name)) r = stackhut_api_user_call("add", data, self.usercfg) log.info("Service {} has been {} and is live".format(service.short_name, r["message"])) log.info("You can now call this service with our client-libs or directly over JSON+HTTP") return 0