Ejemplo n.º 1
0
def __get_dpk_status():
    # Checking DPK download status
    if not os.path.exists(this.config.get('dpk_status_file')):
        logging.debug("DPK Status File missing - creating it now")
        try:
            with open(this.config.get('dpk_status_file'), 'w') as f:
                dpk_status = {
                    "downloaded_patch_files": False,
                    "unpacked_setup_scripts": False
                }
                json.dump(dpk_status, f)
        except FileNotFoundError:
            logging.error(
                "DPK files directory not created. Try again with '--setup-file-system'"
            )
            util.error_timings(timing_key)
            exit(2)
        except:
            logging.error("Issue creating DPK status file")
            raise
    else:
        try:
            with open(this.config.get('dpk_status_file')) as f:
                dpk_status = json.load(f)
        except:
            logging.error("Issue opening DPK status file")

    logging.debug("DPK status: \n" + json.dumps(dpk_status))
    this.config['downloaded_patch_files'] = dpk_status[
        'downloaded_patch_files']
    this.config['unpacked_setup_scripts'] = dpk_status[
        'unpacked_setup_scripts']
    logging.debug("download_patch_files")
Ejemplo n.º 2
0
def mount_file_system():
    logging.debug("Mounting")
    timing_key = "oci block mount"
    util.start_timing(timing_key)

    try:
        Path(config.get('--block-path')).mkdir(parents=True, exist_ok=True)

        mount_tmp_sh = config.get("--home") + "/ioco-oci-block-mount.sh"
        f = open(mount_tmp_sh, "w")
        f.write("#!/bin/bash\n")
        f.write("if ! grep -q '" + part + "' /etc/fstab ; then\n")
        f.write("    echo '# ioco-oci-block-mount' >> /etc/fstab\n")
        f.write("    echo '" + part + " " + config.get("--block-path") + " " +
                fs_type + " defaults 0 2' >> /etc/fstab\n")
        f.write("fi")
        f.close()

        subprocess.run(["sudo", "sh", mount_tmp_sh], check=True)
        subprocess.run(["sudo", "mount", "-a"], check=True)
    except:
        logging.error("Issue mounting partion")
        util.error_timings(timing_key)
        raise

    util.end_timing(timing_key)
Ejemplo n.º 3
0
def __unpack_dpk():
    # Unpack setup scripts
    timing_key = "dpk deploy __unpack_dpk"
    util.start_timing(timing_key)
    if this.config.get('unpacked_setup_scripts'):
        logging.debug("DPK setup scripts are already unpacked")
    else:
        logging.debug("Unpacking DPK setup scripts")

        try:
            for p in Path(this.config.get('dpk_files_dir')).glob("*_1of*.zip"):
                with zipfile.ZipFile(p, 'r') as zip_ref:
                    zip_ref.extractall(this.config.get('dpk_files_dir'))
        except:
            logging.error("Issue unpacking setup scripts")
            util.error_timings(timing_key)
            exit(2)

        try:
            # Change permissions on setup folder
            for root, dirs, files in os.walk(
                    this.config.get('dpk_files_dir') + "/setup"):
                for momo in files:
                    os.chmod(os.path.join(root, momo), 775)
        except:
            logging.error(
                "Issue setting permissions on unpacked setup scripts")
            util.error_timings(timing_key)
            raise
            exit(2)

        logging.debug("Update DPK status - unpacked_setup_scripts: true")
        update_dpk_status('unpacked_setup_scripts', True)

    util.end_timing(timing_key)
Ejemplo n.º 4
0
def install(config):
    timing_key = "rundeck install"
    util.start_timing(timing_key)

    try:
        logging.debug("Setting up for install")
        rd_base = "/u01/app/rundeck"  # TODO - assume this location?
        Path(rd_base).mkdir(parents=True, exist_ok=True)
        shutil.chown(rd_base, user="******", group="opc")  # TODO - assuming opc?

        logging.debug("Adding rundeck rpm")
        subprocess.run(
            ["sudo", "rpm", "-Uvh", "https://repo.rundeck.org/latest.rpm"])

        logging.debug("Installing java and rundeck")
        subprocess.run(
            ["sudo", "yum", "-y", "install", "java-1.8.0", "rundeck"])

        logging.debug("Starting rundeck service")
        subprocess.run(["sudo", "service", "rundeckd", "start"])

        logging.debug("Open firewall")
        subprocess.run(
            ["sudo", "firewall-cmd", "--zone=public", "--add-port=4440/tcp"],
            check=True)
    except:
        logging.error("Error setting up Rundeck")
        util.error_timings(timing_key)

    util.end_timing(timing_key)
Ejemplo n.º 5
0
Archivo: oci.py Proyecto: peytonlc/ioco
def make_file_system():
    logging.debug("Making file system for block volume")
    timing_key = "oci block make_file_system"
    util.start_timing(timing_key)

    # Create partition, if needed
    if os.path.exists(this.part):
        logging.debug("Partition on " + this.config.get("--block-disk") + " is already created")        
    else:
        logging.debug("Creating partition on " + this.config.get("--block-disk"))        
        try:
            fdisk_tmp_sh = config.get("--home") + "/ioco-oci-block-partition.sh"
            f = open(fdisk_tmp_sh,"w")
            f.write("sudo fdisk " + this.config.get("--block-disk") + " <<EOF\nn\np\n\n\n\nw\nEOF")
            f.close()
        except:
            logging.error("Issue creating temp partition setup script")
            util.error_timings(timing_key)

        try:    
            subprocess.run(["sudo","sh", fdisk_tmp_sh], check=True)
        except:
            logging.error("Issue creating partion")
            util.error_timings(timing_key)

    # Create file system, if needed
    logging.debug("Creating file sytem using " + this.part)        
    try:
        subprocess.run(["sudo","mkfs." + this.fs_type, this.part], check=True)
    except:
        logging.info("File system did NOT create, assuming already created")
        util.error_timings(timing_key)
    
    util.end_timing(timing_key)
Ejemplo n.º 6
0
def undeploy():
    # TODO - Issue #3 convert this to more python based, not just bash in sub-process
    timing_key = "dpk undeploy"
    util.start_timing(timing_key)
    try:
        subprocess.run(['sudo','pkill', '-9', '-u', 'psadm2'], \
            stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
        subprocess.run(['sudo','pkill', '-9', '-u', 'oracle2'], \
            stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
        subprocess.run(['sudo','rm','-rf',this.config.get('psft_base_dir')], \
            stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
        subprocess.run(['sudo','rm','-rf','/opt/oracle/psft/db/oraInventory'], \
            stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
        subprocess.run(['sudo','rm','-f','/etc/oraInst.loc'], \
            stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
        logging.info(
            'Done undeploying. An instance reboot may be required before next deploy.'
        )
    except:
        logging.error('Issue undeploying')
        util.error_timings(timing_key)
        raise

    util.end_timing(timing_key)
Ejemplo n.º 7
0
def read_secret_value(config):
    logging.debug("Reading secret from Vault")
    timing_key = "vault read"
    util.start_timing(timing_key)

    try:
        secret_id = config['--secret-id']
        response = this.secret_client.get_secret_bundle(secret_id)
    except:
        logging.error("Error reading from Vault")
        util.error_timings(timing_key)
        raise

    try:
        base64_secret_content = response.data.secret_bundle_content.content
        base64_secret_bytes = base64_secret_content.encode('ascii')
        base64_message_bytes = base64.b64decode(base64_secret_bytes)
        secret_content = base64_message_bytes.decode('ascii')
    except:
        logging.error("Error decoding secert")
        util.error_timings(timing_key)
        raise

    print("{}".format(secret_content))
Ejemplo n.º 8
0
def attach(config):
    timing_key = "cm attach-dpk-files"
    util.start_timing(timing_key)

    logging.debug("Attaching Cloud Manager DPK files repository")

    try:
        cm_nfs_host = config.get("--nfs-host")
        cm_export = config.get("--export")
        cm_mount_path = config.get("--mount-path")
    except KeyError as e:
        logging.error("INVALID config - Missing " + str(e) + " setting.")
        util.error_timings(timing_key)

    try:
        # mkdir
        Path(cm_mount_path).mkdir(parents=True, exist_ok=True)
    except:
        logging.error('Issue creating mount path directory')
        util.error_timings(timing_key)

    # if mount path directory is empty, assume it needs mounting
    if os.listdir(cm_mount_path) == []:
        try:
            # mount
            cmd = [cm_nfs_host + ":" + cm_export, cm_mount_path]
            logging.debug(cmd)
            subprocess.run(["sudo", "mount", "-t", "nfs"] + cmd, check=True)
            #, stdout=subprocess.STDOUT, stderr=subprocess.STDOUT)
            logging.debug('Mount successful')
        except:
            logging.error('Issue mounting path')
            util.error_timings(timing_key)
            raise
    else:
        logging.debug("Mount path already contains files, skip mounting")

    util.end_timing(timing_key)
Ejemplo n.º 9
0
def __setup_dpk():
    logging.info("Running DPK setup scripts")

    timing_key = "dpk deploy __setup_dpk"
    util.start_timing(timing_key)

    # generate_response_file
    logging.debug("Generating response file at " +
                  this.config.get('dpk_files_dir') + "/response.cfg")
    try:
        rsp_file = open(
            this.config.get('dpk_files_dir') + "/response.cfg", "w")
        if this.config.get('--dpk-type') == 'tools':
            type_responses = [
                "env_type  = \"midtier\"\n", "db_platform = \"ORACLE\"\n"
            ]
        else:
            type_responses = [
                "env_type  = \"fulltier\"\n", "db_type = \"DEMO\"\n"
            ]

        responses = type_responses + [
            "psft_base_dir = \"" + this.config.get('psft_base_dir') + "\"\n",
            "dpk_location = \"" + this.config.get('dpk_deploy_dir') + "\"\n",
            "user_home_dir = \"" + this.config.get('user_home_dir') + "\"\n",
            "db_name = \"" + this.config.get('db_name') + "\"\n",
            "db_service_name = \"" + this.config.get('db_service_name') +
            "\"\n",
            "db_host = \"" + this.config.get('db_host') + "\"\n",
            "install_type = \"PUM\"\n",
            "admin_pwd = \"" + this.config.get('admin_pwd') + "\"\n",
            "connect_pwd = \"" + this.config.get('connect_pwd') + "\"\n",
            "access_pwd  = \"" + this.config.get('access_pwd') + "\"\n",
            "opr_id = \"" + this.config.get('opr_id') + "\"\n",
            "opr_pwd = \"" + this.config.get('opr_pwd') + "\"\n",
            # "domain_conn_pwd = \"P@ssw0rd_\"\n",
            "weblogic_admin_pwd  = \"" +
            this.config.get('weblogic_admin_pwd') + "\"\n",
            "webprofile_user_pwd = \"" +
            this.config.get('webprofile_user_pwd') + "\"\n",
            "gw_user_pwd = \"" + this.config.get('gw_user_pwd') + "\"\n",
            "gw_keystore_pwd = \"" + this.config.get('gw_keystore_pwd') +
            "\"\n",
        ]
        rsp_file.writelines(responses)
        rsp_file.close()
    except:
        logging.error('Issue generating response file')
        util.end_timing(timing_key)
        util.print_timings()
        raise
        exit(3)

    # generate psft_customizations.yaml file
    if os.path.exists(this.config.get('--cust-yaml')):
        logging.debug("psft_customizations.yaml file found at " +
                      this.config.get('--cust-yaml'))
    else:
        logging.debug("Generating default psft_customizations.yaml file at " +
                      this.config.get('--cust-yaml'))
        try:
            yaml_file = open(this.config.get('--cust-yaml'), "w")
            yaml_lines = [
                "---", "\n# defaults generated by ioco",
                "\ndpk_location:     " + this.config.get('dpk_deploy_dir'),
                "\nuser_home_dir:    " + this.config.get('user_home_dir'),
                "\nps_config_home:   " + this.config.get('ps_cfg_dir')
            ]
            yaml_file.writelines(yaml_lines)
            yaml_file.close()
        except:
            logging.error('Issue generating response file')
            util.error_timings(timing_key)
            raise
            exit(3)

    # execute_psft_dpk_setup
    logging.debug("Executing DPK Setup")

    if os.name == 'nt':
        logging.warning("Windows is not supported at this time.")
    else:
        setup_script = this.config.get(
            'dpk_files_dir') + "/setup/psft-dpk-setup.sh"
        dpk_logfile = open(
            this.config.get('--logs') + "/psft-dpk-setup.log", "w")
        try:
            subprocess.run(
                [
                    "sh",
                    setup_script,
                    "--silent",
                    "--dpk_src_dir " + this.config.get('dpk_files_dir'),
                    "--response_file " + this.config.get('dpk_files_dir') +
                    "/response.cfg",
                    "--customization_file " + this.config.get('--cust-yaml'),
                    #                    "--no_puppet_run"
                ],
                check=True,
                stdout=dpk_logfile,
                stderr=dpk_logfile)
        except:
            logging.error("DPK setup script failed.")
            util.error_timings(timing_key)
            raise
    util.end_timing(timing_key)
Ejemplo n.º 10
0
def __get_dpk_mos():
    logging.info(" - Downloading files from MOS")
    timing_key = "dpk deploy __get_dpk_mos"
    util.start_timing(timing_key)

    logging.debug("Creating auth cookie from MOS")
    cookie_file = this.config.get('dpk_files_dir') + "/mos.cookie"

    # eat any old cookies
    if os.path.exists(cookie_file):
        os.remove(cookie_file)

    try:
        # Create a session and update headers
        s = requests.session()
        s.headers.update({'User-Agent': 'Mozilla/5.0'})

        # Initiate updates.oracle.com request to get login redirect URL
        logging.debug('Requesting downloads page')
        r = s.get("https://updates.oracle.com/Orion/Services/download",
                  allow_redirects=False)
        login_url = r.headers['Location']
        if not login_url:
            logging.error("Location was empty so login URL can't be set")
            util.error_timings(timing_key)
            exit(2)

        # Create a NEW session, then send Basic Auth to login redirect URL
        logging.debug('Sending Basic Auth to login, using new session')
        s = requests.session()
        logging.debug("Using MOS username: "******"MOS login was successful")
        else:
            logging.error("MOS login was NOT successful.")
            util.error_timings(timing_key)
            exit(2)
    except:
        logging.error("Issue getting MOS auth token")
        util.end_timing(timing_key)
        raise

    try:
        # Use same session to search for downloads
        logging.debug('Search for list of downloads, using same session')
        mos_uri_search = "https://updates.oracle.com/Orion/SimpleSearch/process_form?search_type=patch&patch_number=" + this.config.get(
            'patch_id') + "&plat_lang=226P"
        r = s.get(mos_uri_search)
        search_results = r.content.decode('utf-8')

        # Validate search results
        if r.ok:
            logging.debug("Search results return success")
        else:
            logging.error("Search results did NOT return success")
            util.error_timings(timing_key)
            exit(2)
    except:
        logging.error("Issue getting MOS search results")
        util.end_timing(timing_key)
        raise

    try:
        # Extract download links to list
        pattern = "https://.+?Download/process_form/[^\"]*.zip[^\"]*"
        download_links = re.findall(pattern, search_results)
        download_links_file = this.config.get(
            'dpk_files_dir') + "/mos-download.links"
        # Write download links to file
        f = open(download_links_file, "w")
        for link in download_links:
            # Write download links list to file
            logging.debug(link)
            f.write(link + os.linesep)
        f.close()

        # Validate download links
        if len(download_links) > 0:
            logging.info(" - Downloading " + str(len(download_links)) +
                         " files")
        else:
            logging.error("No download links found")
            util.error_timings(timing_key)
            exit(2)
    except:
        logging.error("Issue creating download links file")
        util.end_timing(timing_key)
        raise

    # multi thread download
    results = ThreadPool(this.config.get('download_threads')).imap_unordered(
        __download_file, download_links)
    for r in results:
        logging.info("    [DONE] " + r)

    logging.debug("Update DPK status - downloaded_patch_files: true")
    update_dpk_status('downloaded_patch_files', True)
    util.end_timing(timing_key)
Ejemplo n.º 11
0
def __get_dpk_cm():
    logging.info(" - Getting files from DPK repo")
    timing_key = "dpk deploy __get_dpk_cm"
    util.start_timing(timing_key)

    # Get dpk source dir
    try:
        repo = Path(this.config.get('cm_dpk_files_dir'))
        dpk_path = repo / 'dpk' / this.config.get(
            '--dpk-platform') / this.config.get('--dpk-type')
        logging.debug("DPK type base: " + str(dpk_path))
    except TypeError:
        logging.error(
            "Issue generating dpk path. Are --dpk-platform and --dpk-type set correctly?"
        )
        exit(5)
    except:
        logging.error("Unknown issue generating dpk path")
        raise

    # Get DPK version
    if this.config.get('--dpk-version'):
        dpk_version = this.config.get('--dpk-version')
    else:
        # Get max DPK version
        try:
            versions = [
                f.name for f in os.scandir(dpk_path)
                if f.is_dir() and f.name.isdigit()
            ]
            logging.debug("DPK versions found: " + str(versions))
        except FileNotFoundError as e:
            logging.error(
                str(e) +
                ". Use CM to subscribe and download this type and platform.")
            util.error_timings(timing_key)
            exit(5)

        versions.sort(reverse=True)
        dpk_version = versions[0]

    dpk_path = dpk_path / dpk_version
    logging.debug(
        this.config.get('--dpk-platform') + " " +
        this.config.get('--dpk-type') + " DPK version found: " +
        str(dpk_version))

    if this.config.get('--dpk-type') == 'tools':
        # Get DPK patch
        if this.config.get('--dpk-patch'):
            dpk_patch = this.config.get('--dpk-patch')
        else:
            # Get max DPK tools patch
            try:
                versions = [
                    f.name for f in os.scandir(dpk_path)
                    if f.is_dir() and f.name.isdigit()
                ]
                logging.debug("DPK tools patches found: " + str(versions))
            except FileNotFoundError as e:
                logging.error(
                    str(e) +
                    ". Use CM to subscribe and download this type and platform."
                )
                util.end_timing(timing_key)
                util.print_timings()
                exit(5)

            versions.sort(reverse=True)
            dpk_patch = versions[0]

        dpk_path = dpk_path / dpk_patch
        logging.debug(
            this.config.get('--dpk-platform') + " " +
            "DPK tools patch found: " + str(dpk_patch))

    # copy dpk files
    try:
        logging.debug("Copying " + str(dpk_path) + " to " +
                      this.config.get('dpk_files_dir'))
        source_dpks = os.listdir(dpk_path)
        for f in source_dpks:
            full_f = os.path.join(dpk_path, f)
            if os.path.isfile(full_f):  #TODO - only zip?
                shutil.copy(full_f, this.config.get('dpk_files_dir'))

        logging.debug("Update DPK status - downloaded_patch_files: true")
        update_dpk_status('downloaded_patch_files', True)

    except FileExistsError:
        logging.error("DPK files in working directory already exist")
        exit(2)
    except:
        logging.error(
            "Issue copying DPK files from source to tmp working directory")
        util.end_timing(timing_key)
        util.print_timings()
        raise

    util.end_timing(timing_key)
Ejemplo n.º 12
0
Archivo: oci.py Proyecto: peytonlc/ioco
    util.start_timing(timing_key)

    try:
<<<<<<< HEAD
        Path(config.get('--mount-path')).mkdir(parents=True, exist_ok=True)
=======
        Path(config.get('--block-path')).mkdir(parents=True, exist_ok=True)
>>>>>>> 9301974fb1ec0a5352f6edb5a3400d9eb4ee392f
        
        mount_tmp_sh = config.get("--home") + "/ioco-oci-block-mount.sh"
        f = open(mount_tmp_sh,"w")
        f.write("#!/bin/bash\n")
        f.write("if ! grep -q '" + part + "' /etc/fstab ; then\n")
        f.write("    echo '# ioco-oci-block-mount' >> /etc/fstab\n")
        f.write("    echo '" + part + " " + config.get("--block-path") + " " + fs_type + " defaults 0 2' >> /etc/fstab\n")
        f.write("fi")
        f.close()
        
        subprocess.run(["sudo","sh", mount_tmp_sh], check=True)
        subprocess.run(["sudo","mount", "-a"], check=True)
    except:
        logging.error("Issue mounting partion")
        util.error_timings(timing_key)
        raise

<<<<<<< HEAD
    util.end_timing(timing_key)
=======
    util.end_timing(timing_key)
>>>>>>> 9301974fb1ec0a5352f6edb5a3400d9eb4ee392f