Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better user-group creating in postinst, BaseDaemon::getDefaultCorePath [#METR-23811] #295

Merged
merged 1 commit into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions dbms/src/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ static std::string getCanonicalPath(std::string && path)
return path;
}

std::string Server::getDefaultCorePath() const
{
return getCanonicalPath(config().getString("path")) + "cores";
}

int Server::main(const std::vector<std::string> & args)
{
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class Server : public BaseDaemon
private:

void attachSystemTables(const std::string & path, bool has_zookeeper) const;

std::string getDefaultCorePath() const override;
};

}
55 changes: 43 additions & 12 deletions debian/clickhouse-server-base.postinst
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
#!/bin/sh
set -e

if [ -x "/etc/init.d/clickhouse-server" ]; then
update-rc.d clickhouse-server defaults 19 19 >/dev/null || exit $?
fi
if [ "$1" = configure ]; then

if [ -x "/etc/init.d/clickhouse-server" ]; then
update-rc.d clickhouse-server defaults 19 19 >/dev/null || exit $?
fi

# Make sure the administrative user exists
if ! getent passwd metrika > /dev/null; then
adduser --system --disabled-login --no-create-home --home /nonexistent \
--shell /bin/false --group --gecos "Clickhouse server" metrika > /dev/null
fi

# if the user was created manually, make sure the group is there as well
if ! getent group metrika > /dev/null; then
addgroup --system metrika > /dev/null
fi

# make sure user is in the correct group
if ! id -Gn metrika | grep -qw metrika; then
adduser metrika metrika > /dev/null
fi

# check validity of user and group
if [ "`id -u metrika`" -eq 0 ]; then
echo "The metrika system user must not have uid 0 (root).
Please fix this and reinstall this package." >&2
exit 1
fi

if [ "`id -g metrika`" -eq 0 ]; then
echo "The metrika system user must not have root as primary group.
Please fix this and reinstall this package." >&2
exit 1
fi

useradd -p "" metrika ||:
if [ ! -d "/opt/clickhouse" ]; then
# ensure home directory ownership
mkdir -p /opt/clickhouse
#su -s /bin/sh metrika -c "test -O /opt/clickhouse && test -G /opt/clickhouse" || \
chown metrika:metrika /opt/clickhouse
chmod 0700 /opt/clickhouse
fi

if id metrika > /dev/null 2>&1; then
mkdir -p /opt/clickhouse/data/default/
mkdir -p /opt/clickhouse/metadata/default/
mkdir -p /opt/cores/
chown metrika: /opt/clickhouse /opt/cores /opt/clickhouse/data /opt/clickhouse/metadata /opt/clickhouse/data/default /opt/clickhouse/metadata/default
# Clean old dynamic compilation results
if [ -d "/opt/clickhouse/build" ]; then
rm -f /opt/clickhouse/build/*.cpp /opt/clickhouse/build/*.so ||:
fi
else
echo "User metrika doesn't exist."
exit 1

fi


exit 0
2 changes: 2 additions & 0 deletions libs/libdaemon/include/daemon/BaseDaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class BaseDaemon : public Poco::Util::ServerApplication
template <class Daemon>
static std::experimental::optional<std::reference_wrapper<Daemon>> tryGetInstance();

virtual std::string getDefaultCorePath() const;

std::unique_ptr<Poco::TaskManager> task_manager;

/// Создание и автоматическое удаление pid файла.
Expand Down
25 changes: 17 additions & 8 deletions libs/libdaemon/src/BaseDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ void BaseDaemon::closeLogs()
logger().warning("Logging to console but received signal to close log file (ignoring).");
}

std::string BaseDaemon::getDefaultCorePath () const
{
return "/opt/cores/";
}

void BaseDaemon::initialize(Application& self)
{
task_manager.reset(new Poco::TaskManager);
Expand Down Expand Up @@ -775,14 +780,18 @@ void BaseDaemon::initialize(Application& self)
* Делаем это после buildLoggers, чтобы не менять текущую директорию раньше.
* Это важно, если конфиги расположены в текущей директории.
*/
Poco::File opt_cores = "/opt/cores";

std::string core_path = config().getString("core_path",
opt_cores.exists() && opt_cores.isDirectory()
? "/opt/cores/"
: (!log_path.empty()
? log_path
: "/opt/"));

std::string core_path = config().getString("core_path", "");
if (core_path.empty())
core_path = getDefaultCorePath();
Poco::File(core_path).createDirectories();

Poco::File cores = core_path;
if (!( cores.exists() && cores.isDirectory() ))
{
core_path = !log_path.empty() ? log_path : "/opt/";
Poco::File(core_path).createDirectories();
}

if (0 != chdir(core_path.c_str()))
throw Poco::Exception("Cannot change directory to " + core_path);
Expand Down
2 changes: 1 addition & 1 deletion release_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function make_control {
do
case "$DAEMON_PKG" in
'clickhouse-server' )
add_daemon_impl clickhouse-server-base '' 'clickhouse-server binary'
add_daemon_impl clickhouse-server-base 'adduser' 'clickhouse-server binary'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read this just now:

useradd is a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead.

What if people are installing debian package on non-debian system (this way is used in practice)? Dependencies are cost too much... I am not sure that adduser gives enough comparing to useradd. BTW, I call these kind of programs "trash-scripts".

[ -n "$BUILD_PACKAGE_FOR_METRIKA" ] && add_daemon_impl clickhouse-server-metrika "clickhouse-server-base(=1.1.$REVISION)" 'Configuration files specific for Metrika project for clickhouse-server-base package'
add_daemon_impl clickhouse-server-common "clickhouse-server-base(=1.1.$REVISION)" 'Common configuration files for clickhouse-server-base package'
;;
Expand Down