Class: EasyManager::Scaleway::Servers

Inherits:
Object
  • Object
show all
Defined in:
lib/easymanager/scaleway/servers.rb

Overview

Class Method Summary collapse

Class Method Details

.action(scw, srv_id, action) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/easymanager/scaleway/servers.rb', line 90

def self.action(scw, srv_id, action)
  data = { action: action }
  Typhoeus.post(
    File.join(scw.api_url, "/instance/v1/zones/#{scw.zone}/servers/#{srv_id}/action"),
    headers: scw.headers,
    body: data.to_json
  )
end

.add_cloud_init(scw, srv_id, cloud_init) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/easymanager/scaleway/servers.rb', line 79

def self.add_cloud_init(scw, srv_id, cloud_init)
  data = Utilities.file_read(cloud_init)
  return if data.nil?

  Typhoeus.patch(
    File.join(scw.api_url, "/instance/v1/zones/#{scw.zone}/servers/#{srv_id}/user_data/cloud-init"),
    headers: { 'X-Auth-Token' => scw.secret_token, 'Content-Type' => 'text/plain' },
    body: data
  )
end

.create(scw, srv_type, image, name_pattern, tags, cloud_init) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/easymanager/scaleway/servers.rb', line 52

def self.create(scw, srv_type, image, name_pattern, tags, cloud_init)
  data = srv_data(scw, srv_type, image, name_pattern, tags)
  return if data.nil?

  response = Typhoeus.post(File.join(scw.api_url, "/instance/v1/zones/#{scw.zone}/servers/"),
                           headers: scw.headers, body: data.to_json)
  return unless response&.code == 201

  body_json = Utilities.parse_json(response.body)
  return unless body_json

  srv_id = body_json['server']['id']
  launch(scw, srv_id, cloud_init)

  body_json['server']
end

.delete(scw, srv_id, srv_ip_id) ⇒ Object



69
70
71
72
# File 'lib/easymanager/scaleway/servers.rb', line 69

def self.delete(scw, srv_id, srv_ip_id)
  Ips.delete(scw, srv_ip_id)
  action(scw, srv_id, 'terminate')
end

.launch(scw, srv_id, cloud_init) ⇒ Object



74
75
76
77
# File 'lib/easymanager/scaleway/servers.rb', line 74

def self.launch(scw, srv_id, cloud_init)
  add_cloud_init(scw, srv_id, cloud_init) if cloud_init
  action(scw, srv_id, 'poweron')
end

.list(scw) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/easymanager/scaleway/servers.rb', line 11

def self.list(scw)
  response = Typhoeus.get(
    File.join(scw.api_url, "instance/v1/zones/#{scw.zone}/servers"),
    headers: scw.headers
  )
  return unless response&.code == 200

  json_body = Utilities.parse_json(response.body)
  return unless json_body

  json_body['servers']
end

.ready?(scw, srv, ssh, cmds) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'lib/easymanager/scaleway/servers.rb', line 28

def self.ready?(scw, srv, ssh, cmds)
  return unless srv_up?(scw, srv)

  cmd_values = SSH.cmd_exec(ssh, srv, cmds)
  return unless cmd_values
  return if cmd_values[cmds[0]].empty?

  cmd_values[cmds[1]].include?('The system is finally up') ||
    cmd_values[cmds[1]].match?(/Cloud-init v.*finished\sat.*Up.*seconds/)
end

.srv_data(scw, srv_type, image, name_pattern, tags) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/easymanager/scaleway/servers.rb', line 99

def self.srv_data(scw, srv_type, image, name_pattern, tags)
  srv_infos = Config.srv_infos(srv_type)
  image_id = Config.image_id(image)
  new_ip = Ips.reserve(scw)
  return if image_id.nil? || srv_infos.nil? || new_ip.nil?

  {
    name: name_pattern.gsub('__RANDOM__', Utilities.random_string),
    commercial_type: srv_type,
    public_ip: new_ip['ip']['id'],
    project: scw.project,
    image: image_id,
    volumes: { '0' => { size: srv_infos[:volume], volume_type: srv_infos[:volume_type] } },
    tags: tags
  }
end

.srv_up?(scw, srv) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/easymanager/scaleway/servers.rb', line 24

def self.srv_up?(scw, srv)
  status(scw, srv['id']) == 'running' && Utilities.elapsed_times(Time.now.to_s, srv['creation_date']) > 90
end

.status(scw, srv_id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/easymanager/scaleway/servers.rb', line 39

def self.status(scw, srv_id)
  response = Typhoeus.get(
    File.join(scw.api_url, "/instance/v1/zones/#{scw.zone}/servers/#{srv_id}"),
    headers: scw.headers
  )
  return unless response&.code == 200

  json_body = Utilities.parse_json(response.body)
  return unless json_body

  json_body['server']['state']
end