def update_project_dir(): logging.debug("unzipping " + MPK_FILE + " to " + INCOMING_MPK_DIR) subprocess.check_call(("rm", "-rf", INCOMING_MPK_DIR)) util.mkdir_p(INCOMING_MPK_DIR) subprocess.check_call(("unzip", "-oqq", MPK_FILE, "-d", INCOMING_MPK_DIR)) new_mpr = os.path.basename(util.get_mpr_file_from_dir(INCOMING_MPK_DIR)) existing_mpr_path = util.get_mpr_file_from_dir(PROJECT_DIR) if existing_mpr_path: existing_mpr = os.path.basename(existing_mpr_path) else: existing_mpr = None logging.debug("rsync from incoming to intermediate") if util.get_buildpack_loglevel() < logging.INFO: quiet_or_verbose = "--verbose" else: quiet_or_verbose = "--quiet" subprocess.call(( "rsync", "--recursive", "--checksum", "--delete", INCOMING_MPK_DIR + "/", INTERMEDIATE_MPK_DIR + "/", )) logging.debug("rsync from intermediate to project") if new_mpr == existing_mpr: update_or_delete = "--update" else: update_or_delete = "--delete" subprocess.call(( "rsync", "--recursive", update_or_delete, quiet_or_verbose, INTERMEDIATE_MPK_DIR + "/", PROJECT_DIR + "/", ))
def get_version(build_path): file_name = os.path.join(build_path, "model", "metadata.json") try: with open(file_name) as file_handle: data = json.loads(file_handle.read()) return MXVersion(data["RuntimeVersion"]) except IOError: mpr = util.get_mpr_file_from_dir(build_path) if not mpr: raise Exception("No model/metadata.json or .mpr found in archive") cursor = sqlite3.connect(mpr).cursor() cursor.execute("SELECT _ProductVersion FROM _MetaData LIMIT 1") record = cursor.fetchone() return MXVersion(record[0])
def get_runtime_version(build_path=BASE_PATH): result = get_metadata_value("RuntimeVersion", build_path) if result == None: logging.debug( "Cannot retrieve runtime version %s from metadata file, falling back to project file" ) mpr = util.get_mpr_file_from_dir(build_path) if not mpr: raise Exception("No model/metadata.json or .mpr found in archive") cursor = sqlite3.connect(mpr).cursor() cursor.execute("SELECT _ProductVersion FROM _MetaData LIMIT 1") record = cursor.fetchone() result = record[0] return MXVersion(result)
def build(): mpr = os.path.abspath(util.get_mpr_file_from_dir(PROJECT_DIR)) response = requests.post( "http://localhost:6666/build", data=json.dumps({ "target": "Deploy", "projectFilePath": mpr, "forceFullDeployment": False, }), headers={"Content-Type": "application/json"}, timeout=120, ) if response.status_code != requests.codes["ok"]: raise MxBuildFailure("MxBuild failure", response.status_code, response.json()) result = response.json() if result["status"] == "Success": try: sync_project_files() logging.info("Syncing project files ...") except Exception: logging.warning("Syncing project files failed: %s", traceback.format_exc()) raise try: send_metadata_to_cloudportal() except Exception: logging.warning( "Failed to send instadeploy feedback to Cloud Portal", exc_info=True, ) else: logging.warning("Not syncing project files. MxBuild result: %s", result) return result
def get_mpr_file(): return util.get_mpr_file_from_dir(BUILD_DIR)
def stage(build_path, cache_path, local_path, runtime_version, java_version): mono_location = mono.ensure_and_get_mono(runtime_version, cache_path) logging.info("Mono available: %s", mono_location) mono_env = mono.get_env_with_monolib(mono_location) mxbuild_location = os.path.join(local_path, "mxbuild") ensure_mxbuild_in_directory(mxbuild_location, runtime_version, cache_path) jvm_location = java.ensure_and_get_jvm(java_version, cache_path, local_path) util.lazy_remove_file(BUILD_ERRORS_JSON) args = [ os.path.join(mono_location, "bin/mono"), "--config", os.path.join(mono_location, "etc/mono/config"), os.path.join(mxbuild_location, "modeler/mxbuild.exe"), "--target=package", "--output=/tmp/model.mda", "--java-home=%s" % jvm_location, "--java-exe-path=%s" % os.path.join(jvm_location, "bin/java"), ] if runtime_version >= 6.4 or os.environ.get("FORCE_WRITE_BUILD_ERRORS"): args.append("--write-errors=%s" % BUILD_ERRORS_JSON) logging.debug("Will write build errors to %s", BUILD_ERRORS_JSON) if os.environ.get("FORCED_MXBUILD_URL"): args.append("--loose-version-check") logging.warning( "Using forced mxbuild version, the model will be converted") args.append(util.get_mpr_file_from_dir(build_path)) try: logging.debug("subprocess call %s", args) subprocess.check_call(args, env=mono_env) except subprocess.CalledProcessError as ex: buildstatus_callback(BUILD_ERRORS_JSON) raise RuntimeError(ex) for file_name in os.listdir(build_path): filepath = os.path.join(build_path, file_name) if file_name != ".local": if os.path.isdir(filepath): shutil.rmtree(filepath) else: os.unlink(filepath) zf = zipfile.ZipFile("/tmp/model.mda") try: zf.extractall(build_path) finally: zf.close() try: with open(os.path.join(build_path, ".sourcepush"), "w") as dsp: dsp.write("sourcepush") except OSError as ex: logging.warning("Could not write source push indicator: %s", str(ex))
def build_from_source( buildpack_path, build_path, cache_path, local_path, runtime_version, java_version, ): logging.info("Building from source...") mono_location = mono.ensure_and_get_mono(runtime_version, buildpack_path, cache_path) mono_env = mono.get_env_with_monolib(mono_location) mxbuild_location = os.path.join(local_path, "mxbuild") runtime.resolve_runtime_dependency( buildpack_path, build_path, cache_path, destination=mxbuild_location, prefix="mxbuild", ) jdk_location = java.ensure_and_get_jvm(java_version, buildpack_path, cache_path, local_path) util.lazy_remove_file(BUILD_ERRORS_JSON) args = [ os.path.join(mono_location, "bin/mono"), "--config", os.path.join(mono_location, "etc/mono/config"), os.path.join(mxbuild_location, "modeler/mxbuild.exe"), "--target=package", "--output=/tmp/model.mda", "--java-home=%s" % jdk_location, "--java-exe-path=%s" % os.path.join(jdk_location, "bin/java"), ] if runtime_version >= 6.4 or os.environ.get("FORCE_WRITE_BUILD_ERRORS"): args.append("--write-errors=%s" % BUILD_ERRORS_JSON) logging.debug("Will write build errors to %s", BUILD_ERRORS_JSON) if os.environ.get("FORCED_MXBUILD_URL"): args.append("--loose-version-check") logging.warning( "Using forced MxBuild version, the model will be converted") args.append(util.get_mpr_file_from_dir(build_path)) try: subprocess.check_call(args, env=mono_env) except subprocess.CalledProcessError as ex: _log_buildstatus_errors(BUILD_ERRORS_JSON) raise RuntimeError(ex) for file_name in os.listdir(build_path): filepath = os.path.join(build_path, file_name) if file_name != ".local": if os.path.isdir(filepath): shutil.rmtree(filepath) else: os.unlink(filepath) zf = zipfile.ZipFile("/tmp/model.mda") try: zf.extractall(build_path) finally: zf.close() try: with open(os.path.join(build_path, ".sourcepush"), "w") as dsp: dsp.write("sourcepush") except OSError as ex: logging.warning("Could not write source push indicator: %s", str(ex)) logging.debug("Deleting Mxbuild, Mono and JDK...") for path in (mono_location, mxbuild_location, jdk_location): shutil.rmtree(path, ignore_errors=False) if os.path.exists(path): logging.error("%s not deleted", path) logging.info("Building from source completed")