From: Adam Stokes <astokes(a)fedoraproject.org>
- Update config and console agents to call methods on agents raising the events. Since we
control both ends (broker<->agent) we give our agent instance name something unique.
In this case our config agent creates a name of "config" in order for us to
programatically pull the agent address when wanting to call a certain method
("configure").
- The configure method for the agent allows passing an argument (url) which can be anytype
of protocol because at this time we only record the url in our "mh_configured"
file. At this point this file does basically 2 things, 1) tells the broker we've
already been configured, 2) tell the config agent where to pull down our configuration
rules. (puppet, cfengine, etc)
- We are leaving the toplevel "console" directory and keeping our agent-console
names directly related, i.e. we have a matahari-qmf-config daemon so our console agent is
named "config".
---
src/CMakeLists.txt | 2 +
src/cmake/modules/FindQPID.cmake | 15 +++++
src/config/CMakeLists.txt | 24 ++++++++
src/config/config-qmf.cpp | 99 ++++++++++++++++++++++++++++++++
src/config/schema.xml | 15 +++++
src/console/CMakeLists.txt | 18 ++++++
src/console/config.cpp | 82 ++++++++++++++++++++++++++
src/include/matahari/mh_agent_config.h | 33 +++++++++++
src/lib/CMakeLists.txt | 6 ++-
src/lib/mh_agent_config.c | 47 +++++++++++++++
10 files changed, 340 insertions(+), 1 deletions(-)
create mode 100644 src/config/CMakeLists.txt
create mode 100644 src/config/config-qmf.cpp
create mode 100644 src/config/schema.xml
create mode 100644 src/console/CMakeLists.txt
create mode 100644 src/console/config.cpp
create mode 100644 src/include/matahari/mh_agent_config.h
create mode 100644 src/lib/mh_agent_config.c
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4762fba..f057d1a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -141,6 +141,8 @@ add_subdirectory(lib)
add_subdirectory(host)
add_subdirectory(network)
add_subdirectory(service)
+add_subdirectory(console)
+add_subdirectory(config)
### Installation
install(FILES ${SCHEMAS} DESTINATION share/matahari)
diff --git a/src/cmake/modules/FindQPID.cmake b/src/cmake/modules/FindQPID.cmake
index 4cb0e89..5fd6f10 100644
--- a/src/cmake/modules/FindQPID.cmake
+++ b/src/cmake/modules/FindQPID.cmake
@@ -15,6 +15,7 @@ if(MSVC)
else(MSVC)
find_library(QMF_LIBRARY qmf)
find_library(QMF2_LIBRARY qmf2)
+ find_library(QMFCONSOLE_LIBRARY qmfconsole)
find_library(QPIDCLIENT_LIBRARY qpidclient)
find_library(QPIDCOMMON_LIBRARY qpidcommon)
find_library(QPIDTYPES_LIBRARY qpidtypes)
@@ -29,6 +30,10 @@ else(MSVC)
set(QMF2_FOUND true)
endif(QMF2_LIBRARY)
+ if(QMFCONSOLE_LIBRARY)
+ set(QMFCONSOLE_FOUND true)
+ endif(QMFCONSOLE_LIBRARY)
+
if(QPIDCLIENT_LIBRARY)
set(QPIDCLIENT_FOUND true)
endif(QPIDCLIENT_LIBRARY)
@@ -66,6 +71,16 @@ else(MSVC)
endif(QPID_FIND_REQUIRED)
endif(QMF2_FOUND)
+ if(QMFCONSOLE_FOUND)
+ if(NOT QPID_FIND_QUIETLY)
+ message(STATUS "Found QMFCONSOLE: ${QMFCONSOLE_LIBRARY}")
+ endif(NOT QPID_FIND_QUIETLY)
+ else(QMFCONSOLE_FOUND)
+ if(QPID_FIND_REQUIRED)
+ message(FATAL_ERROR "Could not find QMFCONSOLE")
+ endif(QPID_FIND_REQUIRED)
+ endif(QMFCONSOLE_FOUND)
+
if(QPIDCLIENT_FOUND)
if(NOT QPID_FIND_QUIETLY)
message(STATUS "Found QPIDCLIENT: ${QPIDCLIENT_LIBRARY}")
diff --git a/src/config/CMakeLists.txt b/src/config/CMakeLists.txt
new file mode 100644
index 0000000..cd97e3f
--- /dev/null
+++ b/src/config/CMakeLists.txt
@@ -0,0 +1,24 @@
+set(BASE "config")
+set(BASE_LIB "m${BASE}")
+set(QMF_AGENT "matahari-qmf-${BASE}d")
+
+# QMF daemon
+if(WITH-QMF)
+ set(SCHEMA_SOURCES
${CMAKE_CURRENT_BINARY_DIR}/qmf/org/matahariproject/QmfPackage.cpp)
+ generate_qmf_schemas(${CMAKE_CURRENT_SOURCE_DIR}/schema.xml ${SCHEMA_SOURCES})
+ include_directories(${CMAKE_CURRENT_BINARY_DIR})
+
+ add_executable(${QMF_AGENT} ${BASE}-qmf.cpp ${SCHEMA_SOURCES})
+ target_link_libraries(${QMF_AGENT} ${BASE_LIB} mcommon mcommon_qmf)
+
+ target_link_libraries(${QMF_AGENT} ${QPIDCOMMON_LIBRARY} ${QPIDCLIENT_LIBRARY}
${QPIDMESSAGING_LIBRARY} ${QMF2_LIBRARY})
+ if(QPIDTYPES_LIBRARY)
+ target_link_libraries(${QMF_AGENT} ${QPIDTYPES_LIBRARY})
+ endif(QPIDTYPES_LIBRARY)
+
+ if(WIN32)
+ target_link_libraries(${QMF_AGENT} wsock32)
+ endif(WIN32)
+
+ install(TARGETS ${QMF_AGENT} DESTINATION sbin)
+endif(WITH-QMF)
diff --git a/src/config/config-qmf.cpp b/src/config/config-qmf.cpp
new file mode 100644
index 0000000..ddbbb49
--- /dev/null
+++ b/src/config/config-qmf.cpp
@@ -0,0 +1,99 @@
+/* config-qmf.cpp - Copyright (C) 2011 Red Hat, Inc.
+ * Written by Adam Stokes <astokes(a)fedoraproject.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef WIN32
+#include "config.h"
+#endif
+
+#include "matahari/mh_agent.h"
+
+#include "qmf/org/matahariproject/QmfPackage.h"
+
+#include <qpid/agent/ManagementAgent.h>
+
+extern "C" {
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include <glib/gprintf.h>
+#include "matahari/mh_agent_config.h"
+#include "matahari/logging.h"
+#include "matahari/network.h"
+#include "matahari/host.h"
+#include <sigar.h>
+#include <sigar_format.h>
+};
+
+class ConfigAgent : public MatahariAgent
+{
+private:
+ qmf::org::matahariproject::PackageDefinition _package;
+public:
+ virtual int setup(qmf::AgentSession session);
+ virtual gboolean invoke(qmf::AgentSession session, qmf::AgentEvent event,
+ gpointer user_data);
+};
+
+int
+main(int argc, char **argv)
+{
+ ConfigAgent agent;
+ int rc = agent.init(argc, argv, "Config");
+ if (rc == 0) {
+ agent.run();
+ }
+ return rc;
+}
+
+int
+ConfigAgent::setup(qmf::AgentSession session)
+{
+ _package.configure(session);
+ _instance = qmf::Data(_package.data_Config);
+
+ _instance.setProperty("hostname", mh_hostname());
+ _instance.setProperty("uuid", mh_uuid());
+
+ _agent_session.addData(_instance, "config");
+ return 0;
+}
+
+gboolean
+ConfigAgent::invoke(qmf::AgentSession session, qmf::AgentEvent event, gpointer
user_data)
+{
+ if (event.getType() != qmf::AGENT_METHOD) {
+ session.methodSuccess(event);
+ return TRUE;
+ }
+
+ const std::string& methodName(event.getMethodName());
+
+ if (methodName == "configure") {
+ int rc = mh_is_configured(
+ event.getArguments()["uri"].asString().c_str());
+ event.addReturnArgument("configured", rc);
+ } else {
+ session.raiseException(event, MH_NOT_IMPLEMENTED);
+ goto bail;
+ }
+
+ session.methodSuccess(event);
+
+bail:
+ return TRUE;
+}
diff --git a/src/config/schema.xml b/src/config/schema.xml
new file mode 100644
index 0000000..39b062c
--- /dev/null
+++ b/src/config/schema.xml
@@ -0,0 +1,15 @@
+<schema package="org.matahariproject">
+
+ <!-- Config API -->
+ <class name="Config">
+ <property name="uuid" type="sstr"
access="RO" desc="Host UUID" />
+ <property name="hostname" type="sstr"
access="RO" desc="Hostname" index="y"/>
+
+ <statistic name="qmf-gen-no-crash" type="absTime"
desc="Dummy stat to stop qmf-gen from crashing." />
+
+ <method name="configure" desc="Send configure
method">
+ <arg name="uri" dir="I"
type="sstr" />
+ <arg name="configured" dir="O"
type="uint32" />
+ </method>
+ </class>
+</schema>
diff --git a/src/console/CMakeLists.txt b/src/console/CMakeLists.txt
new file mode 100644
index 0000000..b0f847a
--- /dev/null
+++ b/src/console/CMakeLists.txt
@@ -0,0 +1,18 @@
+set(CONSOLE_CONFIG_QMF "matahari-console-configd")
+
+# QMF daemon
+if(WITH-QMF)
+ add_executable(${CONSOLE_CONFIG_QMF} config.cpp)
+ target_link_libraries(${CONSOLE_CONFIG_QMF} mcommon mcommon_qmf)
+
+ target_link_libraries(${CONSOLE_CONFIG_QMF} ${QPIDCOMMON_LIBRARY}
${QPIDCLIENT_LIBRARY} ${QPIDMESSAGING_LIBRARY} ${QMF2_LIBRARY})
+ if(QPIDTYPES_LIBRARY)
+ target_link_libraries(${CONSOLE_CONFIG_QMF} ${QPIDTYPES_LIBRARY})
+ endif(QPIDTYPES_LIBRARY)
+
+ if(WIN32)
+ target_link_libraries(${CONSOLE_CONFIG_QMF} wsock32)
+ endif(WIN32)
+
+ install(TARGETS ${CONSOLE_CONFIG_QMF} DESTINATION sbin)
+endif(WITH-QMF)
diff --git a/src/console/config.cpp b/src/console/config.cpp
new file mode 100644
index 0000000..11a6c1f
--- /dev/null
+++ b/src/console/config.cpp
@@ -0,0 +1,82 @@
+/* config.cpp - Copyright (c) 2011 Red Hat, Inc.
+ * Written by Adam Stokes <astokes(a)fedoraproject.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef WIN32
+#include "config.h"
+#endif
+
+#include <qpid/messaging/Connection.h>
+#include <qpid/messaging/Duration.h>
+#include <qmf/ConsoleSession.h>
+#include <qmf/ConsoleEvent.h>
+#include <qmf/Agent.h>
+#include <qmf/Data.h>
+#include <qmf/DataAddr.h>
+#include <qmf/Query.h>
+#include <qpid/types/Variant.h>
+
+#include <string>
+#include <iostream>
+
+using namespace std;
+using namespace qmf;
+using qpid::types::Variant;
+using qpid::messaging::Duration;
+
+int main(int argc, char** argv)
+{
+ string brokerUrl("amqp:tcp:127.0.0.1:49000");
+ qpid::types::Variant::Map connectionOptions;
+ qpid::types::Variant::Map dataProperties;
+ string sessionOptions;
+
+ connectionOptions["reconnect"] = true;
+
+ dataProperties["uri"] = argv[1] ? argv[1] :
"http://localhost/config/uuid";
+
+ qpid::messaging::Connection connection(brokerUrl, connectionOptions);
+ connection.open();
+
+ ConsoleSession session(connection, sessionOptions);
+ session.open();
+
+ // Only filter connecting agents under
matahariproject.org vendor
+ session.setAgentFilter("[eq, _vendor, [quote,
'matahariproject.org']]]");
+ Agent agent;
+ while (true) {
+ ConsoleEvent event;
+ if(session.nextEvent(event)) {
+ switch(event.getType()) {
+ case CONSOLE_AGENT_ADD:
+ {
+ if (session.getAgentCount() == 1) {
+ agent = session.getAgent(0);
+ DataAddr agent_event_addr("config",
agent.getName(), 0);
+ ConsoleEvent result(agent.callMethod("configure",
+ dataProperties,
+ agent_event_addr));
+ cout << result.getArguments() << endl;
+ }
+ }
+ break;
+ default: {}
+ }
+ }
+ }
+ return 0;
+}
diff --git a/src/include/matahari/mh_agent_config.h
b/src/include/matahari/mh_agent_config.h
new file mode 100644
index 0000000..fe87e27
--- /dev/null
+++ b/src/include/matahari/mh_agent_config.h
@@ -0,0 +1,33 @@
+/* host.h - Copyright (C) 2009 Red Hat, Inc.
+ * Written by Arjun Roy <arroy(a)redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * \file
+ * \brief Config API
+ * \ingroup coreapi
+ */
+
+#ifndef __MH_CONFIG_H__
+#define __MH_CONFIG_H__
+
+#include <stdint.h>
+#include <stdlib.h>
+
+extern uint32_t mh_is_configured(const char *uri);
+
+#endif // __MH_CONFIG_H__
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 2538517..38485d3 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -1,6 +1,6 @@
# Library
-set(LIBS mcommon mhost mnetwork mservice)
+set(LIBS mcommon mhost mnetwork mservice mconfig)
include_directories(${pcre_INCLUDE_DIRS})
add_library (mcommon SHARED utilities.c mainloop.c)
@@ -19,6 +19,10 @@ add_library (mservice SHARED services.c services_${VARIANT}.c)
set_target_properties(mservice PROPERTIES SOVERSION 0.0.1)
target_link_libraries(mservice ${pcre_LIBRARIES} mcommon ${SIGAR} ${glib_LIBRARIES})
+add_library (mconfig SHARED mh_agent_config.c)
+set_target_properties(mconfig PROPERTIES SOVERSION 0.0.1)
+target_link_libraries(mconfig ${pcre_LIBRARIES} mcommon ${glib_LIBRARIES})
+
if(WIN32)
foreach (lib ${LIBS})
target_link_libraries(${lib} wsock32)
diff --git a/src/lib/mh_agent_config.c b/src/lib/mh_agent_config.c
new file mode 100644
index 0000000..34282c5
--- /dev/null
+++ b/src/lib/mh_agent_config.c
@@ -0,0 +1,47 @@
+/* mh_agent_config.c - Copyright (C) 2011 Red Hat, Inc.
+ * Written by Adam Stokes <astokes(a)fedoraproject.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef WIN32
+#include "config.h"
+#endif
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <glib.h>
+#include <glib/gprintf.h>
+#include "matahari/mh_agent_config.h"
+#include "matahari/logging.h"
+
+MH_TRACE_INIT_DATA(mh_config);
+
+uint32_t mh_is_configured(const char *uri)
+{
+ int config_file_exist = 0;
+ const char *filename = "/tmp/.mh_configured";
+
+ if(!g_file_test(filename, G_FILE_TEST_EXISTS)) {
+ if(!g_file_set_contents(filename, uri, -1, NULL)) {
+ mh_log(LOG_DEBUG, "Unable to create file.");
+ }
+ config_file_exist = 1;
+ } else {
+ config_file_exist = 1;
+ }
+ return config_file_exist;
+}
--
1.7.4.4