def connectToNodes(batch_client, pool_id, usernameToGive, passwordToGive, expirationDays): # Create the user that will be added to each node # in the pool user = batchmodels.ComputeNodeUser(usernameToGive) user.password = passwordToGive user.is_admin = True user.expiry_time = (datetime.datetime.today() + datetime.timedelta(days=expirationDays)).isoformat() # Get the list of nodes in the pool nodes = batch_client.compute_node.list(pool_id) # Add the user to each node in the pool and print # the connection information for the node for node in nodes: # Add the user to the node batch_client.compute_node.add_user(pool_id, node.id, user) # Obtain SSH login information for the node login = batch_client.compute_node.get_remote_login_settings( pool_id, node.id) # Print the connection info for the node print("{0} | {1} | {2} | {3} | {4} | {5}".format( node.id, node.state, login.remote_login_ip_address, login.remote_login_port, usernameToGive, passwordToGive))
def test_batch_compute_node_user(self, batch_pool, **kwargs): client = self.create_sharedkey_client(**kwargs) nodes = list(client.compute_node.list(batch_pool.name)) while self.is_live and any([n for n in nodes if n.state != models.ComputeNodeState.idle]): time.sleep(10) nodes = list(client.compute_node.list(batch_pool.name)) # Test Add User user_name = 'BatchPythonSDKUser' nodes = list(client.compute_node.list(batch_pool.name)) user = models.ComputeNodeUser(user_name, password='******', is_admin=False) response = client.compute_node.add_user(batch_pool.name, nodes[0].id, user) self.assertIsNone(response) # Test Update User user = models.NodeUpdateUserParameter(password='******') response = client.compute_node.update_user(batch_pool.name, nodes[0].id, user_name, user) self.assertIsNone(response) # Test Get RDP File file_length = 0 with io.BytesIO() as file_handle: response = client.compute_node.get_remote_desktop(batch_pool.name, nodes[0].id) if response: for data in response: file_length += len(data) self.assertTrue(file_length > 0) # Test Delete User response = client.compute_node.delete_user(batch_pool.name, nodes[0].id, user_name) self.assertIsNone(response)
def add_admin_user_to_compute_node( batch_client, pool_id, node, username, ssh_public_key): """Adds an administrative user to the Batch Compute Node :param batch_client: The batch client to use. :type batch_client: `batchserviceclient.BatchServiceClient` :param str pool_id: The pool id containing the node. :param node: The compute node. :type node: `batchserviceclient.models.ComputeNode` :param str username: user name :param str ssh_public_key: ssh rsa public key """ print('adding user {} to node {} in pool {}'.format( username, node.id, pool_id)) batch_client.compute_node.add_user( pool_id, node.id, batchmodels.ComputeNodeUser( username, is_admin=True, password=None, ssh_public_key=common.helpers.decode_string( open(ssh_public_key, 'rb').read())) ) print('user {} added to node {}.'.format(username, node.id))
def create_user(batch_client): path = os.path.join(os.environ["AZTK_WORKING_DIR"], "user.yaml") if not os.path.isfile(path): print("No user to create.") return with open(path, "r", encoding="UTF-8") as file: user_conf = yaml.load(file.read(), Loader=yaml.Loader) try: password = None if user_conf["ssh-key"] else decrypt_password( user_conf) batch_client.compute_node.add_user( pool_id=os.environ["AZ_BATCH_POOL_ID"], node_id=os.environ["AZ_BATCH_NODE_ID"], user=batch_models.ComputeNodeUser( name=user_conf["username"], is_admin=True, password=password, ssh_public_key=str(user_conf["ssh-key"]), expiry_time=datetime.now(timezone.utc) + timedelta(days=365), ), ) except BatchErrorException as e: print(e)
def __create_user(self, id: str, node_id: str, username: str, password: str = None, ssh_key: str = None) -> str: """ Create a pool user :param pool: the pool to add the user to :param node: the node to add the user to :param username: username of the user to add :param password: password of the user to add :param ssh_key: ssh_key of the user to add """ # Create new ssh user for the given node self.batch_client.compute_node.add_user( id, node_id, batch_models.ComputeNodeUser( name=username, is_admin=True, password=password, ssh_public_key=get_ssh_key.get_user_public_key( ssh_key, self.secrets_configuration), expiry_time=datetime.now(timezone.utc) + timedelta(days=365), ), )
def test_batch_compute_node_user(self, batch_pool, **kwargs): client = self.create_sharedkey_client(**kwargs) nodes = list(client.compute_node.list(batch_pool.name)) while self.is_live and any([n for n in nodes if n.state != models.ComputeNodeState.idle]): time.sleep(10) nodes = list(client.compute_node.list(batch_pool.name)) self.assertEqual(len(nodes), 1) # Test Add User user_name = 'BatchPythonSDKUser' nodes = list(client.compute_node.list(batch_pool.name)) user = models.ComputeNodeUser(name=user_name, password='******', is_admin=False) response = client.compute_node.add_user(batch_pool.name, nodes[0].id, user) self.assertIsNone(response) # Test Update User user = models.NodeUpdateUserParameter(password='******') response = client.compute_node.update_user(batch_pool.name, nodes[0].id, user_name, user) self.assertIsNone(response) # Test Get remote login settings remote_login_settings = client.compute_node.get_remote_login_settings(batch_pool.name, nodes[0].id) self.assertIsInstance(remote_login_settings, models.ComputeNodeGetRemoteLoginSettingsResult) self.assertIsNotNone(remote_login_settings.remote_login_ip_address) self.assertIsNotNone(remote_login_settings.remote_login_port) # Test Delete User response = client.compute_node.delete_user(batch_pool.name, nodes[0].id, user_name) self.assertIsNone(response)
def create_user(batch_client): path = os.path.join(os.environ['DOCKER_WORKING_DIR'], "user.yaml") if not os.path.isfile(path): print("No user to create.") return with open(path) as file: user_conf = yaml.load(file.read()) try: password = None if user_conf['ssh-key'] else decrypt_password(user_conf) batch_client.compute_node.add_user( pool_id=os.environ['AZ_BATCH_POOL_ID'], node_id=os.environ['AZ_BATCH_NODE_ID'], user=batch_models.ComputeNodeUser( name=user_conf['username'], is_admin=True, password=password, ssh_public_key=str(user_conf['ssh-key']), expiry_time=datetime.now(timezone.utc) + timedelta(days=365) ) ) except batch_error.BatchErrorException as e: print(e)
def add_admin_user_to_compute_node(batch_client, pool_id, node, username, ssh_public_key): print('adding user {} to node {} in pool {}'.format( username, node.id, pool_id)) batch_client.compute_node.add_user( pool_id, node.id, batchmodels.ComputeNodeUser(username, is_admin=True, password=None, ssh_public_key=decode_string( open(ssh_public_key, 'rb').read()))) print('user {} added to node {}.'.format(username, node.id))
def create_nodeuser(batch_service_client, pool_id): """ Create any number of login/pass needed per nodes & print it : param batch_service_client: A Batch service client. : param str pool_id: ID of the selected pool. """ import getpass # Specify the username and prompt for a password username = "******" password = "******" #getpass.getpass() # Create the user that will be added to each node # in the pool user = batchmodels.ComputeNodeUser(username) user.password = password user.is_admin = True user.expiry_time = (datetime.datetime.today() + datetime.timedelta(days=30)).isoformat() # Get the list of nodes in the pool nodes = batch_service_client.compute_node.list(pool_id) # Add the user to each node in the pool and print # the connection information for the node for node in nodes: # Add the user to the node batch_service_client.compute_node.add_user(pool_id, node.id, user) # Obtain SSH login information for the node login = batch_service_client.compute_node.get_remote_login_settings(pool_id, node.id) # Print the connection info for the node print("{0} | {1} | {2} | {3} | {4} | {5}".format(node.id, node.state, login.remote_login_ip_address, login.remote_login_port, username, password))