View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.lib.linux;
17  
18  import org.argeo.slc.SlcException;
19  import org.argeo.slc.core.deploy.LocalFilesDeployment;
20  import org.argeo.slc.core.deploy.ResourceSet;
21  import org.argeo.slc.core.execution.tasks.SystemCall;
22  import org.argeo.slc.jsch.RemoteExec;
23  import org.argeo.slc.jsch.SshFilesDeployment;
24  import org.argeo.slc.jsch.SshTarget;
25  
26  public class RedhatHostManager {
27  
28  	private SimpleLinuxHost host;
29  
30  	// SSH
31  	private Boolean useSsh = true;
32  	private SshTarget sshTarget = null;
33  
34  	public void installPackages() {
35  		StringBuffer cmd = new StringBuffer("yum --nogpgcheck -y install");
36  		for (String pkg : ((RpmDistribution) host.getDistribution())
37  				.getAdditionalPackages()) {
38  			cmd.append(' ').append(pkg);
39  		}
40  		executeCommand(cmd.toString());
41  
42  		RedhatDeploymentData rdd = (RedhatDeploymentData) host
43  				.getDeploymentData();
44  		executeCommand(rdd.getRunlevelsScript());
45  	}
46  
47  	public void deployConfig() {
48  		RedhatDeploymentData rdd = (RedhatDeploymentData) host
49  				.getDeploymentData();
50  		deploy(rdd.getConfigurationFiles());
51  		executeCommand(rdd.getPermissionsScript());
52  	}
53  
54  	// GENERIC?
55  	protected void deploy(ResourceSet resourceSet) {
56  		if (useSsh)
57  			new SshFilesDeployment(getSshTarget(), resourceSet).run();
58  		else
59  			new LocalFilesDeployment(resourceSet).run();
60  
61  	}
62  
63  	protected void executeCommand(String command) {
64  		if (command == null)
65  			return;
66  
67  		if (useSsh) {
68  			RemoteExec rExec = new RemoteExec(getSshTarget(), command);
69  			rExec.setFailOnBadExitStatus(false);
70  			rExec.run();
71  		} else
72  			new SystemCall(command).run();
73  	}
74  
75  	protected SshTarget getSshTarget() {
76  		if (sshTarget == null)
77  			throw new SlcException("No SSH target defined");
78  		return sshTarget;
79  	}
80  
81  	public void setHost(SimpleLinuxHost host) {
82  		this.host = host;
83  	}
84  
85  	public void setUseSsh(Boolean useSsh) {
86  		this.useSsh = useSsh;
87  	}
88  
89  	public void setSshTarget(SshTarget sshTarget) {
90  		this.sshTarget = sshTarget;
91  	}
92  
93  }