CleanerSession.hpp 7.59 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        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 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program 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.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
18
19

#pragma once

Victor Kotlyar's avatar
Victor Kotlyar committed
20
21
#include "common/log/LogContext.hpp"
#include "common/log/Logger.hpp"
22
#include "common/processCap/ProcessCap.hpp"
23
#include "mediachanger/MediaChangerFacade.hpp"
24
#include "Session.hpp"
25
#include "tapeserver/daemon/TpconfigLine.hpp"
26
27
28
#include "tapeserver/castor/tape/tapeserver/drive/DriveInterface.hpp"
#include "tapeserver/castor/tape/tapeserver/file/Structures.hpp"
#include "tapeserver/castor/tape/tapeserver/SCSI/Device.hpp"
29
#include "tapeserver/castor/tape/tapeserver/daemon/EncryptionControl.hpp"
30
#include "catalogue/Catalogue.hpp"
31
#include "scheduler/Scheduler.hpp"
32
33
34
35
36
37
38
39
40
41

#include <memory>

namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  /**
   * Class responsible for cleaning up a tape drive left in a (possibly) dirty state.
   */
42
  class CleanerSession : public Session {
43
44
45
46
47
    
  public:
    /**
     * Constructor
     * 
48
49
50
51
52
53
54
     * @param capUtils Object providing support for UNIX capabilities.
     * @param mc Object representing the media changer.
     * @param log Object representing the API to the CASTOR logging system.
     * @param driveConfig Configuration of the tape drive to be cleaned.
     * @param sysWrapper Object representing the operating system.
     * @param vid The volume identifier of the mounted tape if known,
     * else the empty string.
55
56
57
58
     * @param waitMediaInDrive true if we want to check the presence of the media in the drive before cleaning,
     * false otherwise.
     * @param waitMediaInDriveTimeout The maximum number of seconds to wait for
     * the media to be ready for operations inside the drive.
59
60
     * @param externalEncryptionKeyScript path to the operator provided script
     * for encryption control.
61
     * @param catalogue the CTA catalogue
62
63
     */
    CleanerSession(
64
      cta::server::ProcessCap &capUtils,
65
      cta::mediachanger::MediaChangerFacade &mc,
Victor Kotlyar's avatar
Victor Kotlyar committed
66
      cta::log::Logger &log,
67
      const cta::tape::daemon::TpconfigLine &driveConfig,
68
      System::virtualWrapper &sysWrapper,
69
      const std::string &vid,
70
      const bool waitMediaInDrive,
71
      const uint32_t waitMediaInDriveTimeout,
72
      const std::string & externalEncryptionKeyScript,
73
74
      cta::catalogue::Catalogue & catalogue,
      cta::Scheduler & scheduler);
75

76
77
78
79
80
81
    /** 
     * Execute the session and return the type of action to be performed
     * immediately after the session has completed.
     *
     * @return Returns the type of action to be performed after the session has
     * completed.
82
     */
83
    EndOfSessionAction execute() throw();
84
85
    
  private:
86

87
88
89
    /**
     * Object providing support for UNIX capabilities.
     */
90
    cta::server::ProcessCap &m_capUtils;
91

92
    /**
93
     * The object representing the media changer.
94
     */
95
    cta::mediachanger::MediaChangerFacade &m_mc;
96
97
98
99
    
    /**
     * The logging object     
     */
Victor Kotlyar's avatar
Victor Kotlyar committed
100
    cta::log::Logger & m_log;
101
102
103
104
    
    /**
     * The configuration of the tape drive to be cleaned.
     */
105
    const cta::tape::daemon::TpconfigLine m_driveConfig;
106
107
108
109
110
    
    /**
     * The system wrapper used to find the device and instantiate the drive object
     */
    System::virtualWrapper & m_sysWrapper;
111
112
113
114
115
116

    /**
     * The volume identifier of the mounted tape if known, else the empty
     * string.
     */
    const std::string m_vid;
117
118

    /**
119
120
121
122
123
124
125
126
     * true if we want to check the presence of the media in the drive before cleaning,
     * false otherwise.
     */    
    const bool m_waitMediaInDrive;
    
    /**
     * The maximum number of seconds to wait for
     * the media to be ready for operations inside the drive.
127
     */
128
    const uint32_t m_tapeLoadTimeout;
129

130
131
132
133
    /** 
     * Encryption helper object 
     */
    EncryptionControl m_encryptionControl;
134
135
136
137
138
139
    
    /**
     * CTA catalogue
     */
    cta::catalogue::Catalogue & m_catalogue;
    
140
141
142
143
144
    /**
     * CTA scheduler
     */
    cta::Scheduler & m_scheduler;
    
145
146
147
148
    /**
     * Variable used to log UPDATE_USER_NAME in the DB
     */
    const std::string c_defaultUserNameUpdate = "cta-taped";
149

150
151
152
153
154
155
156
    /** 
     * Execute the session and return the type of action to be performed
     * immediately after the session has completed.
     *
     * @return Returns the type of action to be performed after the session has
     * completed.
     */
157
    EndOfSessionAction exceptionThrowingExecute();
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    
    /**
     * Logs and clears (just by reading them...) any outstanding tape alerts
     *
     * @param drive The tape drive.
     */
    void logAndClearTapeAlerts(drive::DriveInterface &drive) throw();
    
    /**
     * Does the actual steps to clean the drive
     *
     * @param drive The tape drive.
     */
    void cleanDrive(drive::DriveInterface &drive);
172
173
174
175
176
177
178
179
180
181
182
183
184
185

    /**
     * Sets the capabilities of the process and logs the result.
     *
     * @param capabilities The string representation of the capabilities.
     */
    void setProcessCapabilities(const std::string &capabilities);

    /**
     * Creates and returns the object that represents the tape drive to be
     * cleaned.
     *
     * @return The tape drive.
     */
186
    std::unique_ptr<drive::DriveInterface> createDrive();
187
188
189
190
191
192

    /**
     * Waits for the specified drive to be ready.
     *
     * @param drive The tape drive.
     */
193
    void waitUntilMediaIsReady(drive::DriveInterface &drive);
194
195
196
197
198
199

    /**
     * Rewinds the specified tape drive.
     *
     * @param drive The tape drive.
     */
200
    void rewindDrive(drive::DriveInterface &drive);
201
202
203
204
205
206
207

    /**
     * Checks the tape in the specified tape drive contains some data where no
     * data means the tape does not even contain a volume label.
     *
     * @param drive The tape drive.
     */
208
    void checkTapeContainsData(drive::DriveInterface &drive);
209
210
211
212
213
214
215
216
217

    /**
     * Checks that the tape in the specified drive contains a valid volume
     * label.
     *
     * @param drive The tape drive for which it is assumed the tape to be
     * tested is present and rewound to the beginning.
     * @return The VSN stored within the colue label.
     */
218
    std::string checkVolumeLabel(drive::DriveInterface &drive);
219
220
221
222
223
224
225
226

    /**
     * Unloads the specified tape from the specified tape drive.
     *
     * @param vid The volume identifier of the tape to be unloaded.  Please note
     * that the value of this field is only used for logging purposes.
     * @param drive The tape drive.
     */
227
    void unloadTape(const std::string &vid, drive::DriveInterface &drive);
228
229
230
231
232
233
234

    /**
     * Dismounts the specified tape.
     *
     * @param vid The volume identifier of the tape to be dismounted.
     */
    void dismountTape(const std::string &vid);
235
    
236
237
238
239
240
    /**
     * Put the drive down in case the Cleaner has failed
     */
    void setDriveDownAfterCleanerFailed(const std::string & errorMsg);
    
241
242
243
244
245
  }; // class CleanerSession

} // namespace daemon
} // namespace tapeserver
} // namespace tape
246
} // namespace castor