<commit_before>/*******************************************************************************
 * Copyright (c) 2012 GigaSpaces Technologies Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package org.openspaces.admin.application.deploy;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.openspaces.admin.application.Application;
import org.openspaces.admin.application.ApplicationFileDeployment;
import org.openspaces.admin.application.config.ApplicationConfig;
import org.openspaces.admin.gsm.GridServiceManager;
import org.openspaces.admin.pu.ProcessingUnit;
import org.openspaces.admin.pu.config.UserDetailsConfig;
import org.openspaces.admin.pu.topology.ProcessingUnitConfigHolder;


/**
 * @author itaif
 * @since 9.0.1
 * A utility class with a main() entry point that allows deploying applications from ant or the cli
 */
public class ApplicationDeployCommand extends AbstractApplicationCommand {

        public static void main(String[] args) throws Exception {
            ApplicationDeployCommand deployer = new ApplicationDeployCommand();
            if (args.length < 1) {
                System.out.println(deployer.getUsage());
                return;
            }
            
            deployer.deployAndWait(args);
        }

        
        public void deployAndWait(String[] args) {
            
            try {
                parseArgs(args);
                
                final GridServiceManager gsm = waitForGridServiceManager();
                
                File applicationFolder = new File(args[args.length-1]);
                
                long end = System.currentTimeMillis() + getDeployTimeout();
                
                ApplicationConfig applicationConfig = new ApplicationFileDeployment(applicationFolder).create();
                
                // apply security properties to each pu in the application
                for (ProcessingUnitConfigHolder puConfig : applicationConfig.getProcessingUnits()) {
                    if (getSecured() != null) {
                        puConfig.setSecured(getSecured());
                    }
                    if (getUserDetails() != null) {
                        UserDetailsConfig userDetailsConfig = new UserDetailsConfig();
                        userDetailsConfig.setUsername(getUserDetails().getUsername());
                        userDetailsConfig.setPassword(getUserDetails().getPassword());
                        puConfig.setUserDetails(userDetailsConfig);
                    }
                }
                
                String name = applicationConfig.getName();
                info("Deploying application " + name);
                
                Application dataApp = gsm.deploy(applicationConfig);
                
                for (ProcessingUnit pu : dataApp.getProcessingUnits()) {
                    long remaining = end - System.currentTimeMillis();
                    if (remaining < 0 ||
                        !pu.waitFor(pu.getTotalNumberOfInstances(), remaining, TimeUnit.MILLISECONDS)) {
                        throw new TimeoutException("Application " + name + " deployment timed out");
                    }
                }
                
                info(name + " deployment completed successfully.");
            }
            catch (TimeoutException e) {
                info(e.getMessage());
            }
        }

       public String getUsage() {
            StringBuilder sb = new StringBuilder();
            if (!getManaged()) {
                sb.append("Usage: ApplicationDeployCommand [-groups groups] [-locators hots1 hots2] [-timeout timeoutValue] [-user xxx -password yyy] [-secured true/false] Application_DirOrZip");<commit_msg>Added exit(1) when called from cammdnline and ended with a timeout error<commit_after>/*******************************************************************************
 * Copyright (c) 2012 GigaSpaces Technologies Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package org.openspaces.admin.application.deploy;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.openspaces.admin.application.Application;
import org.openspaces.admin.application.ApplicationFileDeployment;
import org.openspaces.admin.application.config.ApplicationConfig;
import org.openspaces.admin.gsm.GridServiceManager;
import org.openspaces.admin.pu.ProcessingUnit;
import org.openspaces.admin.pu.config.UserDetailsConfig;
import org.openspaces.admin.pu.topology.ProcessingUnitConfigHolder;


/**
 * @author itaif
 * @since 9.0.1
 * A utility class with a main() entry point that allows deploying applications from ant or the cli
 */
public class ApplicationDeployCommand extends AbstractApplicationCommand {

        public static void main(String[] args) throws Exception {
            ApplicationDeployCommand deployer = new ApplicationDeployCommand();
            if (args.length < 1) {
                System.out.println(deployer.getUsage());
                return;
            }
            
            try {
                deployer.deployAndWait(args);
            }
            catch(TimeoutException e) {
                System.exit(1);
            }
        }

        
        public void deployAndWait(String[] args) throws TimeoutException {
            
            try {
                parseArgs(args);
                
                final GridServiceManager gsm = waitForGridServiceManager();
                
                File applicationFolder = new File(args[args.length-1]);
                
                long end = System.currentTimeMillis() + getDeployTimeout();
                
                ApplicationConfig applicationConfig = new ApplicationFileDeployment(applicationFolder).create();
                
                // apply security properties to each pu in the application
                for (ProcessingUnitConfigHolder puConfig : applicationConfig.getProcessingUnits()) {
                    if (getSecured() != null) {
                        puConfig.setSecured(getSecured());
                    }
                    if (getUserDetails() != null) {
                        UserDetailsConfig userDetailsConfig = new UserDetailsConfig();
                        userDetailsConfig.setUsername(getUserDetails().getUsername());
                        userDetailsConfig.setPassword(getUserDetails().getPassword());
                        puConfig.setUserDetails(userDetailsConfig);
                    }
                }
                
                String name = applicationConfig.getName();
                info("Deploying application " + name);
                
                Application dataApp = gsm.deploy(applicationConfig);
                
                for (ProcessingUnit pu : dataApp.getProcessingUnits()) {
                    long remaining = end - System.currentTimeMillis();
                    if (remaining < 0 ||
                        !pu.waitFor(pu.getTotalNumberOfInstances(), remaining, TimeUnit.MILLISECONDS)) {
                        throw new TimeoutException("Application " + name + " deployment timed out");
                    }
                }
                
                info(name + " deployment completed successfully.");
            }
            catch (TimeoutException e) {
                info(e.getMessage());
                throw e;
            }
        }

       public String getUsage() {
            StringBuilder sb = new StringBuilder();
            if (!getManaged()) {
                sb.append("Usage: ApplicationDeployCommand [-groups groups] [-locators hots1 hots2] [-timeout timeoutValue] [-user xxx -password yyy] [-secured true/false] Application_DirOrZip");
{'fraction_non_alphanumeric': 0.11368164711091433, 'fraction_numerical': 0.003763559884879345, 'mean_word_length': 2.093118794933242, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544735', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1943, 'n_words': 548}