break for s in range(5): print(".", end="") time.sleep(1) print() if not ready: raise Exception("Server nodes failed to become healthy!") if __name__ == "__main__": parser = ArgumentParser(prog="install_couchbase_server") config = Configuration() config.load() parser.add_argument("keyname", action="store", type=str, help="The name of the SSH key that the EC2 instances are using") parser.add_argument("--region", action="store", type=str, dest="region", default=config.get(SettingKeyNames.AWS_REGION), help="The EC2 region to query (default %(default)s)") parser.add_argument("--server-name-prefix", action="store", type=str, dest="servername", default=config.get(SettingKeyNames.CBS_SERVER_PREFIX), help="The name of the server to use to reset the Couchbase cluster (default %(default)s)") parser.add_argument("--ssh-key", action="store", type=str, dest="sshkey", help="The key to connect to EC2 instances") parser.add_argument("--setup-only", action="store_true", dest="setuponly", help="Skip the program installation, and configure only") parser.add_argument("--username", action="store", default=config.get(SettingKeyNames.CBS_ADMIN), help="The administrator username for Couchbase Server (default %(default)s)")
class CouchbaseServerInstaller: __config: Configuration __url: str __version: str __build: str __raw_version: str __ssh_keyfile: str __ssh_keypass: Credential @staticmethod def version_to_code(version: str): try: parsed_version = Version(version) except InvalidVersion: print("Non-numeric version {} received, interpreting as codename...".format(version)) return version if parsed_version >= Version("7.0"): return "cheshire-cat" print(colored("This script uses features introduced in 6.5, earlier versions not supported...", "red")) raise UnsupportedException("Unsupported version of Couchbase Server requested") @staticmethod def _parse_version(version: str): version_build = version.split("-") if len(version_build) == 2: return (CouchbaseServerInstaller._version_to_code(version_build[0]), version_build[1]) return (version, None) @staticmethod def _generate_filename(version: str, build: str): if build is None: return "couchbase-server-enterprise-{}-centos7.x86_64.rpm".format(version) return "couchbase-server-enterprise-{}-{}-centos7.x86_64.rpm".format(version, build) def __init__(self, url: str, ssh_keyfile: str, keypass: Credential): self.__config = Configuration() self.__config.load() self.__raw_version = self.__config[SettingKeyNames.CBS_VERSION] self.__url = url self.__ssh_keyfile = ssh_keyfile self.__ssh_keypass = keypass (self.__version, self.__build) = CouchbaseServerInstaller._parse_version(self.__raw_version) def download(self): filename = CouchbaseServerInstaller._generate_filename(self.__version, self.__build) if not Path(filename).exists(): print("Downloading Couchbase Server {}...".format(self.__raw_version)) url = self._generate_download_url(self.__version, self.__build, filename) wget.download(url, filename) def install(self): filename = CouchbaseServerInstaller._generate_filename(self.__version, self.__build) if not Path(filename).exists(): raise Exception("Unable to find installer, please call download first") print("Installing Couchbase Server to {}...".format(self.__url)) ssh_client = SSHClient() ssh_client.load_system_host_keys() ssh_client.set_missing_host_key_policy(WarningPolicy()) ssh_connect(ssh_client, self.__url, self.__ssh_keyfile, str(self.__ssh_keypass)) (_, stdout, _) = ssh_client.exec_command("test -f {}".format(filename)) if stdout.channel.recv_exit_status() == 0: print("Install file already present on remote host, skipping upload...") else: print("Uploading file to remote host...") sftp = ssh_client.open_sftp() sftp_upload(sftp, filename, filename) sftp.close() ssh_command(ssh_client, self.__url, "sudo yum install -y {}".format(filename)) print("Install finished!") def _generate_download_url(self, version: str, build: str, filename: str): # All access via VPN or company network if build is not None: return "http://latestbuilds.service.couchbase.com/builds/latestbuilds/couchbase-server/{}/{}/{}".format( version, build, filename) return "http://latestbuilds.service.couchbase.com/builds/releases/{}/{}".format(version, filename)
class SyncGatewayInstaller: __config: Configuration __url: str __version: str __build: str __raw_version: str __ssh_keyfile: str __ssh_keypass: Credential @staticmethod def _parse_version(version: str): version_build = version.split("-") if len(version_build) == 2: return (version_build[0], version_build[1]) return (version, None) @staticmethod def _generate_filename(version: str, build: str): if build is None: return "couchbase-sync-gateway-enterprise_{}_x86_64.rpm".format( version) return "couchbase-sync-gateway-enterprise_{}-{}_x86_64.rpm".format( version, build) def __init__(self, url: str, ssh_keyfile: str, keypass: Credential): self.__config = Configuration() self.__config.load() self.__raw_version = self.__config[SettingKeyNames.SG_VERSION] self.__url = url self.__ssh_keyfile = ssh_keyfile self.__ssh_keypass = keypass (self.__version, self.__build) = SyncGatewayInstaller._parse_version( self.__raw_version) def download(self): filename = SyncGatewayInstaller._generate_filename( self.__version, self.__build) if not Path(filename).exists(): print("Downloading Sync Gateway {}...".format(self.__raw_version)) url = self._generate_download_url(self.__version, self.__build, filename) wget.download(url, filename) def install(self): filename = SyncGatewayInstaller._generate_filename( self.__version, self.__build) if not Path(filename).exists(): raise Exception( "Unable to find installer, please call download first") print("Installing Sync Gateway to {}...".format(self.__url)) ssh_client = SSHClient() ssh_client.load_system_host_keys() ssh_client.set_missing_host_key_policy(WarningPolicy()) ssh_connect(ssh_client, self.__url, self.__ssh_keyfile, str(self.__ssh_keypass)) (_, stdout, _) = ssh_client.exec_command("test -f {}".format(filename)) if stdout.channel.recv_exit_status() == 0: print( "Install file already present on remote host, skipping upload..." ) else: print("Uploading file to remote host...") sftp = ssh_client.open_sftp() sftp_upload(sftp, filename, filename) sftp.close() ssh_command(ssh_client, self.__url, "sudo yum install -y {}".format(filename)) print("Install finished!") def _generate_download_url(self, version: str, build: str, filename: str): # All access via VPN or company network if build is not None: return "http://latestbuilds.service.couchbase.com/builds/latestbuilds/sync_gateway{}/{}/{}".format( version, build, filename) return "http://latestbuilds.service.couchbase.com/builds/releases/mobile/couchbase-sync-gateway/{}/{}".format( version, filename)