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 8.47 KB
Newer Older
1
2
//================================================================================
// Name        : dcdbslurmjob.cpp
Micha Müller's avatar
Micha Müller committed
3
// Author      : Michael Ott, Micha Mueller
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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.
//================================================================================

Micha Müller's avatar
Micha Müller committed
27
#include "../../common/include/globalconfiguration.h"
28
#include "timestamp.h"
Micha Müller's avatar
Micha Müller committed
29
30
31
32
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <cstdlib>
Alessio Netti's avatar
Alessio Netti committed
33
#include <dcdb/jobdatastore.h>
Micha Müller's avatar
Micha Müller committed
34
35
#include <iostream>
#include <mosquitto.h>
Alessio Netti's avatar
Alessio Netti committed
36
37
38
39
40
41

/*
 * Print usage information
 */
void usage() {
    std::cout << "Usage:" << std::endl;
Micha Müller's avatar
Micha Müller committed
42
    std::cout << "  dcdbslurmjob [-b<host>] [-t<timestamp>] [-n<nodelist>] [-j<jobid>] [-i<userid>] start|stop" << std::endl;
Alessio Netti's avatar
Alessio Netti committed
43
44
45
46
    std::cout << "  dcdbslurmjob -h" << std::endl;
    std::cout << std::endl;

    std::cout << "Options:" << std::endl;
Micha Müller's avatar
Micha Müller committed
47
    std::cout << "  -b<host>      MQTT broker              [default: 127.0.0.1:1883]" << std::endl;
Alessio Netti's avatar
Alessio Netti committed
48
49
50
51
52
53
54
55
    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;
}
56
57
58
59

std::string getEnv(const char* var) {
    char* str = std::getenv(var);
    if (str != NULL) {
Alessio Netti's avatar
Alessio Netti committed
60
	    return std::string(str);
61
    } else {
Alessio Netti's avatar
Alessio Netti committed
62
63
64
65
66
67
68
69
70
71
72
	    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);
73
74
75
    }
}

Micha Müller's avatar
Micha Müller committed
76
77
78
79
80
/**
 * Retrieves Slurm job data from environment variables and pushes them to the
 * specified CollectAgent. Alternatively, job data can be passed as command line
 * options.
 */
81
82
int main(int argc, char** argv) {
    std::cout << "dcdbslurmjob " << VERSION << std::endl << std::endl;
Micha Müller's avatar
Micha Müller committed
83
84
    std::string brokerHost = "127.0.0.1";
    int         brokerPort = 1883;
Alessio Netti's avatar
Alessio Netti committed
85
86
    std::string nodelist="", jobId="", userId="";
    uint64_t ts=0;
87
    
Alessio Netti's avatar
Alessio Netti committed
88
    // Defining options
Micha Müller's avatar
Micha Müller committed
89
    const char *opts = "b:n:t:j:i:h";
Alessio Netti's avatar
Alessio Netti committed
90
91
92
93
94
95
96
97
98
99
100

    char ret;
    while ((ret = getopt(argc, argv, opts))!=-1) {
        switch (ret)
        {
            case 'h':
                usage();
                return 0;
            default:
                break;
        }
101
102
    }
    
Alessio Netti's avatar
Alessio Netti committed
103
104
105
106
107
108
    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;
109
110
    }
    
Alessio Netti's avatar
Alessio Netti committed
111
112
113
    optind = 1;
    while ((ret=getopt(argc, argv, opts))!=-1) {
        switch(ret) {
Micha Müller's avatar
Micha Müller committed
114
115
116
117
118
119
120
121
122
123
124
		case 'b': {
			brokerHost = parseNetworkHost(optarg);
			std::string port = parseNetworkPort(optarg);
			if (port != "") {
				brokerPort = std::stoi(port);
			} else {
				brokerPort = 1883;
			}
			break;
		}
	    case 'n':
Alessio Netti's avatar
Alessio Netti committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
                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;
        }
    }

Micha Müller's avatar
Micha Müller committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    //Initialize Mosquitto library and connect to broker
    struct mosquitto *_mosq;
    char              hostname[256];

    if (gethostname(hostname, 255) != 0) {
	    std::cerr << "Cannot get hostname!";
	    return 1;
    }
    hostname[255] = '\0';
    mosquitto_lib_init();
    _mosq = mosquitto_new(hostname, false, NULL);
    if (!_mosq) {
	    perror(NULL);
	    return 1;
    }

    if (mosquitto_connect(_mosq, brokerHost.c_str(), brokerPort, 1000) != MOSQ_ERR_SUCCESS) {
	    std::cerr << "Could not connect to MQTT broker " << brokerHost << ":" << std::to_string(brokerPort);
	    return 1;
Alessio Netti's avatar
Alessio Netti committed
162
    }
Micha Müller's avatar
Micha Müller committed
163
164

    //collect job data
Alessio Netti's avatar
Alessio Netti committed
165
    DCDB::JobData jd;
Micha Müller's avatar
Micha Müller committed
166
167
    int           retCode = 0;

Alessio Netti's avatar
Alessio Netti committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    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 {
189
190
            jd.jobId     = jobId;
            jd.userId    = userId;
Alessio Netti's avatar
Alessio Netti committed
191
192
193
194
195
            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!";
Micha Müller's avatar
Micha Müller committed
196
197
198
	    retCode = 1;
	    goto exit;
	}
Alessio Netti's avatar
Alessio Netti committed
199
200
201
202
203
204
    } else if (boost::iequals(argv[argc-1], "stop")) {
        
        std::cout << "JOBID = " << jobId << std::endl;
        std::cout << "STOP  = " << ts << std::endl;
        
        try {
Micha Müller's avatar
Micha Müller committed
205
206
207
		jd.jobId = jobId;
		jd.endTime = DCDB::TimeStamp(ts);
	} catch(const std::invalid_argument& e) {
Alessio Netti's avatar
Alessio Netti committed
208
            std::cerr << "Invalid input format!";
Micha Müller's avatar
Micha Müller committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
	    retCode = 1;
	    goto exit;
	}
    }

    {
	    //create job data string in JSON format
	    std::string                 payload = "";
	    std::string                 topic = "/DCDB_JOBDATA/"; //do not change or keep in sync with simplemqttservermessage.h
	    boost::property_tree::ptree config;
	    std::ostringstream          output;
	    config.clear();
	    config.push_back(boost::property_tree::ptree::value_type("jobid", boost::property_tree::ptree(jd.jobId)));
	    config.push_back(boost::property_tree::ptree::value_type("userid", boost::property_tree::ptree(jd.userId)));
	    config.push_back(boost::property_tree::ptree::value_type("starttime", boost::property_tree::ptree(std::to_string(jd.startTime.getRaw()))));
	    config.push_back(boost::property_tree::ptree::value_type("endtime", boost::property_tree::ptree(std::to_string(jd.endTime.getRaw()))));
	    boost::property_tree::ptree nodes;
	    for (const auto &n : jd.nodes) {
		    nodes.push_back(boost::property_tree::ptree::value_type("", boost::property_tree::ptree(n)));
	    }
	    config.push_back(boost::property_tree::ptree::value_type("nodes", nodes));
	    boost::property_tree::write_json(output, config, true);
	    payload = output.str();

	    //std::cout << "Payload:\n" << payload << std::endl;

	    //send it to broker
	    if (mosquitto_publish(_mosq, NULL, topic.c_str(), payload.length(), payload.c_str(), 1, false) != MOSQ_ERR_SUCCESS) {
		    std::cerr << "Broker not reachable! Job data was not published.";
		    retCode = 1;
		    goto exit;
	    }

	    if (mosquitto_loop(_mosq, -1, 1) != MOSQ_ERR_SUCCESS) {
		    std::cerr << "Error in mosquitto_loop!";
		    retCode = 1;
		    goto exit;
	    }
Alessio Netti's avatar
Alessio Netti committed
247
248
    }

Micha Müller's avatar
Micha Müller committed
249
250
251
252
253
254
//hasta la vista
exit:
	mosquitto_disconnect(_mosq);
	mosquitto_destroy(_mosq);
	mosquitto_lib_cleanup();
	return retCode;
255
}