/** * Lancer par signal de NoPublicController quand le processus sshd est démarré dans les containers * serveur et git */ public Application updateEnv(Application application, User user) throws ServiceException { logger.info("--update Env of Server--"); String command = null; Map<String, String> configShellModule = new HashMap<>(); Map<String, String> configShellServer = new HashMap<>(); Module moduleGit = moduleService.findGitModule(user.getLogin(), application); Server server = application.getServers().get(0); String rootPassword = application.getUser().getPassword(); configShellModule.put("port", moduleGit.getSshPort()); configShellModule.put("dockerManagerAddress", moduleGit.getApplication().getManagerIp()); configShellModule.put("password", rootPassword); configShellModule.put("dockerManagerAddress", application.getManagerIp()); logger.info("new server ip : " + server.getContainerIP()); try { int counter = 0; while (!server.getStatus().equals(Status.START) || !moduleGit.getStatus().equals(Status.START)) { if (counter == 100) { break; } Thread.sleep(1000); logger.info(" wait git and server sshd processus start"); logger.info( "SSHDSTATUS = server : " + server.getStatus() + " - module : " + moduleGit.getStatus()); moduleGit = moduleService.findById(moduleGit.getId()); server = serverService.findById(server.getId()); counter++; } command = ". /cloudunit/scripts/update-env.sh " + server.getContainerIP(); logger.info("command shell to execute [" + command + "]"); shellUtils.executeShell(command, configShellModule); configShellServer.put("port", server.getSshPort()); configShellServer.put("dockerManagerAddress", server.getApplication().getManagerIp()); configShellServer.put("password", rootPassword); command = ". /cloudunit/scripts/rm-auth-keys.sh "; logger.info("command shell to execute [" + command + "]"); shellUtils.executeShell(command, configShellServer); String cleanCommand = server.getServerAction().cleanCommand(); if (cleanCommand != null) { shellUtils.executeShell(server.getServerAction().cleanCommand(), configShellServer); } } catch (Exception e) { moduleGit.setStatus(Status.FAIL); moduleGit = moduleService.saveInDB(moduleGit); server.setStatus(Status.FAIL); server = serverService.saveInDB(server); logger.error("Error : Error during update Env var of GIT " + e); throw new ServiceException(e.getLocalizedMessage(), e); } return application; }
@Override @Transactional public Application deploy(File file, Application application) throws ServiceException, CheckException { int code = -1; Map<String, String> configShell = new HashMap<>(); try { // get app with all its components for (Server server : application.getServers()) { // loading server ssh informations String rootPassword = server.getApplication().getUser().getPassword(); configShell.put("port", server.getSshPort()); configShell.put("dockerManagerAddress", application.getManagerIp()); configShell.put("password", rootPassword); String destFile = "/cloudunit/tmp/"; // send the file on container shellUtils.sendFile( file, rootPassword, server.getSshPort(), application.getManagerIp(), destFile); // call deployment script code = shellUtils.executeShell( "bash /cloudunit/scripts/deploy.sh " + file.getName() + " " + application.getUser().getLogin(), configShell); } // if all is ok, create a new deployment tag and set app to starting if (code == 0) { deploymentService.create(application, Type.WAR); } else { throw new CheckException("No way to deploy application " + file + ", " + application); } } catch (Exception e) { throw new ServiceException(e.getLocalizedMessage(), e); } return application; }
/** @return true if the bbcomm process is running */ private static boolean isBloombergProcessRunning() { if (ShellUtils.isProcessRunning(BBCOMM_PROCESS)) { logger.info("{} is started", BBCOMM_PROCESS); return true; } return false; }
private void checkFreeInternet() { if (ShellUtils.isRooted()) { CheckFreeInternetService.execute(this); } else { if (LaunchService.isVpnRunning()) { onFreeInternetChanged(true); } } }
public int killVideoProcessor(boolean asRoot, boolean waitFor) { int killDelayMs = 300; String ffmpegBin = new File(context.getDir("bin", 0), "ffmpeg").getAbsolutePath(); int result = -1; int procId = -1; while ((procId = ShellUtils.findProcessId(ffmpegBin)) != -1) { Log.d(TAG, "Found PID=" + procId + " - killing now..."); String[] cmd = {ShellUtils.SHELL_CMD_KILL + ' ' + procId + ""}; try { result = ShellUtils.doShellCommand( cmd, new ShellCallback() { @Override public void shellOut(String msg) { Log.d(TAG, "Killing ffmpeg:" + msg); } @Override public void processComplete(int exitValue) { // TODO Auto-generated method stub } }, asRoot, waitFor); Thread.sleep(killDelayMs); } catch (Exception e) { } } return result; }
/** * get system install location<br> * can be set by System Menu Setting->Storage->Prefered install location * * @return * @see {@link IPackageManager#getInstallLocation()} */ public static int getInstallLocation() { CommandResult commandResult = ShellUtils.execCommand( "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm get-install-location", false, true); if (commandResult.result == 0 && commandResult.successMsg != null && commandResult.successMsg.length() > 0) { try { int location = Integer.parseInt(commandResult.successMsg.substring(0, 1)); switch (location) { case APP_INSTALL_INTERNAL: return APP_INSTALL_INTERNAL; case APP_INSTALL_EXTERNAL: return APP_INSTALL_EXTERNAL; } } catch (NumberFormatException e) { e.printStackTrace(); Log.e(TAG, "pm get-install-location error"); } } return APP_INSTALL_AUTO; }
/** * uninstall package silent by root * * <ul> * <strong>Attentions:</strong> * <li>Don't call this on the ui thread, it may costs some times. * <li>You should add <strong>android.permission.DELETE_PACKAGES</strong> in manifest, so no * need to request root permission, if you are system app. * </ul> * * @param context file path of package * @param packageName package name of app * @param isKeepData whether keep the data and cache directories around after package removal * @return * <ul> * <li>{@link #DELETE_SUCCEEDED} means uninstall success * <li>{@link #DELETE_FAILED_INTERNAL_ERROR} means internal error * <li>{@link #DELETE_FAILED_INVALID_PACKAGE} means package name error * <li>{@link #DELETE_FAILED_PERMISSION_DENIED} means permission denied */ public static int uninstallSilent(Context context, String packageName, boolean isKeepData) { if (packageName == null || packageName.length() == 0) { return DELETE_FAILED_INVALID_PACKAGE; } /** * if context is system app, don't need root permission, but should add <uses-permission * android:name="android.permission.DELETE_PACKAGES" /> in mainfest */ StringBuilder command = new StringBuilder() .append("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm uninstall") .append(isKeepData ? " -k " : " ") .append(packageName.replace(" ", "\\ ")); CommandResult commandResult = ShellUtils.execCommand(command.toString(), !isSystemApplication(context), true); if (commandResult.successMsg != null && (commandResult.successMsg.contains("Success") || commandResult.successMsg.contains("success"))) { return DELETE_SUCCEEDED; } Log.e( TAG, new StringBuilder() .append("uninstallSilent successMsg:") .append(commandResult.successMsg) .append(", ErrorMsg:") .append(commandResult.errorMsg) .toString()); if (commandResult.errorMsg == null) { return DELETE_FAILED_INTERNAL_ERROR; } if (commandResult.errorMsg.contains("Permission denied")) { return DELETE_FAILED_PERMISSION_DENIED; } return DELETE_FAILED_INTERNAL_ERROR; }
@Transactional public void deployToContainerId( String applicationName, String containerId, File file, String destFile) throws ServiceException { try { Application application = this.findByNameAndUser(authentificationUtils.getAuthentificatedUser(), applicationName); Map<String, String> configShell = new HashMap<>(); String sshPort = application.getSShPortByContainerId(containerId); String rootPassword = application.getUser().getPassword(); configShell.put("port", sshPort); configShell.put("dockerManagerAddress", application.getManagerIp()); configShell.put("password", rootPassword); // send the file on container shellUtils.sendFile(file, rootPassword, sshPort, application.getManagerIp(), destFile); } catch (Exception e) { e.printStackTrace(); } }
/** * install according conditions * * <ul> * <li>if system application or rooted, see {@link #installSilent(Context, String)} * <li>else see {@link #installNormal(Context, String)} * </ul> * * @param context * @param filePath * @return */ public static final int install(Context context, String filePath) { if (PackageUtils.isSystemApplication(context) || ShellUtils.checkRootPermission()) { return installSilent(context, filePath); } return installNormal(context, filePath) ? INSTALL_SUCCEEDED : INSTALL_FAILED_INVALID_URI; }
/** * uninstall according conditions * * <ul> * <li>if system application or rooted, see {@link #uninstallSilent(Context, String)} * <li>else see {@link #uninstallNormal(Context, String)} * </ul> * * @param context * @param packageName package name of app * @return */ public static final int uninstall(Context context, String packageName) { if (PackageUtils.isSystemApplication(context) || ShellUtils.checkRootPermission()) { return uninstallSilent(context, packageName); } return uninstallNormal(context, packageName) ? DELETE_SUCCEEDED : DELETE_FAILED_INVALID_PACKAGE; }
/** * install package silent by root * * <ul> * <strong>Attentions:</strong> * <li>Don't call this on the ui thread, it may costs some times. * <li>You should add <strong>android.permission.INSTALL_PACKAGES</strong> in manifest, so no * need to request root permission, if you are system app. * </ul> * * @param context * @param filePath file path of package * @param pmParams pm install params * @return {@link PackageUtils#INSTALL_SUCCEEDED} means install success, other means failed. * details see {@link PackageUtils}.INSTALL_FAILED_*. same to {@link PackageManager}.INSTALL_* */ public static int installSilent(Context context, String filePath, String pmParams) { if (filePath == null || filePath.length() == 0) { return INSTALL_FAILED_INVALID_URI; } File file = new File(filePath); if (file == null || file.length() <= 0 || !file.exists() || !file.isFile()) { return INSTALL_FAILED_INVALID_URI; } /** * if context is system app, don't need root permission, but should add <uses-permission * android:name="android.permission.INSTALL_PACKAGES" /> in mainfest */ StringBuilder command = new StringBuilder() .append("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install ") .append(pmParams == null ? "" : pmParams) .append(" ") .append(filePath.replace(" ", "\\ ")); CommandResult commandResult = ShellUtils.execCommand(command.toString(), !isSystemApplication(context), true); if (commandResult.successMsg != null && (commandResult.successMsg.contains("Success") || commandResult.successMsg.contains("success"))) { return INSTALL_SUCCEEDED; } Log.e( TAG, new StringBuilder() .append("installSilent successMsg:") .append(commandResult.successMsg) .append(", ErrorMsg:") .append(commandResult.errorMsg) .toString()); if (commandResult.errorMsg == null) { return INSTALL_FAILED_OTHER; } if (commandResult.errorMsg.contains("INSTALL_FAILED_ALREADY_EXISTS")) { return INSTALL_FAILED_ALREADY_EXISTS; } if (commandResult.errorMsg.contains("INSTALL_FAILED_INVALID_APK")) { return INSTALL_FAILED_INVALID_APK; } if (commandResult.errorMsg.contains("INSTALL_FAILED_INVALID_URI")) { return INSTALL_FAILED_INVALID_URI; } if (commandResult.errorMsg.contains("INSTALL_FAILED_INSUFFICIENT_STORAGE")) { return INSTALL_FAILED_INSUFFICIENT_STORAGE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_DUPLICATE_PACKAGE")) { return INSTALL_FAILED_DUPLICATE_PACKAGE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_NO_SHARED_USER")) { return INSTALL_FAILED_NO_SHARED_USER; } if (commandResult.errorMsg.contains("INSTALL_FAILED_UPDATE_INCOMPATIBLE")) { return INSTALL_FAILED_UPDATE_INCOMPATIBLE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_SHARED_USER_INCOMPATIBLE")) { return INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_MISSING_SHARED_LIBRARY")) { return INSTALL_FAILED_MISSING_SHARED_LIBRARY; } if (commandResult.errorMsg.contains("INSTALL_FAILED_REPLACE_COULDNT_DELETE")) { return INSTALL_FAILED_REPLACE_COULDNT_DELETE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_DEXOPT")) { return INSTALL_FAILED_DEXOPT; } if (commandResult.errorMsg.contains("INSTALL_FAILED_OLDER_SDK")) { return INSTALL_FAILED_OLDER_SDK; } if (commandResult.errorMsg.contains("INSTALL_FAILED_CONFLICTING_PROVIDER")) { return INSTALL_FAILED_CONFLICTING_PROVIDER; } if (commandResult.errorMsg.contains("INSTALL_FAILED_NEWER_SDK")) { return INSTALL_FAILED_NEWER_SDK; } if (commandResult.errorMsg.contains("INSTALL_FAILED_TEST_ONLY")) { return INSTALL_FAILED_TEST_ONLY; } if (commandResult.errorMsg.contains("INSTALL_FAILED_CPU_ABI_INCOMPATIBLE")) { return INSTALL_FAILED_CPU_ABI_INCOMPATIBLE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_MISSING_FEATURE")) { return INSTALL_FAILED_MISSING_FEATURE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_CONTAINER_ERROR")) { return INSTALL_FAILED_CONTAINER_ERROR; } if (commandResult.errorMsg.contains("INSTALL_FAILED_INVALID_INSTALL_LOCATION")) { return INSTALL_FAILED_INVALID_INSTALL_LOCATION; } if (commandResult.errorMsg.contains("INSTALL_FAILED_MEDIA_UNAVAILABLE")) { return INSTALL_FAILED_MEDIA_UNAVAILABLE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_VERIFICATION_TIMEOUT")) { return INSTALL_FAILED_VERIFICATION_TIMEOUT; } if (commandResult.errorMsg.contains("INSTALL_FAILED_VERIFICATION_FAILURE")) { return INSTALL_FAILED_VERIFICATION_FAILURE; } if (commandResult.errorMsg.contains("INSTALL_FAILED_PACKAGE_CHANGED")) { return INSTALL_FAILED_PACKAGE_CHANGED; } if (commandResult.errorMsg.contains("INSTALL_FAILED_UID_CHANGED")) { return INSTALL_FAILED_UID_CHANGED; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_NOT_APK")) { return INSTALL_PARSE_FAILED_NOT_APK; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_BAD_MANIFEST")) { return INSTALL_PARSE_FAILED_BAD_MANIFEST; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION")) { return INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_NO_CERTIFICATES")) { return INSTALL_PARSE_FAILED_NO_CERTIFICATES; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES")) { return INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING")) { return INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME")) { return INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID")) { return INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_MANIFEST_MALFORMED")) { return INSTALL_PARSE_FAILED_MANIFEST_MALFORMED; } if (commandResult.errorMsg.contains("INSTALL_PARSE_FAILED_MANIFEST_EMPTY")) { return INSTALL_PARSE_FAILED_MANIFEST_EMPTY; } if (commandResult.errorMsg.contains("INSTALL_FAILED_INTERNAL_ERROR")) { return INSTALL_FAILED_INTERNAL_ERROR; } return INSTALL_FAILED_OTHER; }
@Override public ZNode Invoke(ZNode ParentNode, ZTokenContext TokenContext, ZNode LeftNode) { @Var ZToken CommandToken = TokenContext.GetToken(ZTokenContext._MoveNext); @Var ZNode SymbolNode = ParentNode.GetNameSpace().GetSymbol(ShellUtils._ToCommandSymbol(CommandToken.GetText())); if (SymbolNode == null || !(SymbolNode instanceof ZStringNode)) { return new ZErrorNode(ParentNode, CommandToken, "undefined command symbol"); } @Var String Command = ((ZStringNode) SymbolNode).StringValue; @Var CommandNode CommandNode = new CommandNode(ParentNode, CommandToken, Command); while (TokenContext.HasNext()) { if (TokenContext.MatchToken("|")) { // Match Prefix Option @Var ZNode PrefixOptionNode = TokenContext.ParsePatternAfter( ParentNode, CommandNode, PrefixOptionPatternFunction._PatternName, ZTokenContext._Optional); if (PrefixOptionNode != null) { return CommandNode.AppendPipedNextNode((CommandNode) PrefixOptionNode); } // Match Command Symbol @Var ZNode PipedNode = TokenContext.ParsePattern( ParentNode, CommandSymbolPatternFunction._PatternName, ZTokenContext._Required); if (PipedNode.IsErrorNode()) { return PipedNode; } return CommandNode.AppendPipedNextNode((CommandNode) PipedNode); } // Match Redirect @Var ZNode RedirectNode = TokenContext.ParsePattern( ParentNode, RedirectPatternFunction._PatternName, ZTokenContext._Optional); if (RedirectNode != null) { CommandNode.AppendPipedNextNode((CommandNode) RedirectNode); continue; } // Match Suffix Option @Var ZNode SuffixOptionNode = TokenContext.ParsePattern( ParentNode, SuffixOptionPatternFunction._PatternName, ZTokenContext._Optional); if (SuffixOptionNode != null) { if (SuffixOptionNode.IsErrorNode()) { return SuffixOptionNode; } return CommandNode.AppendPipedNextNode((CommandNode) SuffixOptionNode); } // Match Argument @Var ZNode ArgNode = TokenContext.ParsePattern( ParentNode, SimpleArgumentPatternFunction._PatternName, ZTokenContext._Optional); if (ArgNode == null) { break; } CommandNode.AppendArgNode(ArgNode); } return CommandNode; }
private void checkPickAndPlay() { if (ShellUtils.isRooted()) { CheckPickAndPlayService.execute(this); } }
private void checkWifiRepeater() { if (ShellUtils.isRooted() && Build.VERSION.SDK_INT >= 14) { CheckWifiRepeaterService.execute(this); } }
/** * Lancer par signal de NoPublicController quand le processus sshd est (re)démarré dans container * serveur et git, pour mettre à jour la nouvelle IP du serveur */ @Override public Application sshCopyIDToServer(Application application, User user) throws ServiceException { String command = null; Map<String, String> configShell = new HashMap<>(); Module moduleGit = moduleService.findGitModule(user.getLogin(), application); if (logger.isDebugEnabled()) { logger.debug("--ssh Copy ID To Server--"); logger.debug("ssh port : " + moduleGit.getSshPort()); logger.debug("manager ip : " + application.getManagerIp()); } for (Server server : application.getServers()) { configShell.put("password", server.getApplication().getUser().getPassword()); configShell.put("port", moduleGit.getSshPort()); configShell.put("dockerManagerAddress", application.getManagerIp()); configShell.put("userLogin", server.getApplication().getUser().getLogin()); try { int counter = 0; while (!server.getStatus().equals(Status.START) || !moduleGit.getStatus().equals(Status.START)) { if (counter == 100) { break; } Thread.sleep(1000); logger.info(" wait git and server ssh processus start"); logger.info( "STATUS = server : " + server.getStatus() + " - module : " + moduleGit.getStatus()); moduleGit = moduleService.findById(moduleGit.getId()); server = serverService.findById(server.getId()); counter++; } // To permit ssh access on server from git container command = "expect /cloudunit/scripts/ssh-copy-id-expect.sh " + moduleGit.getApplication().getUser().getPassword(); logger.info("command shell to execute [" + command + "]"); shellUtils.executeShell(command, configShell); } catch (Exception e) { moduleGit.setStatus(Status.FAIL); moduleGit = moduleService.saveInDB(moduleGit); server.setStatus(Status.FAIL); server = serverService.saveInDB(server); logger.error("Error : Error during permit git to access to server " + e); throw new ServiceException(e.getLocalizedMessage(), e); } } try { moduleGit = moduleService.update(moduleGit); application.getModules().add(moduleGit); application.setGitContainerIP(moduleGit.getContainerIP()); } catch (ServiceException e) { moduleGit.setStatus(Status.FAIL); moduleService.saveInDB(moduleGit); logger.error("Error : Error during persist git module " + e); throw new ServiceException(e.getLocalizedMessage(), e); } logger.info( "ApplicationService : Application " + application.getName() + " successfully created."); return application; }