Skip to content

Commit

Permalink
implement tgctl version
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Sep 27, 2023
1 parent 6738abb commit ce19b4f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ file(GLOB TGCTL_SOURCES
"tateyama/process/*.cpp"
"tateyama/configuration/*.cpp"
"tateyama/utils/*.cpp"
"tateyama/version/*.cpp"
)

set_source_files_properties(
Expand Down
41 changes: 26 additions & 15 deletions src/tateyama/process/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,6 @@ tgctl::return_code tgctl_start(const std::string& argv0, bool need_check, tateya
}
}

std::string server_name(server_name_string);
boost::filesystem::path path_for_this{};
if (auto a0f = boost::filesystem::path(argv0); a0f.parent_path().string().empty()) {
path_for_this = boost::filesystem::canonical(boost::process::search_path(a0f));
} else{
path_for_this = boost::filesystem::canonical(a0f);
}
if (!boost::filesystem::exists(path_for_this)) {
std::cerr << "cannot find " << server_name_string << std::endl;
return tgctl::return_code::err;
}

int shm_id = shmget(IPC_PRIVATE, data_size, IPC_CREAT|0666); // NOLINT
// Shared memory create a new with IPC_CREATE
if (shm_id == -1) {
Expand All @@ -154,9 +142,17 @@ tgctl::return_code tgctl_start(const std::string& argv0, bool need_check, tateya
}
*static_cast<pid_t*>(shm_data) = 0;

boost::filesystem::path base_path{};
try {
base_path = get_base_path(argv0);
} catch (std::runtime_error &e) {
std::cerr << e.what() << std::endl;
return tgctl::return_code::err;
}

if (fork() == 0) {
auto base = boost::filesystem::canonical(path_for_this).parent_path().parent_path();
auto exec = base / boost::filesystem::path("libexec") / boost::filesystem::path(server_name);
std::string server_name(server_name_string);
auto exec = base_path / boost::filesystem::path("libexec") / boost::filesystem::path(server_name);
std::vector<std::string> args{};
build_args(args, mode);
boost::process::child cld(exec, boost::process::args (args));
Expand All @@ -165,7 +161,7 @@ tgctl::return_code tgctl_start(const std::string& argv0, bool need_check, tateya
cld.detach();

std::string undertaker_name(undertaker_name_string);
auto undertaker = base / boost::filesystem::path("libexec") / boost::filesystem::path(undertaker_name);
auto undertaker = base_path / boost::filesystem::path("libexec") / boost::filesystem::path(undertaker_name);
execl(undertaker.string().c_str(), undertaker.string().c_str(), std::to_string(child_pid).c_str(), nullptr); // NOLINT
exit(-1); // should not reach here
}
Expand Down Expand Up @@ -604,4 +600,19 @@ tgctl::return_code tgctl_pid() {
}
}

boost::filesystem::path get_base_path(const std::string& argv0) {
boost::filesystem::path path_for_this{};
if (auto a0f = boost::filesystem::path(argv0); a0f.parent_path().string().empty()) {
path_for_this = boost::filesystem::canonical(boost::process::search_path(a0f));
} else{
path_for_this = boost::filesystem::canonical(a0f);
}
if (!boost::filesystem::exists(path_for_this)) {
std::stringstream ss;
ss << "cannot find " << server_name_string << std::endl;
throw std::runtime_error(ss.str());
}
return boost::filesystem::canonical(path_for_this).parent_path().parent_path();
}

} // tateyama::process
3 changes: 3 additions & 0 deletions src/tateyama/process/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
#pragma once

#include <boost/filesystem/path.hpp>

#include <tateyama/framework/boot_mode.h>

#include "tateyama/configuration/bootstrap_configuration.h"
Expand All @@ -29,5 +31,6 @@ namespace tateyama::process {
tgctl::return_code tgctl_shutdown_kill(bool force, bool status_output = true);
tgctl::return_code tgctl_diagnostic();
tgctl::return_code tgctl_pid();
boost::filesystem::path get_base_path(const std::string& argv0);

} // tateyama::process
4 changes: 4 additions & 0 deletions src/tateyama/tgctl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "tateyama/process/process.h"
#include "tateyama/datastore/backup.h"
#include "tateyama/configuration/bootstrap_configuration.h"
#include "tateyama/version/version.h"

// common
DEFINE_string(conf, "", "the file name of the configuration"); // NOLINT
Expand Down Expand Up @@ -111,6 +112,9 @@ int tgctl_main(const std::vector<std::string>& args) {
if (args.at(1) == "quiesce") {
return tateyama::process::tgctl_start(args.at(0), true, tateyama::framework::boot_mode::quiescent_server);
}
if (args.at(1) == "version") {
return tateyama::version::show_version(args.at(0));
}
std::cerr << "unknown command '" << args.at(1) << "'" << std::endl;
return -1;
}
Expand Down
64 changes: 64 additions & 0 deletions src/tateyama/version/version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2019-2023 tsurugi project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>

#include <boost/property_tree/ptree.hpp>
#define BOOST_BIND_GLOBAL_PLACEHOLDERS // to retain the current behavior
#include <boost/property_tree/json_parser.hpp>
#include <boost/filesystem/operations.hpp>

#include "tateyama/process/process.h"

namespace tateyama::version {

constexpr std::string_view info_file_name_string = "tsurugi-info.json";

tgctl::return_code show_version(const std::string& argv0)
{
auto base_path = tateyama::process::get_base_path(argv0);
std::string info_file_name(info_file_name_string);
auto info_file_path = base_path / boost::filesystem::path("lib") / boost::filesystem::path(info_file_name);

if (boost::filesystem::exists(info_file_path) && !boost::filesystem::is_directory(info_file_path)) {
boost::property_tree::ptree pt;
try {
boost::property_tree::read_json(info_file_path.string(), pt);

auto name_opt = pt.get_optional<std::string>("name");
auto version_opt = pt.get_optional<std::string>("version");
auto date_opt = pt.get_optional<std::string>("date");

if (name_opt && version_opt && date_opt) {
// name
std::cout << name_opt.value() << std::endl;
// version
std::cout << "version: " << version_opt.value() << std::endl;
// date
std::cout << "date: " << date_opt.value() << std::endl;
return tgctl::return_code::ok;
}
std::cerr << info_file_path.string() << " is incorrect" << std::endl;
return tgctl::return_code::err;
} catch (boost::property_tree::json_parser_error &e) {
std::cerr << "parse error for " << info_file_path.string() << ":" << e.what() << std::endl;
}
return tgctl::return_code::err;
}
std::cerr << "can't find " << info_file_path.string() << std::endl;
return tgctl::return_code::err;
}

} // namespace tateyama::version
24 changes: 24 additions & 0 deletions src/tateyama/version/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2022-2023 tsurugi project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "tateyama/tgctl/tgctl.h"

namespace tateyama::version {

tgctl::return_code show_version(const std::string& argv0);

} // tateyama::process

0 comments on commit ce19b4f

Please sign in to comment.