使用Ruby SDK

2020年12月31日19:42:54 发表评论 77 次浏览

总览

本指南将向你展示如何以编程方式使用notlogy库执行常见的管理任务, 这些任务也可以通过notlogy控制面板获得。

Ruby Client Library包装了最新版本的notlogy REST API。所有API操作均通过SSL执行, 并使用你的notlogy API令牌进行身份验证。可以在notlogy中运行的实例中访问该API, 也可以直接从任何可以发送HTTPS请求并接收HTTPS响应的应用程序通过Internet访问该API。

要求

  • 可以发送HTTPS请求并接收HTTPS响应的平台(例如服务器)。
  • notlogy Ruby SDK已安装和配置。

操作

使用模块

notlogy REST API官方文档:https://cloudpanel-api.1and1.com/documentation/1and1/v1/en/documentation.html

以下示例旨在为你提供有关notlogy Ruby SDK可以执行的某些操作的一般概述。有关所有方法和功能的详细列表, 请参见/docs/reference.md文件。

创建服务器

创建具有SSH密钥访问权限的服务器

rrequire 'oneandone'

OneAndOne.start('<API-TOKEN>') # Init module with API Key

# Instantiate Server Object
server = OneAndOne::Server.new

# Create HDD's
hdd1 = {
  'size' => 120, 'is_main' => true
}

hdds = [hdd1]

# Assign your public key to a variable
pub_key = '<PUB-KEY>'

# Perform Request
response = server.create(name: 'Example Server', vcore: 1, cores_per_processor: 1, ram: 1, appliance_id: '<IMAGE-ID>', hdds: hdds, rsa_key: pub_key)

puts JSON.pretty_generate(response)

注意:然后, 你可以在终端中执行以下命令, 以SSH方式进入服务器:

ssh –i <path_to_private_key_file> root@SERVER_IP

创建防火墙策略

创建负载均衡器

require 'oneandone'

OneAndOne.start('<API-TOKEN>') # Init module with API Key

# Instantiate Load Balancer Object
load_balancer = OneAndOne::LoadBalancer.new

# Create Rules
rule1 = {
  'protocol' => 'TCP', 'port_balancer' => 80, 'port_server' => 80, 'source' => '0.0.0.0'
}

rules = [rule1]

# Perform Request
response = load_balancer.create(name: 'Test LB', description: 'Example Desc', health_check_test: 'TCP', health_check_interval: 40, persistence: true, persistence_time: 1200, method: 'ROUND_ROBIN', rules: rules)

puts JSON.pretty_generate(response)

创建监控策略

require 'oneandone'

OneAndOne.start('<API-TOKEN>') # Init module with API Key

# Instantiate Monitoring Policy Object
monitoring_policy = OneAndOne::MonitoringPolicy.new

# Create Threshold Limits
thresholds = {
  'cpu' => {
    'warning' => {
      'value' => 90, 'alert' => false
    }, 'critical' => {
      'value' => 95, 'alert' => false
    }
  }, 'ram' => {
    'warning' => {
      'value' => 90, 'alert' => false
    }, 'critical' => {
      'value' => 95, 'alert' => false
    }
  }, 'disk' => {
    'warning' => {
      'value' => 90, 'alert' => false
    }, 'critical' => {
      'value' => 95, 'alert' => false
    }
  }, 'transfer' => {
    'warning' => {
      'value' => 1000, 'alert' => false
    }, 'critical' => {
      'value' => 2000, 'alert' => false
    }
  }, 'internal_ping' => {
    'warning' => {
      'value' => 50, 'alert' => false
    }, 'critical' => {
      'value' => 100, 'alert' => false
    }
  }
}

# Create Ports
port1 = {
  'protocol' => 'TCP', 'port' => 80, 'alert_if' => 'NOT_RESPONDING', 'email_notification' => true
}

ports = [port1]

# Create Processes
process1 = {
  'process' => 'test', 'alert_if' => 'NOT_RUNNING', 'email_notification' => true
}

processes = [process1]

# Perform Request
response = monitoring_policy.create(name: 'Test Monitoring Policy', email: 'test@example.com', agent: true, thresholds: thresholds, ports: ports, processes: processes)

puts JSON.pretty_generate(response)

然后, 添加一两个服务器:

# Add Servers
server1 = '<SERVER-ID>'
server2 = '<SERVER-ID>'

servers = [server1, server2]

# Perform Request
response = monitoring_policy.add_servers(servers: servers)

puts JSON.pretty_generate(response)

更新服务器核心, 内存和磁盘

notlogy允许用户彼此独立地动态更新内核, 内存和磁盘。这消除了需要升级到下一个大小以接收增加的内存的限制。现在, 你可以简单地增加实例内存, 使你的成本与资源需求保持一致。

以下代码说明了如何更新内核和内存:

这是更新服务器磁盘大小的方法:

require 'oneandone'

OneAndOne.start('<API-TOKEN>') # Init module with API Key

# Instantiate Server Object
server = OneAndOne::Server.new

# Perform Request
response = server.modify_hdd(server_id: '<SERVER-ID>', hdd_id: '<HDD-ID>', size: 140)

puts JSON.pretty_generate(response)

列出服务器, 映像, 共享存储等

生成资源列表相当简单。模块中的每个类都配有一个list方法。你可以传递可选的查询参数来帮助过滤结果。默认情况下, 这些参数都设置为nil。

以下是可供你使用的参数:

-page(整数):允许使用分页。指示从哪个页面开始。

-per_page(整数):每页的项目数。

-sort(字符串):sort ='name'检索按字母顺序排序的元素列表。 sort ='creation_date'检索按其创建日期降序排列的元素列表。

-q(字符串):q用于查询。使用此参数仅返回与搜索查询匹配的项目。

-fields(字符串):仅返回请求的参数。 (即, fields ='id, 名称, 说明, hardware.ram')

以下是一些如何列出资源的示例:

require 'oneandone'

OneAndOne.start('<API-TOKEN>') # Init module with API Key

# List all servers on your account
server = OneAndOne::Server.new

response = server.list

# List all servers whose name contains "My"
server = OneAndOne::Server.new

response = server.list(q: 'My')

# List all images on your account
image = OneAndOne::Image.new

response = image.list

示例应用

这个简单的应用程序可创建负载平衡器, 防火墙策略和服务器。然后, 它将负载平衡器和防火墙策略添加到服务器的初始IP地址。你可以使用以下命令访问服务器的初始IPfirst_ip服务器类对象上的"属性"属性, 如下面的示例所示。

示例应用程序的源代码可以在examples / example_app.rb文件。

require 'oneandone'

OneAndOne.start('<API-TOKEN>') # Init module with API key

### Create load balancer
load_balancer = OneAndOne::LoadBalancer.new

rule1 = {
  'protocol' => 'TCP', 'port_balancer' => 80, 'port_server' => 80, 'source' => '0.0.0.0'
}

rules = [rule1]

lb1 = load_balancer.create(name: 'Example App LB', description: 'Example Desc', health_check_test: 'TCP', health_check_interval: 40, persistence: true, persistence_time: 1200, method: 'ROUND_ROBIN', rules: rules)

# Wait for load balancer to deploy
puts "\nCreating load balancer...\n"
puts load_balancer.wait_for

### Create firewall policy
firewall = OneAndOne::Firewall.new

rule1 = {
  'protocol' => 'TCP', 'port_from' => 80, 'port_to' => 80, 'source' => '0.0.0.0'
}

rules = [rule1]

fp1 = firewall.create(name: 'Example App Firewall', description: 'Example Desc', rules: rules)

# Wait for firewall policy to deploy
puts "\nCreating firewall policy...\n"
puts firewall.wait_for

### Create a server
server = OneAndOne::Server.new

server1 = server.create(name: 'Example App Server', fixed_instance_id: '65929629F35BBFBA63022008F773F3EB', appliance_id: '6C902E5899CC6F7ED18595EBEB542EE1', datacenter_id: '5091F6D8CBFEF9C26ACE957C652D5D49')

# Wait for server to deploy
puts "\nCreating server...\n"
puts server.wait_for

### Add the load balancer to the server's initial IP
response = server.add_load_balancer(ip_id: server.first_ip['id'], load_balancer_id: load_balancer.id)

# Wait for load balancer to be added
puts "\nAdding load balancer to server IP...\n"
puts server.wait_for

### Add the firewall policy to the server's initial IP
response = server.add_firewall(ip_id: server.first_ip['id'], firewall_id: firewall.id)

# Wait for firewall policy to be added
puts "\nAdding firewall policy to server IP...\n"
puts server.wait_for
puts "\nEverything looks good!"

### Cleanup
puts "\nLet's clean up the mess we just made.\n"

puts "\nDeleting server...\n"
server.delete
puts "Success!\n"

puts "\nDeleting firewall policy...\n"
firewall.delete
puts "Success!\n"

puts "\nDeleting load balancer...\n"
load_balancer.delete
puts "Success!\n"

puts "\nAll done!\n"

更新于6/22/2017

一盏木

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: