Class: VagrantPlugins::Command::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-arubacloud/command/snapshot.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



8
9
10
# File 'lib/vagrant-arubacloud/command/snapshot.rb', line 8

def self.synopsis
  'implement snapshot for vagrant-arubacloud '
end

Instance Method Details

#executeObject



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vagrant-arubacloud/command/snapshot.rb', line 11

def execute
  options = {:snapop => nil, :namevm => nil}
  myparser = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant snapshot  -t [create|delete|restore|list] -n name_server"
  opts.on('-n', '--name name', 'Name server of virtual machine') do |name|
		options[:namevm] = name;
 	    end
  opts.on('-t', '--type type', 'Type of snapshot : create|delete|restore|list') do |type|
		options[:snapop] = type;
 	    end
   opts.on('-h', '--help', 'Displays Help') do
	      puts opts
	      return
	    end
  end

  myparser.parse!

  if options[:namevm] == nil
	    puts 'Name of VM is missing'
    return 
  end
  if options[:snapop] == nil
	    puts "Type of action:  'create,delete,restore,list'  is missing"
    return 
  end
  ck1 = ["create", "delete", "restore", "list"].include? options[:snapop]
  if !ck1
	    puts 'Valid option type are:  create,delete,restore,list '
    return
  end

  not_found = true
  with_target_vms( nil, :provider => :arubacloud) do |machine|
    # if not machine.id....
    if machine.id == nil
       next
    end
    config1 = machine.provider_config
    #if no url in config....
    unless  config1.url 
      machine.ui.info(" [dc?] " +  I18n.t('vagrant_arubacloud.wrong_dc')) 
      return 
    end

    arubacloud_dc = config1.endpoint
    myprefix = "[#{arubacloud_dc}] "
    params = {
      :provider => :arubacloud,
      :arubacloud_username => config1.arubacloud_username,
      :arubacloud_password => config1.arubacloud_password,
      :url => config1.url
    }
    
    envx = Fog::Compute.new params
    server = envx.servers.get(machine.id)
    if server.name ==  options[:namevm]
    
      machine.ui.info(myprefix  +  I18n.t('vagrant_arubacloud.connect_to_dc'))
      machine.ui.info(myprefix + I18n.t('vagrant_arubacloud.snapshot_req_type') + " '#{options[:snapop]}'" +  " target id:#{machine.id}" )
      not_found = false
      case options[:snapop]
      # output if exist date creation and date expire for snapshot
      when "list"
        begin
          myreq = server.list_snapshot
          if myreq["Success"] 
            machine.ui.info(myprefix + 'snapshot ' + I18n.t('vagrant_arubacloud.snapshot_info_create') + "  #{myreq['credate']} " + I18n.t('vagrant_arubacloud.snapshot_info_expired') + "  #{myreq['expdate']} " )
          else
            machine.ui.warn(myprefix + I18n.t('vagrant_arubacloud.snapshot_info_not_found'))
          end 
        rescue Fog::ArubaCloud::Errors::RequestError => e
          message = ''
          error = nil
          @logger.debug(e.inspect.to_yaml)
          machine.ui.warn(myprefix + " *ERR* list response message: #{e.response.to_yaml}")
        end
      # creation of snapshot ....
      when "create"
        if server.state == Fog::ArubaCloud::Compute::Server::RUNNING
        #
        # if state active and request snapshot create ...
        #
          begin
            myreq = server.create_snapshot
            if myreq 
              machine.ui.info(myprefix + I18n.t('vagrant_arubacloud.snapshot_created'))
            else
              machine.ui.warn(myprefix + I18n.t('vagrant_arubacloud.snapshot_create_err_fog'))
            end 
          rescue Fog::ArubaCloud::Errors::RequestError => e
            message = ''
            error = nil
            @logger.debug(e.inspect.to_yaml)
            machine.ui.warn(myprefix + " *ERROR* response message: #{e.response.to_yaml}")
            #raise error
          end
        #  
        else
          machine.ui.warn(myprefix  + I18n.t('vagrant_arubacloud.snapshot_create_err_not_on'))
        end
      # delete of snapshot...
      when "delete"
        #
        # if snapshot delete ... 
        #  
        begin
          myreq = server.delete_snapshot
          if myreq 
            machine.ui.info(myprefix + I18n.t('vagrant_arubacloud.snapshot_deleted'))
          else
            machine.ui.warn(myprefix + I18n.t('vagrant_arubacloud.snapshot_delete_err_fog'))
          end 
        rescue Fog::ArubaCloud::Errors::RequestError => e
          message = ''
          error = nil
          @logger.debug(e.inspect.to_yaml)
          machine.ui.warn(myprefix + " *ERROR* response message: #{e.response.to_yaml}")
          #raise error
        end
      # if vm is power off and esiste a snapshot resorre vm from snapshot
      # (waring : the machine is not power on after restart and snapshot image is removed) 
      when "restore"
        if server.state == Fog::ArubaCloud::Compute::Server::STOPPED
          #
          # if state stopped (power off) and requets restore
          # WARNING : the snapshot file is removed after restore 
          #
          begin
            myreq = server.apply_snapshot
            if myreq 
              machine.ui.info(myprefix + I18n.t('vagrant_arubacloud.snapshot_restored'))
            else
              machine.ui.warn(myprefix + I18n.t('vagrant_arubacloud.snapshot_restore_err_fog'))
            end 
          rescue Fog::ArubaCloud::Errors::RequestError => e
            message = ''
            error = nil
            @logger.debug(e.inspect.to_yaml)
            machine.ui.warn(myprefix + " *ERROR* response message: #{e.response.to_yaml}")
            #raise error
          end
        else
          machine.ui.warn(myprefix  + I18n.t('vagrant_arubacloud.snapshot_restore_err_not_off'))
        end

      else
        machine.ui.warn(myprefix  + I18n.t('vagrant_arubacloud.snapshot_type_unknow') + " :#{options[:snapop]}" )
      end

    end  # test if found machine.id

  end #

  if not_found
    puts "==> ??? [dc?] " + I18n.t("vagrant_arubacloud.snapshot_server_unknow") + " :#{options[:namevm]}" 
  end  
  
end