Class: OneBackupJobHelper

Inherits:
OpenNebulaHelper::OneHelper show all
Defined in:
lib/one_helper/onebackupjob_helper.rb

Overview

Helper for onebackupjob command

Constant Summary collapse

TEMPLATE_OPTIONS =
[
    {
        :name => 'name',
        :large => '--name name',
        :format => String,
        :description => 'Name of the new backup job'
    },
    {
        :name => 'description',
        :large => '--description description',
        :format => String,
        :description => 'Description for the new Image'
    },
    {
        :name => 'backup_vms',
        :large => '--vms "vm_id1,vm_id2..."',
        :format => String,
        :description => 'List of VM IDs to backup'
    },
    {
        :name => 'keep_last',
        :large => '--keep N',
        :format => String,
        :description => 'Keep N backups for the VMs in this backup job'
    },
    {
        :name => 'mode',
        :large => '--mode <increment|full>',
        :format => String,
        :description => 'Backup mode'
    }
]

Instance Attribute Summary

Attributes inherited from OpenNebulaHelper::OneHelper

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenNebulaHelper::OneHelper

#backup_mode_valid?, #check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, list_layout_help, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #show_resource, #start_pager, #stop_pager, table_conf, template_input_help, #to_id, to_id_desc, #user_name

Constructor Details

This class inherits a constructor from OpenNebulaHelper::OneHelper

Class Method Details

.conf_fileObject



26
27
28
# File 'lib/one_helper/onebackupjob_helper.rb', line 26

def self.conf_file
    'onebackupjob.yaml'
end

.create_backupjob_template(options) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/one_helper/onebackupjob_helper.rb', line 178

def self.create_backupjob_template(options)
    template_options = TEMPLATE_OPTIONS.map do |o|
        o[:name].to_sym
    end

    template_options << :name

    t = ''
    template_options.each do |n|
        t << "#{n.to_s.upcase}=\"#{options[n]}\"\n" if options[n]
    end

    t
end

.rnameObject



22
23
24
# File 'lib/one_helper/onebackupjob_helper.rb', line 22

def self.rname
    'BACKUPJOB'
end

Instance Method Details

#format_pool(options) ⇒ Object



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
# File 'lib/one_helper/onebackupjob_helper.rb', line 63

def format_pool(options)
    config_file = self.class.table_conf

    CLIHelper::ShowTable.new(config_file, self) do
        column :ID, 'ONE identifier for the Backup Job', :size=>4 do |d|
            d['ID']
        end

        column :USER, 'Username of the Backup Job owner', :left,
               :size=>15 do |d|
            helper.user_name(d, options)
        end

        column :GROUP, 'Group of the Backup Job', :left, :size=>15 do |d|
            helper.group_name(d, options)
        end

        column :PRIO, 'Priority of the Backup Job', :left, :size=>4 do |d|
            d['PRIORITY'].to_i
        end

        column :NAME, 'Date for the last backup operation', :left, :size=>15 do |d|
            d['NAME']
        end

        column :LAST, 'Date for the last backup operation', :size => 15 do |d|
            begin
                btime = d['LAST_BACKUP_TIME'].to_i
            rescue StandardError
                btime = 0
            end
            OpenNebulaHelper.time_to_str(btime, false, true, true)
        end

        column :VMS, 'VM IDs part of this backup job', :size => 15 do |d|
            begin
                vm = d['TEMPLATE']['BACKUP_VMS']
                vm[12..-1]='...' if vm.size > 15
                vm
            rescue StandardError
                '-'
            end
        end

        default :ID, :USER, :GROUP, :PRIO, :NAME, :LAST, :VMS
    end
end

#schedule_actions(ids, options, warning = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/one_helper/onebackupjob_helper.rb', line 111

def schedule_actions(ids, options, warning = nil)
    # Verbose by default
    options[:verbose] = true

    message = if options[:schedule].class == Integer
                  "backup scheduled at #{Time.at(options[:schedule])}"
              else
                  "backup scheduled after #{options[:schedule]}s"
              end

    tmp_str = OpenNebulaHelper.schedule_action_tmpl(options, nil, warning)

    perform_actions(ids, options, message) do |bj|
        rc = bj.sched_action_add(tmp_str)

        if OpenNebula.is_error?(rc)
            STDERR.puts rc.message
            exit(-1)
        end
    end
end

#update_schedule_action(id, action_id, file, options) ⇒ Object

Update schedule action

Parameters:

  • id (Integer)

    BackupJob ID

  • action_id (Integer)

    Sched action ID

  • file (String)

    File path with update content

  • options


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
# File 'lib/one_helper/onebackupjob_helper.rb', line 139

def update_schedule_action(id, action_id, file, options)
    perform_action(id, options, 'Sched action updated') do |bj|
        rc = bj.info

        if OpenNebula.is_error?(rc)
            STDERR.puts "Error #{rc.message}"
            exit(-1)
        end

        xpath = "TEMPLATE/SCHED_ACTION[ID=#{action_id}]"

        unless bj.retrieve_elements(xpath)
            STDERR.puts "Sched action #{action_id} not found"
            exit(-1)
        end

        # Get user information
        if file
            str = File.read(file)
        elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
            str = stdin
        else
            str = OpenNebulaHelper.update_template(id, bj, nil, xpath)
        end

        # Add the modified sched action
        tmp_str = "\nSCHED_ACTION = ["
        tmp_str << str.split("\n").join(',')
        tmp_str << ']'

        rc = bj.sched_action_update(action_id, tmp_str)

        if OpenNebula.is_error?(rc)
            STDERR.puts "Error updating: #{rc.message}"
            exit(-1)
        end
    end
end