您的位置:首页 > 其它

cloudify启动源码分析

2013-05-24 15:43 218 查看

注:cloudifySrcRoot为源代码根目录,cloudifyRoot为cloudify安装目录

BootstrapCloud.java(启动云)

$cloudifySrcRoot\esc-commands\src\main\java\org\cloudifysource\esc\shell\commands\BootstrapCloud.java

/*******************************************************************************
* Copyright (c) 2011 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.cloudifysource.esc.shell.commands;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.felix.gogo.commands.Option;
import org.cloudifysource.dsl.cloud.Cloud;
import org.cloudifysource.dsl.internal.CloudifyConstants;
import org.cloudifysource.dsl.internal.CloudifyErrorMessages;
import org.cloudifysource.dsl.internal.DSLUtils;
import org.cloudifysource.dsl.internal.ServiceReader;
import org.cloudifysource.dsl.internal.packaging.FileAppender;
import org.cloudifysource.dsl.utils.RecipePathResolver;
import org.cloudifysource.esc.driver.provisioning.jclouds.DefaultProvisioningDriver;
import org.cloudifysource.esc.installer.AgentlessInstaller;
import org.cloudifysource.esc.shell.installer.CloudGridAgentBootstrapper;
import org.cloudifysource.shell.AdminFacade;
import org.cloudifysource.shell.Constants;
import org.cloudifysource.shell.KeystoreFileVerifier;
import org.cloudifysource.shell.commands.AbstractGSCommand;
import org.cloudifysource.shell.commands.CLIStatusException;

import com.j_spaces.kernel.Environment;

/************
* CLI Command to bootstrap a cloud.
*
* @author barakme
* @since 2.0.0
*
*/
@Command(
scope = "cloudify",
name = "bootstrap-cloud",
description = "Starts Cloudify Agent without any zone, and the Cloudify management processes on the provided "
+ "cloud.")
public class BootstrapCloud extends AbstractGSCommand {//AbstractGSCommand在CLI模块下

private static final int PROGRESS_INTERVAL_SECONDS = 10;
private static final int DEFAULT_TIMEOUT_MINUTES = 60;//默认超时时间(分)
private static final String PATH_SEPARATOR = System.getProperty("file.separator");//系统默认分隔符

@Argument(required = true, name = "provider", description = "The cloud provider to use")//云提供商,如openstack
private String cloudProvider;

@Option(required = false, description = "Server security mode (on/off)", name = "-secured")//是否开启安全模式
private boolean secured;

@Option(required = false, description = "Path to a custom spring security configuration file",//spring安全配置文件路径
name = "-security-file")
private String securityFilePath;

@Option(required = false, description = "The username when connecting to a secure admin server", name = "-user")//安全管理服务器用户名
private String username;

@Option(required = false, description = "The password when connecting to a secure admin server", name = "-password")//安全管理服务器密码
private String password;

@Option(required = false, description = "The path to the keystore used for SSL connections", name = "-keystore")//SSL链接,keystone所在路径
private String keystore;

@Option(required = false, description = "The password to the keystore", name = "-keystore-password")//keystone密码
private String keystorePassword;

@Option(required = false, description = "Path to a file containing override properties", name = "-cloud-overrides")//重写云配置文件路径
private File cloudOverrides;

@Option(required = false, name = "-timeout",//超时时间
description = "The number of minutes to wait until the operation is done.")
private int timeoutInMinutes = DEFAULT_TIMEOUT_MINUTES;

@Option(required = false, name = "-no-web-services",//不部署rest和web-ui服务
description = "if set, no attempt to deploy the rest admin and web-ui will be made")
private boolean noWebServices;

@Option(required = false, name = "-use-existing",//查找已存在的管理服务
description = "if set, will attempt to find existing management servers. "
+ "Management should already have been shut-down with stop-management.")
private boolean useExistingManagers = false;

@Option(required = false, name = "-use-existing-from-file",//从文件中解析管理服务
description = "if set, will attempt to find existing management servers based on server "
+ "details supplied in file. "
+ "Management should already have been shut-down with stop-management.")
private File existingManagersFile = null;

@Option(required = false, name = "-skip-validation",//跳过验证
description = "if set, will attempt to find existing management servers. "
+ "Management should already have been shut-down with stop-management.")
private boolean skipValidation = false;

private String securityProfile = CloudifyConstants.SPRING_PROFILE_NON_SECURE;
// flags to indicate if bootstrap operation created a backup file that
// should be reverted

private static final String CLOUDIFY_HOME = Environment.getHomeDirectory();//cloudify根目录
private static final String DEFAULT_SECURITY_FILE_PATH = CLOUDIFY_HOME + "/config/security/spring-security.xml";//spring权限管理配置
private static final String[] NON_VERBOSE_LOGGERS = { DefaultProvisioningDriver.class.getName(),
AgentlessInstaller.class.getName() };

private final Map<String, Level> loggerStates = new HashMap<String, Level>();

private static final long TEN_K = 10 * FileUtils.ONE_KB;

private File defaultSecurityTargetFile;
private File defaultKeystoreTargetFile;

@Override
protected Object doExecute() throws Exception {

if (this.existingManagersFile != null) {//是否存在管理服务文件
if (!this.existingManagersFile.exists() || !this.existingManagersFile.isFile()) {
throw new CLIStatusException(CloudifyErrorMessages.FILE_NOT_EXISTS.getName(),
this.existingManagersFile.getAbsolutePath());
}
}

if (cloudOverrides != null) {//是否存在重写的配置文件
if (cloudOverrides.length() >= TEN_K) {
throw new CLIStatusException(CloudifyErrorMessages.CLOUD_OVERRIDES_TO_LONG.getName());
}
}

final RecipePathResolver pathResolver = new RecipePathResolver();

File providerDirectory = null;
if (pathResolver.resolveCloud(new File(getCloudProvider()))) {
providerDirectory = pathResolver.getResolved();//获取recipes文件路径
} else {
throw new CLIStatusException("cloud_driver_file_doesnt_exist",
StringUtils.join(pathResolver.getPathsLooked().toArray(), ", "));
}

final File tempFolder = createTempFolder();//创建临时文件
FileUtils.copyDirectoryToDirectory(providerDirectory, tempFolder);//将recipes复制到临时文件目录
providerDirectory = new File(tempFolder, providerDirectory.getName());

defaultSecurityTargetFile = new File(providerDirectory + PATH_SEPARATOR//创建安全配置文件
+ CloudifyConstants.SECURITY_FILE_NAME);

defaultKeystoreTargetFile = new File(providerDirectory + PATH_SEPARATOR//创建keystone文件
+ CloudifyConstants.KEYSTORE_FILE_NAME);

setSecurityMode();
copySecurityFiles(providerDirectory.getAbsolutePath());

// load the cloud file,云配置文件,-cloud.groovy
final File cloudFile = findCloudFile(providerDirectory);

// load properties file,云参数文件,-cloud.properties
final File cloudPropertiesFile = new File(providerDirectory, cloudFile.getName().split("\\.")[0]
+ DSLUtils.PROPERTIES_FILE_SUFFIX);

// check for overrides file,检查是否有重写的云配置文件
Cloud cloud = null;
if (cloudOverrides == null) {
cloud = ServiceReader.readCloud(cloudFile);
} else {

// read cloud with overrides properties so they reflect during bootstrap.
cloud = ServiceReader.
readCloudFromDirectory(providerDirectory.getAbsolutePath(),
FileUtils.readFileToString(cloudOverrides));

// append the overrides file to the existing properties file
final FileAppender appender = new FileAppender(cloudPropertiesFile);
appender.append("Overrides File Properties", cloudOverrides);
appender.flush();
}

// start the installer,启动云安装
final CloudGridAgentBootstrapper installer = new CloudGridAgentBootstrapper();
installer.setProviderDirectory(providerDirectory);
if (this.adminFacade != null) {
installer.setAdminFacade(this.adminFacade);
} else {
installer.setAdminFacade((AdminFacade) session.get(Constants.ADMIN_FACADE));
}
installer.setProgressInSeconds(PROGRESS_INTERVAL_SECONDS);
installer.setVerbose(verbose);
installer.setCloud(cloud);
installer.setCloudFile(cloudFile);
installer.setNoWebServices(noWebServices);
installer.setUseExisting(this.useExistingManagers);
installer.setExistingManagersFile(this.existingManagersFile);
// Bootstrap!

// Note: The cloud driver may be very verbose. This is EXTEREMELY useful
// when debugging ESM
// issues, but can also clutter up the CLI display. It makes more sense to temporarily raise the log level here,
// so that all of these
// messages will not be displayed on the console.
limitLoggingLevel();
logger.info(getFormattedMessage("bootstrapping_cloud", getCloudProvider()));
try {
// TODO: Create the event listeners here and pass them to the installer.
installer.bootstrapCloudAndWait(securityProfile, username, password,
keystorePassword, !skipValidation, getTimeoutInMinutes(), TimeUnit.MINUTES);//安装
return getFormattedMessage("cloud_started_successfully", getCloudProvider());
} finally {
// if an overrides file was passed, then the properties file is dirty. delete it.
if (cloudOverrides != null) {
cloudPropertiesFile.delete();
}
FileUtils.deleteDirectory(tempFolder);
installer.close();
restoreLoggingLevel();
}

}

private File createTempFolder() throws IOException {
final File tempFile = File.createTempFile("cloud-", "");//在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称
tempFile.delete();//删除临时文件
tempFile.mkdir();//创建临时文件目录
return tempFile;
}

private void limitLoggingLevel() {

if (!this.verbose) {
loggerStates.clear();
for (final String loggerName : NON_VERBOSE_LOGGERS) {
final Logger provisioningLogger = Logger.getLogger(loggerName);
final Level logLevelBefore = provisioningLogger.getLevel();
provisioningLogger.setLevel(Level.WARNING);
loggerStates.put(loggerName, logLevelBefore);
}
}
}

private void restoreLoggingLevel() {
if (!verbose) {
final Set<Entry<String, Level>> entries = loggerStates.entrySet();
for (final Entry<String, Level> entry : entries) {
final Logger provisioningLogger = Logger.getLogger(entry.getKey());
provisioningLogger.setLevel(entry.getValue());
}
}

}

private File findCloudFile(final File providerDirectory) throws FileNotFoundException {
if (!providerDirectory.exists() || !providerDirectory.isDirectory()) {//recipes文件是否存在
throw new FileNotFoundException("Could not find cloud provider directory: " + providerDirectory);
}

final File[] cloudFiles = providerDirectory.listFiles(new FilenameFilter() {//

@Override
public boolean accept(final File dir, final String name) {
return name.endsWith("-cloud.groovy");//返回云配置文件
}

});

if (cloudFiles.length == 0) {
throw new FileNotFoundException("Could not find a cloud definition file in: " + providerDirectory
+ ". Definitions file must end with the suffix '-cloud.groovy'");
} else if (cloudFiles.length > 1) {
throw new IllegalArgumentException("Found multiple cloud definition files in: " + providerDirectory
+ ". Only one file may end with the suffix '-cloud.groovy'");
}

return cloudFiles[0];
}

private void setSecurityMode() throws CLIStatusException {//设置安全模式

if (secured) {//是否启用安全模式
// enable security
if (StringUtils.isNotBlank(keystore) && StringUtils.isNotBlank(keystorePassword)) {//keystone和keystonePassword是否存在
logger.info(getFormattedMessage(CloudifyErrorMessages.SETTING_SERVER_SECURITY_PROFILE.getName(),
CloudifyConstants.SPRING_PROFILE_SECURE));
securityProfile = CloudifyConstants.SPRING_PROFILE_SECURE;
} else {
logger.info(getFormattedMessage(CloudifyErrorMessages.SETTING_SERVER_SECURITY_PROFILE.getName(),
CloudifyConstants.SPRING_PROFILE_SECURE_NO_SSL));
securityProfile = CloudifyConstants.SPRING_PROFILE_SECURE_NO_SSL;
}
} else {
// disable security
logger.info(getFormattedMessage(CloudifyErrorMessages.SETTING_SERVER_SECURITY_PROFILE.getName(),
CloudifyConstants.SPRING_PROFILE_NON_SECURE));
securityProfile = CloudifyConstants.SPRING_PROFILE_NON_SECURE;
}

if (securityProfile.equalsIgnoreCase(CloudifyConstants.SPRING_PROFILE_NON_SECURE)) {//非安全模式
if (StringUtils.isNotBlank(username)) {
throw new IllegalArgumentException("'-user' is only valid when '-secured' is set");
}

if (StringUtils.isNotBlank(password)) {
throw new IllegalArgumentException("'-password' is only valid when '-secured' is set");
}

if (StringUtils.isNotBlank(securityFilePath)) {
throw new IllegalArgumentException("'-securityfile' is only valid when '-secured' is set");
}

if (StringUtils.isNotBlank(keystore)) {
throw new IllegalArgumentException("'-keystore' is only valid when '-secured' is set");
}

if (StringUtils.isNotBlank(keystorePassword)) {
throw new IllegalArgumentException("'-keystore-password' is only valid when '-secured' is set");
}
}

if (StringUtils.isNotBlank(username) && StringUtils.isBlank(password)) {//用户名不为空,密码为空
throw new IllegalArgumentException("Password is missing or empty");
}

if (StringUtils.isBlank(username) && StringUtils.isNotBlank(password)) {//用户名为空,密码不为空
throw new IllegalArgumentException("Username is missing or empty");
}

if (StringUtils.isNotBlank(keystore) && StringUtils.isBlank(keystorePassword)) {//keystore不为空,keystorePassword为空
throw new IllegalArgumentException("Keystore password is missing or empty");
}

if (StringUtils.isBlank(keystore) && StringUtils.isNotBlank(keystorePassword)) {//keystore为空,keystorePassword不为空
throw new IllegalArgumentException("Keystore is missing or empty");
}

if (StringUtils.isNotBlank(keystore)) {//keystore不为空
new KeystoreFileVerifier().verifyKeystoreFile(new File(keystore), keystorePassword);
}

if (StringUtils.isNotBlank(keystore)) {
new KeystoreFileVerifier().verifyKeystoreFile(new File(keystore), keystorePassword);
}
}

private void copySecurityFiles(final String providerDirectory) throws Exception {//复制权限配置文件

final File defaultSecuritySourceFile = new File(DEFAULT_SECURITY_FILE_PATH);

if (securityProfile.equalsIgnoreCase(CloudifyConstants.SPRING_PROFILE_NON_SECURE)) {
// copy the default security file (defines no security) to the upload folder
FileUtils.copyFile(defaultSecuritySourceFile, defaultSecurityTargetFile);//复制默认权限配置为临时权限文件
} else {
// handle the configuration file
if (StringUtils.isNotBlank(securityFilePath)) {//配置文件变量不为空
final File securitySourceFile = new File(securityFilePath);
if (!securitySourceFile.isFile()) {
throw new Exception("Security configuration file not found: " + securityFilePath);
}

// copy to the cloud provider's folder, to be copied to all management servers remote directory
if (!securitySourceFile.getCanonicalFile().equals(defaultSecurityTargetFile.getCanonicalFile())) {//默认权限配置文件是否改动
FileUtils.copyFile(securitySourceFile, defaultSecurityTargetFile);
}
} else {
// TODO : should we use the default security location and assume it was edited by the user?
// securityFilePath = CLOUDIFY_HOME + "/config/security/spring-security.xml";
throw new IllegalArgumentException("-security-file is missing or empty");
}

// handle the keystore file
if (StringUtils.isNotBlank(keystore)) {
final File keystoreSourceFile = new File(keystore);
if (!keystoreSourceFile.isFile()) {
throw new Exception("Keystore file not found: " + keystore);
}

// copy to the override folder, to be copied to all management servers as well
final File defaultKeystoreTargetFile = new File(providerDirectory + PATH_SEPARATOR
+ CloudifyConstants.KEYSTORE_FILE_NAME);
if (!keystoreSourceFile.getCanonicalFile().equals(defaultKeystoreTargetFile.getCanonicalFile())) {
FileUtils.copyFile(keystoreSourceFile, defaultKeystoreTargetFile);//复制keystore文件
}
}
}
}

public int getTimeoutInMinutes() {
return timeoutInMinutes;
}

public void setTimeoutInMinutes(final int timeoutInMinutes) {
this.timeoutInMinutes = timeoutInMinutes;
}

public String getCloudProvider() {
return cloudProvider;
}

public void setCloudProvider(final String cloudProvider) {
this.cloudProvider = cloudProvider;
}

public boolean isUseExistingManagers() {
return useExistingManagers;
}

public void setUseExistingManagers(boolean useExistingManagers) {
this.useExistingManagers = useExistingManagers;
}

public File getExistingManagersFile() {
return existingManagersFile;
}

public void setExistingManagersFile(File existingManagersFile) {
this.existingManagersFile = existingManagersFile;
}
}


BootstrapLocalCloud.java(启动本地云)

$cloudifySrcRoot\CLI\src\main\java\org\cloudifysource\shell\commands\BootstrapLocalCloud.java

CloudGridAgentBootstrapper.java(启动类)

$cloudifySrcRoot\esc-commands\src\main\java\org\cloudifysource\esc\shell\installer\CloudGridAgentBootstrapper.java

bootstrap-management.sh(启动管理脚本)

$cloudifyRoot\clouds\openstack\upload\bootstrap-management.sh

#! /bin/bash

#############################################################################
# This script starts a Gigaspaces agent for use with the Gigaspaces
# Cloudify. The agent will function as management depending on the value of $GSA_MODE
#
# Parameters the should be exported beforehand:
# 	$LUS_IP_ADDRESS - Ip of the head node that runs a LUS and ESM. May be my IP. (Required)//运行LUS(lookup server查询服务)和ESM(弹性服务管理)的节点
#   $GSA_MODE - 'agent' if this node should join an already running node. Otherwise, any value.//值为agent则这个节点加入gigaspace services agent
#	$NO_WEB_SERVICES - 'true' if web-services (rest, webui) should not be deployed (only if GSA_MODE != 'agent')//如果为true则在GSA_MODE != 'agent'时,web-services (rest, webui)将不会部署
#   $MACHINE_IP_ADDRESS - The IP of this server (Useful if multiple NICs exist)//服务的IP
# 	$WORKING_HOME_DIRECTORY - This is where the files were copied to (cloudify installation, etc..)//cloudify复制文件的目录
#	$GIGASPACES_LINK - If this url is found, it will be downloaded to $WORKING_HOME_DIRECTORY/gigaspaces.zip//gigaspaces的zip包的下载URL地址
#	$GIGASPACES_OVERRIDES_LINK - If this url is found, it will be downloaded and unzipped into the same location as cloudify//gigaspaces的zip包的下载URL地址
#	$CLOUD_FILE - Location of the cloud configuration file. Only available in bootstrap of management machines.//云配置文件所在目录
#	$NO_WEB_SERVICES - If set to 'true', indicates that the rest and web-ui services should not be deployed in this machine.
#	$GIGASPACES_CLOUD_IMAGE_ID - If set, indicates the image ID for this machine.//镜像ID
#	$GIGASPACES_CLOUD_HARDWARE_ID - If set, indicates the hardware ID for this machine.//系统规模
#	$PASSWORD - the machine password//机器密码
#	$STORAGE_VOLUME_ATTACHED - if set to 'true', storage volume will be mouted. else all storage params will be null.//是否挂载存储盘
#	$STORAGE_FORMAT_TYPE - if set, indicates the file system type for formatting the volume before mount.//系统文件的卷格式
#	$STORAGE_MOUNT_PATH - if set, points to the path where the storage driver will be mounted.//存储盘的路径
#	$STORAGE_DEVICE_NAME - if set, indicated the storage device name.//存储设备的名称
#############################################################################

# args:
# $1 the error code of the last command (should be explicitly passed)
# $2 the message to print in case of an error
#
# an error message is printed and the script exists with the provided error code
function error_exit {
echo "$2 : error code: $1"
exit ${1}
}

# args:
# $1 the error code of the last command (should be explicitly passed)
# $2 the message to print in case of an error
# $3 the threshold to exit on
#
# if (last_error_code [$1]) >= (threshold [$3]) the provided message[$2] is printed and the script
# exists with the provided error code ($1)
function error_exit_on_level {
if [ ${1} -ge ${3} ]; then
error_exit ${1} ${2}
fi
}

echo Checking script path
SCRIPT=`readlink -f $0`#递归找出符号链接所指向的位置
SCRIPTPATH=`dirname $SCRIPT`#获取路径的父目录
echo script path is $SCRIPTPATH

if [ -f ${SCRIPTPATH}/cloudify_env.sh ]; then
ENV_FILE_PATH=${SCRIPTPATH}/cloudify_env.sh
else
if [ -f ${SCRIPTPATH}/../cloudify_env.sh ]; then
ENV_FILE_PATH=${SCRIPTPATH}/../cloudify_env.sh
else
echo Cloudify environment file not found! Bootstrapping cannot proceed!
exit 105
fi

fi

source ${ENV_FILE_PATH}#在本shell中执行脚本

if [ "$STORAGE_VOLUME_ATTACHED" = "true" ]; then
echo Formatting storage volume with fs type ${STORAGE_FORMAT_TYPE} and device name ${STORAGE_DEVICE_NAME}
sudo mkfs -t $STORAGE_FORMAT_TYPE $STORAGE_DEVICE_NAME || error_exit $? "Failed formatting storage volume"#在指定分区建立文件系统
echo Mounting storage volume on path ${STORAGE_MOUNT_PATH}
mkdir -p ~/$STORAGE_MOUNT_PATH
sudo mount $STORAGE_DEVICE_NAME ~/$STORAGE_MOUNT_PATH || error_exit $? "Failed mounting storage volume"
USERNAME=`whoami`
sudo chown $USERNAME storage/
fi

JAVA_32_URL="http://repository.cloudifysource.org/com/oracle/java/1.6.0_32/jdk-6u32-linux-i586.bin"
JAVA_64_URL="http://repository.cloudifysource.org/com/oracle/java/1.6.0_32/jdk-6u32-linux-x64.bin"

# If not JDK specified, determine which JDK to install based on hardware architecture
if [ -z "$GIGASPACES_AGENT_ENV_JAVA_URL" ]; then
ARCH=`uname -m`
echo Machine Architecture -- $ARCH
if [ "$ARCH" = "i686" ]; then
export GIGASPACES_AGENT_ENV_JAVA_URL=$JAVA_32_URL
elif [ "$ARCH" = "x86_64" ]; then
export GIGASPACES_AGENT_ENV_JAVA_URL=$JAVA_64_URL
else
echo Unknown architecture -- $ARCH -- defaulting to 32 bit JDK
export GIGASPACES_AGENT_ENV_JAVA_URL=$JAVA_32_URL
fi

fi

if [ "$GIGASPACES_AGENT_ENV_JAVA_URL" = "NO_INSTALL" ]; then
echo "JDK will not be installed"
else
echo Previous JAVA_HOME value -- $JAVA_HOME
export GIGASPACES_ORIGINAL_JAVA_HOME=$JAVA_HOME

echo Downloading JDK from $GIGASPACES_AGENT_ENV_JAVA_URL
wget -q -O $WORKING_HOME_DIRECTORY/java.bin $GIGASPACES_AGENT_ENV_JAVA_URL || error_exit $? "Failed downloading Java installation from $GIGASPACES_AGENT_ENV_JAVA_URL"
chmod +x $WORKING_HOME_DIRECTORY/java.bin
echo -e "\n" > $WORKING_HOME_DIRECTORY/input.txt
rm -rf ~/java || error_exit $? "Failed removing old java installation directory"
mkdir ~/java
cd ~/java

echo Installing JDK
$WORKING_HOME_DIRECTORY/java.bin < $WORKING_HOME_DIRECTORY/input.txt > /dev/null
mv ~/java/*/* ~/java || error_exit $? "Failed moving JDK installation"
rm -f $WORKING_HOME_DIRECTORY/input.txt
export JAVA_HOME=~/java
fi

export EXT_JAVA_OPTIONS="-Dcom.gs.multicast.enabled=false"

if [ ! -z "$GIGASPACES_LINK" ]; then
echo Downloading cloudify installation from $GIGASPACES_LINK.tar.gz
wget -q $GIGASPACES_LINK.tar.gz -O $WORKING_HOME_DIRECTORY/gigaspaces.tar.gz || error_exit $? "Failed downloading cloudify installation"
fi

if [ ! -z "$GIGASPACES_OVERRIDES_LINK" ]; then
echo Downloading cloudify overrides from $GIGASPACES_OVERRIDES_LINK.tar.gz
wget -q $GIGASPACES_OVERRIDES_LINK.tar.gz -O $WORKING_HOME_DIRECTORY/gigaspaces_overrides.tar.gz || error_exit $? "Failed downloading cloudify overrides"
fi

# Todo: Check this condition
if [ ! -d "~/gigaspaces" -o $WORKING_HOME_DIRECTORY/gigaspaces.tar.gz -nt ~/gigaspaces ]; then
rm -rf ~/gigaspaces || error_exit $? "Failed removing old gigaspaces directory"
mkdir ~/gigaspaces || error_exit $? "Failed creating gigaspaces directory"

# 2 is the error level threshold. 1 means only warnings
# this is needed for testing purposes on zip files created on the windows platform
tar xfz $WORKING_HOME_DIRECTORY/gigaspaces.tar.gz -C ~/gigaspaces || error_exit_on_level $? "Failed extracting cloudify installation" 2

# Todo: consider removing this line
chmod -R 777 ~/gigaspaces || error_exit $? "Failed changing permissions in cloudify installion"
mv ~/gigaspaces/*/* ~/gigaspaces || error_exit $? "Failed moving cloudify installation"

if [ ! -z "$GIGASPACES_OVERRIDES_LINK" ]; then
echo Copying overrides into cloudify distribution
tar xfz $WORKING_HOME_DIRECTORY/gigaspaces_overrides.tar.gz -C ~/gigaspaces || error_exit_on_level $? "Failed extracting cloudify overrides" 2
fi
fi

# if an overrides directory exists, copy it into the cloudify distribution
if [ -d $WORKING_HOME_DIRECTORY/cloudify-overrides ]; then
cp -rf $WORKING_HOME_DIRECTORY/cloudify-overrides/* ~/gigaspaces
fi

# UPDATE SETENV SCRIPT...
echo Updating environment script
cd ~/gigaspaces/bin || error_exit $? "Failed changing directory to bin directory"

sed -i "1i source  ${ENV_FILE_PATH}" setenv.sh || error_exit $? "Failed updating setenv.sh"
sed -i "1i export NIC_ADDR=$MACHINE_IP_ADDRESS" setenv.sh || error_exit $? "Failed updating setenv.sh"
sed -i "1i export LOOKUPLOCATORS=$LUS_IP_ADDRESS" setenv.sh || error_exit $? "Failed updating setenv.sh"
sed -i "1i export PATH=$JAVA_HOME/bin:$PATH" setenv.sh || error_exit $? "Failed updating setenv.sh"
sed -i "1i export JAVA_HOME=$JAVA_HOME" setenv.sh || error_exit $? "Failed updating setenv.sh"

# START AGENT ALONE OR WITH MANAGEMENT
if [ -f nohup.out ]; then
rm nohup.out
fi

if [ -f nohup.out ]; then
error_exit 1 "Failed to remove nohup.out Probably used by another process"
fi

# Privileged mode handling
if [ "$GIGASPACES_AGENT_ENV_PRIVILEGED" = "true" ]; then
# First check if sudo is allowed for current session
export GIGASPACES_USER=`whoami`
if [ "$GIGASPACES_USER" = "root" ]; then
# root is privileged by definition
echo Running as root
else
sudo -n ls > /dev/null || error_exit_on_level $? "Current user is not a sudoer, or requires a password for sudo" 1
fi

# now modify sudoers configuration to allow execution without tty
grep -i ubuntu /proc/version > /dev/null
if [ "$?" -eq "0" ]; then
# ubuntu
echo Running on Ubuntu
if sudo grep -q -E '[^!]requiretty' /etc/sudoers; then
echo creating sudoers user file
echo "Defaults:`whoami` !requiretty" | sudo tee /etc/sudoers.d/`whoami` >/dev/null
sudo chmod 0440 /etc/sudoers.d/`whoami`
else
echo No requiretty directive found, nothing to do
fi
else
# other - modify sudoers file
if [ ! -f "/etc/sudoers" ]; then
error_exit 101 "Could not find sudoers file at expected location (/etc/sudoers)"
fi
echo Setting privileged mode
sudo sed -i 's/^Defaults.*requiretty/#&/g' /etc/sudoers || error_exit_on_level $? "Failed to edit sudoers file to disable requiretty directive" 1
fi

fi

# Execute per-template command
if [ ! -z "$GIGASPACES_AGENT_ENV_INIT_COMMAND" ]; then
echo Executing initialization command
cd $WORKING_HOME_DIRECTORY
$GIGASPACES_AGENT_ENV_INIT_COMMAND
fi

cd ~/gigaspaces/tools/cli || error_exit $? "Failed changing directory to cli directory"

START_COMMAND_ARGS="-timeout 30 --verbose -auto-shutdown"
if [ "$GSA_MODE" = "agent" ]; then
ERRMSG="Failed starting agent"
START_COMMAND="start-agent"
else
ERRMSG="Failed starting management services"
START_COMMAND="start-management"
START_COMMAND_ARGS="${START_COMMAND_ARGS} -cloud-file ${CLOUD_FILE}"
if [ "$NO_WEB_SERVICES" = "true" ]; then
START_COMMAND_ARGS="${START_COMMAND_ARGS} -no-web-services -no-management-space"
fi
fi

nohup ./cloudify.sh $START_COMMAND $START_COMMAND_ARGS	#退出帐户/关闭终端之后继续运行cloudify.sh,所有输出都被重定向到一个名为nohup.out的文件中

RETVAL=$?
echo cat nohup.out
cat nohup.out
if [ $RETVAL -ne 0 ]; then
error_exit $RETVAL $ERRMSG
fi
exit 0


cloudify.sh

($cloudifyRoot\tools\cli\cloudify.sh)

#!/bin/bash

function setJSHome() {
SCRIPT_PATH="${BASH_SOURCE[0]}";#BASH_SOURCE是shell调试内置变量,取得执行shell命令
if([ -h "${SCRIPT_PATH}" ]) then#如果脚本存在并且是一个符号链接
while([ -h "${SCRIPT_PATH}" ]) do SCRIPT_PATH=`readlink "${SCRIPT_PATH}"`; done
fi
pushd . > /dev/null#将当前目录路径重定向到/dev/null中
cd `dirname ${SCRIPT_PATH}` > /dev/null
SCRIPT_PATH=`pwd`;
JSHOMEDIR="$SCRIPT_PATH/../.."#即cloudify根目录,cloudify/
popd  > /dev/null
}

function setEnv() {
. $JSHOMEDIR/bin/setenv.sh
}

function setCloudifyJavaOptions() {#设置jvm参数
CLOUDIFY_DEBUG_OPTIONS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9000 -Xnoagent -Djava.compiler=NONE"
CLOUDIFY_JAVA_OPTIONS="-Xmx500m -Dcom.gigaspaces.logger.RollingFileHandler.debug-level=WARNING ${EXT_JAVA_OPTIONS}"
}

function setCloudifyClassPath() {#设置类路径
CLI_JARS=${JSHOMEDIR}/tools/cli/cli.jar
SIGAR_JARS=${JSHOMEDIR}/lib/platform/sigar/sigar.jar
GROOVY_JARS=${JSHOMEDIR}/tools/groovy/lib/*
DSL_JARS=${JSHOMEDIR}/lib/platform/cloudify/*

# Test whether this is jdk or jre
if [ -f "${JAVA_HOME}/jre/lib/deploy.jar" ]; then
DEPLOY_JARS=${JAVA_HOME}/jre/lib/deploy.jar
else
DEPLOY_JARS=${JAVA_HOME}/lib/deploy.jar
fi

# Add esc dependencies
ESC_JARS=
for jar in `find ${JSHOMEDIR}/lib/platform/esm -mindepth 1 -maxdepth 1 -type f -name \*.jar`#从当前层,深度为0层,查找普通文件
do
if [ "${ESC_JARS}" == "" ]; then
ESC_JARS=${jar}
else
ESC_JARS=${ESC_JARS}${CPS}${jar}
fi
done

# Add plugins and dependencies
PLUGIN_JARS=

for jar in `find ${SCRIPT_PATH}/plugins -mindepth 2 -maxdepth 2 -type f -name \*.jar`
do
if [ "${PLUGIN_JARS}" == "" ]; then
PLUGIN_JARS=${jar}
else
PLUGIN_JARS=${PLUGIN_JARS}${CPS}${jar}
fi
done

CLOUDIFY_CLASSPATH=${CLI_JARS}:${DSL_JARS}${CPS}${DEPLOY_JARS}${CPS}${GS_JARS}${CPS}${SIGAR_JARS}${CPS}${GROOVY_JARS}${CPS}${ESC_JARS}${CPS}${PLUGIN_JARS}
}

function setCommandLine() {
CLI_ENTRY_POINT=org.cloudifysource.shell.GigaShellMain
COMMAND_LINE="${JAVACMD} ${GS_LOGGING_CONFIG_FILE_PROP} ${RMI_OPTIONS} ${LOOKUP_LOCATORS_PROP} ${LOOKUP_GROUPS_PROP} ${CLOUDIFY_JAVA_OPTIONS} -classpath ${PRE_CLASSPATH}${CPS}${CLOUDIFY_CLASSPATH}${CPS}${POST_CLASSPATH} ${CLI_ENTRY_POINT} $*"
}

function init() {
setJSHome
setEnv
setCloudifyJavaOptions
setCloudifyClassPath
setCommandLine $*
}

init $*#$*为所有实参字符串

# add one padding line before the logo
echo
${COMMAND_LINE}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cloudify