<s> __author__ = '<STR_LIT>' <EOL> import platform <EOL> import urllib <EOL> import shutil <EOL> from subprocess import CalledProcessError <EOL> import mongoctl . config as config <EOL> from mongoctl . prompt import prompt_execute_task <EOL> from mongoctl . mongodb_version import MongoDBEdition <EOL> from mongoctl . mongoctl_logging import * <EOL> from mongoctl . errors import MongoctlException <EOL> from mongoctl . utils import ( <EOL> download_url , extract_archive , call_command , which , ensure_dir , <EOL> validate_openssl , execute_command , list_dir_files , is_exe <EOL> ) <EOL> from mongoctl . mongodb_version import make_version_info , is_valid_version_info <EOL> from mongoctl . commands . command_utils import ( <EOL> find__all_mongo_installations , get_mongo_installation <EOL> ) <EOL> from mongoctl . binary_repo import download_mongodb_binary , get_template_args <EOL> LATEST_VERSION_FILE_URL = "<STR_LIT>" "<STR_LIT>" <EOL> def install_command ( parsed_options ) : <EOL> install_mongodb ( parsed_options . version , <EOL> mongodb_edition = parsed_options . edition , <EOL> from_source = parsed_options . fromSource , <EOL> build_threads = parsed_options . buildThreads , <EOL> build_tmp_dir = parsed_options . buildTmpDir , <EOL> include_only = parsed_options . includeOnly ) <EOL> def uninstall_command ( parsed_options ) : <EOL> uninstall_mongodb ( parsed_options . version , edition = parsed_options . edition ) <EOL> def list_versions_command ( parsed_options ) : <EOL> mongo_installations = find__all_mongo_installations ( ) <EOL> bar = "<STR_LIT:->" * <NUM_LIT> <EOL> print bar <EOL> formatter = "<STR_LIT>" <EOL> print formatter % ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> print bar <EOL> for install_dir , version in mongo_installations : <EOL> print formatter % ( version . version_number , <EOL> version . edition , install_dir ) <EOL> print "<STR_LIT:\n>" <EOL> def install_mongodb ( mongodb_version = None , mongodb_edition = None , from_source = False , <EOL> build_threads = <NUM_LIT:1> , <EOL> build_tmp_dir = None , <EOL> include_only = None ) : <EOL> if mongodb_version is None : <EOL> mongodb_version = fetch_latest_stable_version ( ) <EOL> log_info ( "<STR_LIT>" % <EOL> mongodb_version ) <EOL> version_info = make_version_info ( mongodb_version , mongodb_edition ) <EOL> mongo_installation = get_mongo_installation ( version_info ) <EOL> mongodb_edition = version_info . edition <EOL> if mongo_installation is not None : <EOL> log_info ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( version_info , mongo_installation ) ) <EOL> return mongo_installation <EOL> target_dir = get_install_target_dir ( mongodb_version , mongodb_edition ) <EOL> if os . path . exists ( target_dir ) : <EOL> raise MongoctlException ( "<STR_LIT>" % <EOL> target_dir ) <EOL> if mongodb_edition not in MongoDBEdition . ALL : <EOL> raise MongoctlException ( "<STR_LIT>" % <EOL> ( mongodb_edition , MongoDBEdition . ALL ) ) <EOL> if from_source : <EOL> install_from_source ( mongodb_version , mongodb_edition , <EOL> build_threads = build_threads , <EOL> build_tmp_dir = build_tmp_dir ) <EOL> return <EOL> bits = platform . architecture ( ) [ <NUM_LIT:0> ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> os_name = platform . system ( ) . lower ( ) <EOL> if os_name == '<STR_LIT>' and platform . mac_ver ( ) : <EOL> os_name = "<STR_LIT>" <EOL> mongodb_installs_dir = config . get_mongodb_installs_dir ( ) <EOL> if not mongodb_installs_dir : <EOL> raise MongoctlException ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ensure_dir ( mongodb_installs_dir ) <EOL> platform_spec = get_validate_platform_spec ( os_name , bits ) <EOL> log_verbose ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( os_name , bits , version_info , <EOL> platform_spec ) ) <EOL> try : <EOL> archive_path = download_mongodb_binary ( mongodb_version , <EOL> mongodb_edition ) <EOL> archive_name = os . path . basename ( archive_path ) <EOL> mongo_dir_name = extract_archive ( archive_name ) <EOL> if include_only : <EOL> apply_include_only ( mongo_dir_name , include_only ) <EOL> log_info ( "<STR_LIT>" % archive_name ) <EOL> os . remove ( archive_name ) <EOL> target_dir_name = os . path . basename ( target_dir ) <EOL> os . rename ( mongo_dir_name , target_dir_name ) <EOL> if os . getcwd ( ) != mongodb_installs_dir : <EOL> log_info ( "<STR_LIT>" % mongodb_installs_dir ) <EOL> shutil . move ( target_dir_name , mongodb_installs_dir ) <EOL> install_dir = os . path . join ( mongodb_installs_dir , mongo_dir_name ) <EOL> validate_mongodb_install ( target_dir ) <EOL> log_info ( "<STR_LIT>" % version_info ) <EOL> return install_dir <EOL> except Exception , e : <EOL> log_exception ( e ) <EOL> msg = "<STR_LIT>" % ( version_info , e ) <EOL> raise MongoctlException ( msg ) <EOL> def install_from_source ( mongodb_version , mongodb_edition , build_threads = None , <EOL> build_tmp_dir = None ) : <EOL> """<STR_LIT>""" <EOL> version_info = make_version_info ( mongodb_version , mongodb_edition ) <EOL> if build_tmp_dir : <EOL> ensure_dir ( build_tmp_dir ) <EOL> os . chdir ( build_tmp_dir ) <EOL> allowed_build_editions = [ MongoDBEdition . COMMUNITY , <EOL> MongoDBEdition . COMMUNITY_SSL ] <EOL> if mongodb_edition not in allowed_build_editions : <EOL> raise MongoctlException ( "<STR_LIT>" % <EOL> allowed_build_editions ) <EOL> log_info ( "<STR_LIT>" % ( mongodb_version , <EOL> mongodb_edition ) ) <EOL> source_archive_name = "<STR_LIT>" % mongodb_version <EOL> target_dir = get_install_target_dir ( mongodb_version , mongodb_edition ) <EOL> source_url = ( "<STR_LIT>" % <EOL> source_archive_name ) <EOL> response = urllib . urlopen ( source_url ) <EOL> if response . getcode ( ) != <NUM_LIT:200> : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( mongodb_version , response . getcode ( ) ) ) <EOL> raise MongoctlException ( msg ) <EOL> log_info ( "<STR_LIT>" % <EOL> ( mongodb_version , source_url ) ) <EOL> download_url ( source_url ) <EOL> log_info ( "<STR_LIT>" ) <EOL> source_dir = extract_archive ( source_archive_name ) <EOL> log_info ( "<STR_LIT>" ) <EOL> scons_exe = which ( "<STR_LIT>" ) <EOL> if not scons_exe : <EOL> raise MongoctlException ( "<STR_LIT>" ) <EOL> scons_cmd = [ scons_exe , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if build_threads : <EOL> scons_cmd . extend ( [ "<STR_LIT>" , str ( build_threads ) ] ) <EOL> scons_cmd . append ( "<STR_LIT>" % target_dir ) <EOL> if mongodb_edition == MongoDBEdition . COMMUNITY_SSL : <EOL> validate_openssl ( ) <EOL> scons_cmd . append ( "<STR_LIT>" ) <EOL> log_info ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( scons_cmd ) ) <EOL> call_command ( scons_cmd , cwd = source_dir ) <EOL> log_info ( "<STR_LIT>" ) <EOL> try : <EOL> os . remove ( source_archive_name ) <EOL> shutil . rmtree ( source_dir ) <EOL> except Exception , e : <EOL> log_error ( str ( e ) ) <EOL> log_exception ( e ) <EOL> validate_mongodb_install ( target_dir ) <EOL> log_info ( "<STR_LIT>" % version_info ) <EOL> def validate_mongodb_install ( install_dir ) : <EOL> log_info ( "<STR_LIT>" % install_dir ) <EOL> mongod_exe = os . path . join ( install_dir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> cmd = [ mongod_exe , "<STR_LIT>" ] <EOL> try : <EOL> execute_command ( cmd ) <EOL> log_info ( "<STR_LIT>" ) <EOL> except CalledProcessError , cpe : <EOL> log_exception ( cpe ) <EOL> raise MongoctlException ( "<STR_LIT>" % <EOL> ( "<STR_LIT:U+0020>" . join ( cmd ) , cpe . output ) ) <EOL> def uninstall_mongodb ( version_number , edition = None ) : <EOL> version_info = make_version_info ( version_number , edition = edition ) <EOL> if not is_valid_version_info ( version_info ) : <EOL> raise MongoctlException ( "<STR_LIT>" <EOL> "<STR_LIT>" % version_info ) <EOL> mongo_installation = get_mongo_installation ( version_info ) <EOL> if mongo_installation is None : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> version_info ) <EOL> log_info ( msg ) <EOL> return <EOL> log_info ( "<STR_LIT>" % ( version_info , mongo_installation ) ) <EOL> def rm_mongodb ( ) : <EOL> ensure_mongo_home_not_used ( mongo_installation ) <EOL> log_info ( "<STR_LIT>" % mongo_installation ) <EOL> shutil . rmtree ( mongo_installation ) <EOL> log_info ( "<STR_LIT>" % version_info ) <EOL> prompt_execute_task ( "<STR_LIT>" , rm_mongodb ) <EOL> def fetch_latest_stable_version ( ) : <EOL> response = urllib . urlopen ( LATEST_VERSION_FILE_URL ) <EOL> if response . getcode ( ) == <NUM_LIT:200> : <EOL> return response . read ( ) . strip ( ) <EOL> else : <EOL> raise MongoctlException ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( LATEST_VERSION_FILE_URL , response . getcode ( ) ) ) <EOL> def get_install_target_dir ( mongodb_version , mongodb_edition ) : <EOL> template = "<STR_LIT>" <EOL> args = get_template_args ( mongodb_version , mongodb_edition ) <EOL> dir_name = template . format ( ** args ) <EOL> return os . path . join ( config . get_mongodb_installs_dir ( ) , dir_name ) <EOL> def get_validate_platform_spec ( os_name , bits ) : <EOL> if os_name not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:win32>" , "<STR_LIT>" ] : <EOL> raise MongoctlException ( "<STR_LIT>" % os_name ) <EOL> if bits == "<STR_LIT>" : <EOL> return "<STR_LIT>" % os_name <EOL> else : <EOL> if os_name == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> elif os_name in [ "<STR_LIT>" , "<STR_LIT:win32>" ] : <EOL> return "<STR_LIT>" % os_name <EOL> elif os_name == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> def ensure_mongo_home_not_used ( mongo_installation ) : <EOL> output = execute_command ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> if mongo_installation in output : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> mongo_installation ) <EOL> raise MongoctlException ( msg ) <EOL> def apply_include_only ( mongo_dir_name , include_only ) : <EOL> """<STR_LIT>""" <EOL> log_info ( "<STR_LIT>" % <EOL> include_only ) <EOL> bin_dir = os . path . join ( mongo_dir_name , "<STR_LIT>" ) <EOL> exes = list_dir_files ( bin_dir ) <EOL> for exe_name in exes : <EOL> if exe_name == "<STR_LIT>" : <EOL> continue <EOL> exe_path = os . path . join ( bin_dir , exe_name ) <EOL> if is_exe ( exe_path ) : <EOL> if exe_name not in include_only : <EOL> log_info ( "<STR_LIT>" % exe_name ) <EOL> os . remove ( exe_path ) </s>
