View Javadoc

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