12a8491ef41c170b4bbdd23971a66a4e89972871
[public/netxms.git] / src / libnxcl / package.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2004, 2005 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** $module: package.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Lock package database
29 //
30
31 DWORD LIBNXCL_EXPORTABLE NXCLockPackageDB(NXC_SESSION hSession)
32 {
33 return ((NXCL_Session *)hSession)->SimpleCommand(CMD_LOCK_PACKAGE_DB);
34 }
35
36
37 //
38 // Unlock package database
39 //
40
41 DWORD LIBNXCL_EXPORTABLE NXCUnlockPackageDB(NXC_SESSION hSession)
42 {
43 return ((NXCL_Session *)hSession)->SimpleCommand(CMD_UNLOCK_PACKAGE_DB);
44 }
45
46
47 //
48 // Retrieve package list from server
49 //
50
51 DWORD LIBNXCL_EXPORTABLE NXCGetPackageList(NXC_SESSION hSession, DWORD *pdwNumPackages,
52 NXC_PACKAGE_INFO **ppList)
53 {
54 CSCPMessage msg, *pResponse;
55 DWORD dwResult, dwRqId, dwPkgId;
56
57 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
58
59 msg.SetCode(CMD_GET_PACKAGE_LIST);
60 msg.SetId(dwRqId);
61 ((NXCL_Session *)hSession)->SendMsg(&msg);
62
63 *pdwNumPackages = 0;
64 *ppList = NULL;
65
66 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
67 if (dwResult == RCC_SUCCESS)
68 {
69 *pdwNumPackages = 0;
70 do
71 {
72 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_PACKAGE_INFO, dwRqId);
73 if (pResponse != NULL)
74 {
75 dwPkgId = pResponse->GetVariableLong(VID_PACKAGE_ID);
76 if (dwPkgId != 0)
77 {
78 *ppList = (NXC_PACKAGE_INFO *)realloc(*ppList, sizeof(NXC_PACKAGE_INFO) * (*pdwNumPackages + 1));
79 (*ppList)[*pdwNumPackages].dwId = dwPkgId;
80 pResponse->GetVariableStr(VID_PACKAGE_NAME,
81 (*ppList)[*pdwNumPackages].szName,
82 MAX_PACKAGE_NAME_LEN);
83 pResponse->GetVariableStr(VID_FILE_NAME,
84 (*ppList)[*pdwNumPackages].szFileName,
85 MAX_DB_STRING);
86 pResponse->GetVariableStr(VID_PLATFORM_NAME,
87 (*ppList)[*pdwNumPackages].szPlatform,
88 MAX_PLATFORM_NAME_LEN);
89 pResponse->GetVariableStr(VID_PACKAGE_VERSION,
90 (*ppList)[*pdwNumPackages].szVersion,
91 MAX_AGENT_VERSION_LEN);
92 pResponse->GetVariableStr(VID_DESCRIPTION,
93 (*ppList)[*pdwNumPackages].szDescription,
94 MAX_DB_STRING);
95 (*pdwNumPackages)++;
96 }
97 delete pResponse;
98 }
99 else
100 {
101 dwResult = RCC_TIMEOUT;
102 *pdwNumPackages = 0;
103 safe_free(*ppList);
104 *ppList = NULL;
105 break;
106 }
107 } while(dwPkgId != 0);
108 }
109
110 return dwResult;
111 }
112
113
114 //
115 // Remove package from server
116 //
117
118 DWORD LIBNXCL_EXPORTABLE NXCRemovePackage(NXC_SESSION hSession, DWORD dwPkgId)
119 {
120 CSCPMessage msg;
121 DWORD dwRqId;
122
123 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
124
125 msg.SetCode(CMD_REMOVE_PACKAGE);
126 msg.SetId(dwRqId);
127 msg.SetVariable(VID_PACKAGE_ID, dwPkgId);
128 ((NXCL_Session *)hSession)->SendMsg(&msg);
129
130 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
131 }
132
133
134 //
135 // Install package to server
136 //
137
138 DWORD LIBNXCL_EXPORTABLE NXCInstallPackage(NXC_SESSION hSession, NXC_PACKAGE_INFO *pInfo,
139 TCHAR *pszFullPkgPath)
140 {
141 CSCPMessage msg, *pResponse;
142 DWORD dwRqId, dwResult;
143
144 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
145
146 msg.SetCode(CMD_INSTALL_PACKAGE);
147 msg.SetId(dwRqId);
148 msg.SetVariable(VID_PACKAGE_NAME, pInfo->szName);
149 msg.SetVariable(VID_DESCRIPTION, pInfo->szDescription);
150 msg.SetVariable(VID_FILE_NAME, pInfo->szFileName);
151 msg.SetVariable(VID_PLATFORM_NAME, pInfo->szPlatform);
152 msg.SetVariable(VID_PACKAGE_VERSION, pInfo->szVersion);
153 ((NXCL_Session *)hSession)->SendMsg(&msg);
154
155 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
156 if (pResponse != NULL)
157 {
158 dwResult = pResponse->GetVariableLong(VID_RCC);
159 if (dwResult == RCC_SUCCESS)
160 {
161 // Get id assigned to installed package and
162 // update provided package information structure
163 pInfo->dwId = pResponse->GetVariableLong(VID_PACKAGE_ID);
164 }
165 delete pResponse;
166 }
167 else
168 {
169 dwResult = RCC_TIMEOUT;
170 }
171
172 // If everything is OK, send package file to server
173 if (dwResult == RCC_SUCCESS)
174 {
175 dwResult = ((NXCL_Session *)hSession)->SendFile(dwRqId, pszFullPkgPath);
176 if (dwResult == RCC_SUCCESS)
177 {
178 // Wait for final confirmation
179 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
180 }
181 }
182
183 return dwResult;
184 }
185
186
187 //
188 // Parse NPI file
189 //
190
191 DWORD LIBNXCL_EXPORTABLE NXCParseNPIFile(TCHAR *pszInfoFile, NXC_PACKAGE_INFO *pInfo)
192 {
193 FILE *fp;
194 DWORD dwResult = RCC_SUCCESS;
195 TCHAR szBuffer[256], szTag[256], *ptr;
196
197 fp = _tfopen(pszInfoFile, _T("r"));
198 if (fp != NULL)
199 {
200 while(!feof(fp))
201 {
202 if (_fgetts(szBuffer, 256, fp) == NULL)
203 break;
204 ptr = _tcschr(szBuffer, _T('\n'));
205 if (ptr != NULL)
206 *ptr = 0;
207 StrStrip(szBuffer);
208 if ((szBuffer[0] == _T('#')) || (szBuffer[0] == 0))
209 continue; // Empty line or comment
210
211 ptr = ExtractWord(szBuffer, szTag);
212 StrStrip(ptr);
213 _tcsupr(szTag);
214
215 if (!_tcscmp(szTag, _T("NAME")))
216 {
217 nx_strncpy(pInfo->szName, ptr, MAX_PACKAGE_NAME_LEN);
218 }
219 else if (!_tcscmp(szTag, _T("PLATFORM")))
220 {
221 nx_strncpy(pInfo->szPlatform, ptr, MAX_PLATFORM_NAME_LEN);
222 }
223 else if (!_tcscmp(szTag, _T("VERSION")))
224 {
225 nx_strncpy(pInfo->szVersion, ptr, MAX_AGENT_VERSION_LEN);
226 }
227 else if (!_tcscmp(szTag, _T("DESCRIPTION")))
228 {
229 nx_strncpy(pInfo->szDescription, ptr, MAX_DB_STRING);
230 }
231 else if (!_tcscmp(szTag, _T("FILE")))
232 {
233 nx_strncpy(pInfo->szFileName, GetCleanFileName(ptr), MAX_DB_STRING);
234 }
235 else
236 {
237 dwResult = RCC_NPI_PARSE_ERROR;
238 break;
239 }
240 }
241 fclose(fp);
242 }
243 else
244 {
245 dwResult = RCC_IO_ERROR;
246 }
247
248 return dwResult;
249 }
250
251
252 //
253 // Start package deployment
254 //
255
256 DWORD LIBNXCL_EXPORTABLE NXCDeployPackage(NXC_SESSION hSession, DWORD dwPkgId,
257 DWORD dwNumObjects, DWORD *pdwObjectList,
258 DWORD *pdwRqId)
259 {
260 CSCPMessage msg, *pInfo;
261 DWORD dwRqId, dwResult;
262 NXC_DEPLOYMENT_STATUS status;
263
264 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
265 *pdwRqId = dwRqId;
266
267 msg.SetCode(CMD_DEPLOY_PACKAGE);
268 msg.SetId(dwRqId);
269 msg.SetVariable(VID_PACKAGE_ID, dwPkgId);
270 msg.SetVariable(VID_NUM_OBJECTS, dwNumObjects);
271 msg.SetVariableToInt32Array(VID_OBJECT_LIST, dwNumObjects, pdwObjectList);
272 ((NXCL_Session *)hSession)->SendMsg(&msg);
273
274 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
275 if (dwResult == RCC_SUCCESS)
276 {
277 while(1)
278 {
279 // Wait up to 10 minutes for notification message from server
280 pInfo = ((NXCL_Session *)hSession)->WaitForMessage(CMD_INSTALLER_INFO, dwRqId, 600000);
281 if (pInfo != NULL)
282 {
283 status.dwStatus = pInfo->GetVariableShort(VID_DEPLOYMENT_STATUS);
284 if (status.dwStatus == DEPLOYMENT_STATUS_FINISHED)
285 {
286 delete pInfo;
287 break; // Deployment job finished
288 }
289
290 status.dwNodeId = pInfo->GetVariableLong(VID_OBJECT_ID);
291 status.pszErrorMessage = pInfo->GetVariableStr(VID_ERROR_MESSAGE);
292 ((NXCL_Session *)hSession)->callEventHandler(NXC_EVENT_DEPLOYMENT_STATUS, dwRqId, &status);
293 safe_free(status.pszErrorMessage);
294
295 delete pInfo;
296 }
297 else
298 {
299 dwResult = RCC_TIMEOUT;
300 break;
301 }
302 }
303 }
304
305 return dwResult;
306 }