def test_attribute_password(self, getpwnam_mock, popen_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 subproc_mock.stdout = subproc_stdout popen_mock.return_value = subproc_mock getpwnam_mock.return_value = _get_user_entity() with Environment('/') as env: user = User("mapred", action="create", password="******", shell="/bin/bash") popen_mock.assert_called_with([ '/bin/bash', '--login', '--noprofile', '-c', "ambari-sudo.sh PATH=/bin -H -E usermod -s /bin/bash -p secure mapred" ], shell=False, preexec_fn=preexec_fn, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None, close_fds=True) self.assertEqual(popen_mock.call_count, 1)
def test_attribute_groups(self, getpwnam_mock, popen_mock, user_groups_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 user_groups_mock.return_value = ['hadoop'] subproc_mock.stdout = subproc_stdout popen_mock.return_value = subproc_mock getpwnam_mock.return_value = _get_user_entity() with Environment('/') as env: user = User("mapred", action="create", groups=['1', '2', '3'], shell="/bin/bash") popen_mock.assert_called_with([ '/bin/bash', '--login', '--noprofile', '-c', 'ambari-sudo.sh PATH=/bin -H -E usermod -s /bin/bash -G 1,2,3,hadoop mapred' ], shell=False, preexec_fn=preexec_fn, env={'PATH': '/bin'}, close_fds=True, stdout=-1, stderr=-2, cwd=None) self.assertEqual(popen_mock.call_count, 1)
def test_attribute_groups(self, getpwnam_mock, popen_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 subproc_mock.stdout.readline = MagicMock(side_effect=['OK']) popen_mock.return_value = subproc_mock getpwnam_mock.return_value = 1 with Environment('/') as env: user = User("mapred", action="create", groups=['1', '2', '3'], shell="/bin/bash") popen_mock.assert_called_with([ '/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -G 1,2,3 -s /bin/bash mapred" ], shell=False, preexec_fn=None, stderr=-2, stdout=5, bufsize=1, env={'PATH': '/bin'}, cwd=None) self.assertEqual(popen_mock.call_count, 1)
def create_user(params): """ Creates the user required for Elasticsearch. """ Logger.info("Creating user={0} in group={1}".format( params.elastic_user, params.elastic_group)) User(params.elastic_user, action="create", groups=params.elastic_group)
def test_attribute_home(self, getpwnam_mock, popen_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 subproc_mock.stdout.readline = MagicMock(side_effect=['OK']) popen_mock.return_value = subproc_mock getpwnam_mock.return_value = 1 with Environment('/') as env: user = User("mapred", action="create", home="/test/home", shell="/bin/bash") popen_mock.assert_called_with([ '/bin/bash', '--login', '--noprofile', '-c', "ambari-sudo.sh PATH=/bin -H -E usermod -s /bin/bash -d /test/home mapred" ], shell=False, preexec_fn=None, stderr=-2, stdout=5, bufsize=1, env={'PATH': '/bin'}, cwd=None, close_fds=True) self.assertEqual(popen_mock.call_count, 1)
def test_action_create_nonexistent(self, getpwnam_mock, popen_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 popen_mock.return_value = subproc_mock getpwnam_mock.return_value = None with Environment('/') as env: user = User("mapred", action = "create", shell = "/bin/bash") popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'useradd -m -s /bin/bash mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None) self.assertEqual(popen_mock.call_count, 1)
def elastic(): import params Logger.info("Creating user: {0}:{1}".format(params.elastic_user, params.elastic_group)) User(params.elastic_user, action="create", groups=params.elastic_group) params.path_data = params.path_data.replace('"', '') data_path = params.path_data.replace(' ', '').split(',') data_path[:] = [x.replace('"', '') for x in data_path] directories = [params.log_dir, params.pid_dir, params.conf_dir] directories = directories + data_path + [ "{0}/scripts".format(params.conf_dir) ] Logger.info("Creating directories: {0}".format(directories)) Directory(directories, create_parents=True, mode=0755, owner=params.elastic_user, group=params.elastic_group) Logger.info("Master env: " "{0}/elastic-env.sh".format(params.conf_dir)) File("{0}/elastic-env.sh".format(params.conf_dir), owner=params.elastic_user, group=params.elastic_group, content=InlineTemplate(params.elastic_env_sh_template)) configurations = params.config['configurations']['elastic-site'] Logger.info("Master yml: " "{0}/elasticsearch.yml".format(params.conf_dir)) File("{0}/elasticsearch.yml".format(params.conf_dir), content=Template("elasticsearch.master.yaml.j2", configurations=configurations), owner=params.elastic_user, group=params.elastic_group) Logger.info("Master sysconfig: /etc/sysconfig/elasticsearch") File("/etc/sysconfig/elasticsearch", owner="root", group="root", content=InlineTemplate(params.sysconfig_template)) # in some OS this folder may not exist, so create it Logger.info("Ensure PAM limits directory exists: {0}".format( params.limits_conf_dir)) Directory(params.limits_conf_dir, create_parents=True, owner='root', group='root') Logger.info("Master PAM limits: {0}".format(params.limits_conf_file)) File(params.limits_conf_file, content=Template('elasticsearch_limits.conf.j2'), owner="root", group="root")
def test_action_delete(self, getpwnam_mock, popen_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 subproc_mock.stdout.readline = MagicMock(side_effect = ['OK']) popen_mock.return_value = subproc_mock getpwnam_mock.return_value = 1 with Environment('/') as env: user = User("mapred", action = "remove", shell = "/bin/bash") popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', 'userdel mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=5, bufsize=1, env={'PATH': '/bin'}, cwd=None, close_fds=True) self.assertEqual(popen_mock.call_count, 1)
def test_attribute_groups(self, getpwnam_mock, popen_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 popen_mock.return_value = subproc_mock getpwnam_mock.return_value = 1 with Environment('/') as env: user = User("mapred", action = "create", groups = ['1','2','3'], shell = "/bin/bash") popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -G 1,2,3 -s /bin/bash mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None) self.assertEqual(popen_mock.call_count, 1)
def install(self, env): import params user_home = "/home/" + str(params.flume_user) try: pwd.getpwnam(params.flume_user) except KeyError: User(params.flume_user, home=user_home, shell="/bin/bash", ignore_failures=True) self.install_packages(env) env.set_params(params)
def slave(): import params Logger.info("Creating user: {0}:{1}".format(params.elastic_user, params.elastic_group)) User(params.elastic_user, action="create", groups=params.elastic_group) params.path_data = params.path_data.replace('"', '') data_path = params.path_data.replace(' ', '').split(',') data_path[:] = [x.replace('"', '') for x in data_path] directories = [params.log_dir, params.pid_dir, params.conf_dir] directories = directories + data_path Logger.info("Creating directories: {0}".format(directories)) Directory(directories, create_parents=True, mode=0755, owner=params.elastic_user, group=params.elastic_group, cd_access="a") File("{0}/elastic-env.sh".format(params.conf_dir), owner=params.elastic_user, content=InlineTemplate(params.elastic_env_sh_template)) elastic_site = params.config['configurations']['elastic-site'] path = "{0}/elasticsearch.yml".format(params.conf_dir) Logger.info("Creating ES slave configuration.") File(path, content=Template("elasticsearch.slave.yaml.j2", configurations=elastic_site), owner=params.elastic_user, group=params.elastic_group) Logger.info("Slave sysconfig: /etc/sysconfig/elasticsearch") File(format("/etc/sysconfig/elasticsearch"), owner="root", group="root", content=InlineTemplate(params.sysconfig_template)) elastic_env = params.config['configurations']['elastic-env'] Logger.info("Slave PAM limits: {0}".format(params.limits_conf_file)) File(params.limits_conf_file, content=Template('elasticsearch_limits.conf.j2', configurations=elastic_env), owner="root", group="root")
def test_missing_shell_argument(self, getpwnam_mock, popen_mock): subproc_mock = MagicMock() subproc_mock.returncode = 0 subproc_mock.stdout = subproc_stdout popen_mock.return_value = subproc_mock getpwnam_mock.return_value = None with Environment('/') as env: user = User("mapred", action="create") popen_mock.assert_called_with([ '/bin/bash', '--login', '--noprofile', '-c', "ambari-sudo.sh PATH=/bin -H -E useradd -m mapred" ], shell=False, preexec_fn=preexec_fn, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None, close_fds=True) self.assertEqual(popen_mock.call_count, 1)
def elastic(): import params User(params.elastic_user, action="create", groups=params.elastic_group) params.path_data = params.path_data.replace('"', '') data_path = params.path_data.replace(' ', '').split(',') data_path[:] = [x.replace('"', '') for x in data_path] directories = [params.log_dir, params.pid_dir, params.conf_dir] directories = directories + data_path + [ "{0}/scripts".format(params.conf_dir) ] Directory(directories, create_parents=True, mode=0755, owner=params.elastic_user, group=params.elastic_group) print "Master env: " "{0}/elastic-env.sh".format(params.conf_dir) File("{0}/elastic-env.sh".format(params.conf_dir), owner=params.elastic_user, group=params.elastic_group, content=InlineTemplate(params.elastic_env_sh_template)) configurations = params.config['configurations']['elastic-site'] print "Master yml: " "{0}/elasticsearch.yml".format(params.conf_dir) File("{0}/elasticsearch.yml".format(params.conf_dir), content=Template("elasticsearch.master.yaml.j2", configurations=configurations), owner=params.elastic_user, group=params.elastic_group) print "Master sysconfig: /etc/sysconfig/elasticsearch" File("/etc/sysconfig/elasticsearch", owner="root", group="root", content=InlineTemplate(params.sysconfig_template))
def create_user(params): """ Creates the user required for RabbitMQ. """ Logger.info("Creating user={0} in group={1}".format(params.rabbitmq_user, params.rabbitmq_group)) User(params.rabbitmq_user, action="create", groups=params.rabbitmq_group)