civo_cli 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d11ea9765c6587f9b9fe117fb9c3ff15b50cae5fddd6110191f71c819e235b9e
4
- data.tar.gz: cb754f79de7af4b2072c0ecbe51285887d7688f0c569d9057fb466112a3da331
3
+ metadata.gz: 8819a307c3a3c33baaaf05fac0530e93965a4cf78575cfa3adaa0713a26586f6
4
+ data.tar.gz: 2d5be57eb29a3d1fedfc1450485f09d7dd20dfc61a7358bb4c46830d6becaee9
5
5
  SHA512:
6
- metadata.gz: efcabbc75b6bf60c6750a1b7b0dcc8359211ef6ad69139691f925bcd99fe340150cf05206cb695416a187515855fe94e59b6f904d556aea9e8461524bfab47bf
7
- data.tar.gz: cabfc6bfdcf8dd8cbfd7606f42c23cb4881c3960ff7cee8198102e23484acabb7d40d7103940a4fa8a5cbde01c19fbec0008e80ca9e44e4e799cb219daeffe82
6
+ metadata.gz: 4ac35f3f40d3a524a581ee1439b1c2b8265a4c2a0f3fedec9eb01a2bf626a3488dfe50667b4c97b436f2e859f19f1dfe30a9a44f12fc018e4f43795e60656a74
7
+ data.tar.gz: 1513d0d52f85712125aaa292759f2462f3eb24b38f57eb81a9d7a3a86da86f0a6c0431dbff153da7adf4ce073c8a9da9c85e0dfc8f05026f4d698ac4742f3d4c
data/CHANGELOG.md CHANGED
@@ -7,4 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
7
7
  ### Rewrote
8
8
  - Written new version in Ruby for ease of maintenance that replaces the old [Go-based CLI](https://github.com/absolutedevops/civo)
9
9
 
10
+ ## [0.2.0] - 2019-06-19
11
+ ### API endpoints implemented
12
+ - Implemented APIkey, Blueprint, Domain, Domainrecord, Firewall, Instance, Network, Quota, Region, Size, Snapshot, SSHKey, Template and Volume endpoints
13
+
10
14
  [1.0.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...v1.0.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- civo_cli (0.1.1)
4
+ civo_cli (0.2.0)
5
5
  civo (>= 1.1.14)
6
6
  colorize
7
7
  json
@@ -18,7 +18,7 @@ GEM
18
18
  tzinfo (~> 1.1)
19
19
  addressable (2.6.0)
20
20
  public_suffix (>= 2.0.2, < 4.0)
21
- civo (1.1.14)
21
+ civo (1.1.17)
22
22
  commander
23
23
  flexirest (>= 1.4.6)
24
24
  toml
@@ -33,7 +33,7 @@ GEM
33
33
  faraday (0.15.4)
34
34
  multipart-post (>= 1.2, < 3)
35
35
  ffi (1.10.0)
36
- flexirest (1.7.6)
36
+ flexirest (1.7.7)
37
37
  activesupport
38
38
  crack
39
39
  faraday
@@ -54,7 +54,7 @@ GEM
54
54
  guard-compat (~> 1.1)
55
55
  rspec (>= 2.99.0, < 4.0)
56
56
  hashdiff (0.3.8)
57
- highline (2.0.1)
57
+ highline (2.0.2)
58
58
  i18n (1.6.0)
59
59
  concurrent-ruby (~> 1.0)
60
60
  json (2.2.0)
@@ -66,7 +66,7 @@ GEM
66
66
  method_source (0.9.2)
67
67
  minitest (5.11.3)
68
68
  multi_json (1.13.1)
69
- multipart-post (2.0.0)
69
+ multipart-post (2.1.1)
70
70
  nenv (0.3.0)
71
71
  notiffany (0.1.1)
72
72
  nenv (~> 0.1)
data/Rakefile CHANGED
@@ -3,4 +3,13 @@ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
+ task :console do
7
+ require 'irb'
8
+ require 'irb/completion'
9
+ require './lib/civo_cli'
10
+ ARGV.clear
11
+ IRB.start
12
+ end
13
+ task :c => :console
14
+
6
15
  task :default => :spec
data/lib/apikey.rb CHANGED
@@ -24,8 +24,16 @@ module CivoCLI
24
24
 
25
25
  desc "remove NAME", "Remove the API Key with a label of 'NAME'"
26
26
  def remove(name)
27
- CivoCLI::Config.delete_apikey(name)
28
- puts "Removed the API Key #{name.colorize(:green)}"
27
+ keys = CivoCLI::Config.get_apikeys
28
+ if keys.keys.include?(name)
29
+ CivoCLI::Config.delete_apikey(name)
30
+ puts "Removed the API Key #{name.colorize(:green)}"
31
+ else
32
+ puts "The API Key #{name.colorize(:red)} couldn't be found."
33
+ exit 1
34
+ end
35
+
36
+
29
37
  end
30
38
  map "delete" => "remove", "rm" => "remove"
31
39
 
data/lib/blueprint.rb CHANGED
@@ -42,7 +42,7 @@ module CivoCLI
42
42
  params[:dsl_content] = File.read(options["content-file"]) unless options["content-file"].nil?
43
43
  params[:template_id] = options["template-id"] unless options["template-id"].nil?
44
44
  params[:name] = options["name"] unless options["name"].nil?
45
- Civo::Blueprint.save(params)
45
+ Civo::Blueprint.update(params)
46
46
  blueprint = Civo::Blueprint.all.detect {|b| b.id == id }
47
47
  puts "Updated blueprint #{blueprint.name.colorize(:green)}"
48
48
  rescue Flexirest::HTTPException => e
@@ -60,7 +60,8 @@ module CivoCLI
60
60
  params[:dsl_content] = File.read(options["content-file"]) unless options["content-file"].nil?
61
61
  params[:template_id] = options["template-id"] unless options["template-id"].nil?
62
62
  params[:name] = options["name"] unless options["name"].nil?
63
- blueprint = Civo::Blueprint.create(params)
63
+ result = Civo::Blueprint.create(params)
64
+ blueprint = Civo::Blueprint.all.detect {|b| b.id == result.id }
64
65
  puts "Created blueprint #{blueprint.name.colorize(:green)} with ID #{blueprint.id.colorize(:green)}"
65
66
  rescue Flexirest::HTTPException => e
66
67
  puts e.result.reason.colorize(:red)
@@ -1,3 +1,3 @@
1
1
  module CivoCLI
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/civo_cli.rb CHANGED
@@ -23,6 +23,10 @@ module CivoCLI
23
23
  subcommand "domain", CivoCLI::Domain
24
24
  map "domains" => "domain"
25
25
 
26
+ desc "domainrecord", "manage domain name DNS records for a domain"
27
+ subcommand "domainrecord", CivoCLI::DomainRecord
28
+ map "domainrecords" => "domainrecord"
29
+
26
30
  desc "firewall", "manage firewalls"
27
31
  subcommand "firewall", CivoCLI::Firewall
28
32
  map "firewalls" => "firewall"
data/lib/config.rb CHANGED
@@ -38,6 +38,8 @@ module CivoCLI
38
38
 
39
39
  def self.get_meta(key)
40
40
  current["meta"].transform_keys{ |key| key.to_sym rescue key }[key]
41
+ rescue
42
+ nil
41
43
  end
42
44
 
43
45
  def self.set_meta(key, value)
@@ -47,6 +49,8 @@ module CivoCLI
47
49
 
48
50
  def self.current
49
51
  @config ||= JSON.parse(File.read(filename))
52
+ rescue
53
+ @config = {}
50
54
  end
51
55
 
52
56
  def self.reset
data/lib/domain.rb CHANGED
@@ -1,5 +1,39 @@
1
1
  module CivoCLI
2
2
  class Domain < Thor
3
+ desc "list", "list all domain records for a domain"
4
+ def list
5
+ CivoCLI::Config.set_api_auth
6
+ rows = []
7
+ Civo::DnsDomain.all.items.each do |domain|
8
+ rows << [domain.id, domain.name]
9
+ end
10
+ puts Terminal::Table.new headings: ['ID', 'Name'], rows: rows
11
+ end
12
+
13
+ desc "create DOMAIN", "create a new domain name called DOMAIN"
14
+ def create(name)
15
+ CivoCLI::Config.set_api_auth
16
+ domain = Civo::DnsDomain.create(name: name)
17
+ puts "Created a domain called #{name.colorize(:green)} with ID #{domain.id.colorize(:green)}"
18
+ rescue Flexirest::HTTPException => e
19
+ puts e.result.reason.colorize(:red)
20
+ exit 1
21
+ end
22
+ map "new" => "create"
23
+
24
+ desc "remove ID", "remove the domain with ID (or name)"
25
+ def remove(id)
26
+ CivoCLI::Config.set_api_auth
27
+ domain = Civo::DnsDomain.all.items.detect {|key| key.id == id || key.name == id}
28
+ Civo::DnsDomain.remove(id: domain.id)
29
+ puts "Removed the domain #{domain.name.colorize(:green)} with ID #{domain.id.colorize(:green)}"
30
+ rescue Flexirest::HTTPException => e
31
+ puts e.result.reason.colorize(:red)
32
+ exit 1
33
+ end
34
+ map "delete" => "remove", "rm" => "remove"
35
+
36
+ default_task :list
3
37
 
4
38
  end
5
39
  end
@@ -0,0 +1,116 @@
1
+ module CivoCLI
2
+ class DomainRecord < Thor
3
+
4
+ desc "list DOMAIN_ID", "list all entries for DOMAIN_ID (or name)"
5
+ def list(domain_id)
6
+ CivoCLI::Config.set_api_auth
7
+ domain = Civo::DnsDomain.all.items.detect {|key| key.id == domain_id || key.name == domain_id}
8
+
9
+ rows = []
10
+ Civo::DnsRecord.all(domain_id: domain.id).items.each do |record|
11
+ value = (record.value.length > 20 ? record.value[0, 17] + "..." : record.value)
12
+ rows << [record.id, record.type.upcase, "#{record.name}.#{domain.name.colorize(:light_black)}", value, record.ttl, record.priority]
13
+ end
14
+ puts Terminal::Table.new headings: ['ID', 'Type', 'Name', 'Value', 'TTL', 'Priority'], rows: rows
15
+ end
16
+
17
+ desc "show RECORD_ID", "show full information for record RECORD_ID (or full DNS name)"
18
+ def show(record_id)
19
+ CivoCLI::Config.set_api_auth
20
+ Civo::DnsDomain.all.items.each do |domain|
21
+ @domain = domain
22
+ @record = Civo::DnsRecord.all(domain_id: domain.id).detect {|key| key.id == record_id || (key.domain_id == domain.id && key.name == record_id.gsub(/\.#{domain.name}$/, '')) }
23
+ break if @record
24
+ end
25
+ puts " ID: #{@record.id}"
26
+ puts " Type: #{@record.type.upcase}"
27
+ puts " Name: #{@record.name}.#{@domain.name.colorize(:light_black)}"
28
+ puts " TTL: #{@record.ttl}"
29
+ puts " Priority: #{@record.priority}"
30
+ puts ""
31
+ puts "-" * 29 + "VALUE" + "-" * 29
32
+ puts @record.value
33
+ rescue Flexirest::HTTPException => e
34
+ puts e.result.reason.colorize(:red)
35
+ exit 1
36
+ end
37
+
38
+ option :priority, type: :string, desc: "The priority for MX records", aliases: ["-p"], banner: "PRIORITY"
39
+ option :ttl, type: :string, desc: "The Time-To-Live for this record", aliases: ["-t"], banner: "TTL"
40
+ desc "create RECORD TYPE VALUE", "create a new domain record called RECORD TYPE(a/alias, cname/canonical, mx/mail, txt/text) VALUE"
41
+ def create(name, type, value)
42
+ CivoCLI::Config.set_api_auth
43
+ domain, part = find_domain(name)
44
+ if domain
45
+ type = case(type.downcase)
46
+ when "cname", "canonical"
47
+ "cname"
48
+ when "mx", "mail"
49
+ "mx"
50
+ when "txt", "text"
51
+ "txt"
52
+ else
53
+ "a"
54
+ end.upcase
55
+ out = "Created #{type.colorize(:green)} record #{part.colorize(:green)} for #{domain.name.colorize(:green)}"
56
+ if options[:ttl] && options[:ttl] != ""
57
+ out += " with a TTL of #{options[:ttl].colorize(:green)} seconds"
58
+ if options[:priority] && options[:priority] != ""
59
+ out += " and"
60
+ end
61
+ end
62
+ if options[:priority] && options[:priority] != ""
63
+ out += " with a priority of #{options[:priority].colorize(:green)}"
64
+ end
65
+ options[:ttl] ||= 600
66
+ options[:priority] ||= 0
67
+
68
+ record = Civo::DnsRecord.create(type: type, domain_id: domain.id, name: part, value: value, priority: options[:priority], ttl: options[:ttl])
69
+ puts record.inspect
70
+ puts "#{out} with ID #{record.id.colorize(:green)}"
71
+ else
72
+ puts "Unable to find the domain name for #{name}".colorize(:red)
73
+ end
74
+ rescue Flexirest::HTTPException => e
75
+ puts e.inspect
76
+ puts e.result.reason.colorize(:red)
77
+ exit 1
78
+ end
79
+ map "new" => "create"
80
+
81
+ desc "remove ID", "remove the domain record with ID"
82
+ def remove(id)
83
+ CivoCLI::Config.set_api_auth
84
+ Civo::DnsDomain.all.items.each do |d|
85
+ Civo::DnsRecord.all(domain_id: d.id).items.each do |r|
86
+ if r.id == id
87
+ Civo::DnsRecord.remove(domain_id: d.id, id: r.id)
88
+ puts "Removed the record #{r.name.colorize(:green)} record with ID #{r.id.colorize(:green)}"
89
+ end
90
+ end
91
+ end
92
+ rescue Flexirest::HTTPException => e
93
+ puts e.result&.reason&.colorize(:red)
94
+ exit 1
95
+ end
96
+ map "delete" => "remove", "rm" => "remove"
97
+
98
+ default_task :list
99
+
100
+ private
101
+
102
+ def find_domain(record)
103
+ domain = nil
104
+ part = nil
105
+ Civo::DnsDomain.all.items.each do |d|
106
+ if record.end_with? d.name
107
+ domain = d
108
+ part = record.gsub(".#{d.name}", "")
109
+ break
110
+ end
111
+ end
112
+ [domain, part]
113
+ end
114
+
115
+ end
116
+ end
data/lib/firewall.rb CHANGED
@@ -1,5 +1,85 @@
1
1
  module CivoCLI
2
2
  class Firewall < Thor
3
+ desc "create firewall_name", "Create a new firewall"
4
+ def create(firewall_name)
5
+ CivoCLI::Config.set_api_auth
3
6
 
7
+ Civo::Firewall.create(name: firewall_name)
8
+ puts "Created firewall #{firewall_name.colorize(:green)}"
9
+ rescue Flexirest::HTTPException => e
10
+ puts e.result.reason.colorize(:red)
11
+ exit 1
12
+ end
13
+
14
+ desc "list", "Lists all firewalls"
15
+ def list
16
+ CivoCLI::Config.set_api_auth
17
+ rows = []
18
+ Civo::Firewall.all.items.each do |element|
19
+ rows << [element.id, element.name, element.rules_count, element.instances_count]
20
+ end
21
+ puts Terminal::Table.new headings: ['ID', 'Name', 'No. of Rules', 'instances using'], rows: rows
22
+ rescue Flexirest::HTTPException => e
23
+ puts e.result.reason.colorize(:red)
24
+ exit 1
25
+ end
26
+
27
+ desc "remove firewall_ID", "Removes a firewall with Firewall ID provided"
28
+ def remove(firewall_ID)
29
+ CivoCLI::Config.set_api_auth
30
+ Civo::Firewall.remove(id: firewall_ID)
31
+ puts "Removed firewall #{firewall_ID.colorize(:red)}"
32
+ rescue Flexirest::HTTPException => e
33
+ puts e.result.reason.colorize(:red)
34
+ exit 1
35
+ end
36
+
37
+ desc "new-rule", "Create new rule on firewall firewall_id called rule_name with required arguments"
38
+ option :firewall_id, required: true
39
+ option :protocol, default: 'tcp'
40
+ option :start_port, required: true
41
+ option :end_port
42
+ option :cidr, default: '0.0.0.0/0'
43
+ option :direction, default: 'inbound'
44
+ option :label
45
+ def new_rule
46
+ CivoCLI::Config.set_api_auth
47
+
48
+ if !options[:end_port]
49
+ Civo::FirewallRule.create(firewall_id: options[:firewall_id], start_port: options[:start_port], end_port: options[:start_port], cidr: options[:cidr], direction: options[:direction], label: options[:label])
50
+ end
51
+ if options[:end_port]
52
+ Civo::FirewallRule.create(firewall_id: options[:firewall_id], start_port: options[:start_port], end_port: options[:end_port], cidr: options[:cidr], direction: options[:direction], label: options[:label])
53
+ end
54
+ puts "New rule" + (options[:label].nil? ? "" : " #{options[:label].colorize(:green)}") + " created"
55
+ rescue Flexirest::HTTPException => e
56
+ puts e.result.reason.colorize(:red)
57
+ exit 1
58
+ end
59
+
60
+ desc "list-rules firewall_id", "Lists all active rules for firewall ID provided"
61
+ def list_rules(firewall_id)
62
+ CivoCLI::Config.set_api_auth
63
+ rules = Civo::FirewallRule.all(firewall_id: firewall_id)
64
+ rows = []
65
+ rules.each do |rule|
66
+ rows << [rule.id, rule.protocol, rule.start_port, rule.end_port, rule.direction, rule.cidr.items.join(", "), rule.label]
67
+ end
68
+ puts Terminal::Table.new title: "Firewall rules for #{firewall_id}", headings: ['ID', 'Protocol', 'Start Port', 'End Port', 'Direction', 'CIDR', 'Label'], rows: rows
69
+ rescue Flexirest::HTTPException => e
70
+ puts e.result.reason.colorize(:red)
71
+ exit 1
72
+ end
73
+
74
+ desc "delete-rule firewall_id rule_id", "Deletes rule with rule_id from firewall with firewall_id"
75
+ def delete_rule(firewall_id, rule_id)
76
+ CivoCLI::Config.set_api_auth
77
+
78
+ Civo::FirewallRule.remove(firewall_id: firewall_id, id: rule_id)
79
+ puts "Removed Firewall rule #{rule_id.colorize(:red)}"
80
+ rescue Flexirest::HTTPException => e
81
+ puts e.result.reason.colorize(:red)
82
+ exit 1
83
+ end
4
84
  end
5
85
  end
data/lib/instance.rb CHANGED
@@ -13,103 +13,284 @@ module CivoCLI
13
13
  end
14
14
 
15
15
  if CivoCLI::Config.get_meta("admin")
16
- desc "", ""
16
+ desc "high-cpu", "list high CPU using instances"
17
17
  def high_cpu
18
18
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/high_cpu"
19
+ CivoCLI::Config.set_api_auth
20
+ instance = detect_instance_id(id)
21
+
22
+ Civo::Instance.high_cpu
19
23
  end
20
24
  end
21
25
 
22
- desc "", ""
23
- def find
26
+ desc "show ID/HOSTNAME", "show an instance by ID or hostname"
27
+ def show(id)
24
28
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id", requires: [:id]
29
+ CivoCLI::Config.set_api_auth
30
+ rows = []
31
+ instance = detect_instance_id(id)
32
+
33
+ sizes = Civo::Size.all(all: true).items
34
+ ssh_keys = Civo::SshKey.all.items
35
+ networks = Civo::Network.all.items
36
+ firewalls = Civo::Firewall.all.items
37
+
38
+ size = sizes.detect {|s| s.name == instance.size}
39
+ if size
40
+ @size_name = size.description
41
+ end
42
+ @network = networks.detect {|n| n.id == instance.network_id}
43
+ @firewall = firewalls.detect {|fw| fw.id == instance.firewall_id}
44
+
45
+ puts " ID : #{instance.id}"
46
+ puts " Hostname : #{instance.hostname}"
47
+ if instance.reverse_dns
48
+ puts " Reverse DNS : #{instance.reverse_dns}"
49
+ end
50
+ puts " Tags : #{instance.tags.join(", ")}"
51
+ puts " Size : #{@size_name}"
52
+ case instance.status
53
+ when "ACTIVE"
54
+ puts " Status : #{instance.status.colorize(:green)}"
55
+ when /ING$/
56
+ puts " Status : #{instance.status.colorize(:orange)}"
57
+ else
58
+ puts " Status : #{instance.status.colorize(:red)}"
59
+ end
60
+ puts " Private IP : #{instance.private_ip}"
61
+ puts " Public IP : #{[instance.pseudo_ip, instance.public_ip].join(" => ")}"
62
+ puts " Network : #{@network.label} (#{@network.cidr})"
63
+ puts " Firewall : #{@firewall&.name} (rules: #{@firewall&.rules_count})"
64
+ puts " Region : #{instance.region}"
65
+ puts " Initial User : #{instance.initial_user}"
66
+ puts " Initial Password : #{instance.initial_password}"
67
+ if instance.ssh_key.present?
68
+ key = ssh_keys.detect { |k| k.id == instance.ssh_key }
69
+ puts " SSH Key : #{key.name} (#{key.fingerprint})"
70
+ end
71
+ puts " OpenStack ID : #{instance.openstack_server_id}"
72
+ puts " Template ID : #{instance.template_id}"
73
+ puts " Snapshot ID : #{instance.snapshot_id}"
74
+ puts ""
75
+ puts "-" * 29 + " NOTES " + "-" * 29
76
+ puts ""
77
+ puts instance.notes
78
+ rescue Flexirest::HTTPException => e
79
+ puts e.result.reason.colorize(:red)
80
+ exit 1
25
81
  end
26
82
 
27
- desc "", ""
28
- def create
83
+ desc "create --hostname=host_name --size=instance_size [--template=template_id ] [--snapshot=snapshot_id]", "create a new instance with specified hostname, instance size, template/snapshot ID. Optional: region, public_ip (true or false), initial user"
84
+ option :hostname, required: true
85
+ option :size, required: true
86
+ option :region, default: 'lon1'
87
+ option :public_ip, default: 'create'
88
+ option :initial_user, default: "civo"
89
+ option :template
90
+ option :snapshot
91
+ option :ssh_key_id
92
+ option :tags
93
+ def create(*args)
29
94
  # {ENV["CIVO_API_VERSION"] || "1"}/instances", requires: [:hostname, :size, :region],
30
95
  # defaults: {public_ip: true, initial_user: "civo"}
96
+ CivoCLI::Config.set_api_auth
97
+ if options[:template] && options[:snapshot] || !options[:template] && !options[:snapshot]
98
+ puts "Please provide either template OR snapshot ID".colorize(:red)
99
+ exit 1
100
+ end
101
+ if options[:template]
102
+ Civo::Instance.create(hostname: options[:hostname], size: options[:size], template: options[:template], initial_user: options[:initial_user], region: options[:region], ssh_key_id: options[:ssh_key_id], tags: options[:tags])
103
+ end
104
+
105
+ if options[:snapshot]
106
+ Civo::Instance.create(hostname: options[:hostname], size: options[:size], snapshot_id: options[:snapshot], initial_user: options[:initial_user], region: options[:region], ssh_key_id: options[:ssh_key_id], tags: options[:tags])
107
+ end
108
+ puts "Created instance #{options[:hostname].colorize(:green)}"
109
+ rescue Flexirest::HTTPException => e
110
+ puts e.result.reason.colorize(:red)
111
+ exit 1
31
112
  end
32
113
 
33
- desc "", ""
34
- def tags
114
+ desc "tags ID 'tag1 tag2 tag3...'", "retag instance by ID (input no tags to clear all tags)"
115
+ def tags(id, newtags = nil)
116
+ CivoCLI::Config.set_api_auth
117
+ instance = detect_instance_id(id)
35
118
 
119
+ Civo::Instance.tags(id: instance.id, tags: newtags)
120
+ puts "Updated tags on #{instance.hostname.colorize(:green)}. Use 'civo instance show #{instance.hostname}' to see the current tags.'"
121
+ rescue Flexirest::HTTPException => e
122
+ puts e.result.reason.colorize(:red)
123
+ exit 1
36
124
  end
37
125
 
38
- desc "", ""
39
- def update
126
+ desc "update ID/HOSTNAME [--name=new_hostname] [--notes='txt']", "update details of instance. Use --hostname=new_name, --notes='notes' to specify update"
127
+ option :name
128
+ option :notes
129
+ def update(id)
130
+ CivoCLI::Config.set_api_auth
131
+ instance = detect_instance_id(id)
40
132
 
133
+ if options[:name]
134
+ Civo::Instance.update(id: instance.id, hostname: options[:name])
135
+ puts "Instance #{instance.id} now named #{options[:name].colorize(:green)}"
136
+ end
137
+ if options[:notes]
138
+ Civo::Instance.update(id: instance.id, notes: options[:notes])
139
+ puts "Instance #{instance.id} notes are now: #{options[:notes].colorize(:green)}"
140
+ end
141
+ rescue Flexirest::HTTPException => e
142
+ puts e.result.reason.colorize(:red)
143
+ exit 1
41
144
  end
42
145
 
43
- desc "", ""
44
- def remove
146
+ desc "remove ID/HOSTNAME", "removes an instance with ID/hostname entered (use with caution!)"
147
+ def remove(id)
45
148
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id", requires: [:id], send_delete_body: true
149
+ CivoCLI::Config.set_api_auth
150
+ instance = detect_instance_id(id)
151
+
152
+ puts "Removing instance #{instance.hostname.colorize(:red)}"
153
+ instance.remove
154
+ rescue Flexirest::HTTPException => e
155
+ puts e.result.reason.colorize(:red)
156
+ exit 1
46
157
  end
47
158
 
48
- desc "", ""
49
- def reboot
159
+ desc "reboot ID/HOSTNAME", "reboots instance with ID/hostname entered"
160
+ def reboot(id)
50
161
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/reboots", requires: [:id]
51
- end
162
+ CivoCLI::Config.set_api_auth
52
163
 
53
- desc "", ""
54
- def hard_reboot
55
- # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/hard_reboots", requires: [:id]
164
+ instance = detect_instance_id(id)
165
+ puts "Rebooting #{instance.hostname.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status."
166
+ instance.reboot
167
+ rescue Flexirest::HTTPException => e
168
+ puts e.result.reason.colorize(:red)
169
+ exit 1
56
170
  end
171
+ map "hard-reboot" => "reboot"
57
172
 
58
- desc "", ""
59
- def soft_reboot
173
+ desc "soft-reboot ID/HOSTNAME", "soft-reboots instance with ID entered"
174
+ def soft_reboot(id)
60
175
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/soft_reboots", requires: [:id]
61
- end
176
+ CivoCLI::Config.set_api_auth
62
177
 
63
- desc "", ""
64
- def console
65
- # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/console", requires: [:id]
178
+ instance = detect_instance_id(id)
179
+ puts "Soft-rebooting #{instance.hostname.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status."
180
+ instance.soft_reboot
181
+ rescue Flexirest::HTTPException => e
182
+ puts e.result.reason.colorize(:red)
183
+ exit 1
66
184
  end
67
185
 
68
- desc "", ""
69
- def rebuild
70
- # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/rebuild", requires: [:id]
186
+ desc "console ID/HOSTNAME", "outputs a URL for a web-based console for instance with ID provided"
187
+ def console(id)
188
+ # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/console", requires: [:id]
189
+ CivoCLI::Config.set_api_auth
190
+ instance = detect_instance_id(id)
191
+ puts "Access #{instance.hostname.colorize(:green)} at #{instance.console.url}"
192
+ rescue Flexirest::HTTPException => e
193
+ puts e.result.reason.colorize(:red)
194
+ exit 1
71
195
  end
72
196
 
73
- desc "", ""
74
- def stop
197
+ desc "stop ID/HOSTNAME", "shuts down the instance with ID provided"
198
+ def stop(id)
75
199
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/stop", requires: [:id]
200
+ CivoCLI::Config.set_api_auth
201
+ instance = detect_instance_id(id)
202
+ puts "Stopping #{instance.hostname.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status."
203
+ Civo::Instance.stop(id: instance.id)
204
+ rescue Flexirest::HTTPException => e
205
+ puts e.result.reason.colorize(:red)
206
+ exit 1
76
207
  end
77
208
 
78
- desc "", ""
79
- def start
209
+ desc "start ID/HOSTNAME", "starts a stopped instance with ID provided"
210
+ def start(id)
80
211
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/start", requires: [:id]
212
+ CivoCLI::Config.set_api_auth
213
+
214
+ instance = detect_instance_id(id)
215
+ puts "Starting #{instance.hostname.colorize(:green)}. Use 'civo instance show #{instance.hostname}' to see the current status."
216
+ instance.start
217
+ rescue Flexirest::HTTPException => e
218
+ puts e.result.reason.colorize(:red)
219
+ exit 1
81
220
  end
82
221
 
83
- desc "", ""
84
- def upgrade
222
+ desc "upgrade ID new-size", "Upgrade instance with ID to size provided (see civo sizes for size names)"
223
+ def upgrade(id, new_size)
85
224
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/resize", requires: [:size, :id]
86
- end
225
+ CivoCLI::Config.set_api_auth
226
+
227
+ instance = detect_instance_id(id)
87
228
 
88
- desc "", ""
89
- def restore
90
- # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/restore", requires: [:snapshot, :id]
229
+ Civo::Instance.upgrade(id: instance.id, size: new_size)
230
+ puts "Resizing #{instance.hostname.colorize(:green)} to #{new_size.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status."
231
+ rescue Flexirest::HTTPException => e
232
+ puts e.result.reason.colorize(:red)
233
+ exit 1
91
234
  end
92
235
 
93
- desc "", ""
94
- def move_ip
236
+ desc "move-ip targetID IP_Address", "move a public IP_Address to target instance"
237
+ def move_ip(id, ip_address)
95
238
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/ip/:ip", requires: [:ip, :id]
96
- end
239
+ CivoCLI::Config.set_api_auth
97
240
 
98
- desc "", ""
99
- def rescue
100
- # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/rescue", requires: [:id]
101
- end
241
+ instance = detect_instance_id(id)
102
242
 
103
- desc "", ""
104
- def unrescue
105
- # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/unrescue", requires: [:id]
243
+ Civo::Instance.move_ip(id: instance.id, ip: ip_address)
244
+ puts "Moved public IP #{ip_address} to instance #{instance.hostname}"
245
+ rescue Flexirest::HTTPException => e
246
+ puts e.result.reason.colorize(:red)
247
+ exit 1
106
248
  end
107
249
 
108
- desc "", ""
109
- def firewall
250
+ # desc "", ""
251
+ # def rescue
252
+ # # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/rescue", requires: [:id]
253
+ # end
254
+
255
+ # desc "", ""
256
+ # def unrescue
257
+ # # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/unrescue", requires: [:id]
258
+ # end
259
+
260
+ desc "firewall ID/HOSTNAME firewall_id", "set instance with ID/HOSTNAME to use firewall with firewall_id"
261
+ def firewall(id, firewall_id)
110
262
  # {ENV["CIVO_API_VERSION"] || "1"}/instances/:id/firewall", requires: [:firewall_id, :id]
263
+ CivoCLI::Config.set_api_auth
264
+
265
+ instance = detect_instance_id(id)
266
+
267
+ Civo::Instance.firewall(id: instance.id, firewall_id: firewall_id)
268
+ puts "Set #{instance.hostname.colorize(:green)} to use firewall '#{firewall_id.colorize(:yellow)}'"
269
+ rescue Flexirest::HTTPException => e
270
+ puts e.result.reason.colorize(:red)
271
+ exit 1
111
272
  end
112
273
 
113
274
  default_task :list
275
+
276
+ private
277
+
278
+ def detect_instance_id(id)
279
+ result = []
280
+ Civo::Instance.all.items.each do |instance|
281
+ result << instance
282
+ end
283
+ result.select! { |instance| instance.hostname.include?(id) }
284
+
285
+ if result.count.zero?
286
+ puts "No instances found for '#{id}'. Please check your query."
287
+ exit 1
288
+ elsif result.count > 1
289
+ puts "Multiple possible instances found for '#{id}'. Please try with a more specific query."
290
+ exit 1
291
+ else
292
+ result[0]
293
+ end
294
+ end
114
295
  end
115
296
  end
@@ -0,0 +1,9 @@
1
+ class SubCommandBase < Thor
2
+ def self.banner(command, namespace = nil, subcommand = false)
3
+ "#{basename} #{subcommand_prefix} #{command.usage}"
4
+ end
5
+
6
+ def self.subcommand_prefix
7
+ self.name.gsub(%r{.*::}, '').gsub(%r{^[A-Z]}) { |match| match[0].downcase }.gsub(%r{[A-Z]}) { |match| "-#{match[0].downcase}" }
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: civo_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-04-01 00:00:00.000000000 Z
12
+ date: 2019-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -208,6 +208,7 @@ files:
208
208
  - lib/civo_cli/version.rb
209
209
  - lib/config.rb
210
210
  - lib/domain.rb
211
+ - lib/domain_record.rb
211
212
  - lib/firewall.rb
212
213
  - lib/instance.rb
213
214
  - lib/network.rb
@@ -216,6 +217,7 @@ files:
216
217
  - lib/size.rb
217
218
  - lib/snapshot.rb
218
219
  - lib/sshkey.rb
220
+ - lib/sub_command_base.rb
219
221
  - lib/template.rb
220
222
  - lib/volume.rb
221
223
  homepage: https://www.civo.com/api