Merge pull request #1155 from andreaturli/virtualbox

added support to assign passwordless right to the default user of the node
This commit is contained in:
Adrian Cole 2013-01-08 20:51:15 -08:00
commit 24c10f087a
3 changed files with 100 additions and 4 deletions

View File

@ -49,6 +49,7 @@ import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.NodeSpec;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.statements.DeleteGShadowLock;
import org.jclouds.virtualbox.statements.PasswordlessSudo;
import org.jclouds.virtualbox.util.MachineController;
import org.jclouds.virtualbox.util.MachineUtils;
import org.jclouds.virtualbox.util.NetworkUtils;
@ -157,15 +158,19 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
NodeMetadata partialNodeMetadata = buildPartialNodeMetadata(cloned);
// see DeleteGShadowLock for a detailed explanation
machineUtils.runScriptOnNode(partialNodeMetadata, new DeleteGShadowLock(), RunScriptOptions.NONE);
machineUtils.runScriptOnNode(partialNodeMetadata, new DeleteGShadowLock(), RunScriptOptions.NONE);
if(optionalNatIfaceCard.isPresent())
checkState(networkUtils.enableNetworkInterface(partialNodeMetadata, optionalNatIfaceCard.get()),
"cannot enable NAT Interface on vm(%s)", cloneName);
// apply passwordless ssh script to each clone
machineUtils.runScriptOnNode(partialNodeMetadata, new PasswordlessSudo(partialNodeMetadata.getCredentials().identity), RunScriptOptions.Builder.runAsRoot(true));
LoginCredentials credentials = partialNodeMetadata.getCredentials();
return new NodeAndInitialCredentials<IMachine>(cloned,
cloneName, credentials);
return new NodeAndInitialCredentials<IMachine>(cloned, cloneName, credentials);
}
private NodeMetadata buildPartialNodeMetadata(IMachine clone) {
@ -180,7 +185,7 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
nodeMetadataBuilder.credentials(loginCredentials);
return nodeMetadataBuilder.build();
}
private long findSlotForNetworkAttachment(IMachine clone, NetworkAttachmentType networkAttachmentType) {
long slot = -1;
long i = 0;

View File

@ -0,0 +1,54 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.jclouds.virtualbox.statements;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement;
import com.google.common.collect.ImmutableList;
/**
* Assign to user the passwordless sudo rights
*
* @author andrea turli
*
*/
public class PasswordlessSudo implements Statement {
private final String user;
public PasswordlessSudo(String user) {
this.user = user;
}
@Override
public Iterable<String> functionDependencies(OsFamily family) {
return ImmutableList.of();
}
@Override
public String render(OsFamily family) {
if (checkNotNull(family, "family") == OsFamily.WINDOWS)
throw new UnsupportedOperationException("windows not yet implemented");
return String.format("touch /etc/sudoers.d/passwordless && echo \"%s ALL = NOPASSWD: ALL\" > /etc/sudoers.d/passwordless && chmod 0440 /etc/sudoers.d/passwordless", user);
}
}

View File

@ -0,0 +1,37 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.jclouds.virtualbox.statements;
import static org.testng.Assert.assertEquals;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.testng.annotations.Test;
public class PasswordlessTest {
@Test
public void testApplyPasswordlessSudo() {
PasswordlessSudo passwordlessSudo = new PasswordlessSudo("barack");
assertEquals(passwordlessSudo.render(OsFamily.UNIX),
"touch /etc/sudoers.d/passwordless && echo \"barack ALL = NOPASSWD: ALL\" > /etc/sudoers.d/passwordless && chmod 0440 /etc/sudoers.d/passwordless");
}
}