Statistics
| Branch: | Tag: | Revision:

one / src / cli / onegroup @ ae53d437

History | View | Annotate | Download (4.34 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
ONE_LOCATION=ENV["ONE_LOCATION"]
20

    
21
if !ONE_LOCATION
22
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
23
else
24
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
25
end
26

    
27
$: << RUBY_LIB_LOCATION
28

    
29

    
30
require 'OpenNebula'
31
require 'client_utilities'
32
require 'command_parse'
33

    
34

    
35
ShowTableUP={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for group",
39
        :size => 4,
40
        :proc => lambda {|d,e|
41
            d.id
42
        }
43
    },
44
    :uid => {
45
        :name => "OWNER",
46
        :desc => "Owner of the group",
47
        :size => 5,
48
        :proc => lambda {|d,e|
49
            d.uid
50
        }
51
    },
52
    :name => {
53
        :name => "NAME",
54
        :desc => "Name of the group",
55
        :size => 16,
56
        :proc => lambda {|d,e|
57
            d.name
58
        }
59
    },
60

    
61
    :default => [:id, :uid, :name]
62
}
63

    
64
class UPShow
65
    def initialize
66
        @grouppool=OpenNebula::GroupPool.new(get_one_client)
67
        @table=ShowTable.new(ShowTableUP)
68
    end
69

    
70
    def header_up_small
71
        scr_bold
72
        scr_underline
73
        print @table.header_str
74
        scr_restore
75
        puts ""
76
    end
77

    
78
    def list_short(options=nil)
79
        res=@grouppool.info
80
        if options
81
            @table.columns=options[:columns] if options[:columns]
82
        end
83

    
84
        if OpenNebula.is_error?(res)
85
            result=res
86
        else
87
            result=res
88
            header_up_small
89

    
90
            puts @table.data_str(@grouppool, options)
91
            result
92
        end
93
    end
94
end
95

    
96
class OneUPParse < CommandParse
97

    
98
    COMMANDS_HELP=<<-EOT
99

    
100
Description:
101

    
102
This command enables the OpenNebula administrator to manage groups.
103

    
104

    
105
Commands:
106

    
107
* create (Creates a new group)
108
    onegroup create groupname
109

    
110
* delete (Removes a group)
111
    onegroup delete <id>
112

    
113
* list (Lists all the groups in the pool)
114
    onegroup list
115

    
116
EOT
117

    
118
    def text_commands
119
        COMMANDS_HELP
120
    end
121

    
122
    def text_command_name
123
        "onegroup"
124
    end
125

    
126
    def list_options
127
        table=ShowTable.new(ShowTableUP)
128
        table.print_help
129
    end
130

    
131
end
132

    
133
oneup_opts=OneUPParse.new([:list, :xml])
134
oneup_opts.parse(ARGV)
135
ops=oneup_opts.options
136

    
137
result=[false, "Unknown error"]
138

    
139
command=ARGV.shift
140

    
141
case command
142
when "create"
143
    check_parameters("create", 1)
144
    group=OpenNebula::Group.new(OpenNebula::Group.build_xml, get_one_client)
145
    result=group.allocate(ARGV[0])
146

    
147
    if is_successful?(result)
148
        puts "ID: " + group.id.to_s if ops[:verbose]
149
        exit 0
150
    end
151

    
152
when "delete"
153
    check_parameters("delete", 1)
154
    args=expand_args(ARGV)
155

    
156
    args.each do |param|
157
        group_id=get_group_id(param)
158
        group=OpenNebula::Group.new(
159
            OpenNebula::Group.build_xml(group_id), get_one_client)
160
        result=group.delete
161
        if !OpenNebula.is_error?(result)
162
            puts "Group deleted" if ops[:verbose]
163
            break
164
        end
165
    end
166

    
167
when "list"
168
    if !ops[:xml]
169
        uplist=UPShow.new
170
        ops[:columns]=ops[:list] if ops[:list]
171
        result=uplist.list_short(ops)
172
    else
173
        grouppool=OpenNebula::GroupPool.new(get_one_client)
174
        grouppool.info
175
        puts grouppool.to_xml(true)
176
    end
177

    
178
else
179
    oneup_opts.print_help
180
    exit -1
181
end
182

    
183
if OpenNebula.is_error?(result)
184
    puts "Error: " + result.message
185
    exit -1
186
end