Module: CubeCli

Defined in:
lib/cube_cli.rb,
lib/cube_cli/cli.rb,
lib/cube_cli/version.rb

Defined Under Namespace

Classes: CLI, Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.get(region, cube_id) ⇒ Object



36
37
38
39
40
# File 'lib/cube_cli.rb', line 36

def self.get(region, cube_id)
  @region = region
  @cube_id = cube_id
  old_pod
end

.old_podObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cube_cli.rb', line 52

def self.old_pod
  dict = {
    "Action" => 'GetCubePod',
    "CubeId" => @cube_id,
    "Region" => @region,
    "ProjectId" => ENV['UCLOUD_PROJECT_ID'],
    "PublicKey" => ENV['UCLOUD_PUBLIC_KEY']
  }
  dict["Signature"] = signature(dict)
  response = send_request(dict)
  pod = Base64.decode64 response["Pod"]
  YAML.load(pod)
end

.send_request(dict) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cube_cli.rb', line 67

def self.send_request(dict)
  uri = URI('https://api.ucloud.cn/')

  # Create client
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  body = JSON.dump(dict)

  # Create Request
  req =  Net::HTTP::Post.new(uri)
  # Add headers
  req.add_field "Content-Type", "application/json; charset=utf-8"
  # Set body
  req.body = body

  # Fetch Request
  res = http.request(req)
  # puts "Response HTTP Status Code: #{res.code}"
  # puts "Response HTTP Response Body: #{res.body}"
  return JSON.parse(res.body)
rescue StandardError => e
  puts "HTTP Request failed (#{e.message})"
end

.signature(dict) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/cube_cli.rb', line 43

def self.signature(dict)
  a = Hash[ dict.sort_by { |key, val| key } ]
  string = ""
  a.each do |key, value|
    string << key.to_s + value.to_s
  end
  Digest::SHA1.hexdigest(string + ENV['UCLOUD_PRIVATE_KEY'])
end

.update(region, cube_id, image, pod_name) ⇒ Object

Your code goes here…



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cube_cli.rb', line 13

def self.update(region, cube_id, image, pod_name)
  @region = region
  @cube_id = cube_id
  @image = image
  new_pod = old_pod
  new_pod["spec"]["containers"] = old_pod["spec"]["containers"].map do |container|
    container["image"] = image if container["name"] == pod_name
    container
  end

  dict = {
    "Action" => 'RenewCubePod',
    "CubeId" => @cube_id,
    "Region" => @region,
    "ProjectId" => ENV['UCLOUD_PROJECT_ID'],
    "PublicKey" => ENV['UCLOUD_PUBLIC_KEY'],
    "Pod" => Base64.encode64(new_pod.to_yaml).split("\n").join
  }
  dict["Signature"] = signature(dict)
  send_request(dict)
  # 'Update' + image
end