Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

dcdbslurmjob.cpp 7.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//================================================================================
// Name        : dcdbslurmjob.cpp
// Author      : Michael Ott
// Copyright   : Leibniz Supercomputing Centre
// Description : Main file of the dcdbslurmjob command line utility
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2019 Leibniz Supercomputing Centre
//
// 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
// of the License, or (at your option) any later version.
//
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

#include <cstdlib>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include "timestamp.h"
Alessio Netti's avatar
Alessio Netti committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <dcdb/connection.h>
#include <dcdb/jobdatastore.h>
#include "../../common/include/globalconfiguration.h"

DCDB::Connection* dcdbConn;
DCDB::JobDataStore *myJobDataStore;

/*
 * Print usage information
 */
void usage() {
    /*
               1         2         3         4         5         6         7         8
     012345678901234567890123456789012345678901234567890123456789012345678901234567890
     */
    std::cout << "Usage:" << std::endl;
    std::cout << "  dcdbslurmjob [-c<host>] [-u<username>] [-p<password>] [-t<timestamp>] [-n<nodelist>] [-j<jobid>] [-i<userid>] start|stop" << std::endl;
    std::cout << "  dcdbslurmjob -h" << std::endl;
    std::cout << std::endl;

    std::cout << "Options:" << std::endl;
    std::cout << "  -c<host>      Cassandra host           [default: 127.0.0.1:9042]" << std::endl;
    std::cout << "  -u<username>  Cassandra username       [default: none]" << std::endl;
    std::cout << "  -p<password>  Cassandra password       [default: none]" << std::endl;
    std::cout << "  -t<timestamp> Timestamp value          [default: now]" << std::endl;
    std::cout << "  -n<nodelist>  Comma-separated nodelist [default: SLURM_JOB_NODELIST]" << std::endl;
    std::cout << "  -j<jobid>     Numerical job id         [default: SLURM_JOB_ID var]" << std::endl;
    std::cout << "  -i<userid>    Numerical user id        [default: SLURM_JOB_USER var]" << std::endl;
    std::cout << std::endl;
    std::cout << "  -h            This help page" << std::endl;
    std::cout << std::endl;
}
63
64
65
66

std::string getEnv(const char* var) {
    char* str = std::getenv(var);
    if (str != NULL) {
Alessio Netti's avatar
Alessio Netti committed
67
	    return std::string(str);
68
    } else {
Alessio Netti's avatar
Alessio Netti committed
69
70
71
72
73
74
75
76
77
78
79
	    return std::string("");
    }
}

void splitNodeList(const std::string& str, DCDB::NodeList& nl, char delim = ',')
{
    nl.clear();
    std::stringstream ss(str);
    std::string token;
    while (std::getline(ss, token, delim)) {
        nl.push_back(token);
80
81
82
83
84
    }
}

int main(int argc, char** argv) {
    std::cout << "dcdbslurmjob " << VERSION << std::endl << std::endl;
Alessio Netti's avatar
Alessio Netti committed
85
86
87
    std::string cassandraHost="127.0.0.1", cassandraPort="9042", cassandraUser="", cassandraPassword="";
    std::string nodelist="", jobId="", userId="";
    uint64_t ts=0;
88
    
Alessio Netti's avatar
Alessio Netti committed
89
90
91
92
93
94
95
96
97
98
99
100
101
    // Defining options
    const char* opts = "c:u:p:n:t:j:i:h";

    char ret;
    while ((ret = getopt(argc, argv, opts))!=-1) {
        switch (ret)
        {
            case 'h':
                usage();
                return 0;
            default:
                break;
        }
102
103
    }
    
Alessio Netti's avatar
Alessio Netti committed
104
105
106
107
108
109
    if (argc < 2) {
        std::cerr << "At least one argument is required: start or stop" << std::endl;
        return 1;
    } else if(!boost::iequals(argv[argc-1], "start") && !boost::iequals(argv[argc-1], "stop")) {
        std::cerr << "Unsupported action: must either be start or stop" << std::endl;
        return 1;
110
111
    }
    
Alessio Netti's avatar
Alessio Netti committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    optind = 1;
    while ((ret=getopt(argc, argv, opts))!=-1) {
        switch(ret) {
            case 'c':
                cassandraHost = parseNetworkHost(optarg);
                cassandraPort = parseNetworkPort(optarg);
                if(cassandraPort=="") cassandraPort = std::string("9042");
                break;
            case 'u':
                cassandraUser = optarg;
                break;
            case 'p': {
                cassandraPassword = optarg;
                // What does this do? Mask the password?
                size_t pwdLen = strlen(optarg);
                memset(optarg, 'x', (pwdLen >= 3) ? 3 : pwdLen);
                if (pwdLen > 3) {
                    memset(optarg+3, 0, pwdLen-3);
                }
                break;
            }
            case 'n':
                nodelist = optarg;
                break;
            case 't':
                ts = std::stoull(optarg);
                break;
            case 'j':
                jobId = optarg;
                break;
            case 'i':
                userId = optarg;
                break;
            case 'h':
            default:
                usage();
                return 1;
        }
    }

    //Allocate and initialize connection to Cassandra.
    dcdbConn = new DCDB::Connection(cassandraHost, atoi(cassandraPort.c_str()), cassandraUser, cassandraPassword);
    
    if (!dcdbConn->connect()) {
        std::cerr << "Cannot connect to Cassandra!";
        return 1;
    }
    myJobDataStore = new DCDB::JobDataStore(dcdbConn);
    DCDB::JobData jd;
    
    if(ts==0)
        ts = getTimestamp();
    if(jobId=="")
        jobId = getEnv("SLURM_JOB_ID");
    
    if (boost::iequals(argv[argc-1], "start")) {
        
        if(userId=="")
            userId = getEnv("SLURM_JOB_USER");
        if(nodelist=="")
            nodelist = getEnv("SLURM_JOB_NODELIST");
        
        std::cout << "JOBID = " << jobId << std::endl;
        std::cout << "USER  = " << userId << std::endl;
        std::cout << "START = " << ts << std::endl;
        std::cout << "NODES = " << nodelist << std::endl;
        
        DCDB::NodeList nl;
        splitNodeList(nodelist, nl, ',');
        
        try {
            jd.jobId     = std::stoul(jobId);
            jd.userId    = std::stoul(userId);
            jd.startTime = DCDB::TimeStamp(ts);
            jd.endTime   = DCDB::TimeStamp((uint64_t)0);
            jd.nodes     = nl;
        } catch(const std::invalid_argument& e) {
            std::cerr << "Invalid input format!";
            return 1;
        }
        if(myJobDataStore->insertJob(jd) != DCDB::JD_OK) {
            std::cerr << "Job data insert failed!";
            return 1;
        }
        
    } else if (boost::iequals(argv[argc-1], "stop")) {
        
        std::cout << "JOBID = " << jobId << std::endl;
        std::cout << "STOP  = " << ts << std::endl;
        
        try {
            if (myJobDataStore->getJobById(jd, std::stoul(jobId)) != DCDB::JD_OK) {
                std::cerr << "Could not retrieve job to be updated!";
                return 1;
            }
        } catch(const std::invalid_argument& e) {
            std::cerr << "Invalid input format!";
            return 1;
        }
        
        if(myJobDataStore->updateEndtime(jd.jobId, jd.startTime, DCDB::TimeStamp(ts)) != DCDB::JD_OK) {
            std::cerr << "Could not update end time of job!";
            return 1;
        }
        
    }

    delete myJobDataStore;
    dcdbConn->disconnect();
    delete dcdbConn;
222
223
    return 0;
}