org.jclouds.scriptbuilder.statements.login.AdminAccess Java Examples
The following examples show how to use
org.jclouds.scriptbuilder.statements.login.AdminAccess.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CreateUserPolicy.java From brooklyn-server with Apache License 2.0 | 4 votes |
protected void addUser(Entity entity, SshMachineLocation machine) { boolean grantSudo = getRequiredConfig(GRANT_SUDO); boolean resetPassword = getRequiredConfig(RESET_LOGIN_USER); String user = getRequiredConfig(VM_USERNAME); String password = Identifiers.makeRandomId(12); String hostname = machine.getAddress().getHostName(); int port = machine.getPort(); String creds = user + " : " + password + " @ " +hostname + ":" + port; LOG.info("Adding auto-generated user "+user+" @ "+hostname+":"+port); // Build the command to create the user // Note AdminAccess requires _all_ fields set, due to http://code.google.com/p/jclouds/issues/detail?id=1095 // If jclouds grants Sudo rights, it overwrites the /etc/sudoers, which makes integration tests very dangerous! Not using it. AdminAccess adminAccess = AdminAccess.builder() .adminUsername(user) .adminPassword(password) .grantSudoToAdminUser(false) .resetLoginPassword(resetPassword) .loginPassword(password) .authorizeAdminPublicKey(false) .adminPublicKey("ignored") .installAdminPrivateKey(false) .adminPrivateKey("ignore") .lockSsh(false) .build(); org.jclouds.scriptbuilder.domain.OsFamily scriptOsFamily = (machine.getMachineDetails().getOsDetails().isWindows()) ? org.jclouds.scriptbuilder.domain.OsFamily.WINDOWS : org.jclouds.scriptbuilder.domain.OsFamily.UNIX; InitAdminAccess initAdminAccess = new InitAdminAccess(new AdminAccessConfiguration.Default()); initAdminAccess.visit(adminAccess); String cmd = adminAccess.render(scriptOsFamily); // Exec command to create the user int result = machine.execScript(ImmutableMap.of(SshTool.PROP_RUN_AS_ROOT.getName(), true), "create-user-"+user, ImmutableList.of(cmd), ImmutableMap.of("PATH", sbinPath())); if (result != 0) { throw new IllegalStateException("Failed to auto-generate user, using command "+cmd); } // Exec command to grant password-access to sshd (which may have been disabled earlier). cmd = new SshdConfig(ImmutableMap.of("PasswordAuthentication", "yes")).render(scriptOsFamily); result = machine.execScript(ImmutableMap.of(SshTool.PROP_RUN_AS_ROOT.getName(), true), "create-user-"+user, ImmutableList.of(cmd), ImmutableMap.of("PATH", sbinPath())); if (result != 0) { throw new IllegalStateException("Failed to enable ssh-login-with-password, using command "+cmd); } // Exec command to grant sudo rights. if (grantSudo) { List<String> cmds = ImmutableList.of( "cat >> /etc/sudoers <<-'END_OF_JCLOUDS_FILE'\n"+ user+" ALL = (ALL) NOPASSWD:ALL\n"+ "END_OF_JCLOUDS_FILE\n", "chmod 0440 /etc/sudoers"); result = machine.execScript(ImmutableMap.of(SshTool.PROP_RUN_AS_ROOT.getName(), true), "add-user-to-sudoers-"+user, cmds, ImmutableMap.of("PATH", sbinPath())); if (result != 0) { throw new IllegalStateException("Failed to auto-generate user, using command "+cmds); } } entity.sensors().set(VM_USER_CREDENTIALS, creds); }