1 /*  $Id: misc_cmds.cpp 553629 2017-12-19 17:13:23Z sadyrovr $
2  * ===========================================================================
3  *
4  *                            PUBLIC DOMAIN NOTICE
5  *               National Center for Biotechnology Information
6  *
7  *  This software/database is a "United States Government Work" under the
8  *  terms of the United States Copyright Act.  It was written as part of
9  *  the author's official duties as a United States Government employee and
10  *  thus cannot be copyrighted.  This software/database is freely available
11  *  to the public for use. The National Library of Medicine and the U.S.
12  *   Government have not placed any restriction on its use or reproduction.
13  *
14  *  Although all reasonable efforts have been taken to ensure the accuracy
15  *  and reliability of the software and data, the NLM and the U.S.
16  *  Government do not and cannot warrant the performance or results that
17  *  may be obtained by using this software or data. The NLM and the U.S.
18  *  Government disclaim all warranties, express or implied, including
19  *  warranties of performance, merchantability or fitness for any particular
20  *  purpose.
21  *
22  *  Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors:  Dmitry Kazimirov
27  *
28  * File Description: Miscellaneous commands of the grid_cli application
29  *                   (the ones that are not specific to NetCache, NetSchedule,
30  *                   or worker nodes).
31  *
32  */
33 
34 #include <ncbi_pch.hpp>
35 
36 #include "ns_cmd_impl.hpp"
37 #include "util.hpp"
38 
39 USING_NCBI_SCOPE;
40 
Cmd_WhatIs()41 int CGridCommandLineInterfaceApp::Cmd_WhatIs()
42 {
43     if (m_Opts.output_format == eJSON) {
44         CJsonNode result(g_WhatIs(m_Opts.id, m_CompoundIDPool));
45 
46         if (result) {
47             g_PrintJSON(stdout, result);
48             return 0;
49         }
50     } else {
51         try {
52             CNetStorageObjectLoc object_loc(m_CompoundIDPool, m_Opts.id);
53 
54             CJsonNode object_loc_info(CJsonNode::NewObjectNode());
55 
56             object_loc_info.SetString("type", TOKEN_TYPE__NETSTORAGEOBJECT_LOC);
57 
58             object_loc.ToJSON(object_loc_info);
59 
60             g_PrintJSON(stdout, object_loc_info);
61 
62             return 0;
63         }
64         catch (CCompoundIDException&) {
65         }
66         catch (CNetStorageException&) {
67         }
68 
69         CNetCacheKey nc_key;
70         CNetScheduleKey ns_key;
71 
72         if (CNetCacheKey::ParseBlobKey(m_Opts.id.c_str(),
73             m_Opts.id.length(), &nc_key, m_CompoundIDPool)) {
74             printf("type: " TOKEN_TYPE__NETCACHE_BLOB_KEY "\nkey_version: %u\n",
75                     nc_key.GetVersion());
76             PrintBlobMeta(nc_key);
77 
78             if (m_Opts.output_format == eHumanReadable)
79                 printf("\nTo retrieve blob attributes from the server, use\n"
80                         GRID_APP_NAME " blobinfo %s\n", m_Opts.id.c_str());
81             return 0;
82 
83         // Ignoring version 0 as it means any string with a leading digit
84         } else if (ns_key.ParseJobKey(m_Opts.id, m_CompoundIDPool) && ns_key.version) {
85             printf("type: " TOKEN_TYPE__NETSCHEDULE_JOB_KEY "\n"
86                     "key_version: %u\n",
87                     ns_key.version);
88 
89             CPrintJobInfo print_job_info;
90 
91             print_job_info.ProcessJobMeta(ns_key);
92 
93             printf("\nTo retrieve job attributes from the server, use\n"
94                     GRID_APP_NAME " jobinfo %s\n", m_Opts.id.c_str());
95             return 0;
96         }
97     }
98 
99     fprintf(stderr, "Unable to recognize the specified token.\n");
100     return 3;
101 }
102 
Cmd_Login()103 int CGridCommandLineInterfaceApp::Cmd_Login()
104 {
105     if (m_Opts.app_uid.empty()) {
106         fputs("Application UID cannot be empty.\n", stderr);
107         return 2;
108     }
109     if (m_Opts.app_uid == DEFAULT_APP_UID) {
110         fputs("Application UID '" DEFAULT_APP_UID
111                 "' is reserved and cannot be used.\n", stderr);
112         return 2;
113     }
114 
115     CCompoundID cid(m_CompoundIDPool.NewID(eCIC_GenericID));
116 
117     cid.AppendLabel(LOGIN_TOKEN_APP_UID_FIELD);
118     cid.AppendString(m_Opts.app_uid);
119 
120     if (IsOptionSet(eAuth)) {
121         cid.AppendLabel(LOGIN_TOKEN_AUTH_FIELD);
122         cid.AppendString(m_Opts.auth);
123     }
124 
125     cid.AppendLabel(LOGIN_TOKEN_USER_FIELD);
126     cid.AppendString(GetDiagContext().GetUsername());
127     cid.AppendLabel(LOGIN_TOKEN_HOST_FIELD);
128     cid.AppendHost(GetDiagContext().GetHost());
129 
130     if (IsOptionSet(eNetCache)) {
131         cid.AppendLabel(LOGIN_TOKEN_NETCACHE_FIELD);
132         cid.AppendServiceName(m_Opts.nc_service);
133     }
134     if (IsOptionSet(eCache)) {
135         cid.AppendLabel(LOGIN_TOKEN_ICACHE_NAME_FIELD);
136         cid.AppendDatabaseName(m_Opts.cache_name);
137     }
138     if (IsOptionSet(eEnableMirroring)) {
139         cid.AppendLabel(LOGIN_TOKEN_ENABLE_MIRRORING);
140         cid.AppendBoolean(true);
141     }
142 
143     if (IsOptionSet(eNetSchedule)) {
144         cid.AppendLabel(LOGIN_TOKEN_NETSCHEDULE_FIELD);
145         cid.AppendServiceName(m_Opts.ns_service);
146     }
147     if (IsOptionSet(eQueue)) {
148         cid.AppendLabel(LOGIN_TOKEN_QUEUE_FIELD);
149         cid.AppendDatabaseName(m_Opts.queue);
150     }
151 
152     cid.AppendLabel(LOGIN_TOKEN_SESSION_PID_FIELD);
153     cid.AppendID(CDiagContext::GetPID());
154 
155     cid.AppendLabel(LOGIN_TOKEN_SESSION_TIMESTAMP_FIELD);
156     cid.AppendCurrentTime();
157 
158     cid.AppendLabel(LOGIN_TOKEN_SESSION_UID_FIELD);
159     cid.AppendString(GetDiagContext().GetStringUID());
160 
161 #ifdef NCBI_GRID_XSITE_CONN_SUPPORT
162     if (IsOptionSet(eAllowXSiteConn)) {
163         cid.AppendLabel(LOGIN_TOKEN_ALLOW_XSITE_CONN);
164         cid.AppendBoolean(true);
165     }
166 #endif
167 
168     if (IsOptionSet(eNoConnRetries)) {
169         cid.AppendLabel(LOGIN_TOKEN_NO_CONN_RETRIES);
170         cid.AppendBoolean(true);
171     }
172 
173     if (IsOptionSet(eFileTrackSite)) {
174         cid.AppendLabel(LOGIN_TOKEN_FILETRACK_SITE);
175         cid.AppendString(m_Opts.ft_site);
176     }
177     if (IsOptionSet(eFileTrackToken)) {
178         cid.AppendLabel(LOGIN_TOKEN_FILETRACK_TOKEN);
179         cid.AppendString(m_Opts.ft_token);
180     }
181 
182     PrintLine(cid.ToString());
183 
184     return 0;
185 }
186