Class: Fog::Compute::Openvz::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openvz/compute.rb,
lib/fog/openvz/requests/compute/set_server.rb,
lib/fog/openvz/requests/compute/exec_server.rb,
lib/fog/openvz/requests/compute/stop_server.rb,
lib/fog/openvz/requests/compute/exec2_server.rb,
lib/fog/openvz/requests/compute/list_servers.rb,
lib/fog/openvz/requests/compute/mount_server.rb,
lib/fog/openvz/requests/compute/start_server.rb,
lib/fog/openvz/requests/compute/create_server.rb,
lib/fog/openvz/requests/compute/resume_server.rb,
lib/fog/openvz/requests/compute/status_server.rb,
lib/fog/openvz/requests/compute/umount_server.rb,
lib/fog/openvz/requests/compute/compact_server.rb,
lib/fog/openvz/requests/compute/convert_server.rb,
lib/fog/openvz/requests/compute/destroy_server.rb,
lib/fog/openvz/requests/compute/quotaon_server.rb,
lib/fog/openvz/requests/compute/restart_server.rb,
lib/fog/openvz/requests/compute/suspend_server.rb,
lib/fog/openvz/requests/compute/quotaoff_server.rb,
lib/fog/openvz/requests/compute/snapshot_server.rb,
lib/fog/openvz/requests/compute/quotainit_server.rb,
lib/fog/openvz/requests/compute/runscript_server.rb,
lib/fog/openvz/requests/compute/get_server_details.rb,
lib/fog/openvz/requests/compute/snapshot_list_server.rb,
lib/fog/openvz/requests/compute/snapshot_mount_server.rb,
lib/fog/openvz/requests/compute/snapshot_delete_server.rb,
lib/fog/openvz/requests/compute/snapshot_switch_server.rb,
lib/fog/openvz/requests/compute/snapshot_umount_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



74
75
76
# File 'lib/fog/openvz/compute.rb', line 74

def initialize(options={})
  @openvz_connect_command = options[:openvz_connect_command]
end

Instance Method Details

#compact_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/compact_server.rb', line 6

def compact_server(id,options = {})
  vzctl("compact",{:ctid => id}.merge(options))
end

#convert_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/convert_server.rb', line 6

def convert_server(id,options = {})
  vzctl("convert",{:ctid => id}.merge(options))
end

#create_server(options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/create_server.rb', line 6

def create_server(options = {})
  vzctl("create",options)
end

#destroy_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/destroy_server.rb', line 6

def destroy_server(id, options = {})
  vzctl("destroy",{:ctid => id}.merge(options))
end

#exec2_server(id, args) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/exec2_server.rb', line 6

def exec2_server(id,args)
  vzctl("exec2",{:ctid => id},args)
end

#exec_server(id, args = []) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/exec_server.rb', line 6

def exec_server(id,args = [])
  vzctl("exec",{:ctid => id},args)
end

#expand_commands(commands, params, args) ⇒ Object



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
# File 'lib/fog/openvz/compute.rb', line 82

def expand_commands(commands, params, args)
  # For all params unless the ctid
  # pass it to the command
  params.keys.each do |k|
    if (params[k]) && (k.to_s != 'ctid')

      if params[k].is_a?(Array)
        # For arrays we pass the params and key multiple times
        params[k].each do |p|
          commands << "--#{k}"
          commands << "\"#{p}\""
        end
      else
        commands << "--#{k}"
        # For booleans only pass the options
        # We put the values of params between doublequotes
        commands << "\"#{params[k]}\"" unless !!params[k] == params[k]
      end

    end
  end

  # These commands will be passed directly
  args.each do |a|
    commands << a
  end

  # Delete empty commands
  commands.delete("")

  # Now build the full command
  full_command = "#{commands.join(' ')}"

  # If we have a connect command , expand it
  if @openvz_connect_command.nil?
    prefixed_command = "#{full_command}"
  else
    prefixed_command = @openvz_connect_command.sub('@command@',"#{full_command}")
  end
  return prefixed_command
end

#get_server_details(id) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/get_server_details.rb', line 6

def get_server_details(id)
  vzlist({:ctid => id}).first
end

#list_servers(options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/list_servers.rb', line 6

def list_servers(options = {})
  vzlist({})
end

#mount_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/mount_server.rb', line 6

def mount_server(id, options = {})
  vzctl("mount",{:ctid => id}.merge(options))
end

#quotainit_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/quotainit_server.rb', line 6

def quotainit_server(id, options = {})
  vzctl("quotainit",{:ctid => id}.merge(options))
end

#quotaon_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/quotaon_server.rb', line 6

def quotaon_server(id, options = {})
  vzctl("quotaon",{:ctid => id}.merge(options))
end

#quotaooff_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/quotaoff_server.rb', line 6

def quotaooff_server(id, options = {})
  vzctl("quotaoff",{:ctid => id}.merge(options))
end

#reloadObject



78
79
80
# File 'lib/fog/openvz/compute.rb', line 78

def reload
  #@connection.reset
end

#restart_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/restart_server.rb', line 6

def restart_server(id, options = {})
  vzctl("restart",{:ctid => id}.merge(options))
end

#resume_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/resume_server.rb', line 6

def resume_server(id, options = {})
  vzctl("resume",{:ctid => id}.merge(options))
end

#runscript_server(id, args = []) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/runscript_server.rb', line 6

def runscript_server(id,args = [])
  vzctl("runscript",{:ctid => id},args)
end

#set_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/set_server.rb', line 6

def set_server(id,options = {})
  vzctl("set",{:ctid => id}.merge(options))
end

#snapshot_delete_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/snapshot_delete_server.rb', line 6

def snapshot_delete_server(id,options = {})
  vzctl("snapshot-delete",{:ctid => id}.merge(options))
end

#snapshot_list_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/snapshot_list_server.rb', line 6

def snapshot_list_server(id,options = {})
  vzctl("snapshot-list",{:ctid => id}.merge(options))
end

#snapshot_mount_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/snapshot_mount_server.rb', line 6

def snapshot_mount_server(id,options = {})
  vzctl("snapshot-mount",{:ctid => id}.merge(options))
end

#snapshot_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/snapshot_server.rb', line 6

def snapshot_server(id,options = {})
  vzctl("snapshot",{:ctid => id}.merge(options))
end

#snapshot_switch_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/snapshot_switch_server.rb', line 6

def snapshot_switch_server(id,options = {})
  vzctl("snapshot-switch",{:ctid => id}.merge(options))
end

#snapshot_umount_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/snapshot_umount_server.rb', line 6

def snapshot_umount_server(id,options = {})
  vzctl("snapshot-umount",{:ctid => id}.merge(options))
end

#start_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/start_server.rb', line 6

def start_server(id,options={})
  vzctl("start",{:ctid => id}.merge(options))
end

#status_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/status_server.rb', line 6

def status_server(id, options = {})
  vzctl("status",{:ctid => id}.merge(options))
end

#stop_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/stop_server.rb', line 6

def stop_server(id, options = {})
  vzctl("stop",{:ctid => id}.merge(options))
end

#suspend_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/suspend_server.rb', line 6

def suspend_server(id, options = {})
  vzctl("suspend",{:ctid => id}.merge(options))
end

#umount_server(id, options = {}) ⇒ Object



6
7
8
# File 'lib/fog/openvz/requests/compute/umount_server.rb', line 6

def umount_server(id, options = {})
  vzctl("umount",{:ctid => id}.merge(options))
end

#vzctl(command, params, args = []) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fog/openvz/compute.rb', line 124

def vzctl(command, params,args = [])
  commands = [ 'vzctl', command, params['ctid'], params[:ctid] ]
  prefixed_command = expand_commands(commands, params, args)

  result = `#{prefixed_command}`
  exitcode = $?.to_i

  # Tofix - we use backticks to get the exitcode
  # But backticks output stderr
  arg_commands = [ 'exec', 'exec2', 'runscript' ]
  if (arg_commands.include?(command))
    return { :output => result , :exitcode => exitcode }
  else
    raise Fog::Errors::Error.new result unless exitcode == 0
    return result
  end
end

#vzlist(params, args = []) ⇒ Object



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
# File 'lib/fog/openvz/compute.rb', line 142

def vzlist(params,args = [])

  commands = [ 'vzlist', '-a', '-j' , params['ctid'], params[:ctid] ]
  prefixed_command = expand_commands(commands, params, args)

  # We do some wier stuff here:
  # - a simple backtick doesn't capture stderr
  # - popen4 would solve it but would require another external gem dependency
  # - popen3 doesn't capture exitcode (on ruby 1.8) but is a standard call
  # - so we resort to checking the stderr instead
  require 'open3'
  result = ""
  error = ""
  puts prefixed_command
  Open3.popen3("#{prefixed_command}") { |i,o,e,t|
    result = result + o.read
    error = error + e.read
  }

  if (error.length != 0)
    if(error.include?("not found"))
      return []
    else
      raise Fog::Errors::Error.new error
    end
  else
    return Fog::JSON.decode(result)
  end

end