civo 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/civo/account.rb +8 -0
- data/app/models/civo/base.rb +10 -0
- data/app/models/civo/instance.rb +13 -0
- data/app/models/civo/quota.rb +6 -0
- data/app/models/civo/region.rb +5 -0
- data/app/models/civo/size.rb +6 -0
- data/app/models/civo/ssh_key.rb +7 -0
- data/app/models/civo/template.rb +5 -0
- data/lib/civo.rb +16 -1
- data/lib/civo/cli/client.rb +57 -0
- data/lib/civo/cli/commands/accounts.rb +58 -0
- data/lib/civo/cli/commands/instances.rb +87 -0
- data/lib/civo/cli/commands/quota.rb +82 -0
- data/lib/civo/cli/commands/regions.rb +15 -0
- data/lib/civo/cli/commands/sizes.rb +23 -0
- data/lib/civo/cli/commands/sshkeys.rb +46 -0
- data/lib/civo/cli/commands/templates.rb +11 -0
- data/lib/civo/cli/commands/tokens.rb +35 -0
- data/lib/civo/cli/config.rb +53 -0
- data/lib/civo/cli/token.rb +53 -0
- data/lib/civo/version.rb +1 -1
- metadata +35 -4
- data/README.rdoc +0 -3
- data/lib/civo/commands/instance.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 337b9d93da0b0f0236d6fc643bae10391adddfc1
|
4
|
+
data.tar.gz: 977615186c605ccfb5103daeea279c4a04bc83bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89d1e11deab114b018e3b7fa6c84c23629dce3165569fdde59248422a39ab8772c390daa33369729f98c2face728a4606c779e43d8f1a325e801bd5a41343bd2
|
7
|
+
data.tar.gz: 3e5fb94c5b7b1278b5365a625257d6b3cfb2ddf84e7807ec4ec91b232b90995c2e5da393ce9d80dbad1275b7551f57bfb25756288404bc4b203a8dcad212294b
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Civo
|
2
|
+
class Base < Flexirest::Base
|
3
|
+
before_request do |name, request|
|
4
|
+
request.headers["Authorization"] = "bearer #{ENV["CIVO_TOKEN"] || Civo::Token.default}"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
Flexirest::Base.base_url = ENV["CIVO_URL"] || Civo::Config.api_url
|
10
|
+
Flexirest::Logger.logfile = "flexirest.log"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Civo
|
2
|
+
class Instance < Base
|
3
|
+
verbose!
|
4
|
+
get :all, "/v1/instances"
|
5
|
+
post :create, "/v1/instances", requires: [:hostname, :size, :region, :ssh_key],
|
6
|
+
defaults: {public_ip: true, template: "ubuntu-14.04", initial_user: "civo"}
|
7
|
+
delete :remove, "/v1/instances/:id"
|
8
|
+
|
9
|
+
def ip_addresses
|
10
|
+
self._attributes[:ip_addresses].items rescue ""
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/civo.rb
CHANGED
@@ -1,4 +1,19 @@
|
|
1
|
-
|
1
|
+
if defined?(Rails)
|
2
|
+
require "civo/engine"
|
3
|
+
end
|
2
4
|
|
3
5
|
module Civo
|
6
|
+
autoload :Token, "civo/cli/token"
|
7
|
+
autoload :Config, "civo/cli/config"
|
8
|
+
autoload :Client, "civo/cli/client"
|
9
|
+
unless defined?(Rails)
|
10
|
+
autoload :Account, "#{ENGINE_ROOT}/app/models/civo/account"
|
11
|
+
autoload :Base, "#{ENGINE_ROOT}/app/models/civo/base"
|
12
|
+
autoload :SshKey, "#{ENGINE_ROOT}/app/models/civo/ssh_key"
|
13
|
+
autoload :Quota, "#{ENGINE_ROOT}/app/models/civo/quota"
|
14
|
+
autoload :Size, "#{ENGINE_ROOT}/app/models/civo/size"
|
15
|
+
autoload :Region, "#{ENGINE_ROOT}/app/models/civo/region"
|
16
|
+
autoload :Template, "#{ENGINE_ROOT}/app/models/civo/template"
|
17
|
+
autoload :Instance, "#{ENGINE_ROOT}/app/models/civo/instance"
|
18
|
+
end
|
4
19
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Civo
|
2
|
+
class Client
|
3
|
+
def self.tabulate(hash, titles, options = {})
|
4
|
+
longest_key_length = hash.keys.max_by(&:length).try(:length) || 5
|
5
|
+
|
6
|
+
default = hash.delete("**DEFAULT**")
|
7
|
+
title = "%-#{longest_key_length}s | %s" % [titles[0], titles[1]]
|
8
|
+
puts title
|
9
|
+
puts "%s-|-%s" % [("-" * longest_key_length), ("-" * (69-longest_key_length))]
|
10
|
+
keys = hash.keys
|
11
|
+
keys.sort! if options[:sort] == true
|
12
|
+
keys.each do |key|
|
13
|
+
value = hash[key]
|
14
|
+
print "%-#{longest_key_length}s | %s" % [key, value]
|
15
|
+
if key == default
|
16
|
+
print " (DEFAULT)"
|
17
|
+
end
|
18
|
+
puts
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.tabulate_flexirest(data, mappings)
|
23
|
+
columns = mappings.dup
|
24
|
+
columns.each do |k, nice|
|
25
|
+
columns[k] = {max_width: (nice.length > 5 ? nice.length : 5), label: nice }
|
26
|
+
end
|
27
|
+
|
28
|
+
data.each do |record|
|
29
|
+
columns.each do |k, v|
|
30
|
+
length = record.send(k).to_s.length
|
31
|
+
if record.send(k).to_s.length > v[:max_width]
|
32
|
+
v[:max_width] = length
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
puts (columns.keys.map do |k|
|
38
|
+
"%-#{columns[k][:max_width]}s" % columns[k][:label]
|
39
|
+
end.to_a.join(" | "))
|
40
|
+
puts (columns.keys.map do |k|
|
41
|
+
"-" * columns[k][:max_width]
|
42
|
+
end.to_a.join("-+-"))
|
43
|
+
|
44
|
+
data.each do |record|
|
45
|
+
row = []
|
46
|
+
columns.each do |k, v|
|
47
|
+
value = record.send(k)
|
48
|
+
if value.is_a? Array
|
49
|
+
value = value.join(", ")
|
50
|
+
end
|
51
|
+
row << "%-#{columns[k][:max_width]}s" % value
|
52
|
+
end
|
53
|
+
puts (row.join(" | "))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
command "accounts" do |c|
|
2
|
+
c.description = "List known accounts (CIVO-INTERNAL-USE ONLY)"
|
3
|
+
c.action do |args, options|
|
4
|
+
begin
|
5
|
+
accounts = Civo::Account.all
|
6
|
+
Civo::Client.tabulate_flexirest accounts, {username: "Account", api_key: "API Key"}
|
7
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
8
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
command "accounts:create" do |c|
|
14
|
+
c.description = "Create an account (CIVO-INTERNAL-USE ONLY)"
|
15
|
+
c.example "Creates an account called 'testuser'", 'civo accounts:create testuser'
|
16
|
+
c.action do |args, options|
|
17
|
+
begin
|
18
|
+
account = Civo::Account.create(name: args.first)
|
19
|
+
puts "Account '#{args.first}' created. The API key is '#{account.api_key}'"
|
20
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
21
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
command "accounts:reset" do |c|
|
27
|
+
c.description = "Reset the API Key for an account (CIVO-INTERNAL-USE ONLY)"
|
28
|
+
c.example "Resets the account called 'testuser' with a new API key", 'civo accounts:reset testuser'
|
29
|
+
c.action do |args, options|
|
30
|
+
begin
|
31
|
+
account = Civo::Account.reset(name: args.first)
|
32
|
+
puts "Account '#{args.first}' reset, the new API key is '#{account.api_key}'"
|
33
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
34
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
command "accounts:remove" do |c|
|
40
|
+
c.description = "Remove an account (and all instances, networks, etc) (CIVO-INTERNAL-USE ONLY)"
|
41
|
+
c.example "Removes an account called 'testuser'", 'civo accounts:remove testuser'
|
42
|
+
c.action do |args, options|
|
43
|
+
begin
|
44
|
+
account = Civo::Account.remove(name: args.first)
|
45
|
+
if account.result == "ok"
|
46
|
+
puts "Account '#{args.first}' has been removed."
|
47
|
+
else
|
48
|
+
puts "Failed to delete that account: #{account.inspect}"
|
49
|
+
end
|
50
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
51
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
52
|
+
rescue Flexirest::HTTPNotFoundClientException => e
|
53
|
+
puts "Couldn't find that account to remove, maybe it's already been removed?"
|
54
|
+
rescue Flexirest::HTTPServerException => e
|
55
|
+
puts "Unable to remove #{e.result.reason}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
command "instances" do |c|
|
2
|
+
c.description = "List known instances"
|
3
|
+
c.action do |args, options|
|
4
|
+
begin
|
5
|
+
instances = Civo::Instance.all
|
6
|
+
Civo::Client.tabulate_flexirest instances, {id: "ID", hostname: "Hostname", size: "Size", ip_addresses: "IP Addresses", status: "Status"}
|
7
|
+
rescue Flexirest::HTTPServerException => e
|
8
|
+
puts "An error occurred: #{e.result.reason}"
|
9
|
+
exit 3
|
10
|
+
rescue Flexirest::ResponseParseException => e
|
11
|
+
puts "An error occurred: #{e.inspect}"
|
12
|
+
exit 4
|
13
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
14
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
command "instances:create" do |c|
|
20
|
+
c.description = "Create an instance"
|
21
|
+
c.option "--size STRING", String, "The size from 'civo sizes'"
|
22
|
+
c.option "--region STRING", String, "The region from 'civo regions'"
|
23
|
+
c.option "--ssh-key STRING", String, "The SSH key name from 'civo sshkeys'"
|
24
|
+
c.option "--[no-]public-ip", "Should a public IP address be allocated"
|
25
|
+
c.option "--template STRING", String, "The template from 'civo templates'"
|
26
|
+
c.option "--initial-user STRING", String, "The default user to create (defaults to 'civo')"
|
27
|
+
c.example "Creates an instance called 'test1.example.com'", 'civo instances:create test1.example.com --size g1.small --region svg1 --ssh-key default'
|
28
|
+
c.action do |args, options|
|
29
|
+
begin
|
30
|
+
params = {}
|
31
|
+
params[:size] = options.size if options.size
|
32
|
+
params[:region] = options.region if options.region
|
33
|
+
params[:ssh_key] = options.ssh_key if options.ssh_key
|
34
|
+
params[:public_ip] = options.public_ip if options.public_ip
|
35
|
+
params[:template] = options.template if options.template
|
36
|
+
params[:initial_user] = options.initial_user if options.initial_user
|
37
|
+
params[:hostname] = args.first
|
38
|
+
instance = Civo::Instance.create(params)
|
39
|
+
puts "Instance '#{args.first}' created. The ID is '#{instance.id}' and its status is #{instance.status}"
|
40
|
+
rescue Flexirest::HTTPServerException => e
|
41
|
+
puts "An error occurred: #{e.result.reason}"
|
42
|
+
exit 3
|
43
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
44
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
command "instances:reset" do |c|
|
50
|
+
c.description = "Reset the API Key for an account"
|
51
|
+
c.example "Resets the account called 'testuser' with a new API key", 'civo instances:reset testuser'
|
52
|
+
c.action do |args, options|
|
53
|
+
begin
|
54
|
+
account = Civo::Instance.reset(name: args.first)
|
55
|
+
puts "Account '#{args.first}' reset, the new API key is '#{account.api_key}'"
|
56
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
57
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
command "instances:remove" do |c|
|
63
|
+
c.description = "Remove an account (and all instances, networks, etc)"
|
64
|
+
c.example "Removes an account called 'testuser'", 'civo instances:remove testuser'
|
65
|
+
c.action do |args, options|
|
66
|
+
begin
|
67
|
+
if args.first[/(\w{8}(-\w{4}){3}-\w{12}?)/]
|
68
|
+
id = args.first
|
69
|
+
else
|
70
|
+
instance = Civo::Instance.all.detect {|i| i.hostname == args.first}
|
71
|
+
id = instance.id
|
72
|
+
end
|
73
|
+
account = Civo::Instance.remove(id: id)
|
74
|
+
if account.result == "ok"
|
75
|
+
puts "Account '#{args.first}' has been removed."
|
76
|
+
else
|
77
|
+
puts "Failed to delete that account: #{account.inspect}"
|
78
|
+
end
|
79
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
80
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
81
|
+
rescue Flexirest::HTTPNotFoundClientException => e
|
82
|
+
puts "Couldn't find that account to remove, maybe it's already been removed?"
|
83
|
+
rescue Flexirest::HTTPServerException => e
|
84
|
+
puts "Unable to remove #{e.result.reason}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
command "quota" do |c|
|
2
|
+
c.option "--account STRING", String, "The account to get the quota for"
|
3
|
+
|
4
|
+
c.description = "List the quota for the current account"
|
5
|
+
c.action do |args, options|
|
6
|
+
begin
|
7
|
+
if options.account
|
8
|
+
quota = Civo::Quota.current(username: options.account)
|
9
|
+
else
|
10
|
+
quota = Civo::Quota.current
|
11
|
+
end
|
12
|
+
data = {
|
13
|
+
"Number of instances" => "#{quota.instance_count_limit} (used #{quota.instance_count_usage})",
|
14
|
+
"Total CPU cores" => "#{quota.cpu_core_limit} (used #{quota.cpu_core_usage})",
|
15
|
+
"Total RAM" => "#{quota.ram_mb_limit}MB (used #{quota.ram_mb_usage}MB)",
|
16
|
+
"Total disk space" => "#{quota.disk_gb_limit}GB (used #{quota.disk_gb_usage}GB)",
|
17
|
+
"Disk volumes" => "#{quota.disk_volume_count_limit} (used #{quota.disk_volume_count_usage})",
|
18
|
+
"Disk snapshots" => "#{quota.disk_snapshot_count_limit} (used #{quota.disk_snapshot_count_usage})",
|
19
|
+
"Public IP addresses" => "#{quota.public_ip_address_limit} (used #{quota.public_ip_address_usage})",
|
20
|
+
"Private subnets" => "#{quota.subnet_count_limit} (used #{quota.subnet_count_usage})",
|
21
|
+
"Private networks" => "#{quota.network_count_limit} (used #{quota.network_count_usage})",
|
22
|
+
"Security groups" => "#{quota.security_group_limit} (used #{quota.security_group_usage})",
|
23
|
+
"Security group rules" => "#{quota.security_group_rule_limit} (used #{quota.security_group_rule_usage})",
|
24
|
+
"Number of ports (network connections)" => "#{quota.port_count_limit} (used #{quota.port_count_usage})"
|
25
|
+
}
|
26
|
+
|
27
|
+
Civo::Client.tabulate data, ["Title", "Limit"]
|
28
|
+
|
29
|
+
if Civo::Config.admin?
|
30
|
+
puts
|
31
|
+
puts "To set these quota values use:"
|
32
|
+
puts "civo quota:set --instance-count #{quota.instance_count_limit} --cpu-core #{quota.cpu_core_limit} --ram-mb #{quota.ram_mb_limit} --disk-gb #{quota.disk_gb_limit} \\"
|
33
|
+
puts " --disk-volume-count #{quota.disk_volume_count_limit} --disk-snapshot-count #{quota.disk_snapshot_count_limit} --public-ip-address #{quota.public_ip_address_limit} \\"
|
34
|
+
puts " --subnet-count #{quota.subnet_count_limit} --network-count #{quota.network_count_limit} --security-group #{quota.security_group_limit} --security-group-rule #{quota.security_group_rule_limit} \\"
|
35
|
+
puts " --port-count #{quota.port_count_limit} #{"--account #{options.account}" if options.account}"
|
36
|
+
end
|
37
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
38
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
command "quota:set" do |c|
|
44
|
+
c.description = "Update the quotas for the current account (CIVO-INTERNAL-USE ONLY)"
|
45
|
+
c.option "--account STRING", String, "The account to update the quota for"
|
46
|
+
c.option "--instance-count INTEGER", Integer, "The limit to the number of instances available"
|
47
|
+
c.option "--cpu-core INTEGER", Integer, "The limit to the number of CPU cores available"
|
48
|
+
c.option "--ram-mb INTEGER", Integer, "The limit to the amount of RAM (in MB) available"
|
49
|
+
c.option "--disk-gb INTEGER", Integer, "The limit to the of disk space (in GB) available"
|
50
|
+
c.option "--disk-volume-count INTEGER", Integer, "The limit to the number of disk volumes available"
|
51
|
+
c.option "--disk-snapshot-count INTEGER", Integer, "The limit to the number of disk snapshots available"
|
52
|
+
c.option "--public-ip-address INTEGER", Integer, "The limit to the number of public IP addresses available"
|
53
|
+
c.option "--subnet-count INTEGER", Integer, "The limit to the number of subnets available"
|
54
|
+
c.option "--network-count INTEGER", Integer, "The limit to the number of networks available"
|
55
|
+
c.option "--security-group INTEGER", Integer, "The limit to the number of security groups available"
|
56
|
+
c.option "--security-group-rule INTEGER", Integer, "The limit to the number of security group rules available"
|
57
|
+
c.option "--port-count INTEGER", Integer, "The limit to the number of ports (network connections) available"
|
58
|
+
|
59
|
+
c.action do |args, options|
|
60
|
+
begin
|
61
|
+
params = {}
|
62
|
+
params[:name] = options.account if options.account
|
63
|
+
params[:instance_count_limit] = options.instance_count if options.instance_count
|
64
|
+
params[:cpu_core_limit] = options.cpu_core if options.cpu_core
|
65
|
+
params[:ram_mb_limit] = options.ram_mb if options.ram_mb
|
66
|
+
params[:disk_gb_limit] = options.disk_gb if options.disk_gb
|
67
|
+
params[:disk_volume_count_limit] = options.disk_volume_count if options.disk_volume_count
|
68
|
+
params[:disk_snapshot_count_limit] = options.disk_snapshot_count if options.disk_snapshot_count
|
69
|
+
params[:public_ip_address_limit] = options.public_ip_address if options.public_ip_address
|
70
|
+
params[:subnet_count_limit] = options.subnet_count if options.subnet_count
|
71
|
+
params[:network_count_limit] = options.network_count if options.network_count
|
72
|
+
params[:security_group_limit] = options.security_group if options.security_group
|
73
|
+
params[:security_group_rule_limit] = options.security_group_rule if options.security_group_rule
|
74
|
+
params[:port_count_limit] = options.port_count if options.port_count
|
75
|
+
puts params.inspect
|
76
|
+
Civo::Quota.update(params)
|
77
|
+
puts "Quota updated"
|
78
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
79
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
command "regions" do |c|
|
2
|
+
c.description = "List available regions"
|
3
|
+
c.action do |args, options|
|
4
|
+
begin
|
5
|
+
regions = Civo::Region.all
|
6
|
+
puts "Region name"
|
7
|
+
puts "-" * 72
|
8
|
+
regions.each do |region|
|
9
|
+
puts region.name
|
10
|
+
end
|
11
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
12
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
command "sizes" do |c|
|
2
|
+
c.description = "List available instance sizes"
|
3
|
+
c.action do |args, options|
|
4
|
+
begin
|
5
|
+
sizes = Civo::Size.all
|
6
|
+
Civo::Client.tabulate_flexirest sizes, {Name: "Size", Description: "Description"}
|
7
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
8
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
command "sizes:reset" do |c|
|
14
|
+
c.description = "Reset available instance sizes"
|
15
|
+
c.action do |args, options|
|
16
|
+
begin
|
17
|
+
Civo::Size.reset
|
18
|
+
puts "Standard sizes reset in Openstack"
|
19
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
20
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
command "sshkeys" do |c|
|
2
|
+
c.description = "List the SSH public keys you've uploaded"
|
3
|
+
c.action do |args, options|
|
4
|
+
begin
|
5
|
+
sshkeys = Civo::SshKey.all
|
6
|
+
Civo::Client.tabulate_flexirest sshkeys, {name: "Name", label: "SSH Key Label"}
|
7
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
8
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
command "sshkeys:upload" do |c|
|
14
|
+
c.description = "Upload an SSH public key for installing in to new instances"
|
15
|
+
c.example "Uploads an SSH public key, calling it 'default' from file '~/.ssh/id_rsa.pub'", 'civo sshkeys:upload default ~/.ssh/id_rsa.pub'
|
16
|
+
c.action do |args, options|
|
17
|
+
pub_key = File.read(args[1])
|
18
|
+
begin
|
19
|
+
Civo::SshKey.create(name: args.first, public_key: pub_key)
|
20
|
+
puts "SSH key '#{args.first}' uploaded."
|
21
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
22
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
command "sshkeys:remove" do |c|
|
28
|
+
c.description = "Remove an SSH public key from the list you've uploaded"
|
29
|
+
c.example "Removes an SSH public key called 'testuser'", 'civo sshkeys:remove testuser'
|
30
|
+
c.action do |args, options|
|
31
|
+
begin
|
32
|
+
key = Civo::SshKey.remove(name: args.first)
|
33
|
+
if key.result == "ok"
|
34
|
+
puts "SSH public key '#{args.first}' has been removed."
|
35
|
+
else
|
36
|
+
puts "Failed to delete that SSH key: #{key.inspect}"
|
37
|
+
end
|
38
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
39
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
40
|
+
rescue Flexirest::HTTPNotFoundClientException => e
|
41
|
+
puts "Couldn't find that account to remove, maybe it's already been removed?"
|
42
|
+
rescue Flexirest::HTTPServerException => e
|
43
|
+
puts "Unable to remove #{e.result.reason}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
command "templates" do |c|
|
2
|
+
c.description = "List available instance templates"
|
3
|
+
c.action do |args, options|
|
4
|
+
begin
|
5
|
+
templates = Civo::Template.all
|
6
|
+
Civo::Client.tabulate_flexirest templates, {id: "Template", short_description: "Description"}
|
7
|
+
rescue Flexirest::HTTPUnauthorisedClientException, Flexirest::HTTPForbiddenClientException
|
8
|
+
puts "Access denied to your default token, ensure it's set correctly with 'civo tokens'"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
command "tokens" do |c|
|
2
|
+
c.description = "List the tokens you have saved"
|
3
|
+
c.action do |args, options|
|
4
|
+
tokens = Civo::Token.read_all
|
5
|
+
if tokens.keys.size > 0
|
6
|
+
Civo::Client.tabulate tokens, ["Name", "API Key"]
|
7
|
+
else
|
8
|
+
puts "No tokens found, please set one using tokens:save"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
command "tokens:save" do |c|
|
14
|
+
c.description = "Save an API token supplied by Civo.com"
|
15
|
+
c.example "Saves a token called 'master' with API key 'key_goes_here'", 'civo tokens:save master key_goes_here'
|
16
|
+
c.action do |args, options|
|
17
|
+
Civo::Token.save(args[0], args[1])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
command "tokens:default" do |c|
|
22
|
+
c.description = "Set the default token from the list you have saved"
|
23
|
+
c.example "Sets the default token to 'master'", 'civo tokens:default master'
|
24
|
+
c.action do |args, options|
|
25
|
+
Civo::Token.set_default(args[0])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
command "tokens:remove" do |c|
|
30
|
+
c.description = "Remove a token from the list you have saved"
|
31
|
+
c.example "Removes the token 'master'", 'civo tokens:remove master'
|
32
|
+
c.action do |args, options|
|
33
|
+
Civo::Token.remove(args[0])
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Civo
|
2
|
+
class Config
|
3
|
+
FILENAME = "#{ENV["HOME"]}/.civorc"
|
4
|
+
|
5
|
+
def self.header_line
|
6
|
+
"# This file is managed by 'civo'\n"
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.ensure_file_exists!
|
10
|
+
unless File.exists?(FILENAME)
|
11
|
+
update({meta: {version: "1", url: "https://api.civo.com"}}).save
|
12
|
+
FileUtils.chmod(0600, FILENAME)
|
13
|
+
end
|
14
|
+
|
15
|
+
unless File.exists?(FILENAME)
|
16
|
+
puts "#{FILENAME} doesn't exist and couldn't be created"
|
17
|
+
exit 1
|
18
|
+
end
|
19
|
+
|
20
|
+
unless ("%o" % File.stat(FILENAME).mode).to_s[/600$/]
|
21
|
+
puts "The permissions on #{FILENAME} aren't 0600 (#{ ("%o" % File.stat(FILENAME).mode).to_s}), they must be for security reasons"
|
22
|
+
exit 2
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.parse
|
27
|
+
ensure_file_exists!
|
28
|
+
|
29
|
+
@config = TOML.load_file(FILENAME)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.api_url
|
33
|
+
parse["meta"]["url"]
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.admin?
|
37
|
+
parse["meta"]["admin"] == "true"
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.update(config)
|
41
|
+
@config = config
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.save
|
46
|
+
File.open(FILENAME, "w") do |f|
|
47
|
+
f << header_line
|
48
|
+
f << TOML::Generator.new(@config).body
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Civo
|
2
|
+
class Token
|
3
|
+
|
4
|
+
def self.read_all
|
5
|
+
config = Civo::Config.parse
|
6
|
+
config["tokens"] || {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.write_all(tokens)
|
10
|
+
config = Civo::Config.parse
|
11
|
+
config["tokens"] = tokens
|
12
|
+
Civo::Config.update(config).save
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.save(name, key)
|
16
|
+
tokens = read_all
|
17
|
+
tokens[name] = key
|
18
|
+
if tokens["**DEFAULT**"] == nil
|
19
|
+
tokens["**DEFAULT**"] = name
|
20
|
+
end
|
21
|
+
write_all(tokens)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.set_default(name)
|
25
|
+
puts "Setting default token to be #{name}"
|
26
|
+
tokens = read_all
|
27
|
+
tokens["**DEFAULT**"] = name
|
28
|
+
write_all(tokens)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.remove(name)
|
32
|
+
tokens = read_all
|
33
|
+
tokens.delete(name)
|
34
|
+
if tokens["**DEFAULT**"] == name
|
35
|
+
tokens["**DEFAULT**"] = tokens.keys.first
|
36
|
+
if tokens["**DEFAULT**"] == "**DEFAULT**"
|
37
|
+
tokens.delete("**DEFAULT**")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
write_all(tokens)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.default
|
44
|
+
tokens = read_all
|
45
|
+
token = tokens[tokens["**DEFAULT**"]]
|
46
|
+
if token.nil?
|
47
|
+
puts "No default token has been set. Use 'civo tokens:save --name NAME --token TOKEN' to set one"
|
48
|
+
exit 2
|
49
|
+
end
|
50
|
+
token
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/civo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: civo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Jeffries
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -26,6 +26,20 @@ dependencies:
|
|
26
26
|
version: 4.2.4
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: flexirest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.2.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: toml
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - ">="
|
@@ -60,11 +74,28 @@ extensions: []
|
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
62
76
|
- MIT-LICENSE
|
63
|
-
- README.rdoc
|
64
77
|
- Rakefile
|
78
|
+
- app/models/civo/account.rb
|
79
|
+
- app/models/civo/base.rb
|
80
|
+
- app/models/civo/instance.rb
|
81
|
+
- app/models/civo/quota.rb
|
82
|
+
- app/models/civo/region.rb
|
83
|
+
- app/models/civo/size.rb
|
84
|
+
- app/models/civo/ssh_key.rb
|
85
|
+
- app/models/civo/template.rb
|
65
86
|
- config/routes.rb
|
66
87
|
- lib/civo.rb
|
67
|
-
- lib/civo/
|
88
|
+
- lib/civo/cli/client.rb
|
89
|
+
- lib/civo/cli/commands/accounts.rb
|
90
|
+
- lib/civo/cli/commands/instances.rb
|
91
|
+
- lib/civo/cli/commands/quota.rb
|
92
|
+
- lib/civo/cli/commands/regions.rb
|
93
|
+
- lib/civo/cli/commands/sizes.rb
|
94
|
+
- lib/civo/cli/commands/sshkeys.rb
|
95
|
+
- lib/civo/cli/commands/templates.rb
|
96
|
+
- lib/civo/cli/commands/tokens.rb
|
97
|
+
- lib/civo/cli/config.rb
|
98
|
+
- lib/civo/cli/token.rb
|
68
99
|
- lib/civo/engine.rb
|
69
100
|
- lib/civo/version.rb
|
70
101
|
- lib/tasks/civo_tasks.rake
|
data/README.rdoc
DELETED
File without changes
|