-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacewalk-api-custom2
executable file
·155 lines (118 loc) · 3.88 KB
/
spacewalk-api-custom2
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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
#!/usr/bin/perl
use strict;
#
# Copyright (c) 2009--2010 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#
use Data::Dumper;
use Getopt::Long;
use Frontier::Client;
use Term::ReadKey;
print "@ARGV\n";
#set defaults;
my ($server, $user, $password)= ('', '', '');
my $login=1;
my $usage = "Usage: $0 --server=<server> [--login] [--user=<user>] [--password=<password>] [--force]\n";
GetOptions("server=s" => \$server,
"login!" => \$login,
"user:s" => \$user,
"password:s" => \$password,
) or die $usage;
if (not $server) {
print "Error: No server specified.\n";
exit 1;
}
# read username if not given on command line
if ((not $user) and $login) {
print "Enter username: ";
$user = ReadLine 0;
chomp $user;
print "\n";
}
# read password if not given on command line
if ((not $password) and $login) {
print "Enter your password: ";
ReadMode 'noecho';
$password = ReadLine 0;
chomp $password;
ReadMode 'normal';
print "\n";
}
my $client = new Frontier::Client(url => "http://$server/rpc/api");
my $session;
if ($login) {
$session = $client->call('auth.login', $user, $password);
if (not $session) {
print "Error: Log in failed.\n";
exit 2;
}
}
my @params;
while (my $param = shift @ARGV) {
if ($param eq '%session%') {
$param = $session;
} elsif ( $param =~ '%file:(.*)%') {
open F, $1 or die "Error: Could not open $1";
$param = join('', <F>);
close F;
}
if ($param =~ /^\[/) {
print "detected array $param\n";
$param =~ s/[\[\]\"\']//g;
my @param = split(/,/, $param);
print "@param\n";
push @params, \@param;
print "@params\n";
} else {
push @params, $param;
}
}
print "@params\n";
my $data = $client->call(@params);
print Data::Dumper->Dump([$data], ["result"]);
if ($login) {
$client->call('auth.logout', $session);
}
=pod
=head1 NAME
spacewalk-api - Call Spacawalk API from command line.
=head1 SYNOPSIS
spacewalk-api [OPTIONS] --server=spacewalk.domain.com FUNCTION [PARAM1 PARAM2 ...]
=head1 DESCRIPTION
spacewalk-api interact with the Spacewalk server and expose its API layer.
FUNCTION is api method you wish to call and is followed by its parameters.
There is few special substitution available:
%session%
Is replaced with sessionid. If you use --nologin option, then it is replaced by empty string.
%file:/some/file%
Is replaced by content of given file.
=head1 OPTIONS
--server
URL of your Spacewalk server.
--login
--nologin
If we should log in or not. Default is to log in.
--user
Name of user to log in.
--password
If you do not specify this and unless --nologin is specified, you will be prompted for your password.
=head1 EXAMPLES
spacewalk-api --server=spacewalk.com --nologin api.systemVersion
spacewalk-api --server=spacewalk.com --user=foo --password=bar channel.listAllChannels "%session%"
spacewalk-api --server=spacewalk.com --nologin proxy.isProxy '%file:/etc/sysconfig/rhn/systemid%'
=head1 AUTHOR
Miroslav Suchý <[email protected]>
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2009 Red Hat, Inc.
Released under GNU General Public License, version 2 (GPLv2).
=cut