Statistics
| Branch: | Tag: | Revision:

one / src / cli / onehost @ 8cd7d0df

History | View | Annotate | Download (8.47 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
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
    REMOTES_LOCATION="/var/lib/one/remotes/"
24
else
25
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
26
    REMOTES_LOCATION=ONE_LOCATION+"/var/remotes/"
27
end
28

    
29
$: << RUBY_LIB_LOCATION
30
$: << RUBY_LIB_LOCATION+"/cli"
31

    
32
require 'command_parser'
33
require 'one_helper/onehost_helper'
34
require 'one_helper/onecluster_helper'
35

    
36
cmd=CommandParser::CmdParser.new(ARGV) do
37
    usage "`onehost` <command> [<args>] [<options>]"
38
    version OpenNebulaHelper::ONE_VERSION
39

    
40
    helper = OneHostHelper.new
41

    
42
    before_proc do
43
        helper.set_client(options)
44
    end
45

    
46
    ########################################################################
47
    # Global Options
48
    ########################################################################
49
    cmd_options=CommandParser::OPTIONS-[CommandParser::VERBOSE]
50
    set :option, cmd_options+OpenNebulaHelper::CLIENT_OPTIONS
51

    
52
    IM = {
53
    :name   => "im",
54
    :short  => "-i im_mad",
55
    :large  => "--im im_mad" ,
56
    :description => "Set the information driver for the host",
57
    :format => String
58
    }
59

    
60
    VMM = {
61
    :name   => "vm",
62
    :short  => "-v vmm_mad",
63
    :large  => "--vm vmm_mad" ,
64
    :description => "Set the virtualization driver for the host",
65
    :format => String
66
    }
67

    
68
    FORCE = {
69
        :name   => "force",
70
        :large  => "--force" ,
71
        :description => "Force probe upgrade in onehost sync"
72
    }
73

    
74
    RSYNC = {
75
        :name   => "rsync",
76
        :large  => "--rsync" ,
77
        :description => "Use rsync to synchronize remotes. In case some " <<
78
            "probes are no longer in the fronted are deleted in the hosts." <<
79
            " rsync command must be installed in the frontend and nodes."
80
    }
81

    
82
    TYPE = {
83
        :name  => "type",
84
        :short => "-t remote_provider",
85
        :large => "--type remote_provider",
86
        :description => "Use type to create a host using Cloud Bursting ",
87
        :format => String
88
    }
89

    
90
    CREAT_OPTIONS = [ IM, VMM, OneClusterHelper::CLUSTER, TYPE ]
91
    SYNC_OPTIONS  = [ OneClusterHelper::CLUSTER, FORCE, RSYNC ]
92

    
93
    ########################################################################
94
    # Formatters for arguments
95
    ########################################################################
96

    
97
    set :format, :hostid, OneHostHelper.to_id_desc do |arg|
98
        helper.to_id(arg)
99
    end
100

    
101
    set :format, :hostid_list, OneHostHelper.list_to_id_desc do |arg|
102
        helper.list_to_id(arg)
103
    end
104

    
105
    ########################################################################
106
    # Commands
107
    ########################################################################
108

    
109
    create_desc = <<-EOT.unindent
110
        Creates a new Host
111
    EOT
112

    
113
    command :create, create_desc, :hostname, [:file, nil], :options=>CREAT_OPTIONS do
114
        if options[:im].nil? || options[:vm].nil?
115
            STDERR.puts "Drivers are mandatory to create a host:"
116
            STDERR.puts "\t -i information driver"
117
            STDERR.puts "\t -v hypervisor driver"
118
            exit -1
119
        end
120

    
121
        cid = options[:cluster] || ClusterPool::NONE_CLUSTER_ID
122
        helper.create_resource(options) do |host|
123
            if !options[:type].nil?
124
                str = helper.set_hybrid(options[:type], args[1])
125
            end
126
            rc = host.allocate(args[0], options[:im], options[:vm], cid)
127
            host.update(str, true) if !OpenNebula.is_error?(rc) && !options[:type].nil?
128

    
129
            rc
130
        end
131
    end
132

    
133
    delete_desc = <<-EOT.unindent
134
        Deletes the given Host
135
    EOT
136

    
137
    command :delete, delete_desc, [:range, :hostid_list] do
138
        helper.perform_actions(args[0],options,"deleted") do |host|
139
            host.delete
140
        end
141
    end
142

    
143
    enable_desc = <<-EOT.unindent
144
        Enables the given host, fully operational
145
    EOT
146

    
147
    command :enable, enable_desc, [:range,:hostid_list] do
148
        helper.perform_actions(args[0],options,"enabled") do |host|
149
            host.enable
150
        end
151
    end
152

    
153
    disable_desc = <<-EOT.unindent
154
        Disables the given host:
155
          - monitor: enabled
156
          - scheduler deployment: disabled
157
          - manual deployment: enabled
158
    EOT
159

    
160
    command :disable, disable_desc, [:range,:hostid_list] do
161
        helper.perform_actions(args[0],options,"disabled") do |host|
162
            host.disable
163
        end
164
    end
165

    
166
    offline_desc = <<-EOT.unindent
167
        Sets the host offline:
168
          - monitor: disabled
169
          - scheduler deployment: disabled
170
          - manual deployment: disabled
171
    EOT
172

    
173
    command :offline, offline_desc, [:range,:hostid_list] do
174
        helper.perform_actions(args[0],options,"offline") do |host|
175
            host.offline
176
        end
177
    end
178

    
179
    update_desc = <<-EOT.unindent
180
        Update the template contents. If a path is not provided the editor will
181
        be launched to modify the current content.
182
    EOT
183

    
184
    command :update, update_desc, :hostid, [:file, nil],
185
    :options=>OpenNebulaHelper::APPEND do
186
        helper.perform_action(args[0],options,"updated") do |obj|
187
            if options[:append]
188
                str = OpenNebulaHelper.append_template(args[0], obj, args[1])
189
            else
190
                str = OpenNebulaHelper.update_template(args[0], obj, args[1])
191
            end
192

    
193
            helper.set_client(options)
194
            obj = helper.retrieve_resource(obj.id)
195

    
196
            obj.update(str, options[:append])
197
        end
198
    end
199

    
200
    sync_desc = <<-EOT.unindent
201
        Synchronizes probes in /var/lib/one/remotes ($ONE_LOCATION/var/remotes
202
        in self-contained installations) with Hosts.
203
        Examples:
204
            onehost sync
205
            onehost sync -c myCluster
206
            onehost sync host01,host02,host03
207
    EOT
208

    
209
    command :sync, sync_desc, [:range, :hostid_list, nil],
210
            :options=>SYNC_OPTIONS do
211
        helper.sync(args[0], options)
212
    end
213

    
214
    list_desc = <<-EOT.unindent
215
        Lists Hosts in the pool
216
    EOT
217

    
218
    command :list, list_desc,
219
            :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS+
220
            [OpenNebulaHelper::DESCRIBE] do
221
        helper.list_pool(options)
222
    end
223

    
224
    show_desc = <<-EOT.unindent
225
        Shows information for the given Host
226
    EOT
227

    
228
    command :show, show_desc, :hostid,
229
            :options=>OpenNebulaHelper::XML do
230
        helper.show_resource(args[0],options)
231
    end
232

    
233
    top_desc = <<-EOT.unindent
234
        Lists Hosts continuously
235
    EOT
236

    
237
    command :top, top_desc,
238
            :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
239
        helper.list_pool(options, true)
240
    end
241

    
242
    flush_desc = <<-EOT.unindent
243
        Disables the host and reschedules all the running VMs in it.
244
    EOT
245

    
246
    command :flush, flush_desc, [:range,:hostid_list] do
247
        helper.perform_actions(args[0],options,"flush") do |host|
248
            host.flush
249
        end
250
    end
251

    
252
    rename_desc = <<-EOT.unindent
253
        Renames the Host
254
    EOT
255

    
256
    command :rename, rename_desc, :hostid, :name do
257
        helper.perform_action(args[0],options,"renamed") do |o|
258
            o.rename(args[1])
259
        end
260
    end
261

    
262
    importvm_desc = <<-EOT.unindent
263
        Import VM to OpenNebula
264
    EOT
265

    
266
    command :importvm, importvm_desc, :hostid, :name do
267
        helper.perform_action(args[0], options, "imported") do |o|
268
            rc = o.info
269
            next rc if OpenNebula.is_error?(rc)
270

    
271
            o.import_wild(args[1])
272
        end
273
    end
274
end