| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.scriptbuilder; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.scriptbuilder.domain.Statements.call; |
| 23 | import static org.jclouds.scriptbuilder.domain.Statements.createRunScript; |
| 24 | import static org.jclouds.scriptbuilder.domain.Statements.findPid; |
| 25 | import static org.jclouds.scriptbuilder.domain.Statements.forget; |
| 26 | import static org.jclouds.scriptbuilder.domain.Statements.interpret; |
| 27 | import static org.jclouds.scriptbuilder.domain.Statements.kill; |
| 28 | import static org.jclouds.scriptbuilder.domain.Statements.newStatementList; |
| 29 | import static org.jclouds.scriptbuilder.domain.Statements.switchArg; |
| 30 | |
| 31 | import java.util.Map; |
| 32 | |
| 33 | import org.jclouds.scriptbuilder.domain.Statement; |
| 34 | import org.jclouds.scriptbuilder.domain.StatementList; |
| 35 | |
| 36 | import com.google.common.collect.ImmutableMap; |
| 37 | import com.google.common.collect.ImmutableSet; |
| 38 | import com.google.common.collect.Iterables; |
| 39 | |
| 40 | /** |
| 41 | * Creates an init script file |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | public class InitBuilder extends ScriptBuilder { |
| 46 | |
| 47 | private final String instanceName; |
| 48 | private final String instanceHome; |
| 49 | private final String logDir; |
| 50 | |
| 51 | public InitBuilder(String instanceName, String instanceHome, String logDir, Map<String, String> variables, |
| 52 | Iterable<Statement> statements) { |
| 53 | this(instanceName, instanceHome, logDir, variables, ImmutableSet.<Statement>of(), statements); |
| 54 | } |
| 55 | |
| 56 | public InitBuilder(String instanceName, String instanceHome, String logDir, Map<String, String> variables, |
| 57 | Iterable<Statement> initStatements, Iterable<Statement> statements) { |
| 58 | this.instanceName = checkNotNull(instanceName, "instanceName"); |
| 59 | this.instanceHome = checkNotNull(instanceHome, "instanceHome"); |
| 60 | this.logDir = checkNotNull(logDir, "logDir"); |
| 61 | Map<String, String> defaultVariables = ImmutableMap.of("instanceName", instanceName, "instanceHome", |
| 62 | instanceHome, "logDir", logDir); |
| 63 | addEnvironmentVariableScope("default", defaultVariables) |
| 64 | .addEnvironmentVariableScope(instanceName, variables) |
| 65 | .addStatement( |
| 66 | switchArg( |
| 67 | 1, |
| 68 | new ImmutableMap.Builder<String,Statement>() |
| 69 | .put( |
| 70 | "init", |
| 71 | newStatementList(call("default"), call(instanceName), |
| 72 | new StatementList(initStatements), createRunScript( |
| 73 | instanceName,// TODO: convert |
| 74 | // so |
| 75 | // that |
| 76 | // createRunScript |
| 77 | // can take from a |
| 78 | // variable |
| 79 | Iterables.concat(variables.keySet(), |
| 80 | defaultVariables.keySet()), |
| 81 | "{varl}INSTANCE_HOME{varr}", statements))) |
| 82 | .put( |
| 83 | "status", |
| 84 | newStatementList(call("default"), |
| 85 | findPid("{varl}INSTANCE_NAME{varr}"), |
| 86 | interpret("echo [{varl}FOUND_PID{varr}]{lf}"))) |
| 87 | .put( |
| 88 | "stop", |
| 89 | newStatementList(call("default"), |
| 90 | findPid("{varl}INSTANCE_NAME{varr}"), kill())) |
| 91 | .put( |
| 92 | "start", |
| 93 | newStatementList( |
| 94 | call("default"), |
| 95 | forget( |
| 96 | "{varl}INSTANCE_NAME{varr}", |
| 97 | "{varl}INSTANCE_HOME{varr}{fs}{varl}INSTANCE_NAME{varr}.{sh}", |
| 98 | "{varl}LOG_DIR{varr}"))) |
| 99 | .put( |
| 100 | "tail", |
| 101 | newStatementList(call("default"), |
| 102 | interpret("tail {varl}LOG_DIR{varr}{fs}stdout.log{lf}"))) |
| 103 | .put( |
| 104 | "tailerr", |
| 105 | newStatementList(call("default"), |
| 106 | interpret("tail {varl}LOG_DIR{varr}{fs}stderr.log{lf}"))) |
| 107 | .put( |
| 108 | "run", |
| 109 | newStatementList( |
| 110 | call("default"), |
| 111 | interpret("{varl}INSTANCE_HOME{varr}{fs}{varl}INSTANCE_NAME{varr}.{sh}{lf}"))) |
| 112 | .build())); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public int hashCode() { |
| 117 | final int prime = 31; |
| 118 | int result = 1; |
| 119 | result = prime * result + ((instanceHome == null) ? 0 : instanceHome.hashCode()); |
| 120 | result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode()); |
| 121 | result = prime * result + ((logDir == null) ? 0 : logDir.hashCode()); |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean equals(Object obj) { |
| 127 | if (this == obj) |
| 128 | return true; |
| 129 | if (obj == null) |
| 130 | return false; |
| 131 | if (getClass() != obj.getClass()) |
| 132 | return false; |
| 133 | InitBuilder other = (InitBuilder) obj; |
| 134 | if (instanceHome == null) { |
| 135 | if (other.instanceHome != null) |
| 136 | return false; |
| 137 | } else if (!instanceHome.equals(other.instanceHome)) |
| 138 | return false; |
| 139 | if (instanceName == null) { |
| 140 | if (other.instanceName != null) |
| 141 | return false; |
| 142 | } else if (!instanceName.equals(other.instanceName)) |
| 143 | return false; |
| 144 | if (logDir == null) { |
| 145 | if (other.logDir != null) |
| 146 | return false; |
| 147 | } else if (!logDir.equals(other.logDir)) |
| 148 | return false; |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | public String getInstanceName() { |
| 153 | return instanceName; |
| 154 | } |
| 155 | |
| 156 | public String getInstanceHome() { |
| 157 | return instanceHome; |
| 158 | } |
| 159 | |
| 160 | public String getLogDir() { |
| 161 | return logDir; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String toString() { |
| 166 | return "[instanceName=" + instanceName + ", instanceHome=" + instanceHome + ", logDir=" + logDir + "]"; |
| 167 | } |
| 168 | } |