def create_users(): """Create project user and developer users.""" ssh_dir = u"/home/%s/.ssh" % env.project_user system.create_user(env.project_user, groups=[ 'www-data', 'login', ]) sudo('mkdir -p %s' % ssh_dir) user_dir = os.path.join(CONF_ROOT, "users") for username in os.listdir(user_dir): key_file = os.path.normpath(os.path.join(user_dir, username)) system.create_user(username, groups=[ 'dev', 'login', ], key_file=key_file) with open(key_file, 'rt') as f: ssh_key = f.read() # Add ssh key for project user files.append('%s/authorized_keys' % ssh_dir, ssh_key, use_sudo=True) files.append(u'/etc/sudoers', r'%dev ALL=(ALL) NOPASSWD:ALL', use_sudo=True) sudo('chown -R %s:%s %s' % (env.project_user, env.project_user, ssh_dir))
def test_user_already_exists(self): "Don't try to create users which already exist." with patch('argyle.system.user_exists') as exists: exists.return_value = True system.create_user('foo') sudo = self.mocks['sudo'] self.assertFalse(sudo.called)
def test_simple_create_user(self): "Create new user without any groups." with patch('argyle.system.user_exists') as exists: exists.return_value = False system.create_user('foo') # Create user self.assertSudoCommand('useradd -m -s /bin/bash foo') # Disable password self.assertSudoCommand('passwd -d foo')
def test_create_user_with_existing_groups(self): "No need to create groups which already exist." with patch('argyle.system.user_exists') as user_exists: with patch('argyle.system.group_exists') as group_exists: user_exists.return_value = False group_exists.return_value = True system.create_user('foo', groups=['admin', 'ssh']) # Create groups self.assertNoSudoCommand('addgroup admin') self.assertNoSudoCommand('addgroup ssh') # Create user self.assertSudoCommand('useradd -m -G admin,ssh -s /bin/bash foo')
def test_create_user_with_new_groups(self): "Create groups which don't exist and add the user to them." with patch('argyle.system.user_exists') as user_exists: with patch('argyle.system.group_exists') as group_exists: user_exists.return_value = False group_exists.return_value = False system.create_user('foo', groups=['admin', 'ssh']) # Create groups self.assertSudoCommand('addgroup admin') self.assertSudoCommand('addgroup ssh') # Create user self.assertSudoCommand('useradd -m -G admin,ssh -s /bin/bash foo')
def test_create_user_with_existing_groups(self): "No need to create groups which already exist." with patch('argyle.system.user_exists') as user_exists: with patch('argyle.system.group_exists') as group_exists: user_exists.return_value = False group_exists.return_value = True system.create_user('foo', groups=['admin', 'ssh']) # Create groups self.assertNoSudoCommand('addgroup admin') self.assertNoSudoCommand('addgroup ssh') # Create user self.assertSudoCommand( 'useradd -m -G admin,ssh -s /bin/bash foo')
def test_create_user_with_new_groups(self): "Create groups which don't exist and add the user to them." with patch('argyle.system.user_exists') as user_exists: with patch('argyle.system.group_exists') as group_exists: user_exists.return_value = False group_exists.return_value = False system.create_user('foo', groups=['admin', 'ssh']) # Create groups self.assertSudoCommand('addgroup admin') self.assertSudoCommand('addgroup ssh') # Create user self.assertSudoCommand( 'useradd -m -G admin,ssh -s /bin/bash foo')
def create_users(): """Create project user and developer users.""" ssh_dir = u"/home/%s/.ssh" % env.project_user system.create_user(env.project_user, groups=["www-data", "login"]) sudo("mkdir -p %s" % ssh_dir) user_dir = os.path.join(CONF_ROOT, "users") for username in os.listdir(user_dir): key_file = os.path.normpath(os.path.join(user_dir, username)) system.create_user(username, groups=["dev", "login", "admin"], key_file=key_file) with open(key_file, "rt") as f: ssh_key = f.read() # Add ssh key for project user files.append("%s/authorized_keys" % ssh_dir, ssh_key, use_sudo=True) sudo("chown -R %s:%s %s" % (env.project_user, env.project_user, ssh_dir))
def create_users(): """Create project user and developer users.""" ssh_dir = u"/home/%s/.ssh" % env.project_user system.create_user(env.project_user, groups=['www-data', 'login', ]) sudo('mkdir -p %s' % ssh_dir) user_dir = os.path.join(CONF_ROOT, "users") for username in os.listdir(user_dir): key_file = os.path.normpath(os.path.join(user_dir, username)) system.create_user(username, groups=['dev', 'login', ], key_file=key_file) with open(key_file, 'rt') as f: ssh_key = f.read() # Add ssh key for project user files.append('%s/authorized_keys' % ssh_dir, ssh_key, use_sudo=True) files.append(u'/etc/sudoers', r'%dev ALL=(ALL) NOPASSWD:ALL', use_sudo=True) sudo('chown -R %s:%s %s' % (env.project_user, env.project_user, ssh_dir))
def test_create_user_with_key_file(self): "Create a user and push a key file to the remote." key_file = 'foo/key.pub' with patch('argyle.system.user_exists') as exists: exists.return_value = False system.create_user('foo', key_file=key_file) # Create remote ssh directory and set permissions self.assertSudoCommand('mkdir -p /home/foo/.ssh') self.assertSudoCommand('chown -R foo:foo /home/foo/.ssh') put = self.mocks['put'] self.assertTrue(put.called) args, kwargs = put.call_args file_name, remote_path = args self.assertEqual(file_name, key_file) self.assertEqual(remote_path, '/home/foo/.ssh/authorized_keys')